-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybulbCandle.js
180 lines (164 loc) · 6.2 KB
/
playbulbCandle.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
(function() {
'use strict';
let encoder = new TextEncoder('utf-8');
let decoder = new TextDecoder('utf-8');
/* Custom Bluetooth Service UUIDs */
const CANDLE_SERVICE_UUID = 0xFF02;
/* Custom Bluetooth Characteristic UUIDs */
const CANDLE_DEVICE_NAME_UUID = 0xFFFF;
const CANDLE_COLOR_UUID = 0xFFFC;
const CANDLE_EFFECT_UUID = 0xFFFB;
class PlaybulbCandle {
constructor() {
this.device = null;
this.server = null;
this._characteristics = new Map();
this._debug = false;
}
connect() {
let options = {filters:[{
name: 'PLAYBULB CANDLE',
}],
optionalServices: ['battery_service']};
return navigator.bluetooth.requestDevice(options)
.then(device => {
this.device = device;
return device.connectGATT();
})
.then(server => {
this.server = server;
return Promise.all([
server.getPrimaryService(CANDLE_SERVICE_UUID).then(service => {
return Promise.all([
this._cacheCharacteristic(service, CANDLE_DEVICE_NAME_UUID),
this._cacheCharacteristic(service, CANDLE_COLOR_UUID),
this._cacheCharacteristic(service, CANDLE_EFFECT_UUID),
])
}),
server.getPrimaryService('battery_service').then(service => {
return this._cacheCharacteristic(service, 'battery_level')
}),
// TODO: Uncomment when device_information service is actually
// available in Chrome OS. http://crbug.com/532930
/*
server.getPrimaryService('device_information').then(service => {
return Promise.all([
this._cacheCharacteristic(service, 'serial_number_string'),
this._cacheCharacteristic(service, 'hardware_revision_string'),
this._cacheCharacteristic(service, 'firmware_revision_string'),
this._cacheCharacteristic(service, 'software_revision_string'),
this._cacheCharacteristic(service, 'manufacturer_name_string'),
this._cacheCharacteristic(service, 'pnp_id'),
])
}),
*/
]);
})
}
/* Candle Service */
getDeviceName() {
return this._readCharacteristicValue(CANDLE_DEVICE_NAME_UUID)
.then(this._decodeString);
}
setDeviceName(name) {
let data = this._encodeString(name);
return this._writeCharacteristicValue(CANDLE_DEVICE_NAME_UUID, data)
}
setColor(r, g, b) {
let data = [0x00, r, g, b];
return this._writeCharacteristicValue(CANDLE_COLOR_UUID, new Uint8Array(data))
.then(() => [r,g,b]); // Returns color when fulfilled.
}
setCandleEffectColor(r, g, b) {
let data = [0x00, r, g, b, 0x04, 0x00, 0x01, 0x00];
return this._writeCharacteristicValue(CANDLE_EFFECT_UUID, new Uint8Array(data))
.then(() => [r,g,b]); // Returns color when fulfilled.
}
setFlashingColor(r, g, b) {
let data = [0x00, r, g, b, 0x00, 0x00, 0x1F, 0x00];
return this._writeCharacteristicValue(CANDLE_EFFECT_UUID, new Uint8Array(data))
.then(() => [r,g,b]); // Returns color when fulfilled.
}
setPulseColor(r, g, b) {
// We have to correct user color to make it look nice for real...
let newRed = Math.min(Math.round(r / 64) * 64, 255);
let newGreen = Math.min(Math.round(g / 64) * 64, 255);
let newBlue = Math.min(Math.round(b / 64) * 64, 255);
let data = [0x00, newRed, newGreen, newBlue, 0x01, 0x00, 0x09, 0x00];
return this._writeCharacteristicValue(CANDLE_EFFECT_UUID, new Uint8Array(data))
.then(() => [r,g,b]); // Returns color when fulfilled.
}
setRainbow() {
let data = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00];
return this._writeCharacteristicValue(CANDLE_EFFECT_UUID, new Uint8Array(data));
}
setRainbowFade() {
let data = [0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x26, 0x00];
return this._writeCharacteristicValue(CANDLE_EFFECT_UUID, new Uint8Array(data));
}
/* Battery Service */
getBatteryLevel() {
return this._readCharacteristicValue('battery_level')
.then(data => data.getUint8(0));
}
/* Device Info Service */
getSerialNumber() {
return this._readCharacteristicValue('serial_number_string')
.then(this._decodeString);
}
getHardwareRevision() {
return this._readCharacteristicValue('hardware_revision_string')
.then(this._decodeString);
}
getFirmwareRevision() {
return this._readCharacteristicValue('firmware_revision_string')
.then(this._decodeString);
}
getSoftwareRevision() {
return this._readCharacteristicValue('software_revision_string')
.then(this._decodeString);
}
getManufacturerName() {
return this._readCharacteristicValue('manufacturer_name_string')
.then(this._decodeString);
}
getPnpID() {
return this._readCharacteristicValue('pnp_id')
.then(this._decodeString);
}
/* Utils */
_cacheCharacteristic(service, characteristicUuid) {
return service.getCharacteristic(characteristicUuid)
.then(characteristic => {
this._characteristics.set(characteristicUuid, characteristic);
});
}
_readCharacteristicValue(characteristicUuid) {
let characteristic = this._characteristics.get(characteristicUuid);
return characteristic.readValue()
.then(value => {
// In Chrome 50+, a DataView is returned instead of an ArrayBuffer.
value = value.buffer ? value : new DataView(value);
if (this._debug) {
for (var i = 0, a = []; i < value.byteLength; i++) { a.push(value.getUint8(i)); }
console.debug('READ', characteristic.uuid, a);
}
return value;
});
}
_writeCharacteristicValue(characteristicUuid, value) {
let characteristic = this._characteristics.get(characteristicUuid);
if (this._debug) {
console.debug('WRITE', characteristic.uuid, value);
}
return characteristic.writeValue(value);
}
_decodeString(data) {
return decoder.decode(data);
}
_encodeString(data) {
return encoder.encode(data);
}
}
window.playbulbCandle = new PlaybulbCandle();
})();