Adding web serial.

This commit is contained in:
Brad Nelson
2024-08-05 10:24:03 -07:00
parent de39be5392
commit 8ab745ab9c
2 changed files with 89 additions and 0 deletions

View File

@ -984,6 +984,87 @@ JSWORD: random { n -- n }
return Math.floor(Math.random() * n); return Math.floor(Math.random() * n);
~ ~
r~
context.serial_buffer = [];
context.ports = [];
context.port = null;
context.serial_writer = null;
async function RouteSerial(port) {
await port.open({
baudRate: 115200,
});
if (port.writable) {
context.serial_writer = port.writable.getWriter();
}
while (port.readable) {
const reader = port.readable.getReader();
try {
while (true) {
const { value, done } = await reader.read();
if (done) {
reader.releaseLock();
break;
}
for (var i = 0; i < value.length; ++i) {
context.serial_buffer.push(value[i]);
}
}
} catch (error) {
}
}
}
context.UpdateSerial = function() {
navigator.serial.getPorts().then((ports) => {
context.ports = ports;
if (context.ports.length > 0) {
context.port = context.ports[0];
} else {
context.port = null;
}
context.serial_writer = null;
if (context.port) {
RouteSerial(context.port);
}
});
};
if (!globalObj.write && navigator && navigator.serial) {
navigator.serial.addEventListener("connect", (e) => {
context.UpdateSerial();
});
navigator.serial.addEventListener("disconnect", (e) => {
context.UpdateSerial();
});
context.UpdateSerial();
}
~ jseval
JSWORD: pairserial { -- }
navigator.serial
.requestPort()
.then((port) => {
context.UpdateSerial();
})
.catch((e) => {
console.log('No serial port selected.');
context.UpdateSerial();
});
~
JSWORD: serial-key-raw { -- n }
if (context.serial_buffer.length) {
return context.serial_buffer.shift();
} else {
return -1;
}
~
JSWORD: serial-type { a n -- }
if (!context.port || !context.serial_writer) {
return;
}
context.serial_writer.write(u8.slice(a, a + n));
~
0 0 importScripts constant scripts# 0 0 importScripts constant scripts#
create scripts scripts# allot create scripts scripts# allot
scripts scripts# importScripts drop scripts scripts# importScripts drop

View File

@ -72,4 +72,12 @@ web definitions
' yielding 10 10 task yielding-task ' yielding 10 10 task yielding-task
yielding-task start-task yielding-task start-task
: serial
begin
pause
begin serial-key-raw dup 0< 0= while emit repeat drop
begin key? while key >r rp@ 1 serial-type rdrop repeat
again
;
forth definitions forth definitions