-
Notifications
You must be signed in to change notification settings - Fork 5
/
node_helper.js
132 lines (109 loc) · 3.05 KB
/
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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Magic Mirror
* Node Helper: {MMM-Navigate}
*
* By {AxLED}
* {MIT} Licensed.
*/
//Debugging
//tail -f ~/.pm2/logs/mm-out-0.log
//tail -f ~/.pm2/logs/mm-error-0.log
const Gpio = require('onoff').Gpio;
var NodeHelper = require("node_helper");
const exec = require("child_process").exec;
const url = require("url");
//Variables
var lastStateCLK = 0;
var lastdir = '';
module.exports = NodeHelper.create({
// Subclass start method.
start: function() {
var self = this;
this.loaded = false;
this.createRoutes();
},
intializeRotary: function() {
//Rotary Code..
this.loaded = true;
var self = this;
console.log('MMM-Navigate, listen on GPIO PINs (BCM): '+self.config.GPIOPins[0]+','+self.config.GPIOPins[1]+','+self.config.GPIOPins[2]);
const CLK = new Gpio(self.config.GPIOPins[1], 'in', 'both',{debounceTimeout : 0 }); //BCM Pin 20
const DT = new Gpio(self.config.GPIOPins[0], 'in', 'both',{debounceTimeout : 0 }); //BCM Pin 26
const SW = new Gpio(self.config.GPIOPins[2], 'in', 'both',{debounceTimeout : 20 }); //BCM Pin 19
CLK.read(function (err, value) {
if (err) {
throw err;
}
this.lastStateCLK = value;
this.a = value;
});
DT.read(function (err, value) {
if (err) {
throw err;
}
this.b = value;
});
CLK.watch(function (err, value) {
if (err) {
throw err;
}
this.a = value;
});
DT.watch(function (err, value) {
if (err) {
throw err;
}
this.b = value;
tick();
});
SW.watch(function (err, value) {
if (err) {
throw err;
}
if(value == 0){
self.sendSocketNotification('PRESSED',{inputtype: 'PRESSED'});
}
});
function tick() {
const { a, b } = this;
if (a != lastStateCLK && a == 1){//only do action, if rotary was moved and only count one step
if (b != a){
self.sendSocketNotification('CW',{inputtype: 'CW'});
lastdir = 'CW';
} else {
self.sendSocketNotification('CCW',{inputtype: 'CCW'});
lastdir = 'CCW';
}
}
//catch missing count when changing from CCW to CW
if (a == lastStateCLK && b == 0 && lastdir == 'CCW') {
self.sendSocketNotification('CW',{inputtype: 'CW'});
lastdir = 'CW';
}
lastStateCLK = a;
return this;
}
},
// Override socketNotificationReceived method.
socketNotificationReceived: function(notification, payload) {
if (notification === 'BUTTON_CONFIG') {
this.config = payload.config;
if (this.loaded === false) {//AxLED 2020-04
this.intializeRotary();
}
}
if (notification === 'SHELLCOMMAND') {
console.log("MMM-Navigate, received Shellcommand:", payload);
exec(payload, null);
}
},
createRoutes: function() {
var self = this;
this.expressApp.get("/MMM-Navigate/remote", function(req, res) {
var query = url.parse(req.url, true).query;
if(query.notification=='CW' || query.notification=='CCW' || query.notification=='PRESSED'){
self.sendSocketNotification(query.notification,{inputtype: ""+query.notification+""});
}
res.send("MMM-Navigate, data received: "+ JSON.stringify(query));
});
},
});