This repository has been archived by the owner on Aug 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnode_helper.js
116 lines (105 loc) · 3.58 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
'use strict';
/* Magic Mirror
* Module: MMM-Swipe
*
* By Luke Moch http://github.com/mochman/
* MIT Licensed
*/
const NodeHelper = require('node_helper');
const usonic = require('mmm-usonic');
const statistics = require('math-statistics');
const gpio = require('mmm-gpio');
module.exports = NodeHelper.create({
start: function () {
const self = this;
usonic.init(function (error) {
if (error) {
console.log(error);
} else {
self.initSensor();
}
});
gpio.init(function (error) {
if (error) {
console.log(error);
} else {
self.initSensor();
}
});
},
socketNotificationReceived: function (notification, payload) {
const self = this;
this.config = payload;
var buttonWait = 0;
var sensorLeft;
var sensorRight;
if ( notification === 'CALIBRATE') {
self.sensorLeft = usonic.createSensor(self.config.echoLeftPin, self.config.triggerLeftPin, self.config.sensorTimeout);
self.sensorRight = usonic.createSensor(self.config.echoRightPin, self.config.triggerRightPin, self.config.sensorTimeout);
var distances = [this.sensorLeft().toFixed(2), this.sensorRight().toFixed(2)];
self.sendSocketNotification('CALIBRATION', distances);
} else if (notification === 'INIT') {
self.sensorLeft = usonic.createSensor(self.config.echoLeftPin, self.config.triggerLeftPin, self.config.sensorTimeout);
self.sensorRight = usonic.createSensor(self.config.echoRightPin, self.config.triggerRightPin, self.config.sensorTimeout);
if (this.sensorLeft().toFixed(2) <= self.config.leftDistance) {
var distancesLeft;
var distancesRight;
var i = 5;
var countdownTime = self.config.swipeSpeed / i;
(function measure(x) {
if(x == i) {
self.distancesLeft = [];
self.distancesRight = [];
} else if (x == 1 ) {
var avgLeft = statistics.median(self.distancesLeft).toFixed(0);
var avgRight = statistics.median(self.distancesRight).toFixed(0);
if( avgLeft <= self.config.leftDistance && avgRight <= self.config.rightDistance) {
self.sendSocketNotification('MOVEMENT', 'Press');
} else if ( avgRight * 1.3 <= avgLeft ) {
self.sendSocketNotification('MOVEMENT', 'Swipe Right');
}
}
setTimeout(function () {
self.distancesLeft.push(self.sensorLeft());
self.distancesRight.push(self.sensorRight());
if(--x) measure(x);
}, countdownTime);
})(i);
} else if (this.sensorRight().toFixed(2) <= self.config.rightDistance) {
var distancesLeft;
var distancesRight;
var i = 5;
var countdownTime = self.config.swipeSpeed / i;
(function measure(x) {
if(x == i) {
self.distancesLeft = [];
self.distancesRight = [];
} else if (x == 1 ) {
var avgLeft = statistics.median(self.distancesLeft).toFixed(0);
var avgRight = statistics.median(self.distancesRight).toFixed(0);
if( avgLeft <= self.config.leftDistance && avgRight <= self.config.rightDistance) {
self.sendSocketNotification('MOVEMENT', 'Press');
} else if ( avgLeft * 1.3 <= avgRight ) {
self.sendSocketNotification('MOVEMENT', 'Swipe Left');
}
}
setTimeout(function () {
self.distancesLeft.push(self.sensorLeft());
self.distancesRight.push(self.sensorRight());
if(--x) measure(x);
}, countdownTime);
})(i);
} else {
self.sendSocketNotification('STATUS', "Waiting for Movement");
}
} else if (notification === 'PRESS') {
var trigger = gpio.createOutput(payload);
setTimeout(function(){
trigger(false);
},1000);
trigger(true);
}
},
initSensor: function () {
}
});