-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
71 lines (55 loc) · 1.58 KB
/
index.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
var piglowInterface = require('piglow');
var depugger = require('depugger');
var Clock = require('./lib/Clock');
var MAX_DIGIT_NUMBER = 6;
var myClock, piglow, debug;
function start(options, p) {
piglow = p;
var brightness = options.brightness ? piglowInterface.processValue(options.brightness) : 10;
debug = depugger(options.debug, 'clock');
myClock = new Clock();
myClock.on('second', function(time) {
debug('%dh %dmin %ds %dms', time.h, time.m, time.s, time.ms);
piglow.startTransaction();
piglow.reset;
map(0, time.h, brightness, piglow);
map(1, time.m, brightness, piglow);
map(2, time.s, brightness, piglow);
piglow.commitTransaction();
});
}
function stop(callback) {
piglow.reset;
myClock.stop();
setTimeout(callback, 100);
}
function map(leg, value, brightness, piglow) {
var list = (+value)
.toString(2)
.split('')
.map(function(v) {
return +v;
});
while(list.length < MAX_DIGIT_NUMBER) {
list.unshift(0);
}
list.forEach(function(value, index) {
if(value === 1) {
piglow['l_' + leg + '_' + index] = brightness;
}
});
}
module.exports = {
start: function(options, callback) {
options = options || {};
callback = callback || function () {};
piglowInterface(function(error, piGlowHandler) {
if(error) {
return callback(error);
}
start(options, piGlowHandler);
callback(null);
});
},
stop: stop
};