-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplaybulb.js
196 lines (182 loc) · 6.53 KB
/
playbulb.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
var noble = require('noble');
var SETCOLOR = -1;
var types = {
COLOR: {
colorUuid: "0018",
effectsUuid: "0016",
modes: {
FLASH: 0,
PULSE: 1,
RAINBOWJUMP: 2,
RAINBOWFADE: 3
}
},
CANDLE: {
colorUuid: "fffc",
effectsUuid: "fffb",
modes: {
FADE: 0,
JUMPRGB: 1,
FADERGB: 2,
FLICKER: 3
}
}
};
var Playbulb = function (type, playbulbName, colorUuid, effectsUuid) {
playbulbName = playbulbName || "Playbulb";
colorUuid = colorUuid || types[type].colorUuid;
effectsUuid = effectsUuid || types[type].effectsUuid;
var colorChar, effectsChar;
var yesReady = false;
var waiting = [];
var modes = types[type].modes;
var isReady = function (callback) {
if (callback) {
if (yesReady) {
setTimeout(callback, 0); // run async
} else {
waiting.push(callback);
}
} else if (colorChar !== null && effectsChar !== null) {
yesReady = true;
var waiter;
while (waiting.length > 0) {
waiter = waiting.pop(0);
setTimeout(waiter, 0); // run each waiter async
}
}
};
var decimalToHexBytes = function (speed, max) {
var speedRanged = speed * max;
var speedHex = speedRanged.toString(16);
while (speedHex.length < 4) {
speedHex = "0" + speedHex;
}
return [parseInt(speedHex.substring(0, 2), 16), parseInt(speedHex.substring(2, 4), 16)];
};
var runEffect = function (saturation, r, g, b, effect, speed) {
if (!isReady) {
throw "playbulb not ready";
}
if (saturation < 0 || saturation > 255 || r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
throw "saturation, r, g and b must be between 0 and 255";
}
if (effect !== SETCOLOR) {
if (speed < 0 || speed > 1) {
throw "speed must be between 0 and 1";
}
var max;
var speedBytes;
if (type === "COLOR") {
speed = 1 - speed; // 0 is slow, 1 is fast
max = effect === modes.PULSE ? 7710 : 17990;
speedBytes = decimalToHexBytes(speed, max); // max hex is: 1E 1E
} else if (type === "CANDLE") {
max = 255;
// special handling for this:
// 00-> ff, 00 => really slow, 01 => really fast, 02 => slower
if (speed === 0) {
speedBytes = 0;
} else if (speed === 1) {
speedBytes = 1;
} else {
speedBytes = decimalToHexBytes(speed, max); // max hex is: 1E 1E
if (speedBytes < 3) {
speedBytes = 3;
}
}
}
var effectBytes = new Buffer([0, r, g, b, effect, 0, speedBytes[0], speedBytes[1]]);
effectsChar.write(effectBytes);
} else {
var colorBytes = new Buffer([0, r, g, b]);
colorChar.write(colorBytes);
}
};
// connect to device and get characteristics, callback to ready listeners when done
noble.startScanning();
noble.on('discover', function (peripheral) {
if (peripheral.advertisement.localName === playbulbName) {
peripheral.connect(function (error) {
if (error) {
throw error;
}
peripheral.discoverAllServicesAndCharacteristics();
peripheral.on('servicesDiscover', function (services) {
services.map(function (service) {
service.on('characteristicsDiscover', function (characteristics) {
characteristics.map(function (characteristic) {
if (characteristic.uuid === colorUuid) {
colorChar = characteristic;
isReady();
} else if (characteristic.uuid === effectsUuid) {
effectsChar = characteristic;
isReady();
}
});
});
});
});
});
}
});
return {
runEffect: runEffect,
ready: function (callback) {
isReady(callback);
}
};
};
var PlaybulbColor = function (deviceName) {
var type = "COLOR";
var pb = new Playbulb(type, deviceName);
return {
setColor: function (saturation, r, g, b) {
pb.runEffect(saturation, r, g, b, SETCOLOR);
},
setPulse: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.PULSE, speed);
},
setFlash: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.FLASH, speed);
},
setRainbowJump: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.RAINBOWJUMP, speed);
},
setRainbowFade: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.RAINBOWFADE, speed);
},
ready: function (callback) {
pb.ready(callback);
}
};
};
var PlaybulbCandle = function (deviceName) {
var type = "CANDLE";
var pb = new Playbulb(type, deviceName);
return {
setColor: function (saturation, r, g, b) {
pb.runEffect(saturation, r, g, b, SETCOLOR);
},
setFade: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.FADE, speed);
},
setJumpRGB: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.JUMPRGB, speed);
},
setFadeRGB: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.FADERGB, speed);
},
setFlicker: function (saturation, r, g, b, speed) {
pb.runEffect(saturation, r, g, b, types[type].modes.FLICKER, speed);
},
ready: function (callback) {
pb.ready(callback);
}
};
};
module.exports = {
PlaybulbColor: PlaybulbColor,
PlaybulbCandle: PlaybulbCandle
};