From 49d2ae0c4e6dd5c7831b368fa49d96f522bf8aaa Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Fri, 10 Jan 2025 21:39:52 +0800 Subject: [PATCH] optimize image transfer speed --- EPD/EPD_Test.h | 3 --- html/js/main.js | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) delete mode 100644 EPD/EPD_Test.h diff --git a/EPD/EPD_Test.h b/EPD/EPD_Test.h deleted file mode 100644 index dd37fdb..0000000 --- a/EPD/EPD_Test.h +++ /dev/null @@ -1,3 +0,0 @@ -#include "DEV_Config.h" - -void EPD_4in2_test(void); diff --git a/html/js/main.js b/html/js/main.js index bb5c3ab..abf044c 100644 --- a/html/js/main.js +++ b/html/js/main.js @@ -8,6 +8,7 @@ let canvas; let startTime; const MAX_PACKET_SIZE = 20; +const INTERLEAVED_COUNT = 50; const EpdCmd = { SET_PINS: 0x00, INIT: 0x01, @@ -45,7 +46,7 @@ async function handleError(error) { } } -async function write(cmd, data) { +async function write(cmd, data, withResponse=true) { if (!epdCharacteristic) { addLog("服务不可用,请检查蓝牙连接"); return; @@ -60,13 +61,17 @@ async function write(cmd, data) { throw new Error("BLE packet too large!"); } addLog(` ${bytes2hex(payload)}`); - await epdCharacteristic.writeValue(Uint8Array.from(payload)); + if (withResponse) + await epdCharacteristic.writeValueWithResponse(Uint8Array.from(payload)); + else + await epdCharacteristic.writeValueWithoutResponse(Uint8Array.from(payload)); } async function epdWrite(cmd, data) { const chunkSize = MAX_PACKET_SIZE - 1; const count = Math.round(data.length / chunkSize); let chunkIdx = 0; + let noReplyCount = INTERLEAVED_COUNT; if (typeof data == 'string') data = hex2bytes(data); @@ -74,7 +79,13 @@ async function epdWrite(cmd, data) { for (let i = 0; i < data.length; i += chunkSize) { let currentTime = (new Date().getTime() - startTime) / 1000.0; setStatus(`命令:0x${cmd.toString(16)}, 数据块: ${chunkIdx+1}/${count+1}, 总用时: ${currentTime}s`); - await write(EpdCmd.SEND_DATA, data.slice(i, i + chunkSize)); + if (noReplyCount > 0) { + await write(EpdCmd.SEND_DATA, data.slice(i, i + chunkSize), false); + noReplyCount--; + } else { + await write(EpdCmd.SEND_DATA, data.slice(i, i + chunkSize), true); + noReplyCount = INTERLEAVED_COUNT; + } chunkIdx++; } }