Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/typescript/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ <h3>Console </h3>
<input class="btn btn-info" type="button" id="consoleStartButton" value="Start" />
<input class="btn btn-info" type="button" id="consoleStopButton" value="Stop" />
<input class="btn btn-info" type="button" id="resetButton" value="Reset" />

<br><br>

<input class="btn btn-info btn-sm" type="file" id="addElfFile" value="Add Elf files" accept=".elf" multiple />
<hr/>
</div>
<div id="terminal"></div>
Expand Down
69 changes: 67 additions & 2 deletions examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
const lblConnTo = document.getElementById("lblConnTo");
const table = document.getElementById("fileTable") as HTMLTableElement;
const alertDiv = document.getElementById("alertDiv");
const addElfFileButton = document.getElementById("addElfFile") as HTMLInputElement;

const debugLogging = document.getElementById("debugLogging") as HTMLInputElement;

// This is a frontend example of Esptool-JS using local bundle file
// To optimize use a CDN hosted version like
// https://unpkg.com/esptool-js@0.5.0/bundle.js
import { ESPLoader, FlashOptions, LoaderOptions, Transport } from "../../../lib";
import { ESPLoader, FlashOptions, LoaderOptions, Transport, AddressDecoder } from "../../../lib";
import { serial } from "web-serial-polyfill";

const serialLib = !navigator.serial && navigator.usb ? serial : navigator.serial;
Expand Down Expand Up @@ -72,6 +73,26 @@
reader.readAsBinaryString(file);
}

/**
* File reader handler to read given local files.
* @param {Event} evt File Select event
*/
async function handleElfFileSelect(evt) {
const files = evt.target.files;

if (files.length === 0) return;
// get all files as an array of arrayBuffers
const elfFileBuffers = await Promise.all(Array.from(files).map((file: File) => file.arrayBuffer()));
await AddressDecoder.update(elfFileBuffers);
}

addElfFileButton.onchange = handleElfFileSelect;

const encoder = new TextEncoder();
export const stringToUInt8Array = function (textString: string) {
return encoder.encode(textString);
};

const espLoaderTerminal = {
clean() {
term.clear();
Expand Down Expand Up @@ -102,7 +123,7 @@

// Temporarily broken
// await esploader.flashId();
console.log("Settings done for :" + chip);

Check warning on line 126 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
lblBaudrate.style.display = "none";
lblConnTo.innerHTML = "Connected to device: " + chip;
lblConnTo.style.display = "block";
Expand All @@ -114,7 +135,7 @@
filesDiv.style.display = "initial";
consoleDiv.style.display = "none";
} catch (e) {
console.error(e);

Check warning on line 138 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
term.writeln(`Error: ${e.message}`);
}
};
Expand All @@ -138,7 +159,7 @@
try {
await esploader.eraseFlash();
} catch (e) {
console.error(e);

Check warning on line 162 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
term.writeln(`Error: ${e.message}`);
} finally {
eraseButton.disabled = false;
Expand Down Expand Up @@ -231,12 +252,28 @@
cleanUp();
};

/**
* Handles incoming data from the terminal and writes it to the transport device.
* @param {string} data - The string data received from the terminal.
*/
function onDataHandler(data: string) {
const writer = transport.device.writable?.getWriter();
if (writer) {
writer.write(stringToUInt8Array(data));
writer.releaseLock();
} else {
console.error("Unable to write to serial port");

Check warning on line 265 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
}
}
let onDataDispose: () => void;

let isConsoleClosed = false;
consoleStartButton.onclick = async () => {
if (device === null) {
device = await serialLib.requestPort({});
transport = new Transport(device, true);
}
onDataDispose = term.onData(onDataHandler).dispose;
lblConsoleFor.style.display = "block";
lblConsoleBaudrate.style.display = "none";
consoleBaudrates.style.display = "none";
Expand All @@ -248,19 +285,34 @@
await transport.connect(parseInt(consoleBaudrates.value));
isConsoleClosed = false;

const output = (line: string) => {
term.writeln(line);
};
let lastLine = "";
while (true && !isConsoleClosed) {
const readLoop = transport.rawRead();
const { value, done } = await readLoop.next();

if (done || !value) {
break;
}
term.write(value);

const valueStr = uInt8ArrayToString(value);
lastLine += valueStr;
const splitLine = lastLine.split("\r\n");
while (splitLine.length > 1) {
const line = splitLine.shift();
if (line !== undefined) {
AddressDecoder.parser(line, output);
}
}
lastLine = splitLine[0];
}
console.log("quitting console");

Check warning on line 311 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
};

consoleStopButton.onclick = async () => {
onDataDispose();
isConsoleClosed = true;
if (transport) {
await transport.disconnect();
Expand All @@ -277,6 +329,19 @@
cleanUp();
};

/**
* Convert a Uint8Array to a string
* @param {Uint8Array} fileBuffer Uint8Array to convert
* @returns {string} String representation of the Uint8Array
*/
export function uInt8ArrayToString(fileBuffer: Uint8Array): string {
let fileBufferString = "";
for (let i = 0; i < fileBuffer.length; i++) {
fileBufferString += String.fromCharCode(fileBuffer[i]);
}
return fileBufferString;
}

/**
* Validate the provided files images and offset to see if they're valid.
* @returns {string} Program input validation result
Expand Down Expand Up @@ -357,7 +422,7 @@
await esploader.writeFlash(flashOptions);
await esploader.after();
} catch (e) {
console.error(e);

Check warning on line 425 in examples/typescript/src/index.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
term.writeln(`Error: ${e.message}`);
} finally {
// Hide progress bars and show erase buttons
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"atob-lite": "^2.0.0",
"jselftools": "^0.2.5",
"pako": "^2.1.0",
"tslib": "^2.4.1"
},
Expand Down
Loading
Loading