-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBafangWebCfg.html
628 lines (585 loc) · 19.7 KB
/
BafangWebCfg.html
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
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bafang Web Config</title>
<style>
body {
font-family: sans-serif;
margin-bottom: 10em;
max-width: 600px;
margin-left: auto;
margin-right: auto;
font-size: 11pt;
padding: 1em;
}
table {
border: 0; /*1px solid gray;*/
border-collapse: collapse;
width: 100%;
margin-top: 1em;
}
td, th {
border: 0;
border-top: 1px solid gray;
padding: 5px;
border: 1px solid gray;
}
th {
text-align: left;
}
td {
min-width: 10ex;
text-align: left;
background: #eee;
font-family: monospace;
}
div.block {
margin-top: 1em;
}
h1 {
/*border-bottom: 2px solid black;*/
background: #777;
color: white;
padding: 8px;
border-radius: 3px;
}
h2 {
border-bottom: 2px solid black;
clear: both;
margin-top: 2em;
}
button {
/*margin-bottom: 1em;*/
}
input.cfg {
border: 0;
font: inherit;
background: transparent;
font-family: monospace;
width: 99%;
padding-left: 4px;
}
a {
text-decoration: none;
}
div.creds {
margin-top: 2em;
text-align: right;
font-size: 80%;
color: #555;
}
.error-display {
/*display: inline-block;*/
float: right;
/* min-height: 1em;
padding: 5px;
background: #eee;
text-align: center;
margin-bottom: 1em;
margin-top: 1em;
border-radius: 3px;*/
}
#file-pick {
margin-bottom: 1em;
/*width: 100%;*/
/*background: #eee;*/
}
button {
/*min-width: 20%;*/
}
select.cfg {
width: 100%;
font-size: 100%;
font-family: monospace;
background: transparent;
border: 0;
}
div#saveload {
/*border-top: 2px solid black;*/
padding: 15px;
border: 1px solid #aaa;
margin-top: 15px;
border-radius: 3px;
}
#port-status {
border-radius: 5px;
color: white;
font-size: 80%;
background: #777;
padding: 3px;
padding-left: 8px;
padding-right: 8px;
margin-left: 1ex;
float: right;
}
.reload-cell {
min-width: 3em;
width: 3em;
padding: 0;
text-align: right;
}
.reload-cell button {
margin: 0;
/*margin-left: 8px;*/
font-size: 12pt;
min-width: 2em;
padding: 0;
/*border: 1px solid #bbb;*/
}
.highlight-error {
background: yellow !important;
}
.read-write {
/*float: left;*/
}
.barchart {
/*background: #eee;*/
/* height: 200px;
width: 100%;
*/ margin-top: 1em;
}
</style>
</head>
<body>
<h1>Bafang BBSxx Configuration</h1>
<div>
<button id="connect-to-serial">Open serial port</button>
<button id="disconnect-serial">Close serial port</button>
<span id="port-status"></span>
</div>
<div id="saveload">
<div>
<label>Load File:
<input type="file" id="file-pick" accept=".json,.el"/>
</label>
</div>
<div>
<label>Save File:
<button id="save-file" type="button">Download</button>
</label>
</div>
</div>
<div class="block">
<span class="error-display" id="info"></span>
<div class="read-write">
<button id="read-all" class="read" type="button" disabled=1>Read All</button>
<button id="write-all" class="write" type="button" disabled=1>Write All</button>
</div>
<h2>Info</h2>
<!--<button id="read-gen" type="button" disabled=1>Connect controller</button>-->
<table id="info">
<tbody>
<tr><th>Manufacturer</th> <td id="manufacturer"></td></tr>
<tr><th>Model</th><td id="model"></td></tr>
<tr><th>HW Version</th><td id="hw_ver"></td></tr>
<tr><th>FW Version</th><td id="fw_ver"></td></tr>
<tr><th>Voltage</th><td id="voltage"></td></tr>
<tr><th>Max Current (A)</th><td id="max_current"></td></tr>
</tbody>
</table>
</div>
<div class="block">
<h2>Basic</h2>
<span class="error-display" id="basic"></span>
<div class="read-write">
<button id="read-bas" class="read" type="button" disabled=1>Read</button>
<button id="write-bas" class="write" type="button" disabled=1>Write</button>
</div>
<table id="basic">
<tbody>
<!--<tr><th colspan="2">Low Battery Protect (V)</th><td contenteditable='true' id="low_battery_protect" class="cfg"></td></tr>-->
<tr><th colspan="2">Low Battery Protect (V)</th><td><input type="number" id="low_battery_protect" class="cfg" min=18 max=70/></td></tr>
<tr><th colspan="2">Current Limit (A)</th> <td> <input type="number" id="current_limit" class="cfg" min=1 max=30 /></td></tr>
<tr><th colspan="2">Wheel Diameter</th> <td><select id="wheel_size" class="cfg">
<option>16"</option>
<option>17"</option>
<option>18"</option>
<option>19"</option>
<option>20"</option>
<option>21"</option>
<option>22"</option>
<option>23"</option>
<option>24"</option>
<option>25"</option>
<option>26"</option>
<option>27"</option>
<option>28"</option>
<option>29"</option>
<option>30"</option>
<option>700C</option>
</select></td></tr>
<tr><th colspan="2">Speedmeter Model</th> <td><select id="speedmeter_model" class="cfg">
<option>External</option>
<option>Internal</option>
<option>Motorphase</option>
</select></td></tr>
<tr><th colspan="2">Speedmeter Signals</th> <td><input type="number" id="speedmeter_signals" class="cfg" min=0 max=63/></td></tr>
<tr><th colspan="3" style="border:0;"></th></tr>
<tr><th>Assist</th><th>Current (%)</th><th>Speed (%)</th></tr>
</tbody>
</table>
<canvas class="barchart" id="chart_current" width=600 height=150></canvas>
</div>
<div class="block">
<h2>Pedal Assist</h2>
<span class="error-display" id="pedal"></span>
<div class="read-write">
<button id="read-pas" class="read" type="button" disabled=1>Read</button>
<button id="write-pas" class="write" type="button" disabled=1>Write</button>
</div>
<table id="pedal">
<tbody>
<tr><th>Pedal Type</th> <td><select id="pedal_type" class="cfg">
<option>None</option>
<option>DH-Sensor-12</option>
<option>BB-Sensor-32</option>
<option>DoubleSignal-24</option>
</select></td></tr>
<tr><th>Designated Assist (0-9)</th> <td><select id="designated_assist" class="cfg"/></td></tr>
<tr><th>Speed Limit (km/h)</th> <td><input list="by-display" id="speed_limit" class="cfg"/></td></tr>
<tr><th>Start Current (%)</th> <td><input type="number" min=0 max=100 id="start_current" class="cfg"/></td></tr>
<tr><th>Slow-Start Mode (1-8)</th> <td><select id="slow_start_mode" class="cfg"/></td></tr>
<tr><th>Startup Degree (Signal No.)</th> <td><input type="number" id="startup_degree" class="cfg"/></td></tr>
<tr><th>Work Mode (pedal/wheel*10)</th> <td><input list="work-modes" id="work_mode" class="cfg"/></td></tr>
<datalist id="work-modes">
<option value="Undetermined" />
</datalist>
<tr><th>Time of Stop (x 10 ms)</th> <td><input type="number" id="time_of_stop" class="cfg"/></td></tr>
<tr><th>Current Decay (1-8)</th> <td><select id="current_decay" class="cfg"/></td></tr>
<tr><th>Stop Decay (x 10 ms)</th> <td><input type="number" id="stop_decay" class="cfg"/></td></tr>
<tr><th>Keep Current (%)</th> <td><input type="number" min=1 max=100 id="keep_current" class="cfg"/></td></tr>
</tbody>
</table>
</div>
<datalist id="by-display">
<option value="Display"/>
</datalist>
<div class="block">
<h2>Throttle</h2>
<span class="error-display" id="throttle"></span>
<div class="read-write">
<button id="read-thr" class="read" type="button" disabled=1>Read</button>
<button id="write-thr" class="write" type="button" disabled=1>Write</button>
</div>
<table id="throttle">
<tbody>
<tr><th>Start Voltage (x 100 mV)</th> <td><input type="number" id="start_voltage" class="cfg"/></td></tr>
<tr><th>End Voltage (x 100 mV)</th> <td><input type="number" id="end_voltage" class="cfg"/></td></tr>
<tr><th>Mode</th> <td><select id="mode" class="cfg">
<option>Speed</option>
<option>Current</option>
</select></td></tr>
<tr><th>Designated Assist (0-9)</th> <td><select id="designated_assist" class="cfg"/></td></tr>
<tr><th>Speed Limit (km/h)</th> <td><input list="by-display" id="speed_limit" class="cfg"/></td></tr>
<tr><th>Start Current (%)</th> <td><input type="number" min=0 max=100 id="start_current" class="cfg"></td></tr>
</tbody>
</table>
</div>
<pre id="serial-messages-container"></pre>
<div class="creds">By Jonatan Liljedahl - <a href="http://kymatica.com">www.kymatica.com</a></div>
<script src="./app.js"></script>
<script>
/***********************************
Bafang Web Config
Copyright (c) 2021 Jonatan Liljedahl
************************************/
const bafang = new BafangConfig();
const connectButton = document.getElementById('connect-to-serial');
const disconnectButton = document.getElementById('disconnect-serial');
const serialMessagesContainer = document.getElementById('serial-messages-container');
const readAllButton = document.getElementById('read-all');
const writeAllButton = document.getElementById('write-all');
// const readGenButton = document.getElementById('read-gen');
const readBasButton = document.getElementById('read-bas');
const readPasButton = document.getElementById('read-pas');
const readThrButton = document.getElementById('read-thr');
const writeBasButton = document.getElementById('write-bas');
const writePasButton = document.getElementById('write-pas');
const writeThrButton = document.getElementById('write-thr');
const filePicker = document.getElementById('file-pick');
const saveFileButton = document.getElementById('save-file');
window.onload = () => {
let t = document.querySelector("table#basic");
for(let i=0;i<10;i++) {
let row = t.insertRow();
let th = document.createElement("th");
th.innerText = i;
row.appendChild(th);
let cell1 = row.insertCell();
let num1 = document.createElement("input");
cell1.appendChild(num1);
let cell2 = row.insertCell();
let num2 = document.createElement("input");
cell2.appendChild(num2);
num1.id = "assist"+i+"_current";
num2.id = "assist"+i+"_speed";
for (let e of [num1,num2]) {
e.setAttribute("type","number");
e.setAttribute("min",0);
e.setAttribute("max",100);
e.classList.add("cfg");
e.addEventListener('change', () => {
updateChart();
});
}
bafang.onSerialConnect(false);
}
const sections = ["basic","pedal","throttle"];
for (let s of sections) {
let t = document.querySelector("table#"+s);
for (let r of t.rows) {
let last = r.cells[r.cells.length-1];
if(last.tagName == "TD") {
let cell = r.insertCell();
cell.style.border = 0;
cell.style.background = 0;
cell.classList.add("reload-cell");
let b = document.createElement("button");
b.innerHTML = "↻"
cell.appendChild(b);
let configs = r.querySelectorAll(".cfg");
b.setAttribute("title","Reset to last read value"); // TODO: could update tooltip with old value in onRead()
b.addEventListener('click', () => {
for (let e of configs) {
if(bafang.data[s])
updateCfgField(e, bafang.data[s][e.id]);
}
});
}
}
}
const a = document.querySelectorAll("select#designated_assist");
for (let sel of a) {
for(let i=0;i<10;i++) {
let opt = document.createElement("option");
opt.innerText = "Assist "+i;
opt.value = i;
sel.appendChild(opt);
}
let opt = document.createElement("option");
opt.innerText = "Display";
// opt.innerText = "Set by display";
// opt.value = "Display";
sel.appendChild(opt);
}
//slow_start_mode current_decay
for (let id of ["slow_start_mode","current_decay"]) {
const sel = document.querySelector("select#"+id);
for(let i=1;i<9;i++) {
let opt = document.createElement("option");
opt.innerText = i;
sel.appendChild(opt);
}
}
if (window.File && window.FileReader) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
updateChart();
};
bafang.parseTable = (blk) => {
const name = blockKeys[blk];
console.log("Parsing table",name);
let info = {};
bafang.data[name] = info;
clearErrorHL();
const nodes = document.querySelectorAll("table#"+name+" .cfg");
for (let e of nodes) {
const val = e.tagName == "TD" ? e.innerText : e.value;
if(!val) {
bafang.logError(blk,"Missing value");
errorHL(blk,e.id);
return false;
}
const num = parseInt(val);
if(num==val) {
if(num > 255 || num < 0) {
bafang.logError(blk,"Value out range");
errorHL(blk,e.id);
return false;
}
info[e.id] = num;
} else {
info[e.id] = val;
}
}
bafang.logMsg(blk,"");
return true;
};
function updateCfgField(e,val) {
if(e) {
if(e.tagName == "TD")
e.innerText = val;
else
e.value = val;
}
}
function updateTable(name, info) {
console.log("Updating table",name);
if(!info) info = bafang.data[name];
const infoTable = document.querySelector("table#"+name);
for (key in info) {
const e = infoTable.querySelector("#"+key);
updateCfgField(e,info[key]);
}
}
function clearErrorHL() {
let a = document.querySelectorAll(".highlight-error");
for (e of a)
e.classList.remove("highlight-error");
}
function errorHL(blk, key) {
blk = blockKeys[blk];
console.log("Error at",blk,key);
let e = document.querySelector("table#"+blk+" #"+key);
while (e.tagName != "TD") e = e.parentNode;
e.classList.add("highlight-error");
}
bafang.onWrite = (blk, key) => {
clearErrorHL();
if(!!key) {
// window.scrollTo({top:0, behaviour: 'smooth'});
errorHL(blk, key);
}
};
bafang.onRead = (blk) => {
clearErrorHL();
if (blk == BLK_GEN) { // Connected to controller
for (let b of document.querySelectorAll("button.read, button.write")) {
b.removeAttribute('disabled');
}
}
if(blk) {
updateTable(blockKeys[blk]);
updateChart();
}
};
bafang.onSerialConnect = (state) => {
let st = document.querySelector("#port-status");
if(state) {
// readGenButton.removeAttribute('disabled');
st.innerText = "Connected";
st.style.background = "#20bf20";
connectButton.setAttribute('disabled',1);
disconnectButton.removeAttribute('disabled');
} else {
// readGenButton.setAttribute('disabled',1);
st.innerText = "Not Connected";
st.style.background = "#f55";
disconnectButton.setAttribute('disabled',1);
connectButton.removeAttribute('disabled');
for(let e of document.querySelectorAll("button.read, button.write")) {
e.setAttribute('disabled',1);
}
}
};
connectButton.addEventListener('click', () => {
bafang.init();
});
disconnectButton.addEventListener('click', () => {
bafang.close();
});
// readGenButton.addEventListener('click', () => {
// bafang.connectDevice();
// });
readAllButton.addEventListener('click', () => {
bafang.readAllBlocks();
});
writeAllButton.addEventListener('click', () => {
bafang.writeAllBlocks();
});
readBasButton.addEventListener('click', () => {
bafang.readBlock(BLK_BAS);
});
readPasButton.addEventListener('click', () => {
bafang.readBlock(BLK_PAS);
});
readThrButton.addEventListener('click', () => {
bafang.readBlock(BLK_THR);
});
writeBasButton.addEventListener('click', () => {
bafang.writeBlock(BLK_BAS);
});
writePasButton.addEventListener('click', () => {
bafang.writeBlock(BLK_PAS);
});
writeThrButton.addEventListener('click', () => {
bafang.writeBlock(BLK_THR);
});
filePicker.addEventListener('change', (evt) => {
bafang.readFile(evt.target.files[0]);
});
saveFileButton.addEventListener('click', () => {
bafang.saveFile();
});
const canvas = document.getElementById('chart_current');
function updateChart() {
/* TODO:
update when any relevant field changes
*/
if (!canvas.getContext) return;
var ctx = canvas.getContext('2d');
const h = canvas.height;
const w = canvas.width;
ctx.clearRect(0, 0, w, h);
const xofs = 40;
const marg = 15;
const barDx = Math.floor((w-xofs*2)/10);
const barWidth = Math.floor(barDx*0.75);
ctx.strokeStyle = '#ccc';
ctx.font = "14px monospace";
ctx.fillStyle = 'black';
ctx.textBaseline = 'middle';
let dy = (h-marg*2)/4;
let amps = parseInt(document.getElementById("current_limit").value)||0;
ctx.beginPath();
ctx.setLineDash([3,3]);
for (let i = 0; i<5; i++) {
let y = Math.floor(marg+dy*i)+0.5;
let v = 1-(i/4);
ctx.moveTo(xofs,y);
ctx.lineTo(w-xofs,y);
ctx.textAlign = 'end';
ctx.fillText(v*100+'%',xofs-5,y);
ctx.textAlign = 'start';
ctx.fillText(Math.round(v*amps)+'A',w-xofs+5,y);
}
ctx.stroke();
ctx.setLineDash([]);
ctx.textBaseline = 'top';
ctx.textAlign = 'center';
let xofs2 = xofs+Math.floor((barDx-barWidth)/2);
let keep = (parseInt(document.getElementById("keep_current").value)||0)/100.0;
for (let x = 0; x < 10; x++) {
let e = document.getElementById("assist"+x+"_current");
let v = (parseInt(e.value) || 0)/100.0;
// let dy = Math.max(1,(h-marg*2)*v);
let dy = (h-marg*2)*v;
let xx = x*barDx+xofs2;
ctx.fillStyle = '#888';
ctx.fillRect(xx,h-dy-marg,barWidth,dy+1);
ctx.fillStyle = '#aaa';
ctx.fillRect(xx,h-dy*keep-marg,barWidth,dy*keep+1);
ctx.fillStyle = 'black';
ctx.fillText(x,xx+barWidth/2,h-marg+2);
}
}
window.addEventListener('resize', () => {
canvas.width = canvas.parentNode.clientWidth;
updateChart();
});
for(let id of ["current_limit","keep_current"]) {
document.getElementById(id).addEventListener('change', updateChart);
}
// navigator.serial.addEventListener("connect", (event) => {
// TODO: Automatically open event.target or warn user a port is available.
// console.log("Connected "+event.target);
// });
// navigator.serial.addEventListener("disconnect", (event) => {
// // TODO: Remove |event.target| from the UI.
// // If the serial port was opened, a stream error would be observed as well.
// console.log("Disconnected "+event.target);
// });
</script>
</body></html>