Skip to content

Commit

Permalink
Consistent formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cinderblock committed May 11, 2023
1 parent 0ad930f commit 9f166ef
Show file tree
Hide file tree
Showing 32 changed files with 456 additions and 269 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# If using prettier
DisableFormat: true
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
insert_final_newline = true
trim_trailing_whitespace = true

[*.sh]
indent_style = tab
12 changes: 12 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extends:
- eslint:recommended
- plugin:@typescript-eslint/recommended
- airbnb
- prettier
parser: '@typescript-eslint/parser'
plugins:
- '@typescript-eslint'
rules:
'@typescript-eslint/no-unused-vars': error
'@typescript-eslint/no-explicit-any': error
'@typescript-eslint/no-floating-promises': error
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AUTHORS text
ChangeLog text
COPYING text
NEWS text

*.md text
11 changes: 11 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/coverage
/dist
/.dist
/yarn.lock
/node_modules
/package-lock.json

# No Parser
.gitignore
.editorconfig
.prettierignore
4 changes: 4 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
printWidth: 120
singleQuote: true
arrowParens: avoid
trailingComma: all
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"recommendations": [
"streetsidesoftware.code-spell-checker",
"editorconfig.editorconfig",
"dbaeumer.vscode-eslint",
"me-dutour-mathieu.vscode-github-actions",
"esbenp.prettier-vscode"
]
}
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files.exclude": {
"node_modules/": true
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": ["source.formatDocument", "source.fixAll.eslint"],
"typescript.updateImportsOnFileMove.enabled": "always"
}
19 changes: 19 additions & 0 deletions CSpell/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Dictionaries for CSpell

This is a collection of dictionaries for [CSpell](https://cspell.org/)

## `dependencies.txt`

Words from dependencies.

## `export.txt`

Words we export.

## `names.txt`

Names of people, places, and things.

## `words.txt`

Words that are not in the other dictionaries.
1 change: 1 addition & 0 deletions CSpell/dependencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parens
Empty file added CSpell/export.txt
Empty file.
2 changes: 2 additions & 0 deletions CSpell/names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Tacklind
cinderblock
3 changes: 3 additions & 0 deletions CSpell/words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
microcontrollers
bootloader
baudrate
22 changes: 22 additions & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '0.2'
language: en
ignorePaths:
- '**/*.svg'
- .vscode/extensions.json
- .git
dictionaries:
- en_US
- filetypes
- names
- export
- words
- dependencies
dictionaryDefinitions:
- name: dependencies
path: ./CSpell/dependencies.txt
- name: export
path: ./CSpell/export.txt
- name: names
path: ./CSpell/names.txt
- name: words
path: ./CSpell/words.txt
23 changes: 15 additions & 8 deletions src/Handshake.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {InSystemProgramming} from './InSystemProgramming';
import { InSystemProgramming } from './InSystemProgramming';

const ECHO = false;

export function handshake(isp: InSystemProgramming, count: number = Infinity, timeout: number = 20): Promise<InSystemProgramming> {
export function handshake(
isp: InSystemProgramming,
count: number = Infinity,
timeout: number = 20,
): Promise<InSystemProgramming> {
return new Promise<InSystemProgramming>((resolve, reject) => {
console.log(`Sync'ing...`);
(function synchronize() {
let ret = isp.write('?')
let ret = isp
.write('?')
.then(() => isp.assert(/^\?*Synchronized/, timeout))
.then(isp => isp.writeln('Synchronized'))
.then(isp => isp.assert(/Synchronized/))
Expand All @@ -16,12 +21,14 @@ export function handshake(isp: InSystemProgramming, count: number = Infinity, ti
.then(isp => isp.assertOK());
if (!InSystemProgramming.VLAB_MODE) {
// XXX our custom bootloader implements only a subset
ret = ret.then(isp => isp.setEcho(ECHO))
.then(isp => isp.readPartIdentification())
.then(partId => isp.readBootcodeVersion())
.then(bootVer => isp)
ret = ret
.then(isp => isp.setEcho(ECHO))
.then(isp => isp.readPartIdentification())
.then(partId => isp.readBootcodeVersion())
.then(bootVer => isp);
}
ret.then(isp => resolve(isp))
ret
.then(isp => resolve(isp))
.catch(error => {
if (count-- <= 0) {
return reject(error);
Expand Down
121 changes: 78 additions & 43 deletions src/InSystemProgramming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SerialPort, ReadlineParser, SerialPortOpenOptions } from 'serialport';
import { AutoDetectTypes } from '@serialport/bindings-cpp';
import * as ReturnCode from './ReturnCode';

const UNLOCK_CODE = 0x5A5A;
const UNLOCK_CODE = 0x5a5a;

interface DataQueue<T> {
push(data: T): void;
Expand All @@ -12,31 +12,50 @@ interface DataQueue<T> {

class LineQueue implements DataQueue<string> {
private queue: string[] = [];
drain(): void { this.queue.length = 0; }
pop(): string { return this.queue.shift(); }
push(data: string): void { this.queue.push(data); }
drain(): void {
this.queue.length = 0;
}
pop(): string {
return this.queue.shift();
}
push(data: string): void {
this.queue.push(data);
}
}

interface Logger<T> { log(msg: T): void; }
class VerboseLogger implements Logger<string> { log(msg: string) { console.log(msg); } }
class QuiteLogger implements Logger<string> { log(msg: string) { /* nothing */ } }
interface Logger<T> {
log(msg: T): void;
}
class VerboseLogger implements Logger<string> {
log(msg: string) {
console.log(msg);
}
}
class QuiteLogger implements Logger<string> {
log(msg: string) {
/* nothing */
}
}

const LINE_QUEUE = new LineQueue;
const LINE_QUEUE = new LineQueue();

const _baudRateSym = Symbol();
const _bootVerSym = Symbol();
const _partIdSym = Symbol();

export class InSystemProgramming {

public static get VLAB_MODE() { return process.env['ISP'] === 'vlab' };
public static get VLAB_MODE() {
return process.env['ISP'] === 'vlab';
}

private serialport;
private serialportParser;

private queue: DataQueue<string> = LINE_QUEUE;

set verbose(b: boolean) { this.logger = b ? new VerboseLogger() : new QuiteLogger(); }
set verbose(b: boolean) {
this.logger = b ? new VerboseLogger() : new QuiteLogger();
}

private logger: Logger<string> = new QuiteLogger();

Expand Down Expand Up @@ -91,16 +110,22 @@ export class InSystemProgramming {
clearTimeout(to);
resolve(data);
},
pop: (): string => { throw new Error('Not implemented'); },
drain: (): void => { throw new Error('Not implemented'); }
pop: (): string => {
throw new Error('Not implemented');
},
drain: (): void => {
throw new Error('Not implemented');
},
};
})(setTimeout(() => {
try {
reject(new Error(`Timed out: > ${timeout}ms`));
} finally {
this.queue = LINE_QUEUE;
}
}, timeout));
})(
setTimeout(() => {
try {
reject(new Error(`Timed out: > ${timeout}ms`));
} finally {
this.queue = LINE_QUEUE;
}
}, timeout),
);
});
}

Expand Down Expand Up @@ -139,14 +164,16 @@ export class InSystemProgramming {
sendLine(data: string): Promise<InSystemProgramming> {
let p = this.writeln(data);
if (this.echo) {
p = p.then(() => {
return this.read();
}).then((ack) => {
if (ack !== data) {
throw new Error(`Not acknowledged: ${JSON.stringify(ack)}`);
}
return this;
});
p = p
.then(() => {
return this.read();
})
.then(ack => {
if (ack !== data) {
throw new Error(`Not acknowledged: ${JSON.stringify(ack)}`);
}
return this;
});
}
return p;
}
Expand All @@ -156,8 +183,8 @@ export class InSystemProgramming {
}

assertSuccess(): Promise<InSystemProgramming> {
return this.read().then((data) => {
if (!(/^\d+$/.test(data))) {
return this.read().then(data => {
if (!/^\d+$/.test(data)) {
throw new TypeError(`Not a number: ${JSON.stringify(data)}`);
}
ReturnCode.rethrow(~~data);
Expand All @@ -166,7 +193,7 @@ export class InSystemProgramming {
}

assertOK(): Promise<InSystemProgramming> {
return this.read().then((data) => {
return this.read().then(data => {
if (data !== 'OK') {
throw new Error(`Not "OK": ${JSON.stringify(data)}`);
}
Expand All @@ -175,7 +202,7 @@ export class InSystemProgramming {
}

assert(ack: RegExp, timeout?: number): Promise<InSystemProgramming> {
return this.read(timeout).then((data) => {
return this.read(timeout).then(data => {
if (data.match(ack) === null) {
throw new Error(`Not /${ack.source}/: ${JSON.stringify(data)}`);
}
Expand All @@ -197,16 +224,20 @@ export class InSystemProgramming {
}

setEcho(echo: boolean): Promise<InSystemProgramming> {
return this.sendCommand(`A ${echo ? 1 : 0}`)
.then(() => {
this.echo = echo;
return this;
});
return this.sendCommand(`A ${echo ? 1 : 0}`).then(() => {
this.echo = echo;
return this;
});
}

get baudRate(): number { return this[_baudRateSym]; }
get baudRate(): number {
return this[_baudRateSym];
}

setBaudRate(baud: number, stop: SerialPortOpenOptions<AutoDetectTypes>['stopBits'] = 1): Promise<InSystemProgramming> {
setBaudRate(
baud: number,
stop: SerialPortOpenOptions<AutoDetectTypes>['stopBits'] = 1,
): Promise<InSystemProgramming> {
baud = ~~baud;
if (this.baudRate === baud) {
return Promise.resolve(this);
Expand All @@ -218,21 +249,25 @@ export class InSystemProgramming {
.then(() => this.open());
}

get partIdentification(): number { return this[_partIdSym]; }
get partIdentification(): number {
return this[_partIdSym];
}

readPartIdentification(): Promise<string> {
return this.sendCommand('J')
.then(() => this.read())
.then(partId => this[_partIdSym] = partId);
.then(partId => (this[_partIdSym] = partId));
}

get bootcodeVersion(): number { return this[_bootVerSym]; }
get bootcodeVersion(): number {
return this[_bootVerSym];
}

readBootcodeVersion(): Promise<string> {
let major_ = '';
return this.sendCommand('K')
.then(() => this.read())
.then(major => Promise.all([major, this.read()]))
.then(ver => this[_bootVerSym] = `${ver[0]}.${ver[1]}`);
.then(ver => (this[_bootVerSym] = `${ver[0]}.${ver[1]}`));
}
}
1 change: 0 additions & 1 deletion src/MemoryBlock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export interface MemoryBlock {
address: number;
length: number;
Expand Down
Loading

0 comments on commit 9f166ef

Please sign in to comment.