-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathATC_RF03_Writer.html
More file actions
808 lines (730 loc) · 26.3 KB
/
ATC_RF03_Writer.html
File metadata and controls
808 lines (730 loc) · 26.3 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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ATC_RF03_Ring</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f9;
color: #333;
}
h1 {
color: #11b8c4;
text-align: center;
}
button {
background-color: #11b8c4;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
cursor: pointer;
border-radius: 5px;
font-size: 16px;
}
button:hover {
background-color: #11b8c4;
}
#log {
border: 1px solid #ccc;
overflow-y: auto;
height: 160px;
background-color: #ffffff;
padding: 10px;
margin-top: 20px;
border-radius: 5px;
font-family: monospace;
}
.progress-container,
.debug-container {
background-color: #ffffff;
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
}
.progress-bar {
height: 24px;
width: 0;
background-color: #11b8c4;
text-align: center;
color: rgb(0, 0, 0);
border-radius: 5px;
transition: width 0.3s;
}
input[type="file"],
input[type="text"] {
padding: 5px;
margin: 5px 0;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
.instructions {
font-size: 14px;
color: #777;
}
.warning {
color: red;
font-weight: bold;
}
canvas {
display: block;
}
#container {
width: 100%;
height: 40%;
display: none;
/* Start with the div hidden */
}
#controls {
display: flex;
justify-content: center;
margin-top: 20px;
}
.slider-container {
margin: 0 10px;
}
</style>
</head>
<body>
<script src="three.min.js"></script>
<script>
let rotateX = 0;
let rotateY = 0;
let rotateZ = 0;
let scene, camera, renderer, cylinder, animationId;
function startAnimation() {
let container = document.getElementById('container');
if (container.style.display != 'block') {
container.style.display = 'block';
init();
animate();
}
sendDataArray(hexToBytes("A10404"));
}
function init() {
let container = document.getElementById('container');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
camera.position.z = 5;
renderer = new THREE.WebGLRenderer();
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
const radiusTop = 1;
const radiusBottom = 1;
const height = 0.6;
const radialSegments = 100;
const heightSegments = 1;
const openEnded = true;
const geometry = new THREE.CylinderGeometry(radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded);
const material = new THREE.MeshBasicMaterial({ color: 0x0077ff, wireframe: true });
cylinder = new THREE.Mesh(geometry, material);
scene.add(cylinder);
// Create a sphere
const sphereGeometry = new THREE.SphereGeometry(0.2, 32, 32); // Adjust radius and segments as needed
const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
// Calculate the position to attach the sphere on the cylinder
const attachPoint = new THREE.Vector3(1, 0, 0); // Example: attach at the top middle of the cylinder
// Attach the sphere to the cylinder
cylinder.add(sphere);
sphere.position.copy(attachPoint);
window.addEventListener('resize', onWindowResize);
}
function animate() {
animationId = requestAnimationFrame(animate);
if (cylinder) {
cylinder.rotation.x = (rotateX);
cylinder.rotation.y = (rotateY);
cylinder.rotation.z = (rotateZ);
}
renderer.render(scene, camera);
}
function onWindowResize() {
let container = document.getElementById('container');
if (renderer && camera) {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
}
function int16(uint16) {
return uint16 > 32767 ? uint16 - 65536 : uint16;
}
function int12(uint12) {
return uint12 > 2047 ? uint12 - 4096 : uint12;
}
// Function to convert raw ADC value to G value for a ±4G accelerometer
function convertRawToG(rawValue) {
const rangeG = 4; // ±4G
return (rawValue / 2048) * rangeG;
}
</script>
<script>
let bleDevice;
let gattServer;
let Theservice;
let ServiceMain;
let settingsCharacteristics;
let writeCharacteristic;
let notifyCharacteristic;
let settingsNotifyCharacteristics;
let busy = false;
let reconnectTrys = 0;
let currentOTAstate = 0;
let currentOTAposition = 0;
let firmwareArray;
let lenCompleteOTA = 0;
let crc16allOTA;
let checksumallOTA;
function handleNotifyOTA(dataIn) {
if (currentOTAstate == 0) {
return; // nothing OTA related
}
//addLog("Now we handle the notify :)");
if (checkACKdata(dataIn)) {
let inCMD = hexToBytes(dataIn)[1] & 0xff;
//addLog("In CMD: " + inCMD + " State: " + currentOTAstate);
if (currentOTAstate == 1 && inCMD == 1) {
sendcmd(bytesToHex(addHeader(2, hexToBytes("01" + intToHex4invers(lenCompleteOTA) + intToHex(crc16allOTA) + intToHex(checksumallOTA)))));
currentOTAstate = 2;
}
else if (currentOTAstate == 2 && inCMD == 2) {
if (otaBigPart())
currentOTAstate = 3;
else {
resetOTAVariables();
addLog("Here the OTA update should not be finished already!!!");
}
}
else if (currentOTAstate == 3 && inCMD == 3) {
if (!otaBigPart()) {
currentOTAstate = 4;// Next step if data uplaod if done
sendcmd(bytesToHex(addHeader(4, null)));
}
}
else if (currentOTAstate == 4 && inCMD == 4) {
sendcmd(bytesToHex(addHeader(5, null)));
resetOTAVariables();
addLog("The upload is done :), expect a disconnection now!");
}
} else {
resetOTAVariables();
return; // Wrong Rxed data
}
}
function checkACKdata(arrayIn) {
arrayIn = hexToBytes(arrayIn);
if (arrayIn.length >= 6) {
if (arrayIn[0] == 0xbc && arrayIn[2] == ((arrayIn.length - 6) & 0xff) && arrayIn[3] == (((arrayIn.length - 6) >> 8) & 0xff)) {
var arrayCheck = new Uint8Array(arrayIn.length - 6);
for (let i = 0; i < arrayCheck.length; i++) {
arrayCheck[6 + i] = arrayIn[6 + i] & 0xff;
}
if (((arrayIn[5] << 8) | arrayIn[4]) == crc16(arrayCheck)) {
return true;
}
addLog("Rx wrong CRC!");
} else {
addLog("Rx wrong header!");
}
} else
addLog("Rx wrong LEN!");
return false;
}
function addHeader(type, arrayIn) {
var arrayOut = new Uint8Array((arrayIn == null ? 0 : arrayIn.length) + 6);
arrayOut[0] = 0xBC;
arrayOut[1] = type & 0xff;
if (arrayIn != null && arrayIn.length > 0) {
partCRC = crc16(arrayIn);
arrayOut[2] = arrayIn.length & 0xff;
arrayOut[3] = (arrayIn.length >> 8) & 0xff;
arrayOut[4] = partCRC & 0xff;
arrayOut[5] = (partCRC >> 8) & 0xff;
for (let i = 0; i < arrayIn.length; i++) {
arrayOut[6 + i] = arrayIn[i] & 0xff;
}
} else {
arrayOut[2] = 0;
arrayOut[3] = 0;
arrayOut[4] = 0xff;
arrayOut[5] = 0xff;
}
//addLog('Now ' + arrayIn.length + ' long part: ' + bytesToHex(arrayOut));
return arrayOut;
}
function otaBigPart() {
let i = currentOTAposition * 0x400;
if (i < firmwareArray.length) {
let lenLeft = 0x400;
if (firmwareArray.length - i < 0x400)
lenLeft = firmwareArray.length - i;
var arrayOut = new Uint8Array(lenLeft + 2);
arrayOut[0] = (currentOTAposition + 1) & 0xff;
arrayOut[1] = ((currentOTAposition + 1) >> 8) & 0xff;
for (let i1 = 0; i1 < lenLeft; i1++) {
arrayOut[2 + i1] = firmwareArray[i + i1] & 0xff;
}
bigPacket = addHeader(3, arrayOut);
addLog("Sending OTA Part: " + currentOTAposition + " from: " + Math.round(firmwareArray.length / 0x400));
updateProgressBar();
BLEsendBig(bigPacket);
currentOTAposition++;
return true;
}
addLog("No OTA Parts left");
return false;
}
function otaStart() {
currentOTAstate = 0;
currentOTAposition = 0;
lenCompleteOTA = firmwareArray.length;
if (lenCompleteOTA > 12288000) {
addLog("OTA Size is too big!");
lenCompleteOTA = 0;
return;
}
if (lenCompleteOTA == 0) {
addLog("OTA Size is 0 no file selected?");
return;
}
crc16allOTA = crc16(firmwareArray);
checksumallOTA = checksum16(firmwareArray);
addLog('Length OTA: ' + intToHex4invers(lenCompleteOTA));
addLog('CRC All: ' + intToHex(crc16allOTA));
addLog('Checksum All: ' + intToHex(checksumallOTA));
currentOTAstate = 1;
sendcmd(bytesToHex(addHeader(1, null)));
}
async function BLEsendBig(arrayIn) {
let sizeBlock = 200 * 2;
let theData = bytesToHex(arrayIn);
while (theData.length > 0) {
let currentLen = sizeBlock;
if (theData.length < sizeBlock)
currentLen = theData.length;
await sendCommand(hexToBytes(theData.substring(0, currentLen)));
theData = theData.substring(currentLen);
delay(50);
}
}
////// Other non OTA cmd
function checkNotifyData(dataIn) {
dataIn = hexToBytes(dataIn);
if (dataIn.length != 16)
return;
let crcTemp = 0;
for (let i = 0; i < dataIn.length - 1; i++) {
crcTemp = (crcTemp + dataIn[i]) & 0xff;
}
if (crcTemp == dataIn[15]) {
switch (dataIn[0]) {
case 0x73:// Notification
{
switch (dataIn[1]) {
case 0x01:// Something sync today data HRS
break;
case 0x02:// Something sync single bp
break;
case 0x03:// Something sync today SPo2 data
break;
case 0x04:// Something sync step detailsingle
break;
case 0x05:// Something temperature
break;
case 0x06:// Something Sync today sport
break;
case 0x07:// Something sport ended
break;
case 0x10:// Something targed setting resp
break;
case 0x0C:
addLog("Battery at: " + dataIn[2] + "%");
break;
case 0x0D:// Something blood sugar
break;
case 0x12:// steps, calorie, distance each uint24
addLog("Steps: " + ((dataIn[2] << 16) | (dataIn[3] << 8) | dataIn[4]) + " Calorie: " + ((dataIn[5] << 16) | (dataIn[6] << 8) | dataIn[7]) / 1000 + " Distance: " + ((dataIn[8] << 16) | (dataIn[9] << 8) | dataIn[10]));
break;
}
}
break;
case 0x03:
if (dataIn[1] != 0)
addLog("Battery at: " + dataIn[1] + "%");
break;
case 0xA1:
if (dataIn[1] == 1) {
addLog("Raw Blood: " + int16((dataIn[2] << 8) | dataIn[3]) + " Max:" + dataIn[5] + " Max:" + dataIn[7] + " Max:" + dataIn[9]);
}
if (dataIn[1] == 2) {
addLog("HRS Raw: " + int16((dataIn[2] << 8) | dataIn[3]) + " Max:" + int16((dataIn[4] << 8) | dataIn[5]) + " Min:" + int16((dataIn[6] << 8) | dataIn[7]) + " Difference:" + int16((dataIn[8] << 8) | dataIn[9]));
}
if (dataIn[1] == 3) {
let rawY = int12((((dataIn[2] << 4) | (dataIn[3] & 0xf)) & 0xfff));
let rawZ = int12((((dataIn[4] << 4) | (dataIn[5] & 0xf)) & 0xfff));
let rawX = int12((((dataIn[6] << 4) | (dataIn[7] & 0xf)) & 0xfff));
const Ax = convertRawToG(rawX);
const Ay = convertRawToG(rawY);
const Az = convertRawToG(rawZ);
rotateX = Math.atan2(Ax, Math.sqrt(Ay * Ay + Az * Az));
rotateY = Math.atan2(Ay, Math.sqrt(Ax * Ax + Az * Az));
rotateZ = Math.atan2(Az, Math.sqrt(Ax * Ax + Ay * Ay));
addLog("Raw ACCL Data X:" + rawX + " Y:" + rawY + " Z:" + rawZ);
}
break;
case 0x69:
if (dataIn[3])
addLog("Heart Rate at: " + dataIn[3] + " bpm");
else
addLog("Please wait...");
if (dataIn[2])
addLog("Ring not on finger!");
break;
}
}
}
async function sendDataArray(arrayIn) {
if (arrayIn.length > 15) {
addLog("Data too long!");
return;
}
var arrayOut = new Uint8Array(16);
for (let i = 0; i < arrayIn.length; i++) {
arrayOut[i] = arrayIn[i] & 0xff;
arrayOut[15] = (arrayOut[15] + arrayOut[i]) & 0xff;
}
await sendSettingcmd(bytesToHex(arrayOut));
}
async function sendcmdCMD(cmd, dataIn) {
sendcmd(bytesToHex(addHeader(hexToBytes(cmd)[0], hexToBytes(dataIn))));
}
////// Other non OTA cmd
function resetOTAVariables() {
currentOTAstate = 0;
currentOTAposition = 0;
lenCompleteOTA = 0;
firmwareArray = null;
updateProgressBar();
}
function resetVariables() {
busy = false;
gattServer = null;
Theservice = null;
writeCharacteristic = null;
settingsCharacteristics = null;
notifyCharacteristic = null;
settingsNotifyCharacteristics = null;
resetOTAVariables();
document.getElementById("log").value = '';
}
function handleError(error) {
console.log(error);
resetVariables();
if (bleDevice == null)
return;
if (reconnectTrys <= 5) {
reconnectTrys++;
connect();
}
else {
addLog("Was not able to connect, aborting");
reconnectTrys = 0;
}
}
async function sendCommand(cmd) {
if (writeCharacteristic) {
await writeCharacteristic.writeValue(cmd);
}
}
async function sendSettingscmd(cmd) {
if (settingsCharacteristics) {
await settingsCharacteristics.writeValue(cmd);
}
}
function sendcmd(cmdTXT) {
console.log('SendCMDnow');
let cmd = hexToBytes(cmdTXT);
addLog('Send CMD: ' + cmdTXT);
console.log('Send CMD: ');
console.log(cmdTXT);
console.log('Send CMD bytes: ');
console.log(cmd);
sendCommand(cmd).then(() => {
console.log('CMD was Send');
})
.catch(handleError);
}
function sendSettingcmd(cmdTXT) {
console.log('SendCMDnow');
let cmd = hexToBytes(cmdTXT);
addLog('Send CMD: ' + cmdTXT);
console.log('Send CMD: ');
console.log(cmdTXT);
console.log('Send CMD bytes: ');
console.log(cmd);
sendSettingscmd(cmd).then(() => {
console.log('CMD was Send');
})
.catch(handleError);
}
function disconnect() {
resetVariables();
console.log('Disconnected.');
addLog('Disconnected.');
document.getElementById("connectbutton").innerHTML = 'Connect';
}
function handleNotify(data) {
addLog("Got bytes: " + bytesToHex(data.buffer));
handleNotifyOTA(bytesToHex(data.buffer));
}
function handleSettingsNotify(data) {
addLog("Got Settings bytes: " + bytesToHex(data.buffer));
checkNotifyData(bytesToHex(data.buffer));
}
function preConnect() {
if (gattServer != null && gattServer.connected) {
if (bleDevice != null && bleDevice.gatt.connected)
bleDevice.gatt.disconnect();
}
else {
connectTrys = 0;
navigator.bluetooth.requestDevice({ optionalServices: ['de5bf728-d711-4e47-af26-65e3012a5dc7', '6e40fff0-b5a3-f393-e0a9-e50e24dcca9e'], acceptAllDevices: true }).then(device => {
device.addEventListener('gattserverdisconnected', disconnect);
bleDevice = device;
connect();
}).catch(handleError);
}
}
function reConnect() {
connectTrys = 0;
if (bleDevice != null && bleDevice.gatt.connected)
bleDevice.gatt.disconnect();
resetVariables();
addLog("Reconnect");
setTimeout(function () { connect(); }, 300);
}
function connect() {
if (writeCharacteristic == null) {
addLog("Connecting to: " + bleDevice.name);
bleDevice.gatt.connect().then(server => {
console.log('> Found GATT server');
gattServer = server;
return gattServer.getPrimaryService('de5bf728-d711-4e47-af26-65e3012a5dc7');
})
.then(service => {
console.log('> Found service');
Theservice = service;
return Theservice.getCharacteristic('de5bf72a-d711-4e47-af26-65e3012a5dc7');
})
.then(characteristic => {
console.log('> Found write characteristic');
addLog('> Found write characteristic');
document.getElementById("connectbutton").innerHTML = 'Disconnect';
writeCharacteristic = characteristic;
return Theservice.getCharacteristic('de5bf729-d711-4e47-af26-65e3012a5dc7');
}).then(characteristic => {
notifyCharacteristic = characteristic;
return notifyCharacteristic.startNotifications().then(() => {
notifyCharacteristic.addEventListener('characteristicvaluechanged', event => {
var value = event.target.value;
handleNotify(value);
});
connect_to_rxtx();
});
})
.catch(handleError);
}
}
function connect_to_rxtx() {
gattServer.getPrimaryServices().then(services => {
for (var i = 0; i < services.length; i++) {
console.log("Services: " + services[i].uuid);
if (services[i].uuid == "6e40fff0-b5a3-f393-e0a9-e50e24dcca9e") {
gattServer.getPrimaryService('6e40fff0-b5a3-f393-e0a9-e50e24dcca9e')
.then(service => {
addLog("Found custom Main service");
ServiceMain = service;
return ServiceMain.getCharacteristic('6e400002-b5a3-f393-e0a9-e50e24dcca9e');
}).then(characteristic => {
addLog("Found custom write characteristic");
settingsCharacteristics = characteristic;
return ServiceMain.getCharacteristic('6e400003-b5a3-f393-e0a9-e50e24dcca9e');
}).then(characteristic => {
addLog("Found custom notify characteristic");
settingsNotifyCharacteristics = characteristic;
addLog("CONNECTED, you can now select the firmware and it will be directly flashed.");
sendDataArray(hexToBytes("03"));
return settingsNotifyCharacteristics.startNotifications().then(() => {
settingsNotifyCharacteristics.addEventListener('characteristicvaluechanged', event => {
var value = event.target.value;
handleSettingsNotify(value);
});
});
}).catch();
return;
}
}
}).catch();
}
function addLog(logTXT) {
var today = new Date();
var time = ("0" + today.getHours()).slice(-2) + ":" + ("0" + today.getMinutes()).slice(-2) + ":" + ("0" + today.getSeconds()).slice(-2) + " : ";
document.getElementById("log").innerHTML += time + logTXT + '<br>';
console.log(time + logTXT);
while ((document.getElementById("log").innerHTML.match(/<br>/g) || []).length > 10) {
var logs_br_position = document.getElementById("log").innerHTML.search("<br>");
document.getElementById("log").innerHTML = document.getElementById("log").innerHTML.substring(logs_br_position + 4);
}
}
function delay(delayInms) {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, delayInms);
});
}
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return new Uint8Array(bytes);
}
function bytesToHex(data) {
return new Uint8Array(data).reduce(function (memo, i) {
return memo + ("0" + i.toString(16)).slice(-2);
}, "");
}
function byteToHex(intIn) {
var stringOut = "";
stringOut = ("0" + intIn.toString(16)).substr(-2)
return stringOut;
}
function intToHex(intIn) {
var stringOut = "";
stringOut = ("0000" + intIn.toString(16)).substr(-4)
return stringOut.substring(2, 4) + stringOut.substring(0, 2);
}
function intToHex4(intIn) {
var stringOut = "";
stringOut = ("00000000" + intIn.toString(16)).substr(-8)
return stringOut;
}
function intToHex4invers(intIn) {
var stringOut = "";
stringOut = ("00000000" + intIn.toString(16)).substr(-8)
return stringOut.substring(6, 8) + stringOut.substring(4, 6) + stringOut.substring(2, 4) + stringOut.substring(0, 2);
}
function crc16(arrayIn) {
let crcOut = 0xffff;
if (arrayIn.length == 0)
return crcOut;
for (let i = 0; i < arrayIn.length; i++) {
crcOut ^= arrayIn[i] & 0xff;
for (let i1 = 0; i1 < 8; i1++) {
crcOut = (crcOut & 1) != 0 ? (crcOut >> 1) ^ 0xA001 : crcOut >> 1;
}
}
return crcOut & 0xffff;
}
function checksum16(arrayIn) {
let ChecksumOut = 0;
if (arrayIn.length == 0)
return ChecksumOut;
for (let i = 0; i < arrayIn.length; i++) {
ChecksumOut += arrayIn[i] & 0xff;
}
return ChecksumOut & 0xffff;
}
window.onload = function () {
document.querySelector("#file").addEventListener("change", function () {
var reader = new FileReader();
reader.onload = function () {
firmwareArray = hexToBytes(bytesToHex(this.result));
addLog("File was selected, size: " + firmwareArray.length + " bytes");
otaStart();
}
if (this.files[0] != null)
reader.readAsArrayBuffer(this.files[0]);
else
addLog("No file selected");
}, false);
}
function resetFileSelector() {
document.getElementById("file").value = '';
};
function updateProgressBar() {
const progressBar = document.getElementById("progress-bar");
percentage = 0;
if (firmwareArray != null)
percentage = Math.floor((currentOTAposition / Math.ceil(firmwareArray.length / 0x400)) * 100);
progressBar.style.width = percentage + '%';
progressBar.textContent = percentage + '%';
}
function handleKeyPressArray(event) {
if (event.keyCode === 13) { // 13 is the Enter key code
event.preventDefault(); // Prevent the default action (form submission)
sendDataArray(hexToBytes(document.getElementById('cmdSTXT').value));
}
}
function handleKeyPressCmd(event) {
if (event.keyCode === 13) { // 13 is the Enter key code
event.preventDefault(); // Prevent the default action (form submission)
sendcmdCMD(document.getElementById('cmdTXT').value, document.getElementById('cmdTXTdata').value);
}
}
</script>
<h1>ATC RF03 Ring OTA Flasher</h1>
<div class="debug-container">
This tool gives a simple way to update the "Colmi" Smart Bluetooth Ring which does use a BlueX RF03 SoC.<br>
To start press on Connect to select YOUR ;) Ring out of the selector, make sure to use a WebBluetooth Enabled
Browser like Chrome<br>
Wait for a successful connection and then select the right firmware file for your Ring.<br>
When the upload is finished the Ring will disconnect and reboot to the new firmware.<br><br>
Detailed Infos: <a href="https://github.com/atc1441/ATC_RF03_Ring"
target="_blank">https://github.com/atc1441/ATC_RF03_Ring</a><br>
</div>
<button id="connectbutton" type="button" onclick="preConnect();">Connect</button>
<button type="button" onclick="reConnect();">Reconnect</button>
<button type="button" onclick="document.getElementById('log').innerHTML = '';">Clear Log</button>
<div class="instructions">
<p>Make sure to select the correct firmware! This tool cannot check if the file is correct!!!</p>
<p class="warning">A wrong file can damage and/or brick your Smart Ring!</p>
</div>
<label for="file">Select Firmware:</label>
<input type="file" accept=".bin" id="file" onclick="resetFileSelector();" />
<div class="progress-container">
<div id="progress-bar" class="progress-bar">0%</div>
</div>
<h2>Logging:</h2>
<div id="log"></div>
<h3>Extra Commands:</h3>
<div class="debug-container">
Not needed for normal firmware update.<br><br>
Examples:<br>
08 = Reboot Ring<br>
FF = Reset to default<br>
10 = Blink twice<br>
690101 = Measure Heart rate<br>
0204 = Enable Shoot Camera motion feedback<br>
0206 = Disable Camera feedback<br>
03 = Get Charging and Battery state<br>
A10404 = Enable Raw sensor data<br>
A102 = Disable Raw sensor data<br>
<label for="cmdSTXT">Setting Command:</label>
<input type="text" id="cmdSTXT" value="03" onkeydown="handleKeyPressArray(event)">
<button type="button" onclick="sendDataArray(hexToBytes(document.getElementById('cmdSTXT').value));">Send Setting
CMD</button><br><br>
<label for="cmdTXT">Command:</label>
<input type="text" id="cmdTXT" value="2A" onkeydown="handleKeyPressCmd(event)">
<label for="cmdTXTdata">Command:</label>
<input type="text" id="cmdTXTdata" value="00" onkeydown="handleKeyPressCmd(event)">
<button type="button"
onclick="sendcmdCMD(document.getElementById('cmdTXT').value,document.getElementById('cmdTXTdata').value);">Send
CMD</button><br>
<button type="button" onclick="startAnimation();">Enable Animation</button>
</div>
<div id="container"></div>
</body>
</html>