send epd config on connect

This commit is contained in:
Shuanglei Tao
2024-11-14 22:55:51 +08:00
parent 18db97f5be
commit 32eb020b62
4 changed files with 62 additions and 55 deletions

View File

@@ -60,7 +60,7 @@ static void epd_config_load(epd_config_t *cfg)
static void epd_config_save(epd_config_t *cfg) static void epd_config_save(epd_config_t *cfg)
{ {
nrf_nvmc_page_erase(BLE_EPD_CONFIG_ADDR); nrf_nvmc_page_erase(BLE_EPD_CONFIG_ADDR);
nrf_nvmc_write_words(BLE_EPD_CONFIG_ADDR, (uint32_t*)cfg, sizeof(epd_config_t) / sizeof(uint32_t)); nrf_nvmc_write_bytes(BLE_EPD_CONFIG_ADDR, (uint8_t*)cfg, sizeof(epd_config_t) / sizeof(uint8_t));
} }
/**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the S110 SoftDevice. /**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the S110 SoftDevice.
@@ -178,6 +178,7 @@ static void on_write(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
if (ble_srv_is_notification_enabled(p_evt_write->data)) if (ble_srv_is_notification_enabled(p_evt_write->data))
{ {
p_epd->is_notification_enabled = true; p_epd->is_notification_enabled = true;
ble_epd_string_send(p_epd, (uint8_t *)&p_epd->config, sizeof(epd_config_t));
} }
else else
{ {

View File

@@ -26,14 +26,14 @@
/**< EPD Service Configs */ /**< EPD Service Configs */
typedef struct typedef struct
{ {
uint32_t mosi_pin; uint8_t mosi_pin;
uint32_t sclk_pin; uint8_t sclk_pin;
uint32_t cs_pin; uint8_t cs_pin;
uint32_t dc_pin; uint8_t dc_pin;
uint32_t rst_pin; uint8_t rst_pin;
uint32_t busy_pin; uint8_t busy_pin;
uint32_t bs_pin; uint8_t bs_pin;
uint32_t driver_id; uint8_t driver_id;
} epd_config_t; } epd_config_t;
/**< EPD Service command IDs. */ /**< EPD Service command IDs. */

View File

@@ -27,12 +27,12 @@
<div style="float: right;"> <div style="float: right;">
驱动: 驱动:
<select id="epddriver"> <select id="epddriver">
<option value="01" selected="selected">EPD_4in2</option> <option value="01">EPD_4in2</option>
<option value="02">EPD_4in2_V2</option> <option value="02">EPD_4in2_V2</option>
<option value="03">EPD_4in2b_V2</option> <option value="03">EPD_4in2b_V2</option>
</select> </select>
引脚: 引脚:
<input id="epdpins" type="text" value="0508090A0B0C0D"> <input id="epdpins" type="text" value="">
<button id="setDriverbutton" type="button" onclick="setDriver();">确认</button> <button id="setDriverbutton" type="button" onclick="setDriver();">确认</button>
</div> </div>
<button id="connectbutton" type="button" onclick="preConnect();">连接</button> <button id="connectbutton" type="button" onclick="preConnect();">连接</button>

View File

@@ -1,7 +1,7 @@
let bleDevice; let bleDevice;
let gattServer; let gattServer;
let Theservice; let epdService;
let writeCharacteristic; let epdCharacteristic;
let reconnectTrys = 0; let reconnectTrys = 0;
let canvas; let canvas;
@@ -10,29 +10,29 @@ let chunkSize = 38;
function resetVariables() { function resetVariables() {
gattServer = null; gattServer = null;
Theservice = null; epdService = null;
writeCharacteristic = null; epdCharacteristic = null;
document.getElementById("log").value = ''; document.getElementById("log").value = '';
} }
function handleError(error) { async function handleError(error) {
console.log(error); console.log(error);
resetVariables(); resetVariables();
if (bleDevice == null) if (bleDevice == null)
return; return;
if (reconnectTrys <= 5) { if (reconnectTrys <= 5) {
reconnectTrys++; reconnectTrys++;
connect(); await connect();
} }
else { else {
addLog("Was not able to connect, aborting"); addLog("连接失败!");
reconnectTrys = 0; reconnectTrys = 0;
} }
} }
async function sendCommand(cmd) { async function sendCommand(cmd) {
if (writeCharacteristic) { if (epdCharacteristic) {
await writeCharacteristic.writeValue(cmd); await epdCharacteristic.writeValue(cmd);
} else { } else {
addLog("服务不可用,请检查蓝牙连接"); addLog("服务不可用,请检查蓝牙连接");
} }
@@ -40,7 +40,7 @@ async function sendCommand(cmd) {
async function sendcmd(cmdTXT) { async function sendcmd(cmdTXT) {
let cmd = hexToBytes(cmdTXT); let cmd = hexToBytes(cmdTXT);
addLog('Send CMD: ' + cmdTXT); addLog(`发送命令: ${cmdTXT}`);
await sendCommand(cmd); await sendCommand(cmd);
} }
@@ -67,10 +67,9 @@ async function sendIMGArray(imgArray, type = 'bw'){
for (let i = 0; i < imgArray.length; i += chunkSize) { for (let i = 0; i < imgArray.length; i += chunkSize) {
let currentTime = (new Date().getTime() - startTime) / 1000.0; let currentTime = (new Date().getTime() - startTime) / 1000.0;
let chunk = imgArray.substring(i, i + chunkSize); let chunk = imgArray.substring(i, i + chunkSize);
setStatus('正在发送' + (type === 'bwr' ? "红色" : '黑白') + '块: ' setStatus(`发送${type === 'bwr' ? "红色" : '黑白'}块: ${chunkIdx+1}/${count+1}, 用时: ${currentTime}s`);
+ (chunkIdx+1) + "/" + (count+1) + ", 用时: " + currentTime + "s"); addLog(`发送块: ${chunk}`);
addLog('Sending chunk: ' + chunk); await sendCommand(hexToBytes(`04${chunk}`))
await sendCommand(hexToBytes("04" + chunk))
chunkIdx++; chunkIdx++;
} }
} }
@@ -93,8 +92,8 @@ async function sendimg(cmdIMG) {
await sendcmd("05"); await sendcmd("05");
let sendTime = (new Date().getTime() - startTime) / 1000.0; let sendTime = (new Date().getTime() - startTime) / 1000.0;
addLog("Done! Time used: " + sendTime + "s"); addLog(`发送完成!耗时: ${sendTime}s`);
setStatus("发送完成!耗时: " + sendTime + "s"); setStatus(`发送完成!耗时: ${sendTime}s`);
await sendcmd("06"); await sendcmd("06");
} }
@@ -110,55 +109,62 @@ function updateButtonStatus() {
function disconnect() { function disconnect() {
resetVariables(); resetVariables();
addLog('Disconnected.'); addLog('已断开连接.');
document.getElementById("connectbutton").innerHTML = '连接'; document.getElementById("connectbutton").innerHTML = '连接';
updateButtonStatus(); updateButtonStatus();
} }
function preConnect() { async function preConnect() {
if (gattServer != null && gattServer.connected) { if (gattServer != null && gattServer.connected) {
if (bleDevice != null && bleDevice.gatt.connected) if (bleDevice != null && bleDevice.gatt.connected)
bleDevice.gatt.disconnect(); bleDevice.gatt.disconnect();
} }
else { else {
connectTrys = 0; connectTrys = 0;
navigator.bluetooth.requestDevice({ bleDevice = await navigator.bluetooth.requestDevice({
optionalServices: ['62750001-d828-918d-fb46-b6c11c675aec'], optionalServices: ['62750001-d828-918d-fb46-b6c11c675aec'],
acceptAllDevices: true acceptAllDevices: true
}).then(device => { });
device.addEventListener('gattserverdisconnected', disconnect); await bleDevice.addEventListener('gattserverdisconnected', disconnect);
bleDevice = device; try {
connect(); await connect();
}).catch(handleError); } catch (e) {
await handleError(e);
}
} }
} }
function reConnect() { async function reConnect() {
connectTrys = 0; connectTrys = 0;
if (bleDevice != null && bleDevice.gatt.connected) if (bleDevice != null && bleDevice.gatt.connected)
bleDevice.gatt.disconnect(); bleDevice.gatt.disconnect();
resetVariables(); resetVariables();
addLog("Reconnect"); addLog("正在重连");
setTimeout(function () { connect(); }, 300); setTimeout(async function () { await connect(); }, 300);
} }
function connect() { async function connect() {
if (writeCharacteristic == null) { if (epdCharacteristic == null) {
addLog("Connecting to: " + bleDevice.name); addLog("正在连接: " + bleDevice.name);
bleDevice.gatt.connect().then(server => {
addLog('> Found GATT Server'); gattServer = await bleDevice.gatt.connect();
gattServer = server; addLog('> 找到 GATT Server');
return gattServer.getPrimaryService('62750001-d828-918d-fb46-b6c11c675aec');
}).then(service => { epdService = await gattServer.getPrimaryService('62750001-d828-918d-fb46-b6c11c675aec');
addLog('> Found Service'); addLog('> 找到 EPD Service');
Theservice = service;
return Theservice.getCharacteristic('62750002-d828-918d-fb46-b6c11c675aec'); epdCharacteristic = await epdService.getCharacteristic('62750002-d828-918d-fb46-b6c11c675aec');
}).then(characteristic => { addLog('> 找到 Characteristic');
addLog('> Found Characteristic');
document.getElementById("connectbutton").innerHTML = '断开'; await epdCharacteristic.startNotifications();
updateButtonStatus(); epdCharacteristic.addEventListener('characteristicvaluechanged', (event) => {
writeCharacteristic = characteristic; addLog(`> 收到配置:${bytesToHex(event.target.value.buffer.slice(0,8))}`);
}).catch(handleError); document.getElementById("epdpins").value = bytesToHex(event.target.value.buffer.slice(0, 7));
document.getElementById("epddriver").value = bytesToHex(event.target.value.buffer.slice(7, 8));
});
document.getElementById("connectbutton").innerHTML = '断开';
updateButtonStatus();
} }
} }