Skip to content

Commit dd7d43f

Browse files
committed
update html
1 parent dd153f9 commit dd7d43f

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

html/js/main.js

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
let bleDevice, gattServer;
22
let epdService, epdCharacteristic;
3-
let reconnectTrys = 0;
4-
5-
let canvas, ctx;
6-
let startTime;
3+
let startTime, msgIndex;
4+
let canvas, ctx, textDecoder;
75

86
const EpdCmd = {
97
SET_PINS: 0x00,
@@ -26,24 +24,10 @@ function resetVariables() {
2624
gattServer = null;
2725
epdService = null;
2826
epdCharacteristic = null;
27+
msgIndex = 0;
2928
document.getElementById("log").value = '';
3029
}
3130

32-
async function handleError(error) {
33-
console.error(error);
34-
resetVariables();
35-
if (bleDevice == null)
36-
return;
37-
if (reconnectTrys <= 5) {
38-
reconnectTrys++;
39-
await connect();
40-
}
41-
else {
42-
addLog("连接失败!");
43-
reconnectTrys = 0;
44-
}
45-
}
46-
4731
async function write(cmd, data, withResponse=true) {
4832
if (!epdCharacteristic) {
4933
addLog("服务不可用,请检查蓝牙连接");
@@ -63,7 +47,7 @@ async function write(cmd, data, withResponse=true) {
6347
await epdCharacteristic.writeValueWithoutResponse(Uint8Array.from(payload));
6448
} catch (e) {
6549
console.error(e);
66-
if (e.message) addLog(e.message);
50+
if (e.message) addLog("write: " + e.message);
6751
return false;
6852
}
6953
return true;
@@ -183,15 +167,15 @@ async function preConnect() {
183167
}
184168
}
185169
else {
186-
reconnectTrys = 0;
170+
resetVariables();
187171
try {
188172
bleDevice = await navigator.bluetooth.requestDevice({
189173
optionalServices: ['62750001-d828-918d-fb46-b6c11c675aec'],
190174
acceptAllDevices: true
191175
});
192176
} catch (e) {
193177
console.error(e);
194-
if (e.message) addLog(e.message);
178+
if (e.message) addLog("requestDevice: " + e.message);
195179
addLog("请检查蓝牙是否已开启,且使用的浏览器支持蓝牙!建议使用以下浏览器:");
196180
addLog("• 电脑: Chrome/Edge");
197181
addLog("• Android: Chrome/Edge");
@@ -200,49 +184,67 @@ async function preConnect() {
200184
}
201185

202186
await bleDevice.addEventListener('gattserverdisconnected', disconnect);
203-
try {
204-
await connect();
205-
} catch (e) {
206-
await handleError(e);
207-
}
187+
setTimeout(async function () { await connect(); }, 300);
208188
}
209189
}
210190

211191
async function reConnect() {
212-
reconnectTrys = 0;
213192
if (bleDevice != null && bleDevice.gatt.connected)
214193
bleDevice.gatt.disconnect();
215194
resetVariables();
216195
addLog("正在重连");
217196
setTimeout(async function () { await connect(); }, 300);
218197
}
219198

199+
function handleNotify(value, idx) {
200+
const data = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
201+
if (idx == 0) {
202+
addLog(`收到配置:${bytes2hex(data)}`);
203+
const epdpins = document.getElementById("epdpins");
204+
const epddriver = document.getElementById("epddriver");
205+
epdpins.value = bytes2hex(data.slice(0, 7));
206+
if (data.length > 10) epdpins.value += bytes2hex(data.slice(10, 11));
207+
epddriver.value = bytes2hex(data.slice(7, 8));
208+
filterDitheringOptions();
209+
} else {
210+
if (textDecoder == null) textDecoder = new TextDecoder();
211+
const msg = textDecoder.decode(data);
212+
addLog(`<span class="action">⇓</span> ${msg}`);
213+
}
214+
}
215+
220216
async function connect() {
221-
if (epdCharacteristic == null && bleDevice != null) {
222-
addLog("正在连接: " + bleDevice.name);
217+
if (bleDevice == null || epdCharacteristic != null) return;
223218

219+
try {
220+
addLog("正在连接: " + bleDevice.name);
224221
gattServer = await bleDevice.gatt.connect();
225222
addLog(' 找到 GATT Server');
226-
227223
epdService = await gattServer.getPrimaryService('62750001-d828-918d-fb46-b6c11c675aec');
228224
addLog(' 找到 EPD Service');
229-
230225
epdCharacteristic = await epdService.getCharacteristic('62750002-d828-918d-fb46-b6c11c675aec');
231226
addLog(' 找到 Characteristic');
227+
} catch (e) {
228+
console.error(e);
229+
if (e.message) addLog("connect: " + e.message);
230+
disconnect();
231+
return;
232+
}
232233

234+
try {
233235
await epdCharacteristic.startNotifications();
234236
epdCharacteristic.addEventListener('characteristicvaluechanged', (event) => {
235-
addLog(`<span class="action">⇓</span> ${bytes2hex(event.target.value.buffer)}`);
236-
document.getElementById("epdpins").value = bytes2hex(event.target.value.buffer.slice(0, 7));
237-
document.getElementById("epddriver").value = bytes2hex(event.target.value.buffer.slice(7, 8));
238-
filterDitheringOptions();
237+
handleNotify(event.target.value, msgIndex++);
239238
});
239+
} catch (e) {
240+
console.error(e);
241+
if (e.message) addLog("startNotifications: " + e.message);
242+
}
240243

241-
await write(EpdCmd.INIT);
244+
await write(EpdCmd.INIT);
242245

243-
document.getElementById("connectbutton").innerHTML = '断开';
244-
updateButtonStatus();
245-
}
246+
document.getElementById("connectbutton").innerHTML = '断开';
247+
updateButtonStatus();
246248
}
247249

248250
function setStatus(statusText) {
@@ -355,6 +357,7 @@ function checkDebugMode() {
355357
}
356358

357359
document.body.onload = () => {
360+
textDecoder = null;
358361
canvas = document.getElementById('canvas');
359362
ctx = canvas.getContext("2d");
360363

0 commit comments

Comments
 (0)