Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor transport into interface to allow extending serial implementations. #122

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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 @@

// 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 @@
term.open(terminal);

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

Expand Down Expand Up @@ -79,30 +79,58 @@
},
};

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);

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

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
this.traceBuffer += traceMessage + "\n";
}

async returnTrace() {
try {
await navigator.clipboard.writeText(this.traceBuffer);
console.log("Text copied to clipboard!");

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

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
} catch (err) {
console.error("Failed to copy text:", err);

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

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
}
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();

// Temporarily broken
// await esploader.flashId();
} catch (e) {
console.error(e);

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

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
term.writeln(`Error: ${e.message}`);
}

console.log("Settings done for :" + chip);

Check warning on line 133 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 @@ -116,8 +144,8 @@
};

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

Expand All @@ -134,7 +162,7 @@
try {
await esploader.eraseFlash();
} catch (e) {
console.error(e);

Check warning on line 165 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,7 +259,7 @@
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,19 +268,20 @@
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 {
break;
}
}
console.log("quitting console");

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

View workflow job for this annotation

GitHub Actions / ci

Unexpected console statement
};

consoleStopButton.onclick = async () => {
Expand Down Expand Up @@ -351,7 +380,7 @@
} as FlashOptions;
await esploader.writeFlash(flashOptions);
} catch (e) {
console.error(e);

Check warning on line 383 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
Loading
Loading