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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"rules": {
"no-console": 1, // Means warning
"prettier/prettier": 2 // Means error
"prettier/prettier": 2, // Means error
"@typescript-eslint/no-inferrable-types": "off"
}
}
3 changes: 2 additions & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"dev": "npm run clean && npm run genDocs && parcel src/index.html",
"build": "npm run clean && npm run genDocs && parcel build src/index.html --no-optimize --public-url ./",
"clean": "rimraf dist .parcel-cache",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "npm run build"
},
"parcelIgnore": [
"./docs/.+"
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ <h4 align="center">A Serial Flasher utility for Espressif chips</h4>
<hr/>
<div id="program">
<h3> Program </h3>
<label for="baudrates" id="lblBaudrate">Baudrate:</label>
<label style="display:none" id="lblConnTo">Connected to device: </label>
<label for="baudrates" id="lblBaudrate">Baudrate:</label>
<select name="baudrates" id="baudrates">
<option value="921600">921600</option>
<option value="460800">460800</option>
Expand Down
53 changes: 41 additions & 12 deletions examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const alertDiv = document.getElementById("alertDiv");

// 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.2.0/bundle.js
import { ESPLoader, FlashOptions, LoaderOptions, Transport } from "../../../lib";
// https://unpkg.com/esptool-js/bundle.js
import { ESPLoader, FlashOptions, LoaderOptions, WebSerialTransport, SerialOptions, ITrace } from "../../../lib";

declare let Terminal; // Terminal is imported in HTML script
declare let CryptoJS; // CryptoJS is imported in HTML script
Expand All @@ -32,7 +32,7 @@ const term = new Terminal({ cols: 120, rows: 40 });
term.open(terminal);

let device = null;
let transport: Transport;
let transport: WebSerialTransport;
let chip: string = null;
let esploader: ESPLoader;

Expand Down Expand Up @@ -79,19 +79,47 @@ const espLoaderTerminal = {
},
};

class TraceObject implements ITrace {
traceBuffer: string;
private lastTraceTime = Date.now();

trace(message: string) {
const delta = Date.now() - this.lastTraceTime;
const prefix = `TRACE ${delta.toFixed(3)}`;
const traceMessage = `${prefix} ${message}`;
console.log(traceMessage);
this.traceBuffer += traceMessage + "\n";
}

async returnTrace() {
try {
await navigator.clipboard.writeText(this.traceBuffer);
console.log("Text copied to clipboard!");
} catch (err) {
console.error("Failed to copy text:", err);
}
return this.traceBuffer;
}
}

const traceObj = new TraceObject();

connectButton.onclick = async () => {
if (device === null) {
device = await navigator.serial.requestPort({});
transport = new Transport(device, true);
transport = new WebSerialTransport(device, traceObj);
}

const serialOptions = { baudRate: parseInt(baudrates.value) } as SerialOptions;

try {
const flashOptions = {
const loaderOptions = {
transport,
baudrate: parseInt(baudrates.value),
serialOptions,
terminal: espLoaderTerminal,
tracer: traceObj,
} as LoaderOptions;
esploader = new ESPLoader(flashOptions);
esploader = new ESPLoader(loaderOptions);

chip = await esploader.main();

Expand All @@ -116,8 +144,8 @@ connectButton.onclick = async () => {
};

traceButton.onclick = async () => {
if (transport) {
transport.returnTrace();
if (traceObj) {
traceObj.returnTrace();
}
};

Expand Down Expand Up @@ -231,7 +259,7 @@ let isConsoleClosed = false;
consoleStartButton.onclick = async () => {
if (device === null) {
device = await navigator.serial.requestPort({});
transport = new Transport(device, true);
transport = new WebSerialTransport(device, traceObj);
}
lblConsoleFor.style.display = "block";
lblConsoleBaudrate.style.display = "none";
Expand All @@ -240,12 +268,13 @@ consoleStartButton.onclick = async () => {
consoleStopButton.style.display = "initial";
resetButton.style.display = "initial";
programDiv.style.display = "none";
const serialOptions = { baudRate: parseInt(consoleBaudrates.value) } as SerialOptions;

await transport.connect(parseInt(consoleBaudrates.value));
await transport.connect(serialOptions);
isConsoleClosed = false;

while (true && !isConsoleClosed) {
const val = await transport.rawRead();
const val = await transport.read();
if (typeof val !== "undefined") {
term.write(val);
} else {
Expand Down
Loading