Skip to content

Commit

Permalink
feat: add device config pin definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Mefjus committed Aug 1, 2021
1 parent 65afa0f commit 0a7764e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
20 changes: 18 additions & 2 deletions packages/device/src/actions/actionExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import rpio from 'rpio';

import actionsConfig from '../config/actions.config.json';
import {
Action,
ActionConfig,
ActionMode,
ActionPin,
ActionValue,
OpenAction,
SleepAction,
Expand Down Expand Up @@ -31,16 +34,29 @@ const modeMapper = (mode: ActionMode): number => {
}
};

const pinMapper = (pin: ActionPin): number => {
let definedPin = pin;

if (typeof pin === 'string') {
definedPin = (actionsConfig as ActionConfig).pinDefinition[pin];
}

if (typeof definedPin === 'number') {
return definedPin;
}
throw Error(`Invalid pin value: ${pin}`);
};

const sleepAction = ({ time }: SleepAction) => {
rpio.msleep(time);
};

const writeAction = ({ pin, value }: WriteAction) => {
rpio.write(pin, valueMapper(value));
rpio.write(pinMapper(pin), valueMapper(value));
};

const openAction = ({ pin, value, mode }: OpenAction) => {
rpio.open(pin, modeMapper(mode), valueMapper(value));
rpio.open(pinMapper(pin), modeMapper(mode), valueMapper(value));
};

const execute = (actions: Array<Action>) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/device/src/actions/actions.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
export type ActionMode = 'OUTPUT' | 'INPUT';
export type ActionValue = 'LOW' | 'HIGH';
export type ActionPin = string | number;

export type OpenAction = {
type: 'OPEN';
pin: number;
pin: ActionPin;
mode: ActionMode;
value: ActionValue;
};

export type WriteAction = {
type: 'WRITE';
pin: number;
pin: ActionPin;
value: ActionValue;
};

Expand All @@ -24,4 +25,5 @@ export type Action = OpenAction | WriteAction | SleepAction;
export type ActionConfig = {
onInit: Array<Action>;
onToggle: Array<Action>;
pinDefinition: Record<string, number>;
};
16 changes: 10 additions & 6 deletions packages/device/src/config/actions.config.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
{
"pinDefinition": {
"LOCK_POWER": 38,
"CAMERA_POWER": 40
},
"onInit": [
{
"type": "OPEN",
"pin": 38,
"pin": "LOCK_POWER",
"mode": "OUTPUT",
"value": "HIGH"
},
{
"type": "OPEN",
"pin": 40,
"pin": "CAMERA_POWER",
"mode": "OUTPUT",
"value": "HIGH"
}
],
"onToggle": [
{
"type": "WRITE",
"pin": 38,
"pin": "LOCK_POWER",
"value": "LOW"
},
{
"type": "WRITE",
"pin": 40,
"pin": "CAMERA_POWER",
"value": "LOW"
},
{
Expand All @@ -30,12 +34,12 @@
},
{
"type": "WRITE",
"pin": 38,
"pin": "LOCK_POWER",
"value": "HIGH"
},
{
"type": "WRITE",
"pin": 40,
"pin": "CAMERA_POWER",
"value": "HIGH"
}
]
Expand Down

0 comments on commit 0a7764e

Please sign in to comment.