-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
262 lines (206 loc) · 6.36 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
window.store = { hue: 0 }
window.addEventListener('load', async function() {
onclick('#connection', connection)
onclick('#on', ev => c9Command('on'))
onclick('#off', ev => c9Command('off'))
onchange('#lightness', ev => paintColorsWheel(ev.target.value))
onmousemove('#panel', ev => mouseoverPanel(ev.offsetX, ev.offsetY))
onclick('#panel', ev => clickPanel(ev.offsetX, ev.offsetY))
onchange('#command', ev => {
const command = localStorage.commandSelectedOption = ev.target.value
showParams(command)
})
onclick('.go', ev => go(ev.target))
elem('#colorsUI', el => {
const style = getComputedStyle(el)
el.width = 200
el.height = 200
store.ctx = el.getContext('2d')
})
if (!localStorage.commandSelectedOption)
localStorage.commandSelectedOption = 'rgb'
updateView()
elem('#panel', el => el.dataset.show = true)
})
async function connection() {
if (store.gatt) {
bluetoothDisconnect()
delete store.gatt
delete store.light
}
else {
await bluetoothConnect()
}
updateView()
}
async function go(el) {
const command = elem('#command')[0].value
switch (command) {
case 'brightness': {
const paramBrightness = +elem('#param_brightness')[0].value
await c9Command('brightness', paramBrightness)
}; break
case 'rgb': {
const paramRGB = getRGB(elem('#param_rgb')[0].value)
await c9Command('rgb', ...paramRGB)
}; break
default: console.error('Unknown command', command)
}
}
function elem(sel, f) {
if ('function' != typeof f) f = _ => _
return Array.from(document.querySelectorAll(sel)).map(f)
}
function onclick(sel, f) {
if ('function' != typeof f) f = ev => console.debug(ev)
return elem(sel, el => el.addEventListener('click', f))
}
function onchange(sel, f) {
if ('function' != typeof f) f = ev => console.debug(ev)
return elem(sel, el => el.addEventListener('change', f))
}
function onmousemove(sel, f) {
if ('function' != typeof f) f = ev => console.debug(ev)
return elem(sel, el => el.addEventListener('mousemove', f))
}
function updateView() {
const connected = !!store.gatt
elem('#panel', el => el.dataset.show = connected)
elem('#connection', el => el.innerText = connected ? 'disconnect' : 'connect')
paintColorsWheel(elem('#lightness')[0].value)
elem('#command', el => el.value = localStorage.commandSelectedOption)
showParams(localStorage.commandSelectedOption)
}
function showParams(command) {
elem('.params', el => el.dataset.show = false)
elem('#cmd_' + command, el => el.dataset.show = true)
}
function paintColorsWheel(lightness) {
const d2r = Math.PI / 180
const d = 5
const ctx = store.ctx
ctx.lineWidth = 50
for (let i = 0; i < 360; i += d) {
ctx.strokeStyle = hsl(i, lightness)
ctx.beginPath()
ctx.arc(100, 100, 70, i * d2r , (i + d + 0.5) * d2r)
ctx.stroke()
}
}
function paintCentralColor(hue, lightness) {
const ctx = store.ctx
ctx.fillStyle = hsl(hue, lightness)
ctx.fillRect(75, 75, 50, 50)
ctx.lineWidth = 1
ctx.strokeStyle = 'grey'
ctx.strokeRect(74, 74, 52, 52)
}
function hsl(hue, lightness) {
return `hsl(${hue},100%,${lightness}%)`
}
function mouseoverPanel(x, y) {
const hue = getHue(x, y)
if (isNaN(hue)) return
store.hue = hue
paintCentralColor(hue, elem('#lightness')[0].value)
}
function clickPanel(x, y) {
const hue = getHue(x, y)
if (isNaN(hue)) return
store.hue = hue
const lightness = elem('#lightness')[0].value
paintCentralColor(hue, elem)
c9Command('rgb', ...getRGB(hsl(hue, lightness)))
}
function getHue(x, y) {
x -= 100
y -= 100
const r = Math.sqrt(x * x + y * y)
if (r < 40 || r > 90) return NaN
const theta = Math.atan2(y, x)
return 180 * theta / Math.PI
}
function getRGB(cssColor) {
const colorConverterElement = elem('#cconv')[0]
colorConverterElement.style.color = cssColor
return getComputedStyle(colorConverterElement).color
.match(/^rgb\((\d+), *(\d+), *(\d+)\)$/)
.slice(1).map(x => +x)
}
// --- Web Bluetooth functions ---
async function bluetoothConnect() {
const device = await navigator.bluetooth.requestDevice({
filters: [{ name: '' }, { namePrefix: 'SML' }],
optionalServices: [ 0x180a, 0xfff0 ]
})
store.gatt = await device.gatt.connect()
const service = await store.gatt.getPrimaryService(0xfff0)
store.light = await service.getCharacteristic(0xfff1)
if (!store.light) throw new Error('characteristic not ready')
}
async function bluetoothDisconnect() {
store.gatt.disconnect()
}
async function c9Command(what, ...args) {
const headBytes = [0xAA, 0x0A, 0xFC, 0x3A, 0x86, 0x01]
const onBytes = [0x0A, 0x01, 0x01, 0x00, 0x28]
const offBytes = [0x0A, 0x01, 0x00, 0x01, 0x28]
const brightnessBytes = [0x0C, 0x01]
const rgb1Bytes = [0x0D, 0x06]
const rgb2Bytes = [0x20, 0x30]
const lastByte = 0x0d
switch (what) {
case 'on': await send(onCode()); break
case 'off': await send(offCode()); break
case 'rgb': await send(rgbCode(...args)); break
case 'brightness': await send(brightnessCode(...args)); break
default: console.error('Unknown command', what)
}
async function send(code) {
console.debug('sending', codeToString(code))
await store.light.writeValue(new Uint8Array(code))
console.debug('sent!')
}
function onCode() { return [...headBytes, ...onBytes, lastByte] }
function offCode() { return [...headBytes, ...offBytes, lastByte] }
function rgbCode(r, g, b, slow) {
console.log(`rgb(${r},${g},${b})`)
const code = [
...headBytes, ...rgb1Bytes,
toByte(slow), toByte(r), toByte(g), toByte(b),
...rgb2Bytes, 137,
]
code.push(checksum(code))
code.push(lastByte)
return code
}
function brightnessCode(brightness) {
const code = [
...headBytes, ...brightnessBytes, toByte(9 * brightness + 2),
]
code.push(checksum(code))
code.push(lastByte)
return code
}
// see dragouf/aws-smartlight lib.js:129 https://goo.gl/mxMRxu
function checksum(code) {
return (code.slice(1).reduce((a, b) => a + b) + 85) & 0xFF
}
function codeToString(code) {
return code.map(_ => _.toString(16).padStart(2, '0')).join(' ')
}
function toByte(x) {
x = x | 0
return x < 0 ? 0 : x > 255 ? 255 : x
}
}
// 0x2a24 SML-c9
// 0x2a26 1.0
// 0x2a27 1.0
// 0x2a28 1.0.0
// 0x2a29 AwoX
// 0xfff1 write bytes and read one byte
// 0xfff2 read one byte
// 0xfff3 write bytes
// 0xfff4 subscribe
// 0xfff5 pair