diff options
| author | s-ol <s+removethis@s-ol.nu> | 2024-03-12 17:57:09 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2024-03-12 17:57:27 +0000 |
| commit | ab627790da4597e4addc1c7816b7516f366ec339 (patch) | |
| tree | d239cb7ea547eea57efc48e2413902447215de04 | |
| parent | web: basic styling for sidebar (diff) | |
| download | t937-serial-ab627790da4597e4addc1c7816b7516f366ec339.tar.gz t937-serial-ab627790da4597e4addc1c7816b7516f366ec339.zip | |
web: convert tabs to spaces
| -rw-r--r-- | web/index.html | 10 | ||||
| -rw-r--r-- | web/main.js | 214 | ||||
| -rw-r--r-- | web/protocol.js | 10 |
3 files changed, 122 insertions, 112 deletions
diff --git a/web/index.html b/web/index.html index ee91e01..9d55a13 100644 --- a/web/index.html +++ b/web/index.html @@ -45,6 +45,16 @@ <input type="checkbox" id="cool_mode" /> </div> </section> + <section class="grow"> + <h1>profiles</h1> + <div> + <button id="load" class="grow">load</button> + <button id="save" class="grow">save</button> + <button id="create" class="grow">create</button> + </div> + <select id="storedprofile" multiple class="grow"> + </select> + </section> </aside> <svg id="plot" /> <script type="module" src="main.js" ></script> diff --git a/web/main.js b/web/main.js index 9209796..7ba1731 100644 --- a/web/main.js +++ b/web/main.js @@ -34,46 +34,46 @@ let TIMEOUT; let TEMPS = ''; const elements = [ - 'temps', - 'connect', 'status', 'profile', 'start', 'read', 'write', - 'heat', 'heat_mode', 'cool', 'cool_mode', + 'temps', + 'connect', 'status', 'profile', 'start', 'read', 'write', + 'heat', 'heat_mode', 'cool', 'cool_mode', ]; const el = Object.fromEntries(elements.map(id => [id, document.getElementById(id)])); const updateUI = () => { - el.connect.innerText = !PORT ? 'connect' : 'disconnect'; - const ready = STATUS && STATUS !== 'connecting'; - el.profile.disabled = STATUS === 'running'; - el.start.innerText = (STATUS !== 'running' && STATUS !== 'manual') ? 'start' : 'stop'; - el.start.disabled = !ready; - el.read.disabled = !ready; - el.write.disabled = !ready; - - el.temps.innerText = TEMPS; - const end = END_TIME ?? Date.now(); - const duration = START_TIME && Math.floor((end - START_TIME) / 1000); - - switch (STATUS) { - case null: - el.status.innerText = '(disconnected)'; - break; - - case 'connecting': - el.status.innerText = '(connecting)'; - break; - - case 'idle': - el.status.innerText = 'idle'; - break; - - case 'manual': - el.status.innerText = `manual mode - ${duration}s`; - break; - - case 'running': - el.status.innerText = `profile ${+el.profile.value + 1} - ${duration}s`; - break; - } + el.connect.innerText = !PORT ? 'connect' : 'disconnect'; + const ready = STATUS && STATUS !== 'connecting'; + el.profile.disabled = STATUS === 'running'; + el.start.innerText = (STATUS !== 'running' && STATUS !== 'manual') ? 'start' : 'stop'; + el.start.disabled = !ready; + el.read.disabled = !ready; + el.write.disabled = !ready; + + el.temps.innerText = TEMPS; + const end = END_TIME ?? Date.now(); + const duration = START_TIME && Math.floor((end - START_TIME) / 1000); + + switch (STATUS) { + case null: + el.status.innerText = '(disconnected)'; + break; + + case 'connecting': + el.status.innerText = '(connecting)'; + break; + + case 'idle': + el.status.innerText = 'idle'; + break; + + case 'manual': + el.status.innerText = `manual mode - ${duration}s`; + break; + + case 'running': + el.status.innerText = `profile ${+el.profile.value + 1} - ${duration}s`; + break; + } }; updateUI(); @@ -84,17 +84,17 @@ const disconnect = async () => { STATUS = null; updateUI(); - await port.reader.cancel().catch(() => null); - await port.writer.close().catch(() => null); + await port.reader.cancel().catch(() => null); + await port.writer.close().catch(() => null); await port.port.close(); }; const onMessage = async (msg) => { - if (msg.func === FUNC.O_TEMPS) { - console.debug(msg.toString(), msg); - } else { - console.info(msg.toString(), msg); - } + if (msg.func === FUNC.O_TEMPS) { + console.debug(msg.toString(), msg); + } else { + console.info(msg.toString(), msg); + } if (msg.address !== ADDR.CTRL) { console.warn(`unknown address ${hex(msg.address)}`); @@ -112,19 +112,19 @@ const onMessage = async (msg) => { case ARG.CONNECT_CONNECTED: console.log('connection established'); - STATUS = 'idle'; + STATUS = 'idle'; break; } break; } case FUNC.O_TEMPS: { - clearTimeout(TIMEOUT); - TIMEOUT = setTimeout(() => { - if (!PORT) return; - console.error('port timed out'); - disconnect(); - }, 2500); + clearTimeout(TIMEOUT); + TIMEOUT = setTimeout(() => { + if (!PORT) return; + console.error('port timed out'); + disconnect(); + }, 2500); const temp1 = msg.get16(0); const temp2 = msg.get16(2); @@ -132,9 +132,9 @@ const onMessage = async (msg) => { TEMPS = `${temp1}°C - ${temp2}°C // ${temp3}°C`; if (!STATUS || STATUS === 'connecting') { - STATUS = 'idle'; + STATUS = 'idle'; } else if (STATUS === 'running' || STATUS == 'manual') { - plot.addPoint((temp1 + temp2) / 2); + plot.addPoint((temp1 + temp2) / 2); } break; } @@ -144,7 +144,7 @@ const onMessage = async (msg) => { break; } - updateUI(); + updateUI(); } const loop = async () => { @@ -166,7 +166,7 @@ const loop = async () => { }; el.connect.onclick = async () => { - if (!!PORT) return disconnect(); + if (!!PORT) return disconnect(); const port = await navigator.serial.requestPort(); await port.open({ baudRate: 38400 }); @@ -177,8 +177,8 @@ el.connect.onclick = async () => { reader = port.readable.getReader(); writer = port.writable.getWriter(); PORT = { port, reader, writer }; - STATUS = 'connecting'; - updateUI(); + STATUS = 'connecting'; + updateUI(); await loop(); } finally { @@ -189,60 +189,60 @@ el.connect.onclick = async () => { }; const pwms = [ - [el.heat, el.heat_mode], - [el.cool, el.cool_mode], + [el.heat, el.heat_mode], + [el.cool, el.cool_mode], ]; for (const [slider, checkbox] of pwms) { - slider.onchange = async (e) => { - if (!PORT) return; - - checkbox.checked = +slider.value; - await send8(FUNC.C_PWM_HEAT, +el.heat.value, el.heat_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); - await send8(FUNC.C_PWM_COOL, +el.cool.value, el.cool_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); - if (STATUS === 'idle' && el.heat_mode.checked) { - STATUS = 'manual'; - START_TIME = Date.now(); - END_TIME = null; - updateUI(); - } - }; - checkbox.onchange = async (e) => { - if (!PORT) return; - - if (checkbox.checked && +slider.value === 0) { - slider.value = 0x64; - } - await send8(FUNC.C_PWM_HEAT, +el.heat.value, el.heat_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); - await send8(FUNC.C_PWM_COOL, +el.cool.value, el.cool_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); - if (STATUS === 'idle' && el.heat_mode.checked) { - STATUS = 'manual'; - START_TIME = Date.now(); - END_TIME = null; - updateUI(); - } - }; + slider.onchange = async (e) => { + if (!PORT) return; + + checkbox.checked = +slider.value; + await send8(FUNC.C_PWM_HEAT, +el.heat.value, el.heat_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); + await send8(FUNC.C_PWM_COOL, +el.cool.value, el.cool_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); + if (STATUS === 'idle' && el.heat_mode.checked) { + STATUS = 'manual'; + START_TIME = Date.now(); + END_TIME = null; + updateUI(); + } + }; + checkbox.onchange = async (e) => { + if (!PORT) return; + + if (checkbox.checked && +slider.value === 0) { + slider.value = 0x64; + } + await send8(FUNC.C_PWM_HEAT, +el.heat.value, el.heat_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); + await send8(FUNC.C_PWM_COOL, +el.cool.value, el.cool_mode.checked ? ARG.PWM_ON : ARG.PWM_OFF); + if (STATUS === 'idle' && el.heat_mode.checked) { + STATUS = 'manual'; + START_TIME = Date.now(); + END_TIME = null; + updateUI(); + } + }; } el.start.onclick = () => { - if (STATUS === 'idle') { - STATUS = el.profile.value === '' ? 'manual' : 'running'; - START_TIME = Date.now(); - END_TIME = null; - - if (el.profile.value !== '') { - const i = +el.profile.value; - send8(FUNC.C_STARTSTOP, i, ARG.STARTSTOP_START); - plot.clear(); - } - } else if (STATUS === 'running') { - STATUS = 'idle'; - END_TIME = Date.now(); - - const i = +el.profile.value; - send8(FUNC.C_STARTSTOP, i, ARG.STARTSTOP_STOP); - } else if (STATUS === 'manual') { - STATUS = 'idle'; - END_TIME = Date.now(); - } - updateUI(); + if (STATUS === 'idle') { + STATUS = el.profile.value === '' ? 'manual' : 'running'; + START_TIME = Date.now(); + END_TIME = null; + + if (el.profile.value !== '') { + const i = +el.profile.value; + send8(FUNC.C_STARTSTOP, i, ARG.STARTSTOP_START); + plot.clear(); + } + } else if (STATUS === 'running') { + STATUS = 'idle'; + END_TIME = Date.now(); + + const i = +el.profile.value; + send8(FUNC.C_STARTSTOP, i, ARG.STARTSTOP_STOP); + } else if (STATUS === 'manual') { + STATUS = 'idle'; + END_TIME = Date.now(); + } + updateUI(); }; diff --git a/web/protocol.js b/web/protocol.js index 9c93f8e..5454739 100644 --- a/web/protocol.js +++ b/web/protocol.js @@ -70,14 +70,14 @@ export class Message { } getDataArray() { - return new Uint8Array(this.buf, 4, this.buf.byteLength - 6); + return new Uint8Array(this.buf, 4, this.buf.byteLength - 6); } toString() { - const address = lookup(ADDR, this.address) ?? "????"; - const func = lookup(FUNC, this.func) ?? "?_??"; - const data = this.getDataArray(); - return `to ${address}: ${func.substr(2)} [${data}]`; + const address = lookup(ADDR, this.address) ?? "????"; + const func = lookup(FUNC, this.func) ?? "?_??"; + const data = this.getDataArray(); + return `to ${address}: ${func.substr(2)} [${data}]`; } static from8(addr, func, data) { |
