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 b30d318
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
3 changes: 0 additions & 3 deletions EPD/EPD_Test.h

This file was deleted.

3 changes: 3 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ <h3>4.2 寸电子墨水屏蓝牙控制器(nRF51)</h3>
<div>
<button id="clearcanvasbutton" type="button" class="secondary" onclick="clear_canvas()">清除画布</button>
<button id="sendimgbutton" type="button" class="primary" onclick="sendimg()">发送图片</button>
<label for="interleavedcount" style="margin-left: 20px;">确认间隔</label>
<input type="number" id="interleavedcount" value="50">
</div>
<div>
<button id="clearscreenbutton" type="button" class="secondary" onclick="clearscreen()">清除屏幕</button>
Expand Down Expand Up @@ -107,6 +109,7 @@ <h3>4.2 寸电子墨水屏蓝牙控制器(nRF51)</h3>
<ul>
<li><b>驱动选择:</b>黑白屏可尝试 EPD_4in2 / EPD_4in2_V2, 三色屏选择 EPD_4in2b_V2 (选错驱动可能会导致任何未知的异常,重启即可恢复)</li>
<li><b>引脚配置:</b>格式为十六进制,顺序:MOSI/SCLK/CS/DC/ST/BUSY/BS,必须按此顺序包含完整的 7 个引脚配置(没有用到的引脚可配置为 <code>FF</code></li>
<li><b>确认间隔: </b>这个间隔指的是数据包数量间隔,即发送此数量的不确认响应的数据包后才发送一次需确认响应的数据包。加大此值可优化传图速度,但是丢包风险也更大(你可能会发现图片有部分位置显示不正常,此时需调小这个值)。
<li>
<b>指令列表(指令和参数全部要使用十六进制):</b>
<ul>
Expand Down
17 changes: 14 additions & 3 deletions html/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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 +60,32 @@ 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);
const interleavedCount = document.getElementById('interleavedcount').value;
let chunkIdx = 0;
let noReplyCount = interleavedCount;

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 = interleavedCount;
}
chunkIdx++;
}
}
Expand Down

0 comments on commit b30d318

Please sign in to comment.