diff options
author | Ernesto Castellotti <mail@ernestocastellotti.it> | 2023-01-11 00:55:41 +0100 |
---|---|---|
committer | Simone <26844016+simonebortolin@users.noreply.github.com> | 2023-01-12 00:02:02 +0100 |
commit | 85e80a257f014abf1dae161ad1052d65bb6bea85 (patch) | |
tree | 2b0662a0b06a484f705febb0f4546e4e86978eca /assets/js/utils.js | |
parent | Moved all root process to rootLantiq.js (diff) | |
download | hack-gpon.github.io-85e80a257f014abf1dae161ad1052d65bb6bea85.tar hack-gpon.github.io-85e80a257f014abf1dae161ad1052d65bb6bea85.tar.gz hack-gpon.github.io-85e80a257f014abf1dae161ad1052d65bb6bea85.tar.bz2 hack-gpon.github.io-85e80a257f014abf1dae161ad1052d65bb6bea85.tar.lz hack-gpon.github.io-85e80a257f014abf1dae161ad1052d65bb6bea85.tar.xz hack-gpon.github.io-85e80a257f014abf1dae161ad1052d65bb6bea85.tar.zst hack-gpon.github.io-85e80a257f014abf1dae161ad1052d65bb6bea85.zip |
Diffstat (limited to 'assets/js/utils.js')
-rw-r--r-- | assets/js/utils.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/assets/js/utils.js b/assets/js/utils.js new file mode 100644 index 0000000..8ce3c6e --- /dev/null +++ b/assets/js/utils.js @@ -0,0 +1,29 @@ +function delay(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function openPortLineBreak(port, baudRate, outputErrorCallback) { + try { + await port.open({ baudRate: baudRate }); + } catch (err) { + outputErrorCallback(`Error: ${err.message}`); + return; + } + + const textDecoder = new TextDecoderStream(); + const readableStreamClosed = port.readable.pipeTo(textDecoder.writable); + const reader = await textDecoder.readable.pipeThrough(new TransformStream(new LineBreakTransformer())).getReader(); + const textEncoderStream = new TextEncoderStream(); + const writerStreamClosed = textEncoderStream.readable.pipeTo(port.writable); + const writer = await textEncoderStream.writable.getWriter(); + + return { reader, writer, readableStreamClosed, writerStreamClosed }; +} + +async function closePortLineBreak(port, reader, writer, readableStreamClosed, writerStreamClosed) { + reader.cancel(); + await readableStreamClosed.catch(() => { /* Ignore the error */ }); + writer.close(); + await writerStreamClosed; + await port.close(); +} |