-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_helper.js
45 lines (40 loc) · 1018 Bytes
/
node_helper.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
'use strict';
/* Magic Mirror
* Module: MMM-Pins
*
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Gpio = require('onoff').Gpio;
module.exports = NodeHelper.create({
start: function () {
this.started = false;
},
socketNotificationReceived: function(notification, payload) {
const self = this
if (notification === 'PIN_CONFIG' && self.started === false) {
let pinConfigs = payload;
if (Gpio.accessible) {
self.gpio = []
for (let index = 0; index < pinConfigs.length; ++index) {
let pinConfig = pinConfigs[index];
self.gpio[String(pinConfig.pin)] = new Gpio(pinConfig.pin, pinConfig.direction);
}
}
self.started = true;
}
else if (notification === 'TOGGLE_PIN') {
let pinNumber = payload;
let pin = self.gpio[String(pinNumber)];
let value = pin.readSync();
if (value !== 1) {
value = 1;
}
else{
value = 0;
}
pin.writeSync(value);
console.log(`Pin ${pinNumber} switched to ${value}`);
};
}
});