-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathble.js
More file actions
317 lines (283 loc) · 12.5 KB
/
Copy pathble.js
File metadata and controls
317 lines (283 loc) · 12.5 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// MotherLEDisa — BLE Communication Layer (Web Bluetooth API)
// Supports ELK-BLEDOM, MELK, and MELK-OT21 tower lights
const BLE = (() => {
const SERVICE_UUID = '0000fff0-0000-1000-8000-00805f9b34fb';
const CHAR_UUID = '0000fff3-0000-1000-8000-00805f9b34fb';
const devices = new Map();
const listeners = new Set();
const MIN_CMD_GAP = 20;
let lastWrite = 0;
function notify() { listeners.forEach(fn => fn(getConnected())); }
function onChange(fn) { listeners.add(fn); return () => listeners.delete(fn); }
function getConnected() {
return Array.from(devices.entries()).map(([id, d]) => ({
id, name: d.name, connected: d.server?.connected ?? false,
}));
}
async function scan() {
if (!navigator.bluetooth) throw new Error('Web Bluetooth not supported. Use Chrome or Edge.');
if (!window.isSecureContext) throw new Error('Web Bluetooth requires HTTPS or localhost.');
console.log('[BLE] Requesting device...');
return navigator.bluetooth.requestDevice({
filters: [
{ namePrefix: 'ELK-' },
{ namePrefix: 'MELK-' },
{ namePrefix: 'LEDBLE' },
{ namePrefix: 'LED-' },
],
optionalServices: [SERVICE_UUID],
});
}
async function connect(device) {
const id = device.id;
if (devices.has(id) && devices.get(id).server?.connected) return devices.get(id);
devices.delete(id);
console.log(`[BLE] Connecting to ${device.name}...`);
const server = await device.gatt.connect();
const service = await server.getPrimaryService(SERVICE_UUID);
const characteristic = await service.getCharacteristic(CHAR_UUID);
const props = characteristic.properties;
const entry = {
device, server, characteristic,
name: device.name || 'Unknown',
writeMethod: props.writeWithoutResponse ? 'no-resp' : 'resp',
};
// MELK init sequence
if (device.name?.toUpperCase().startsWith('MELK')) {
console.log('[BLE] MELK init...');
try {
await writeBytes(entry, [0x7E, 0x07, 0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF]);
await sleep(100);
await writeBytes(entry, [0x7E, 0x04, 0x04, 0xF0, 0x00, 0x01, 0xFF, 0x00, 0xEF]);
await sleep(100);
} catch (e) { console.warn('[BLE] MELK init warning:', e.message); }
}
devices.set(id, entry);
device.addEventListener('gattserverdisconnected', () => {
console.log(`[BLE] ${entry.name} disconnected`);
devices.delete(id);
notify();
});
notify();
console.log(`[BLE] Connected: ${entry.name}`);
return entry;
}
async function writeBytes(entry, bytes) {
const wait = MIN_CMD_GAP - (Date.now() - lastWrite);
if (wait > 0) await sleep(wait);
lastWrite = Date.now();
const data = new Uint8Array(bytes);
if (entry.writeMethod === 'no-resp') {
await entry.characteristic.writeValueWithoutResponse(data);
} else {
await entry.characteristic.writeValueWithResponse(data);
}
}
async function disconnect(id) {
const e = devices.get(id);
if (e) { try { e.device.gatt.disconnect(); } catch {} devices.delete(id); notify(); }
}
async function write(id, bytes) {
const entry = devices.get(id);
if (!entry) return;
try { await writeBytes(entry, bytes); }
catch (e) {
console.warn(`[BLE] Write fail (${entry.name}):`, e.message);
if (!entry.server?.connected) { devices.delete(id); notify(); }
}
}
async function writeAll(bytes) {
await Promise.allSettled(Array.from(devices.keys()).map(id => write(id, bytes)));
}
async function writeTarget(t, bytes) {
if (t === 'all') await writeAll(bytes); else await write(t, bytes);
}
function clamp(v, lo, hi) { return Math.max(lo, Math.min(hi, Math.round(v))); }
function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
// --- Protocol Commands (9-byte fixed) ---
const CMD = {
powerOn: () => [0x7E, 0x04, 0x04, 0xF0, 0x00, 0x01, 0xFF, 0x00, 0xEF],
powerOff: () => [0x7E, 0x04, 0x04, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xEF],
setColor: (r,g,b) => [0x7E, 0x07, 0x05, 0x03, r&0xFF, g&0xFF, b&0xFF, 0x10, 0xEF],
setBrightness: (v) => [0x7E, 0x04, 0x01, clamp(v,0,100), 0xFF, 0x00, 0xFF, 0x00, 0xEF],
setEffect: (id,spd) => [0x7E, 0x05, 0x03, id&0xFF, clamp(spd,0,100), 0xFF, 0xFF, 0x00, 0xEF],
setSpeed: (v) => [0x7E, 0x04, 0x02, clamp(v,0,100), 0xFF, 0xFF, 0xFF, 0x00, 0xEF],
micEnable: () => [0x7E, 0x04, 0x07, 0x01, 0xFF, 0xFF, 0xFF, 0x00, 0xEF],
micDisable: () => [0x7E, 0x04, 0x07, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0xEF],
micEffect: (id) => [0x7E, 0x05, 0x03, id&0xFF, 0x04, 0xFF, 0xFF, 0x00, 0xEF],
micSensitivity:(v) => [0x7E, 0x04, 0x06, clamp(v,0,100), 0xFF, 0xFF, 0xFF, 0x00, 0xEF],
disableEffects: () => [0x7E, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0xEF],
// MELK-OA21 extended commands
// Symphony: audio sync point (10-1000, little-endian 16-bit)
setSymphony: (point) => {
const p = Math.max(10, Math.min(1000, Math.round(point)));
return [0x7E, 0x07, 0x21, p & 0xFF, (p >> 8) & 0xFF, 0x00, 0x00, 0x00, 0xEF];
},
// Scene: 28 preset scenes (0-27)
setScene: (id) => [0x7E, 0x05, 0x31, id & 0xFF, 0x07, 0xFF, 0xFF, 0x01, 0xEF],
};
// High-level API
async function powerOn(t) { await writeTarget(t, CMD.powerOn()); }
async function powerOff(t) { await writeTarget(t, CMD.powerOff()); }
async function setColor(t, r, g, b) { await writeTarget(t, CMD.setColor(r, g, b)); }
async function setBrightness(t, v) { await writeTarget(t, CMD.setBrightness(v)); }
async function setEffect(t, id, s) { await writeTarget(t, CMD.setEffect(id, s)); }
async function setSpeed(t, v) { await writeTarget(t, CMD.setSpeed(v)); }
async function enableMic(t) { await writeTarget(t, CMD.micEnable()); }
async function disableMic(t) { await writeTarget(t, CMD.micDisable()); }
async function setMicEffect(t, id) { await writeTarget(t, CMD.micEffect(id)); }
async function setMicSensitivity(t,v){ await writeTarget(t, CMD.micSensitivity(v)); }
// Activate sound reactive: enable mic, set effect, set sensitivity
async function activateSoundMode(t, effectId, sensitivity) {
await writeTarget(t, CMD.micEnable());
await sleep(50);
await writeTarget(t, CMD.micEffect(effectId));
await sleep(50);
await writeTarget(t, CMD.micSensitivity(sensitivity));
}
// Deactivate sound reactive
async function deactivateSoundMode(t) {
await writeTarget(t, CMD.micDisable());
}
// --- Experimental: reverse engineering commands ---
// Send raw 9-byte command for testing
async function sendRaw(t, bytes) {
console.log('[BLE:RAW]', bytes.map(b => b.toString(16).padStart(2,'0')).join(' '));
await writeTarget(t, bytes);
}
// Test: set color while mic mode is active (does it tint the effect?)
async function testMicWithColor(t, effectId, sensitivity, r, g, b) {
await writeTarget(t, CMD.micEnable());
await sleep(50);
await writeTarget(t, CMD.micEffect(effectId));
await sleep(50);
await writeTarget(t, CMD.micSensitivity(sensitivity));
await sleep(50);
// Try sending color on top of mic mode
await writeTarget(t, CMD.setColor(r, g, b));
console.log('[BLE:EXP] Sent color on top of mic mode');
}
// Test: set color first, then enable mic (does mic use the color as base?)
async function testColorThenMic(t, r, g, b, effectId, sensitivity) {
await writeTarget(t, CMD.setColor(r, g, b));
await sleep(100);
await writeTarget(t, CMD.micEnable());
await sleep(50);
await writeTarget(t, CMD.micEffect(effectId));
await sleep(50);
await writeTarget(t, CMD.micSensitivity(sensitivity));
console.log('[BLE:EXP] Set color then mic mode');
}
// Test: undocumented streaming commands
async function testStreamCmd(t, cmdId, param) {
const cmd = [0x7E, 0x04, cmdId & 0xFF, param & 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xEF];
console.log('[BLE:EXP] Stream cmd', cmdId.toString(16), 'param', param.toString(16));
await writeTarget(t, cmd);
}
// Test: set effect while mic mode is on (combine hardware effect with mic?)
async function testMicWithEffect(t, micEffectId, hwEffectId, speed, sensitivity) {
await writeTarget(t, CMD.micEnable());
await sleep(50);
await writeTarget(t, CMD.micEffect(micEffectId));
await sleep(50);
await writeTarget(t, CMD.micSensitivity(sensitivity));
await sleep(50);
// Now try a hardware effect on top
await writeTarget(t, CMD.setEffect(hwEffectId, speed));
console.log('[BLE:EXP] Sent hw effect on top of mic mode');
}
// Test: brightness during mic mode
async function testMicWithBrightness(t, effectId, sensitivity, brightness) {
await writeTarget(t, CMD.micEnable());
await sleep(50);
await writeTarget(t, CMD.micEffect(effectId));
await sleep(50);
await writeTarget(t, CMD.micSensitivity(sensitivity));
await sleep(50);
await writeTarget(t, CMD.setBrightness(brightness));
console.log('[BLE:EXP] Set brightness during mic mode');
}
// Test: symphony command (MELK-OA21 audio sync point)
async function testSymphony(t, point) {
const cmd = CMD.setSymphony(point);
console.log('[BLE:EXP] Symphony point', point, '->', cmd.map(b=>b.toString(16).padStart(2,'0')).join(' '));
await writeTarget(t, cmd);
}
// Test: scene command (MELK-OA21 preset scenes)
async function testScene(t, sceneId) {
const cmd = CMD.setScene(sceneId);
console.log('[BLE:EXP] Scene', sceneId, '->', cmd.map(b=>b.toString(16).padStart(2,'0')).join(' '));
await writeTarget(t, cmd);
}
// Test: symphony while mic is active
async function testMicWithSymphony(t, micEffect, sensitivity, symphonyPoint) {
await writeTarget(t, CMD.micEnable());
await sleep(50);
await writeTarget(t, CMD.micEffect(micEffect));
await sleep(50);
await writeTarget(t, CMD.micSensitivity(sensitivity));
await sleep(50);
await writeTarget(t, CMD.setSymphony(symphonyPoint));
console.log('[BLE:EXP] Mic + Symphony point', symphonyPoint);
}
// Test: speed during mic mode
async function testMicWithSpeed(t, effectId, sensitivity, speed) {
await writeTarget(t, CMD.micEnable());
await sleep(50);
await writeTarget(t, CMD.micEffect(effectId));
await sleep(50);
await writeTarget(t, CMD.micSensitivity(sensitivity));
await sleep(50);
await writeTarget(t, CMD.setSpeed(speed));
console.log('[BLE:EXP] Set speed during mic mode');
}
// Effects library
const EFFECTS = [
{ id: 0x87, name: 'Jump RGB', cat: 'jump' },
{ id: 0x88, name: 'Jump Rainbow', cat: 'jump' },
{ id: 0x89, name: 'Crossfade RGB', cat: 'fade' },
{ id: 0x8A, name: 'Crossfade Rainbow',cat: 'fade' },
{ id: 0x8B, name: 'Crossfade Red', cat: 'fade' },
{ id: 0x8C, name: 'Crossfade Green', cat: 'fade' },
{ id: 0x8D, name: 'Crossfade Blue', cat: 'fade' },
{ id: 0x8E, name: 'Crossfade Yellow', cat: 'fade' },
{ id: 0x8F, name: 'Crossfade Cyan', cat: 'fade' },
{ id: 0x90, name: 'Crossfade Purple', cat: 'fade' },
{ id: 0x91, name: 'Crossfade White', cat: 'fade' },
{ id: 0x92, name: 'Crossfade R-G', cat: 'fade' },
{ id: 0x93, name: 'Crossfade R-B', cat: 'fade' },
{ id: 0x94, name: 'Crossfade G-B', cat: 'fade' },
{ id: 0x95, name: 'Strobe Rainbow', cat: 'strobe' },
{ id: 0x96, name: 'Strobe Red', cat: 'strobe' },
{ id: 0x97, name: 'Strobe Green', cat: 'strobe' },
{ id: 0x98, name: 'Strobe Blue', cat: 'strobe' },
{ id: 0x99, name: 'Strobe Yellow', cat: 'strobe' },
{ id: 0x9A, name: 'Strobe Cyan', cat: 'strobe' },
{ id: 0x9B, name: 'Strobe Purple', cat: 'strobe' },
{ id: 0x9C, name: 'Strobe White', cat: 'strobe' },
];
const MIC_EFFECTS = [
{ id: 0x80, name: 'Energetic' },
{ id: 0x81, name: 'Rhythm' },
{ id: 0x82, name: 'Spectrum' },
{ id: 0x83, name: 'Rolling' },
{ id: 0x84, name: 'Pulse' },
{ id: 0x85, name: 'Cascade' },
{ id: 0x86, name: 'Rainbow' },
{ id: 0x87, name: 'Wave' },
];
return {
scan, connect, disconnect, getConnected, onChange,
powerOn, powerOff, setColor, setBrightness,
setEffect, setSpeed, enableMic, disableMic,
setMicEffect, setMicSensitivity,
activateSoundMode, deactivateSoundMode,
// Experimental
sendRaw, testMicWithColor, testColorThenMic,
testStreamCmd, testMicWithEffect,
testMicWithBrightness, testMicWithSpeed,
testSymphony, testScene, testMicWithSymphony,
write, writeAll, writeTarget,
CMD, EFFECTS, MIC_EFFECTS, devices, sleep,
};
})();