-
Notifications
You must be signed in to change notification settings - Fork 0
/
roller_device.js
66 lines (58 loc) · 1.41 KB
/
roller_device.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
var Device = require("zetta-device");
var util = require("util");
var RollerDevice = module.exports = function(j5Led, j5Input, name) {
Device.call(this);
this._led = j5Led;
this._led.off();
this._input = j5Input;
this.ticks = 0;
this.name = name;
};
util.inherits(RollerDevice, Device);
RollerDevice.prototype.init = function(config) {
var self = this;
config
.name(self.name)
.monitor("ticks")
.type("roller")
.state("waiting")
.when("waiting", { allow: ["start-counting"] })
.when("counting", { allow: ["stop-counting"] })
.map("start-counting", self.startCounting)
.map("stop-counting", self.stopCounting);
self._input.on("up", function() {
console.log(self.name + " up");
});
self._input.on("down", function() {
console.log(self.name + " down");
if (self.state === "counting") {
self.ticks++;
console.log(self.name + " " + self.ticks + " ticks");
}
});
};
RollerDevice.prototype.startCounting = function(cb) {
var self = this;
self.state = "counting";
self.turnGreen();
self.ticks = 0;
if (cb) {
cb();
}
};
RollerDevice.prototype.stopCounting = function(cb) {
var self = this;
self.state = "waiting";
self.turnRed();
if (cb) {
cb();
}
};
RollerDevice.prototype.turnGreen = function() {
var self = this;
self._led.on();
};
RollerDevice.prototype.turnRed = function() {
var self = this;
self._led.off();
};