web: move packet formation out of serial.js
This commit is contained in:
parent
7fb9d8bebc
commit
e2752e30d5
|
|
@ -1,65 +1,34 @@
|
|||
// serial.js
|
||||
let port;
|
||||
|
||||
async function connect() {
|
||||
async function getPort() {
|
||||
if (port) return port;
|
||||
try {
|
||||
port = await navigator.serial.requestPort();
|
||||
[port] = await navigator.serial.getPorts();
|
||||
await port.open({ baudRate: 115200 });
|
||||
console.log('Connected to serial device:', port);
|
||||
return port;
|
||||
} catch (error) {
|
||||
console.error('Error connecting to serial device:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function write(addr, data) {
|
||||
try {
|
||||
if (!port) {
|
||||
console.error('Serial port not connected');
|
||||
return;
|
||||
}
|
||||
|
||||
// Format the data to be sent
|
||||
const sendData = `W${addr.toString(16).padStart(4, '0')}${data.toString(16).padStart(4, '0')}\r\n`;
|
||||
|
||||
// Example: Sending data to the device
|
||||
const writer = port.writable.getWriter();
|
||||
await writer.write(new TextEncoder().encode(sendData));
|
||||
await writer.releaseLock();
|
||||
console.log('Sent:', sendData);
|
||||
} catch (error) {
|
||||
console.error('Error sending data:', error);
|
||||
}
|
||||
async function write(data) {
|
||||
const port = await getPort();
|
||||
const writer = port.writable.getWriter();
|
||||
await writer.write(new TextEncoder().encode(data));
|
||||
await writer.releaseLock();
|
||||
console.log('Sent:', data);
|
||||
}
|
||||
|
||||
async function read(addr) {
|
||||
try {
|
||||
if (!port) {
|
||||
console.error('Serial port not connected');
|
||||
return;
|
||||
}
|
||||
async function read() {
|
||||
const port = await getPort();
|
||||
const reader = port.readable.getReader();
|
||||
const {value, done} = await reader.read();
|
||||
reader.releaseLock();
|
||||
|
||||
// Format the read command
|
||||
const readCmd = `R${addr.toString(16).padStart(4, '0')}\r\n`;
|
||||
|
||||
// Send the read command to the device
|
||||
const writer = port.writable.getWriter();
|
||||
await writer.write(new TextEncoder().encode(readCmd));
|
||||
await writer.releaseLock();
|
||||
console.log('Sent:', readCmd);
|
||||
|
||||
// Read back the response
|
||||
const reader = port.readable.getReader(); // Create a new reader
|
||||
const { value, done } = await reader.read(); // Read from the stream
|
||||
reader.releaseLock(); // Release the reader's lock
|
||||
|
||||
if (!done) {
|
||||
const responseData = new TextDecoder().decode(value);
|
||||
console.log('Received:', responseData);
|
||||
return responseData;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error reading data:', error);
|
||||
if (!done) {
|
||||
const data = new TextDecoder().decode(value);
|
||||
console.log('Received:', data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('connectButton').addEventListener('click', connect);
|
||||
|
|
@ -18,7 +18,7 @@ function runAfterUpload() {
|
|||
}
|
||||
|
||||
// Your additional JavaScript code to run after file upload
|
||||
web_terminal(data);
|
||||
WebTerminal(data);
|
||||
}
|
||||
|
||||
runAfterUpload(); // Start checking for file upload completion
|
||||
|
|
@ -1,4 +1,10 @@
|
|||
async function web_terminal(data){
|
||||
document.getElementById('connectButton').addEventListener('click', selectPort);
|
||||
|
||||
async function selectPort(){
|
||||
await navigator.serial.requestPort();
|
||||
}
|
||||
|
||||
async function WebTerminal(data){
|
||||
let pyodide = await loadPyodide();
|
||||
|
||||
// Load Manta.yaml into pyodide's file system
|
||||
|
|
@ -45,8 +51,9 @@ async function web_terminal(data){
|
|||
|
||||
async def barfoo():
|
||||
print("entering barfoo")
|
||||
print(await read(m.my_io_core._base_addr))
|
||||
print(await read(m.my_io_core._base_addr + 1))
|
||||
await write("R0000\\r\\n")
|
||||
print(await read())
|
||||
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(barfoo())
|
||||
|
|
@ -54,4 +61,4 @@ async function web_terminal(data){
|
|||
#await foobar() # doesn't work either! await outside function
|
||||
#await barfoo() # doesn't work either! await outside function
|
||||
`);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
|
||||
<!-- Load Javascript -->
|
||||
<script src="../javascripts/web_terminal.js"></script>
|
||||
<script src="../javascripts/webTerminal.js"></script>
|
||||
<script src="../javascripts/serial.js"></script>
|
||||
<script src="../javascripts/upload.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/pyodide/v0.25.1/full/pyodide.js"></script>
|
||||
|
|
|
|||
Loading…
Reference in New Issue