-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import rpio from 'rpio'; | ||
|
||
import { | ||
Action, | ||
ActionMode, | ||
ActionValue, | ||
OpenAction, | ||
SleepAction, | ||
WriteAction, | ||
} from './actions.types'; | ||
|
||
const valueMapper = (value: ActionValue): number => { | ||
switch (value) { | ||
case 'HIGH': | ||
return rpio.HIGH; | ||
case 'LOW': | ||
return rpio.LOW; | ||
default: | ||
throw Error(`Can't map action value.`); | ||
} | ||
}; | ||
|
||
const modeMapper = (mode: ActionMode): number => { | ||
switch (mode) { | ||
case 'OUTPUT': | ||
return rpio.OUTPUT; | ||
case 'INPUT': | ||
return rpio.INPUT; | ||
default: | ||
throw Error(`Can't map action mode.`); | ||
} | ||
}; | ||
|
||
const sleepAction = ({ time }: SleepAction) => { | ||
rpio.msleep(time); | ||
}; | ||
|
||
const writeAction = ({ pin, value }: WriteAction) => { | ||
rpio.write(pin, valueMapper(value)); | ||
}; | ||
|
||
const openAction = ({ pin, value, mode }: OpenAction) => { | ||
rpio.open(pin, modeMapper(mode), valueMapper(value)); | ||
}; | ||
|
||
const execute = (actions: Array<Action>) => { | ||
actions.forEach((action) => { | ||
switch (action.type) { | ||
case 'OPEN': | ||
return openAction(action); | ||
case 'SLEEP': | ||
return sleepAction(action); | ||
case 'WRITE': | ||
return writeAction(action); | ||
default: | ||
throw Error(`Can't map action type.`); | ||
} | ||
}); | ||
}; | ||
|
||
export default execute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export type ActionMode = 'OUTPUT' | 'INPUT'; | ||
export type ActionValue = 'LOW' | 'HIGH'; | ||
|
||
export type OpenAction = { | ||
type: 'OPEN'; | ||
pin: number; | ||
mode: ActionMode; | ||
value: ActionValue; | ||
}; | ||
|
||
export type WriteAction = { | ||
type: 'WRITE'; | ||
pin: number; | ||
value: ActionValue; | ||
}; | ||
|
||
export type SleepAction = { | ||
type: 'SLEEP'; | ||
time: number; | ||
}; | ||
|
||
export type Action = OpenAction | WriteAction | SleepAction; | ||
|
||
export type ActionConfig = { | ||
onInit: Array<Action>; | ||
onToggle: Array<Action>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"onInit": [ | ||
{ | ||
"type": "OPEN", | ||
"pin": 36, | ||
"mode": "OUTPUT", | ||
"value": "LOW" | ||
}, | ||
{ | ||
"type": "OPEN", | ||
"pin": 38, | ||
"mode": "OUTPUT", | ||
"value": "LOW" | ||
}, | ||
{ | ||
"type": "OPEN", | ||
"pin": 40, | ||
"mode": "OUTPUT", | ||
"value": "LOW" | ||
} | ||
], | ||
"onToggle": [ | ||
{ | ||
"type": "WRITE", | ||
"pin": 36, | ||
"value": "HIGH" | ||
}, | ||
{ | ||
"type": "WRITE", | ||
"pin": 38, | ||
"value": "LOW" | ||
}, | ||
{ | ||
"type": "WRITE", | ||
"pin": 40, | ||
"value": "LOW" | ||
}, | ||
{ | ||
"type": "SLEEP", | ||
"time": 1000 | ||
}, | ||
{ | ||
"type": "WRITE", | ||
"pin": 36, | ||
"value": "LOW" | ||
}, | ||
{ | ||
"type": "WRITE", | ||
"pin": 38, | ||
"value": "HIGH" | ||
}, | ||
{ | ||
"type": "WRITE", | ||
"pin": 40, | ||
"value": "HIGH" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module '*.json' { | ||
const value: never; | ||
export default value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters