-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.js
More file actions
44 lines (39 loc) · 992 Bytes
/
button.js
File metadata and controls
44 lines (39 loc) · 992 Bytes
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
var events = require('events'),
util = require('util');
/*
* Main Button constructor
* Process options
* Tell the board to set it up
*/
var Button = function (options) {
if (!options || !options.board) throw new Error('Must supply required options to Button');
this.board = options.board;
this.pin = options.pin || 13;
this.down = false;
this.board.pinMode(this.pin, 'in');
var self = this;
setInterval(function () {
self.board.digitalRead(self.pin);
}, 50);
this.board.on('data', function (m) {
m = m.slice(0, -1).split('::');
var err = null;
if (m.length > 1 && m[0] == self.pin) {
// 0 is up
// 1 is down
if (m[1] == 0 && self.down) {
self.down = false;
self.emit('up', err);
}
if (m[1] == 1 && !self.down) {
self.down = true;
self.emit('down', err);
}
}
});
}
/*
* EventEmitter, I choose you!
*/
util.inherits(Button, events.EventEmitter);
module.exports = Button;