Skip to content

Commit

Permalink
refactor: update STK500 instantiation and modernized deprecated node …
Browse files Browse the repository at this point in the history
…functions
  • Loading branch information
barrenechea committed Oct 9, 2024
1 parent 1d7424b commit 3bee6f4
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 191 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { SerialPort } from "serialport";
import fs from "fs/promises";
import STK500, { type Board } from "stk500-esm";

const stk = new STK500();

const board: Board = {
name: "Arduino Uno",
baudRate: 115200,
Expand All @@ -42,8 +40,9 @@ async function upload(path: string) {
const hexData = await fs.readFile("path/to/your/sketch.hex", {
encoding: "utf8",
});
serialPort = new SerialPort({ path, baudRate: board.baud });
await stk.bootload(serialPort, hexData, board);
serialPort = new SerialPort({ path, baudRate: board.baudRate });
const stk = new STK500(serialPort, board);
await stk.bootload(hexData);
console.log("Programming successful!");
} catch (error) {
console.error("Programming failed:", error);
Expand Down Expand Up @@ -82,11 +81,12 @@ Replace `uno.ts` with the appropriate example file and `/dev/ttyACM0` with your

The main class `STK500` provides the following methods:

- `bootload(stream: NodeJS.ReadWriteStream, hexData: string | Buffer, opt: Board): Promise<void>`
- `sync(stream: NodeJS.ReadWriteStream, attempts: number, timeout: number): Promise<Buffer>`
- `verifySignature(stream: NodeJS.ReadWriteStream, signature: Buffer, timeout: number): Promise<Buffer>`
- `upload(stream: NodeJS.ReadWriteStream, hexData: string | Buffer, pageSize: number, timeout: number, use8BitAddresses = false): Promise<void>`
- `verify(stream: NodeJS.ReadWriteStream, hexData: string | Buffer, pageSize: number, timeout: number, use8BitAddresses = false): Promise<void>`
- `constructor(stream: NodeJS.ReadWriteStream, board: Board, opts?: STK500Options)`
- `bootload(hexData: string | Buffer): Promise<void>`
- `sync(attempts: number): Promise<Buffer>`
- `verifySignature(): Promise<Buffer>`
- `upload(hexData: string | Buffer): Promise<void>`
- `verify(hexData: string | Buffer): Promise<void>`

For more detailed API information, please refer to the TypeScript definitions or the source code.

Expand Down
5 changes: 2 additions & 3 deletions examples/avr4809.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from "fs/promises";
import { SerialPort } from "serialport";
import STK500, { type Board } from "../src/index.js";

const stk = new STK500();

const board: Board = {
name: "ATmega4809",
baudRate: 19200,
Expand Down Expand Up @@ -37,7 +35,8 @@ async function upload(path) {
encoding: "utf8",
});
serialPort = await createSerialPort(path, board.baudRate);
await stk.bootload(serialPort, hex, board);
const stk = new STK500(serialPort, board);
await stk.bootload(hex);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
Expand Down
5 changes: 2 additions & 3 deletions examples/diecimila-duemilanove168.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from "fs/promises";
import { SerialPort } from "serialport";
import STK500, { type Board } from "../src/index.js";

const stk = new STK500();

const board: Board = {
name: "Diecimila / Duemilanove 168",
baudRate: 19200,
Expand Down Expand Up @@ -36,7 +34,8 @@ async function upload(path) {
encoding: "utf8",
});
serialPort = await createSerialPort(path, board.baudRate);
await stk.bootload(serialPort, hex, board);
const stk = new STK500(serialPort, board);
await stk.bootload(hex);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
Expand Down
5 changes: 2 additions & 3 deletions examples/duemilanove328.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from "fs/promises";
import { SerialPort } from "serialport";
import STK500, { type Board } from "../src/index.js";

const stk = new STK500();

const board: Board = {
name: "Duemilanove 328",
baudRate: 57600,
Expand Down Expand Up @@ -37,7 +35,8 @@ async function upload(path) {
{ encoding: "utf8" }
);
serialPort = await createSerialPort(path, board.baudRate);
await stk.bootload(serialPort, hex, board);
const stk = new STK500(serialPort, board);
await stk.bootload(hex);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
Expand Down
5 changes: 2 additions & 3 deletions examples/lgt8f328.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from "fs/promises";
import { SerialPort } from "serialport";
import STK500, { type Board } from "../src/index.js";

const stk = new STK500();

const board: Board = {
name: "LGT8F328",
baudRate: 57600,
Expand Down Expand Up @@ -36,7 +34,8 @@ async function upload(path) {
encoding: "utf8",
});
serialPort = await createSerialPort(path, board.baudRate);
await stk.bootload(serialPort, hex, board);
const stk = new STK500(serialPort, board);
await stk.bootload(hex);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
Expand Down
5 changes: 2 additions & 3 deletions examples/nano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from "fs/promises";
import { SerialPort } from "serialport";
import STK500, { type Board } from "../src/index.js";

const stk = new STK500();

const board: Board = {
name: "Arduino Nano",
baudRate: 115200,
Expand Down Expand Up @@ -36,7 +34,8 @@ async function upload(path) {
encoding: "utf8",
});
serialPort = await createSerialPort(path, board.baudRate);
await stk.bootload(serialPort, hex, board);
const stk = new STK500(serialPort, board);
await stk.bootload(hex);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
Expand Down
5 changes: 2 additions & 3 deletions examples/uno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import fs from "fs/promises";
import { SerialPort } from "serialport";
import STK500, { type Board } from "../src/index.js";

const stk = new STK500();

const board: Board = {
name: "Arduino Uno",
baudRate: 115200,
Expand Down Expand Up @@ -36,7 +34,8 @@ async function upload(path) {
encoding: "utf8",
});
serialPort = await createSerialPort(path, board.baudRate);
await stk.bootload(serialPort, hex, board);
const stk = new STK500(serialPort, board);
await stk.bootload(hex);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stk500-esm",
"version": "1.1.2",
"version": "1.2.0",
"description": "A modern, ESM-compatible, TypeScript implementation of the STK500v1 protocol for programming Arduino boards directly from Node.js or the browser.",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down Expand Up @@ -29,10 +29,10 @@
"@eslint/js": "^9.12.0",
"@types/node": "^22.7.5",
"eslint": "^9.12.0",
"globals": "^15.10.0",
"globals": "^15.11.0",
"serialport": "^12.0.0",
"tsx": "^4.19.1",
"typescript": "^5.6.2",
"typescript": "^5.6.3",
"typescript-eslint": "^8.8.1"
}
}
Loading

0 comments on commit 3bee6f4

Please sign in to comment.