-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
566 lines (485 loc) · 16.5 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import Base64 from 'base64-arraybuffer';
// Services
const CUBE_STATE_SERVICE = '0000aadb-0000-1000-8000-00805f9b34fb';
const CUBE_INFO_SERVICE = '0000aaaa-0000-1000-8000-00805f9b34fb';
// Characteristics
const CUBE_STATE_RESPONSE = '0000aadc-0000-1000-8000-00805f9b34fb';
const CUBE_INFO_RESPONSE = '0000aaab-0000-1000-8000-00805f9b34fb';
const CUBE_INFO_REQUEST = '0000aaac-0000-1000-8000-00805f9b34fb';
// Commands for cube info service
const WRITE_MOVE_COUNT = 0xCC;
const WRITE_RESET_SOLVED = 0xA1;
const WRITE_RESET_CUSTOM = 0xA4;
const WRITE_BATTERY = 0xB5;
// Charging possible states
const batteryStates = [
{ code: 1, description: 'Charged' },
{ code: 2, description: 'Charging' },
{ code: 3, description: 'Not Charging' }
];
// face indexes
const B = 0;
const D = 1;
const L = 2;
const U = 3;
const R = 4;
const F = 5;
const faces = ['B', 'D', 'L', 'U', 'R', 'F'];
// color indexes
const b = 0;
const y = 1;
const o = 2;
const w = 3;
const r = 4;
const g = 5;
const colors = ['blue', 'yellow', 'orange', 'white', 'red', 'green'];
const turns = {
0: 1,
1: 2,
2: -1,
8: -2,
};
const cornerColors = [
[y, r, g],
[r, w, g],
[w, o, g],
[o, y, g],
[r, y, b],
[w, r, b],
[o, w, b],
[y, o, b]
];
const cornerLocations = [
[D, R, F],
[R, U, F],
[U, L, F],
[L, D, F],
[R, D, B],
[U, R, B],
[L, U, B],
[D, L, B]
];
const edgeLocations = [
[F, D],
[F, R],
[F, U],
[F, L],
[D, R],
[U, R],
[U, L],
[D, L],
[B, D],
[B, R],
[B, U],
[B, L]
];
const edgeColors = [
[g, y],
[g, r],
[g, w],
[g, o],
[y, r],
[w, r],
[w, o],
[y, o],
[b, y],
[b, r],
[b, w],
[b, o]
];
class EventEmitter {
constructor() {
this.listeners = {};
}
on(label, callback) {
if (!this.listeners[label]) {
this.listeners[label] = [];
}
this.listeners[label].push(callback);
}
off(label, callback) {
let listeners = this.listeners[label];
if (listeners && listeners.length > 0) {
let index = listeners.indexOf(callback);
if (index > -1) {
listeners.splice(index, 1);
this.listeners[label] = listeners;
return true;
}
}
return false;
}
emit(label, ...args) {
let listeners = this.listeners[label];
if (listeners && listeners.length > 0) {
listeners.forEach((listener) => {
listener(...args);
});
return true;
}
return false;
}
}
export default class Giiker extends EventEmitter {
// This constructor needs a Device object from 'react-native-ble-plx' package
constructor(device) {
super();
this._isConnected = false;
this._cubeReseted = false;
this._state = {};
this._device = device;
this._services = [];
this._stateService = null;
this._infoService = null;
this._stateServiceCharacteristics = [];
this._infoServiceCharacteristics = [];
this._cubeStateCharacteristic = null;
// Sometimes the cube triggers a 'move' event right after connecting, which would emit a 'fake move event'
// This flag is used to check if the first value (read before monitor the characteristic) is the same of the next one
this._firstStateFlag = true;
}
// Connect to the device
async connect() {
this._device.connect().then((device) => {
return device.discoverAllServicesAndCharacteristics();
}).then((device) => {
this._device = device;
this._setup();
}).catch((error) => {
// Handle errors
console.log(error);
this.disconnect();
});
}
/**
* Disconnects from the GiiKER cube. Will fire the `disconnected` event once done.
*/
disconnect() {
if (!this._device) {
return;
}
return this._device.cancelConnection();
}
// Get services and characteristics
async _setup() {
//get device services
this._services = await this._device.services();
// get main services
this._stateService = this._services.find(srv => srv.uuid == CUBE_STATE_SERVICE);
this._infoService = this._services.find(srv => srv.uuid == CUBE_INFO_SERVICE);
// get service's characteristics
this._stateServiceCharacteristics = await this._stateService.characteristics();
this._infoServiceCharacteristics = await this._infoService.characteristics();
// get cube state characteristic
this._cubeStateCharacteristic = this._stateServiceCharacteristics.find(char => char.uuid == CUBE_STATE_RESPONSE);
// read cube state
const valuedCharacteristic = await this._cubeStateCharacteristic.read();
// converts base64 to ArrayBuffer
const arrayBuffer = Base64.decode(valuedCharacteristic.value);
// parse cube value
this._state = this._parseCubeValue(new DataView(arrayBuffer)).state;
// handle moves on the cube
this._cubeStateCharacteristic.monitor((error, characteristic) => {
// Handle possible errors
if (error) return;
// Verify if the 'fake move event' was triggered. If true, skip it
if (this._firstStateFlag && valuedCharacteristic.value == characteristic.value) {
this._firstStateFlag = false;
return;
}
const { state, moves } = this._parseCubeValue(new DataView(Base64.decode(characteristic.value)));
this._state = state;
// After reset cube to solved, a move event is triggered by giiker
// This prevent emit 'move' event to listeners
if (this._cubeReseted) {
this.emit('update state');
} else {
this.emit('move', moves[0]);
}
this._cubeReseted = false;
this._firstStateFlag = false;
});
// handle disconnection
this._device.onDisconnected((error, device) => {
this.emit("disconnected");
});
this.connected = true;
this.emit("connected");
}
/**
* Returns a promise that will resolve to the current battery level as percentage and the charging status, it will
* also emit an event containing the data.
*
* If stopMonitor param given was false, it will continue listening the characteristic for new updates
*
* In order to stop monitoring, just call stopBatteryMonitor function
*/
async getBatteryLevel(stopMonitor = true) {
const readCharacteristic = this._infoServiceCharacteristics.find(char => char.uuid == CUBE_INFO_RESPONSE);
const writeCharacteristic = this._infoServiceCharacteristics.find(char => char.uuid == CUBE_INFO_REQUEST);
writeCharacteristic.writeWithoutResponse(Base64.encode(new Uint8Array([WRITE_BATTERY])));
return new Promise((resolve) => {
this._batteryMonitor = readCharacteristic.monitor((error, characteristic) => {
// Handle possible errors
if (error) return;
const value = new DataView(Base64.decode(characteristic.value));
// Verify if the characteristic returned the BATTERY INFO. It's necessary, because the
// same characterisct may return different information, like battery, move count, ...
if (value.getUint8(0) == WRITE_BATTERY) {
const batteryLevel = value.getUint8(1);
const chargingState = batteryStates.find((state) => state.code == value.getUint8(2));
this.emit('battery', { batteryLevel, chargingState });
resolve({ batteryLevel, chargingState });
if (stopMonitor)
this.stopBatteryMonitor();
}
}, 'MONITOR_BATTERY');
});
}
/**
* Returns a promise that will resolve to the total number of moves performed with this cube, it will also emit an
* event containing the data.
*
* If stopMonitor param given was false, it will continue listening the characteristic for new updates
*
* In order to stop monitoring, just call stopMoveCountMonitor function
*/
async getMoveCount(stopMonitor = true) {
const readCharacteristic = this._infoServiceCharacteristics.find(char => char.uuid == CUBE_INFO_RESPONSE);
const writeCharacteristic = this._infoServiceCharacteristics.find(char => char.uuid == CUBE_INFO_REQUEST);
writeCharacteristic.writeWithoutResponse(Base64.encode(new Uint8Array([WRITE_MOVE_COUNT])));
return new Promise((resolve) => {
this._moveCountMonitor = readCharacteristic.monitor((error, characteristic) => {
// Handle possible errors
if (error) return;
const value = new DataView(Base64.decode(characteristic.value));
// Verify if the characteristic returned the MOVE COUNT. It's necessary, because the
// same characterisct may return different information, like battery, move count, ...
if (value.getUint8(0) == WRITE_MOVE_COUNT) {
const moveCount = value.getUint8(4) +
(256 * value.getUint8(3)) +
(65536 * value.getUint8(2)) +
(16777216 * (value.getUint8(1)));
resolve(moveCount);
this.emit('move count', moveCount);
if (stopMonitor)
this.stopMoveCountMonitor();
}
}, 'MONITOR_MOVE_COUNT');
});
}
/**
* Stop monitoring battery
*/
stopBatteryMonitor() {
this._batteryMonitor.remove();
}
/**
* Stop monitoring move count
*/
stopMoveCountMonitor() {
this._moveCountMonitor.remove();
}
/**
* Check if Giiker is connected
*/
isConnected() {
return this._isConnected;
}
/**
* Reset the cube's internal state to solved
*/
async resetSolved() {
const writeCharacteristic = this._infoServiceCharacteristics.find(char => char.uuid == CUBE_INFO_REQUEST);
await writeCharacteristic.writeWithoutResponse(Base64.encode(new Uint8Array([WRITE_RESET_SOLVED])));
this._cubeReseted = true;
}
_parseCubeValue(value) {
const state = {
cornerPositions: [],
cornerOrientations: [],
edgePositions: [],
edgeOrientations: []
};
const moves = [];
for (let i = 0; i < value.byteLength; i++) {
const move = value.getUint8(i);
const highNibble = move >> 4;
const lowNibble = move & 0b1111;
if (i < 4) {
state.cornerPositions.push(highNibble, lowNibble);
} else if (i < 8) {
state.cornerOrientations.push(highNibble, lowNibble);
} else if (i < 14) {
state.edgePositions.push(highNibble, lowNibble);
} else if (i < 16) {
state.edgeOrientations.push(!!(move & 0b10000000));
state.edgeOrientations.push(!!(move & 0b01000000));
state.edgeOrientations.push(!!(move & 0b00100000));
state.edgeOrientations.push(!!(move & 0b00010000));
if (i === 14) {
state.edgeOrientations.push(!!(move & 0b00001000));
state.edgeOrientations.push(!!(move & 0b00000100));
state.edgeOrientations.push(!!(move & 0b00000010));
state.edgeOrientations.push(!!(move & 0b00000001));
}
} else {
moves.push(this._parseMove(highNibble, lowNibble));
}
}
return { state, moves };
}
_parseMove(faceIndex, turnIndex) {
const face = faces[faceIndex - 1];
const amount = turns[turnIndex - 1];
let notation = face;
switch (amount) {
case 2: notation = `${face}2`; break;
case -1: notation = `${face}'`; break;
case -2: notation = `${face}2'`; break;
}
return { face, amount, notation };
}
_mapCornerColors(colors, orientation, position) {
const actualColors = [];
if (orientation !== 3) {
if (position === 0 || position === 2 || position === 5 || position === 7) {
orientation = 3 - orientation;
}
}
switch (orientation) {
case 1:
actualColors[0] = colors[1];
actualColors[1] = colors[2];
actualColors[2] = colors[0];
break;
case 2:
actualColors[0] = colors[2];
actualColors[1] = colors[0];
actualColors[2] = colors[1];
break;
case 3:
actualColors[0] = colors[0];
actualColors[1] = colors[1];
actualColors[2] = colors[2];
break;
}
return actualColors;
}
_mapEdgeColors(colors, orientation) {
const actualColors = [...colors];
if (orientation) {
actualColors.reverse();
}
return actualColors;
}
/**
* Returns the current state of the cube as arrays of corners and edges.
*
* Example how to interpret the state:
*
* Corner:
* ```
* {
* position: ['D', 'R', 'F'],
* colors: ['yellow', 'red', 'green']
* }
* ```
* The corner in position DRF has the colors yellow on D, red on R and green ON F.
*
* Edge:
* ```
* {
* position: ['F', 'U'],
* colors: ['green', 'white']
* }
* ```
* The edge in position FU has the colors green on F and white on U.
*/
get state() {
const state = {
corners: [],
edges: []
};
this._state.cornerPositions.forEach((cp, index) => {
const mappedColors = this._mapCornerColors(
cornerColors[cp - 1],
this._state.cornerOrientations[index],
index
);
state.corners.push({
position: cornerLocations[index].map((f) => faces[f]),
colors: mappedColors.map((c) => colors[c])
});
});
this._state.edgePositions.forEach((ep, index) => {
const mappedColors = this._mapEdgeColors(
edgeColors[ep - 1],
this._state.edgeOrientations[index]
);
state.edges.push({
position: edgeLocations[index].map((f) => faces[f]),
colors: mappedColors.map((c) => colors[c])
});
});
return state;
}
/**
* Returns the current state of the cube as a string compatible with cubejs.
*
* See https://github.com/ldez/cubejs#cubefromstringstr
*/
get stateString() {
const cornerFaceIndices = [
[29, 15, 26],
[9, 8, 20],
[6, 38, 18],
[44, 27, 24],
[17, 35, 51],
[2, 11, 45],
[36, 0, 47],
[33, 42, 53]
];
const edgeFaceIndices = [
[25, 28],
[23, 12],
[19, 7],
[21, 41],
[32, 16],
[5, 10],
[3, 37],
[30, 43],
[52, 34],
[48, 14],
[46, 1],
[50, 39]
];
const colorFaceMapping = {
blue: 'B',
yellow: 'D',
orange: 'L',
white: 'U',
red: 'R',
green: 'F'
};
const state = this.state;
const faces = [];
state.corners.forEach((corner, cornerIndex) => {
corner.position.forEach((face, faceIndex) => {
faces[cornerFaceIndices[cornerIndex][faceIndex]] = colorFaceMapping[corner.colors[faceIndex]];
});
});
state.edges.forEach((edge, edgeIndex) => {
edge.position.forEach((face, faceIndex) => {
faces[edgeFaceIndices[edgeIndex][faceIndex]] = colorFaceMapping[edge.colors[faceIndex]];
});
});
faces[4] = 'U';
faces[13] = 'R';
faces[22] = 'F';
faces[31] = 'D';
faces[40] = 'L';
faces[49] = 'B';
return faces.join('');
}
}