Skip to content

Commit

Permalink
optimize image transfer speed
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 16, 2025
1 parent 60d48ad commit 49d2ae0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 0 additions & 3 deletions EPD/EPD_Test.h

This file was deleted.

17 changes: 14 additions & 3 deletions html/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let canvas;
let startTime;

const MAX_PACKET_SIZE = 20;
const INTERLEAVED_COUNT = 50;
const EpdCmd = {
SET_PINS: 0x00,
INIT: 0x01,
Expand Down Expand Up @@ -45,7 +46,7 @@ async function handleError(error) {
}
}

async function write(cmd, data) {
async function write(cmd, data, withResponse=true) {
if (!epdCharacteristic) {
addLog("服务不可用,请检查蓝牙连接");
return;
Expand All @@ -60,21 +61,31 @@ async function write(cmd, data) {
throw new Error("BLE packet too large!");
}
addLog(`<span class="action">⇑</span> ${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);

await write(EpdCmd.SEND_CMD, [cmd]);
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++;
}
}
Expand Down

0 comments on commit 49d2ae0

Please sign in to comment.