-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.js
80 lines (56 loc) · 2.07 KB
/
gpio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const LynxariDevice = require(process.lynxari.device);
const device = require('@agilatech/gpio');
module.exports = class Gpio extends LynxariDevice {
constructor(config) {
if ((config['gpio'] == null) || (config['gpio'] === 'undefined')) {
throw 'Gpio exception: gpio pin not defined';
}
const hardware = new device(config['gpio'], config['direction'], config['edge'], config['debounce']);
super(hardware, config);
this.hardware.watch((this._inputEvent).bind(this));
}
addDeviceFunctionsToStates(config, onAllow, offAllow) {
if (this.hardware.direction == 'out') {
onAllow.push('change-output', 'toggle-output');
config.map('change-output', this.changeOutput, [{name:'level'}]);
config.map('toggle-output', this.toggleOutput);
}
else if (this.hardware.direction == 'in') {
onAllow.push('trigger-input');
config.map('trigger-input', this.triggerInput);
}
}
changeOutput(level, callback) {
const status = this.hardware.sendCommandSync(level);
if (status > 0) {
this.syncLevel();
}
callback();
}
toggleOutput(callback) {
const status = this.hardware.sendCommandSync('toggle');
if (status > 0) {
this.syncLevel();
}
callback();
}
triggerInput(callback) {
const status = this.hardware.sendCommandSync('trigger');
callback();
}
syncLevel() {
this.deviceProperties['level'].cur = this.hardware.valueAtIndexSync(0);
this.level = this.deviceProperties['level'].cur;
this.info(`${this.name} level changed to ${this.deviceProperties['level'].cur}`);
}
_inputEvent(err, val) {
if (err) {
this.error("GPIO Input error: " + err, {"error":err});
}
else {
const propertyName = this.hardware.nameAtIndex(0);
this[propertyName] = val;
this.deviceProperties[propertyName].cur = val;
}
}
}