-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentsoreak.js
62 lines (49 loc) · 1.15 KB
/
sentsoreak.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
var five = require("johnny-five");
var Emitter = require("events").EventEmitter;
var util = require("util");
function Sentsoreak(opts){
var self = this;
var board;
var testModeDelay = 500;
if(typeof opts == "undefined"){
opts = {};
}
var data1, data2;
if(opts.testMode){
setInterval(function(){
var output = {
sens1:Math.round(Math.random() * (20 - 1) + 1),
sens2:Math.round(Math.random() * (20 - 1) + 1)
}
self.emit('sens:change',output);
},testModeDelay);
} else {
var board = new five.Board({repl:false,port:opts.port});
board.on("ready", function() {
self.emit('sens:ready');
var sens1 = new five.Proximity({
controller: "HCSR04",
pin: 7
});
var sens2 = new five.Proximity({
controller: "HCSR04",
pin: 8
});
sens1.on('change',function(data){
data1 = data.cm;
sendData();
});
sens2.on('change',function(data){
data2 = data.cm;
sendData();
});
});
}
var sendData = function(){
if(opts.debug)console.log(data1 + ":" + data2);
self.emit('sens:change',{sens1:data1,sens2:data2});
}
return self;
}
util.inherits(Sentsoreak, Emitter);
module.exports = Sentsoreak;