mirror of
https://github.com/susabolca/cc2640r2-etag.git
synced 2025-12-06 14:42:48 +08:00
125 lines
4.5 KiB
HTML
125 lines
4.5 KiB
HTML
<html lang="zh-CN">
|
|
|
|
<script>
|
|
let bleDevice;
|
|
let gattServer;
|
|
let Theservice;
|
|
let epdService;
|
|
let epochCharacter;
|
|
|
|
async function doConnect() {
|
|
if (gattServer != null && gattServer.connected) {
|
|
if (bleDevice != null && bleDevice.gatt.connected)
|
|
bleDevice.gatt.disconnect();
|
|
}
|
|
else {
|
|
bleDevice = await navigator.bluetooth.requestDevice({
|
|
filters: [{ namePrefix: ['C26_'] }],
|
|
optionalServices: [
|
|
0xfff0,
|
|
],
|
|
//acceptAllDevices: true
|
|
});
|
|
await bleDevice.addEventListener('gattserverdisconnected', disconnect);
|
|
await connect();
|
|
}
|
|
}
|
|
|
|
async function connect() {
|
|
if (epochCharacter == null) {
|
|
info("Connect to " + bleDevice.name)
|
|
gattServer = await bleDevice.gatt.connect();
|
|
info('> Found GATT server');
|
|
epdService = await gattServer.getPrimaryService(0xfff0);
|
|
info('> Found EPD service');
|
|
epochCharacter = await epdService.getCharacteristic(0xfff1);
|
|
document.getElementById("btnConnect").innerHTML = 'Disconnect';
|
|
document.getElementById('etagFn').style.visibility='';
|
|
}
|
|
}
|
|
|
|
function disconnect() {
|
|
bleDevice = null;
|
|
epdService = null;
|
|
epochCharacter = null;
|
|
info('Disconnected.');
|
|
|
|
document.getElementById("btnConnect").innerHTML = 'Connect';
|
|
document.getElementById('etagFn').style.visibility='hidden';
|
|
}
|
|
|
|
async function doSetTime() {
|
|
var epoch = Date.now() / 1000 | 0;
|
|
var buf = new ArrayBuffer(4);
|
|
var arr = new Uint32Array(buf);
|
|
arr[0] = epoch;
|
|
await epochCharacter.writeValueWithResponse(arr);
|
|
info("Write unix epoch: " + epoch);
|
|
}
|
|
|
|
async function doReadEtag() {
|
|
var host_epoch = Date.now() / 1000 | 0;
|
|
|
|
// read current time
|
|
var chr = await epdService.getCharacteristic(0xfff1);
|
|
var epoch = (await chr.readValue()).getUint32(0, 1);
|
|
|
|
// read time zone
|
|
var chr = await epdService.getCharacteristic(0xfff2);
|
|
var tz_min = (await chr.readValue()).getInt32(0, 1);
|
|
info(`# host time: ${host_epoch}, diff (${epoch - host_epoch}) seconds.`);
|
|
info(`# etag time: ${epoch}, tz: ${tz_min} minutes of UTC.`);
|
|
|
|
// battery
|
|
var chr = await epdService.getCharacteristic(0xfff3);
|
|
var batt = (await chr.readValue()).getUint16(0, 1);
|
|
|
|
// Temperature
|
|
var chr = await epdService.getCharacteristic(0xfff4);
|
|
var temp = (await chr.readValue()).getInt8(0, 1);
|
|
info(`# etag sensor: battery(${batt}mv), temperature(${temp}'C). `);
|
|
|
|
// RTC Collaborate
|
|
var chr = await epdService.getCharacteristic(0xfff5);
|
|
var rtc_collab = (await chr.readValue()).getInt8(0, 1);
|
|
info(`# rtc collab: ${rtc_collab} every 1 second.`);
|
|
}
|
|
|
|
async function doRtcCollab() {
|
|
var col = prompt("对 32.768kHz 晶振补偿频漂,走时快补偿负数,走时慢补偿正数。可选范围 (-3 ~ 3)", 0);
|
|
if (col == null || col < -3 || col > 3) return;
|
|
var chr = await epdService.getCharacteristic(0xfff5);
|
|
var buf = new ArrayBuffer(1);
|
|
var arr = new Int8Array(buf);
|
|
arr[0] = parseInt(col);
|
|
await chr.writeValueWithResponse(arr);
|
|
info(`write RTC collabration: ${col}`);
|
|
}
|
|
|
|
function info(logTXT) {
|
|
var today = new Date();
|
|
var time = ("0" + today.getHours()).slice(-2) + ":" + ("0" + today.getMinutes()).slice(-2) + ":" + ("0" + today.getSeconds()).slice(-2) + " : ";
|
|
document.getElementById("log").innerHTML += time + logTXT + '<br>';
|
|
console.log(time + logTXT);
|
|
while ((document.getElementById("log").innerHTML.match(/<br>/g) || []).length > 10) {
|
|
var logs_br_position = document.getElementById("log").innerHTML.search("<br>");
|
|
document.getElementById("log").innerHTML = document.getElementById("log").innerHTML.substring(logs_br_position + 4);
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<p>
|
|
<label> Choose </label>
|
|
<button id="btnConnect" type="button" onclick="doConnect()">Connect</button>
|
|
</p>
|
|
<p id="etagFn" style="visibility:hidden;">
|
|
<button id="btnReadEtag" type="button" onclick="doReadEtag()">ReadEtag</button>
|
|
<button id="btnSetTime" type="button" onclick="doSetTime()">SetTime</button>
|
|
<button id="btnRtcCollab" type="button" onclick="doRtcCollab()">RtcCollab</button>
|
|
</p>
|
|
<p>
|
|
<div id="log">
|
|
CC2640R2-ETAG Webtool. <br/>
|
|
</div>
|
|
</p>
|
|
</html> |