Skip to content

Commit

Permalink
refactor: move to TypeScript (#1)
Browse files Browse the repository at this point in the history
* chore: initial setup

* refactor: update examples to use async/await

* refactor: rename index.js to index.ts and add TypeScript typings for STK500 class

* refactor: lib files to TypeScript

* refactor: update uno.js to use async/await

* refactor: rename example files to TypeScript, changed ts runner

* refactor: remove allowJs option from tsconfig.json after migration

* chore: add .npmignore, updated copyright

* chore: add GitHub Actions workflow for quality checks
  • Loading branch information
barrenechea authored Oct 7, 2024
1 parent a255983 commit 451fc57
Show file tree
Hide file tree
Showing 25 changed files with 1,053 additions and 309 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Quality

on: pull_request

concurrency:
group: ${{ github.workflow }}-${{ github.ref_name }}
cancel-in-progress: true

jobs:
# lint:
# name: Linting rules
# runs-on: ubuntu-latest

# steps:
# - name: Checkout
# uses: actions/checkout@v4

# - name: Setup Node.js
# uses: actions/setup-node@v4
# with:
# node-version-file: .nvmrc

# - name: Install dependencies
# run: npm ci

# - name: Linting rules
# run: npm run lint

build:
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

test:
name: Tests
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: .nvmrc

- name: Install dependencies
run: npm ci

- name: Run test suite
run: npm test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules/
dist/
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!dist
!package.json
!README.md
!LICENSE
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22
3 changes: 1 addition & 2 deletions LICENSE.md → LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Jacob Rosenthal
Copyright (c) 2024 Sebastian Barrenechea

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

49 changes: 0 additions & 49 deletions examples/avr4809.js

This file was deleted.

63 changes: 63 additions & 0 deletions examples/avr4809.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fs from "fs/promises";
import { SerialPort } from "serialport";
import intel_hex from "intel-hex";
import Stk500 from "../src/index.js";

const stk = new Stk500();

const board = {
name: "ATmega4809",
baud: 19200,
signature: Buffer.from([0x1e, 0x94, 0x06]),
pageSize: 128,
timeout: 400,
};

async function readHexFile(filePath) {
const data = await fs.readFile(filePath, { encoding: "utf8" });
return intel_hex.parse(data).data;
}

function createSerialPort(path, baudRate) {
return new Promise((resolve, reject) => {
const serialPort = new SerialPort({ path, baudRate });
serialPort.on("open", () => resolve(serialPort));
serialPort.on("error", reject);
});
}

async function closeSerialPort(serialPort) {
return new Promise<void>((resolve) => {
serialPort.close((error) => {
if (error) console.log(error);
resolve();
});
});
}

async function upload(path) {
let serialPort;
try {
const hex = await readHexFile("arduino-1.0.6/168/avr4809.cpp.hex");
serialPort = await createSerialPort(path, board.baud);
await stk.bootload(serialPort, hex, board, false);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
} finally {
if (serialPort) {
await closeSerialPort(serialPort);
}
}
}

async function main() {
if (process.argv[2]) {
await upload(process.argv[2]);
} else {
console.log("Call with a path like /dev/tty.something");
}
process.exit(0);
}

main().catch(console.error);
49 changes: 0 additions & 49 deletions examples/diecimila-duemilanove168.js

This file was deleted.

63 changes: 63 additions & 0 deletions examples/diecimila-duemilanove168.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fs from "fs/promises";
import { SerialPort } from "serialport";
import intel_hex from "intel-hex";
import Stk500 from "../src/index.js";

const stk = new Stk500();

const board = {
name: "Diecimila / Duemilanove 168",
baud: 19200,
signature: Buffer.from([0x1e, 0x94, 0x06]),
pageSize: 128,
timeout: 400,
};

async function readHexFile(filePath) {
const data = await fs.readFile(filePath, { encoding: "utf8" });
return intel_hex.parse(data).data;
}

function createSerialPort(path, baudRate) {
return new Promise((resolve, reject) => {
const serialPort = new SerialPort({ path, baudRate });
serialPort.on("open", () => resolve(serialPort));
serialPort.on("error", reject);
});
}

async function closeSerialPort(serialPort) {
return new Promise<void>((resolve) => {
serialPort.close((error) => {
if (error) console.log(error);
resolve();
});
});
}

async function upload(path) {
let serialPort;
try {
const hex = await readHexFile("arduino-1.0.6/168/StandardFirmata.cpp.hex");
serialPort = await createSerialPort(path, board.baud);
await stk.bootload(serialPort, hex, board, false);
console.log("Programming SUCCESS!");
} catch (error) {
console.error("Programming failed:", error);
} finally {
if (serialPort) {
await closeSerialPort(serialPort);
}
}
}

async function main() {
if (process.argv[2]) {
await upload(process.argv[2]);
} else {
console.log("Call with a path like /dev/tty.something");
}
process.exit(0);
}

main().catch(console.error);
50 changes: 0 additions & 50 deletions examples/duemilanove328.js

This file was deleted.

Loading

0 comments on commit 451fc57

Please sign in to comment.