Skip to content

Commit

Permalink
Work on issues #2 and #3
Browse files Browse the repository at this point in the history
  • Loading branch information
maehw committed May 9, 2024
1 parent effded4 commit 411128d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
63 changes: 48 additions & 15 deletions communication/rcxSerial.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
let serialPort;
let serialReader;
let serialWriter;
let serialConnected = false;

// define various control elements
const log = document.getElementById('logArea');
Expand Down Expand Up @@ -127,26 +128,42 @@ async function transceiveCommand(opcode, params = new Uint8Array(), timeout = 50
* Opens a Web Serial connection to an RCX programmable brick
*/
async function serialConnect() {
let success = true; // think positive!

// Request a port and open a connection.
serialPort = await navigator.serial.requestPort();
// Wait for the port to open.
// Configure 2400 baud, 8-O-1, increase buffer size instead of the default 255 bytes
// FIXME: Some buffer does not seem to be consumed or flushed properly?! Waiting for a line break?!
const serialParams = { baudRate: 2400, parity: "odd", bufferSize: 3*32*1024 };
await serialPort.open(serialParams);
try {
serialPort = await navigator.serial.requestPort();
}
catch(e) {
showErrorMsg("Failed to open serial port: '" + e.message + "'");
success = false;
}

if(success) {
// Wait for the port to open.
// Configure 2400 baud, 8-O-1, increase buffer size instead of the default 255 bytes
// FIXME: Some buffer does not seem to be consumed or flushed properly?! Waiting for a line break?!
const serialParams = { baudRate: 2400, parity: "odd", bufferSize: 3*32*1024 };
await serialPort.open(serialParams);

const serialPortInfo = serialPort.getInfo();
showInfoMsg("Connected to serial device (baudrate: " + serialParams.baudRate + ").");
const serialPortInfo = serialPort.getInfo();
showInfoMsg("Connected to serial device (baudrate: " + serialParams.baudRate + ").");

serialReader = serialPort.readable.getReader();
serialWriter = serialPort.writable.getWriter();
let versionInfo = null;
serialReader = serialPort.readable.getReader();
serialWriter = serialPort.writable.getWriter();
let versionInfo = null;

let success = await ping();
if(!success) {
success = await ping();

if(!success) {
showErrorMsg("No communication with RCX possible.\n" +
"RCX needs to be switched on and placed close to and in line of sight of the IR tower.");
disableDownloadsBtns();
"RCX needs to be switched on and placed close to the IR tower and also in line of sight.");
}
}

if(!success) {
// Disable the download buttons if no communication is possible (no serial port or no ping from the RCX)
disableDownloadsBtns();
}

if(success) {
Expand Down Expand Up @@ -201,6 +218,16 @@ async function serialConnect() {

// Allow the serial port to be closed later.
serialWriter.releaseLock();

// Disconnect if connect has not been successful.
if(success) {
serialConnectBtn.innerHTML = '🔗 Serial Disconnect';
}
else {
serialDisconnect();
}

serialConnected = success;
}

async function serialReadWithTimeout(timeout) {
Expand Down Expand Up @@ -245,6 +272,7 @@ async function serialDisconnect() {
// Close the port.
await serialPort.close();
serialPort = null;
serialConnectBtn.innerHTML = '🔗 Serial Connect';

log.textContent += "Disconnected from serial device.\n";
}
Expand Down Expand Up @@ -304,6 +332,11 @@ async function clickProgramDownload() {
else {
showInfoMsg("Program download requested.");

// show an info message if the code has been touched after last build in the meantime
if(codeModified) {
showInfoMsg("❗ Code has been modified after last build. Consider re-building the current version of the code!");
}

serialWriter = serialPort.writable.getWriter();

const programNumber = 0; // TODO: make program slot selectable
Expand Down
7 changes: 2 additions & 5 deletions ide/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const logArea = document.getElementById('logArea');
const lineNumbers = document.querySelector('.line-numbers');

let usbConnected = false;
let serialConnected = false;
let codeModified = true; // code modified after build?

// Helper to check if browser supports WASM
const wasmSupported = (() => {
Expand Down Expand Up @@ -97,6 +97,7 @@ function syncScroll() {
// Update line numbers when content changes
codeArea.addEventListener('input', () => {
updateLineNumbers();
codeModified = true;
});

// Synchronize scrolling
Expand Down Expand Up @@ -197,14 +198,10 @@ usbConnectBtn.addEventListener('click', () => {

serialConnectBtn.addEventListener('click', () => {
if (!serialConnected) {
serialConnected = true;
serialConnectBtn.innerHTML = '🔗 Serial Disconnect';
disableUsbConnectBtn();
enableDownloadBtn();
enableDownloadFirmwareBtn();
} else {
serialConnected = false;
serialConnectBtn.innerHTML = '🔗 Serial Connect';
enableUsbConnectBtn();
disableDownloadBtn();
disableDownloadFirmwareBtn();
Expand Down
2 changes: 2 additions & 0 deletions ide/nqcWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ async function clickConvert() {
logDebug("Binary output length: " + out.length);
logDebug("Binary output as HEX: " + array2hex(out));

codeModified = false;

// copy binary to global variable
rcxBinary = out;
}
Expand Down

0 comments on commit 411128d

Please sign in to comment.