diff --git a/types/firmata/index.d.ts b/types/firmata/index.d.ts index 820e4d5c8b..f18294d110 100644 --- a/types/firmata/index.d.ts +++ b/types/firmata/index.d.ts @@ -16,7 +16,7 @@ export = Board; * guarantee that it cannot be improved. */ declare class Board extends NodeJS.EventEmitter { - constructor(serialPort: string, callback?: (error: any) => void) + constructor(serialPort: string, optionsOrCallback?: Board.Options|((error: any) => void), callback?: (error: any) => void) MODES: Board.PinModes; STEPPER: Board.StepperConstants; I2C_MODES: Board.I2cModes; @@ -151,6 +151,13 @@ declare class Board extends NodeJS.EventEmitter { } declare namespace Board { + // https://github.com/firmata/firmata.js/blob/master/lib/firmata.js#L429-L451 + interface Options { + reportVersionTimeout?: number; + samplingInterval?: number; + serialport?: SerialPort.options; + } + interface PinModes { INPUT: PIN_MODE; OUTPUT: PIN_MODE; @@ -251,9 +258,8 @@ declare namespace Board { minor: number; } - // TODO these enums could actually be non-const in the future (provides some benefits) --- TWW // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L449-L464 - const enum PIN_MODE { + enum PIN_MODE { INPUT = 0x00, OUTPUT = 0x01, ANALOG = 0x02, @@ -270,30 +276,30 @@ declare namespace Board { UNKNOWN = 0x10, } - const enum PIN_STATE { + enum PIN_STATE { LOW = 0, HIGH = 1 } - const enum REPORTING { + enum REPORTING { ON = 1, OFF = 0, } - const enum PULLUP_STATE { + enum PULLUP_STATE { ENABLED = 1, DISABLED = 0, } // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L474-L478 - const enum STEPPER_TYPE { + enum STEPPER_TYPE { DRIVER = 1, TWO_WIRE = 2, FOUR_WIRE = 4, } // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L479-L484 - const enum STEPPER_RUN_STATE { + enum STEPPER_RUN_STATE { STOP = 0, ACCEL = 1, DECEL = 2, @@ -301,13 +307,13 @@ declare namespace Board { } // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L485-L488 - const enum STEPPER_DIRECTION { + enum STEPPER_DIRECTION { CCW = 0, CW = 1, } // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L466-L471 - const enum I2C_MODE { + enum I2C_MODE { WRITE = 0, READ = 1, CONTINUOUS_READ = 2, @@ -315,13 +321,13 @@ declare namespace Board { } // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L491-L494 - const enum SERIAL_MODE { + enum SERIAL_MODE { CONTINUOUS_READ = 0x00, STOP_READING = 0x01, } // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L497-L512 - const enum SERIAL_PORT_ID { + enum SERIAL_PORT_ID { HW_SERIAL0 = 0x00, HW_SERIAL1 = 0x01, HW_SERIAL2 = 0x02, @@ -334,7 +340,7 @@ declare namespace Board { } // https://github.com/firmata/firmata.js/blob/v0.15.0/lib/firmata.js#L515-L524 - const enum SERIAL_PIN_TYPE { + enum SERIAL_PIN_TYPE { RES_RX0 = 0x00, RES_TX0 = 0x01, RES_RX1 = 0x02, diff --git a/types/serialport/index.d.ts b/types/serialport/index.d.ts index 5b284cc669..ba632b7322 100644 --- a/types/serialport/index.d.ts +++ b/types/serialport/index.d.ts @@ -3,52 +3,87 @@ // Definitions by: Jeremy Foster // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare module 'serialport' { - class SerialPort { - //openImmediately already removed in 4.0.7 - constructor(path: string, options?: Object, callback?: (err: any) => void) - isOpen(): boolean; - on(event: string, callback?: (data?: any) => void): void; - open(callback?: (err: any) => void): void; - write(buffer: any, callback?: (err: any, bytesWritten: number) => void): void - pause(): void; - resume(): void; - disconnected(err: Error): void; - close(callback?: (err: any) => void): void; - flush(callback?: (err: any) => void): void; - set(options: SerialPort.setOptions, callback: (err: any) => void): void; - drain(callback?: (err: any) => void): void; - update(options: SerialPort.updateOptions, callback?: (err: any) => void): void; - static list(callback: (err: any, ports: SerialPort.portConfig[]) => void): void; - static parsers: { - readline: (delimiter: string) => void, - raw: (emitter: any, buffer: string) => void - }; - } +/// +/// - namespace SerialPort { - interface portConfig { - comName: string; - manufacturer: string; - serialNumber: string; - pnpId: string; - locationId: string; - vendorId: string; - productId: string; - } +export = SerialPort; - interface setOptions { - brk?: boolean; - cts?: boolean; - dsr?: boolean; - dtr?: boolean; - rts?: boolean; - } - - interface updateOptions { - baudRate?: number; - } - } - - export = SerialPort; +declare class SerialPort extends Stream { + // openImmediately already removed in 4.0.7 + constructor(path: string, options?: SerialPort.options|SerialPort.callback, callback?: SerialPort.callback); + isOpen(): boolean; + on(event: string, callback?: (data?: any) => void): void; + open(callback?: SerialPort.callback): void; + write(buffer: any, callback?: (err: any, bytesWritten: number) => void): void; + pause(): void; + resume(): void; + disconnected(err: Error): void; + close(callback?: SerialPort.callback): void; + flush(callback?: SerialPort.callback): void; + set(options: SerialPort.setOptions, callback: SerialPort.callback): void; + drain(callback?: SerialPort.callback): void; + update(options: SerialPort.updateOptions, callback?: SerialPort.callback): void; + static list(callback: (err: any, ports: SerialPort.portConfig[]) => void): void; + // https://github.com/EmergingTechnologyAdvisors/node-serialport/blob/4.0.7/lib/parsers.js + static parsers: SerialPort.parsers; +} + +declare namespace SerialPort { + interface portConfig { + comName: string; + manufacturer: string; + serialNumber: string; + pnpId: string; + locationId: string; + vendorId: string; + productId: string; + } + + interface setOptions { + brk?: boolean; + cts?: boolean; + dsr?: boolean; + dtr?: boolean; + rts?: boolean; + } + + interface updateOptions { + baudRate?: number; + } + + type serialParser = (emitter: NodeJS.EventEmitter, buffer: Buffer|string) => void; + + type readlineParser = (delimiter: string, encoding?: 'ascii'|'utf8'|'utf16le'|'ucs2'|'base64'|'binary'|'hex') => serialParser; + + type byteLengthParser = (delimiter: number) => serialParser; + + type byteDelimiterParser = (delimiter: number[]) => serialParser; + + type callback = (error: any) => void; + + interface parsers { + raw: serialParser; + readline: readlineParser; + byteLength: byteLengthParser; + byteDelimiter: byteDelimiterParser; + } + + // https://github.com/EmergingTechnologyAdvisors/node-serialport/blob/4.0.7/README.md#user-content-serialport-path-options-opencallback + interface options { + autoOpen?: boolean; + lock?: boolean; + baudRate?: 115200|57600|38400|19200|9600|4800|2400|1800|1200|600|300|200|150|134|110|75|50|number; + dataBits?: 8|7|6|5; + stopBits?: 1|2; + parity?: 'none'|'even'|'mark'|'odd'|'space'; + rtscts?: boolean; + xon?: boolean; + xoff?: boolean; + bufferSize?: number; + parser?: serialParser; + platformOptions?: { + vmin?: number; + vtime?: number; + }; + } } diff --git a/types/serialport/serialport-tests.ts b/types/serialport/serialport-tests.ts index 60ca6f119c..7b1c69bf74 100644 --- a/types/serialport/serialport-tests.ts +++ b/types/serialport/serialport-tests.ts @@ -1,36 +1,60 @@ -// Tests for serialport.d.ts -// Project: https://github.com/EmergingTechnologyAdvisors/node-serialport -// Definitions by: Jeremy Foster -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // Tests taken from documentation samples. import * as SerialPort from 'serialport'; function test_basic_connect() { - let port = new SerialPort(""); + let port = new SerialPort(''); } function test_connect_config() { - let port = new SerialPort("", { - baudrate: 0, - disconnectedCallback: function () { }, - parser: SerialPort.parsers.readline("\n") - }); + let port1 = new SerialPort('', { + baudRate: 0, + parser: SerialPort.parsers.raw + }, (err: any) => {}); + + let port2 = new SerialPort('', { + baudRate: 0, + parser: SerialPort.parsers.readline('\n', 'ascii') + }, (err: any) => {}); + + let port3 = new SerialPort('', { + baudRate: 0, + parser: SerialPort.parsers.byteLength(7) + }, (err: any) => {}); + + let port4 = new SerialPort('', { + baudRate: 0, + parser: SerialPort.parsers.byteDelimiter([3, 4, 5]) + }, (err: any) => {}); + + let port5 = new SerialPort('', { + autoOpen: false, + lock: false, + baudRate: 115200, + dataBits: 5, + stopBits: 2, + parity: 'odd', + rtscts: true, + xon: true, + xoff: true, + bufferSize: 1024, + platformOptions: { + vmin: 1, + vtime: 1 + } + }, (err: any) => {}); } function test_write() { - let port = new SerialPort(""); - port.write("main screen turn on", (err, bytesWritten) => { - }); + let port = new SerialPort(''); + port.write('main screen turn on', (err, bytesWritten) => {}); } function test_events() { - let port = new SerialPort(""); - port.on("open", function () { }); + let port = new SerialPort(''); + port.on('open', () => {}); } function test_list_ports() { - SerialPort.list( (err: string, ports: SerialPort.portConfig[]) => { - - }); + SerialPort.list((err: string, ports: SerialPort.portConfig[]) => {}); } diff --git a/types/serialport/tsconfig.json b/types/serialport/tsconfig.json index f70ef60163..72f0329252 100644 --- a/types/serialport/tsconfig.json +++ b/types/serialport/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/serialport/tslint.json b/types/serialport/tslint.json new file mode 100644 index 0000000000..2221e40e4a --- /dev/null +++ b/types/serialport/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } \ No newline at end of file