From 4f5f33cbfbf12ee9a8d6c7a71d4dea619ec64460 Mon Sep 17 00:00:00 2001 From: vaishnav98 Date: Wed, 27 Jun 2018 00:58:02 +0530 Subject: [PATCH 1/2] add TypeScript definitions originally written by @troywweber7 also submitted to @types : https://github.com/DefinitelyTyped/DefinitelyTyped/pull/26814 --- package.json | 3 + src/index.d.ts | 256 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 src/index.d.ts diff --git a/package.json b/package.json index b7ec2bd2..4acee259 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "url": "git://github.com/jadonk/bonescript" }, "main": "main", + "types": "./src/index.d.ts", "engines": { "node": ">= 6.0.0" }, @@ -32,10 +33,12 @@ "socket.io": "1.4.5", "systemd": "0.3.1", "winston": "2.1.1", + "@types/verror": "latest", "shelljs": "0.8.2" }, "optionalDependencies": { "serialport": "6.0.5", + "@types/serialport": "latest", "i2c": "0.2.3", "epoll": "0.1.18", "pi-spi": "1.0.2", diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 00000000..6b09af0f --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,256 @@ +// Type definitions for bonescript 0.6 +// Project: https://github.com/jadonk/bonescript +// Definitions by: Troy W. +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import * as SerialPort from 'serialport'; +import * as VError from 'verror'; + +// OBJECTS +export const bone: BoneObj; + +// FUNCTIONS - TODO obliterate all "any" types where possible! --- TWW +// https://github.com/jadonk/bonescript#system + +export function getPlatform(callback?: (error: ErrorType, platform: any) => void): any; + +export function getEeproms(callback?: (error: ErrorType, eeproms: any) => void): any; + +export function echo(data: any, callback?: (error: ErrorType, data: any) => void): any; + +export function readTextFile(filename: string, callback?: (error: ErrorType, data: any) => void): any; + +export function writeTextFile(filename: string, data: any, callback?: (error: ErrorType, data: any) => void): void; + +export function writeCModule(filename: string, data: any, callback?: (error: ErrorType, data: any) => void): void; + +export function setDate(date: any, callback?: (error: ErrorType, data: any) => void): void; + +export function analogRead(pin: string, callback?: (err: ErrorType, value: number) => void): any; + +export function analogWrite(pin: string, value: number, freq?: number, callback?: ErrorCb): void; + +export function attachInterrupt( + pin: string, + handler: InterruptFn, + mode: InterruptType, + callback?: AttachIntCb +): void; + +export function detachInterrupt(pin: string, callback?: DetachIntCb): void; + +export function digitalRead(pin: string, callback?: (err: ErrorType, value: number) => void): any; + +export function digitalWrite(pin: string, value: PinStateType, callback?: DigitalWriteCb): void; + +export function pinMode( + pin: string, + direction: any, + mux?: any, + pullup?: any, + slew?: any, + callback?: PinModeCb +): void; + +export function getPinMode(pin: string, callback?: (err: ErrorType, mode: PinModeObj) => void): any; + +export function shiftOut(dataPin: any, clockPin: any, bitOrder: any, val: any, callback?: ErrorCb): void; + +// https://github.com/jadonk/bonescript#serial +export function serialOpen(port: any, options: any, callback?: ErrorCb): void; + +export function serialWrite(port: any, data: any, callback?: ErrorCb): void; + +export const serialParsers: typeof SerialPort.parsers; + +export function i2cOpen(port: any, address: any, options: any, callback?: ErrorCb): void; + +export function i2cScan(port: any, callback?: ErrorCb): void; + +export function i2cWriteByte(port: any, byte: any, callback?: ErrorCb): void; + +export function i2cWriteBytes(port: any, command: any, bytes: any, callback?: ErrorCb): void; + +export function i2cReadByte(port: any, callback?: ErrorCb): void; + +export function i2cReadBytes(port: any, command: any, length: any, callback?: ErrorCb): void; + +export function i2cStream(port: any, command: any, length: any, callback?: ErrorCb): void; +// ffi +export function loadCModule(filename: string, data: any, isMRAA?: boolean): any; + +export function lowByte(value: number): number; + +export function highByte(value: number): number; + +export function bitRead(value: number, bitnum: number): number; + +export function bitWrite(value: number, bitnum: number, bitdata: any): number; + +export function bitSet(value: number, bitnum: number): number; + +export function bitClear(value: number, bitnum: number): number; + +export function bit(bitnum: number): number; + +export function min(x: number, y: number): number; + +export function max(x: number, y: number): number; + +export function abs(x: number): number; + +export function constrain(x: number, a: number, b: number): number; + +export function map(value: number, fromLow: number, fromHigh: number, toLow: number, toHigh: number): number; + +export function pow(x: number, y: number): number; + +export function sqrt(x: number): number; + +export function sin(radians: number): number; + +export function cos(radians: number): number; + +export function tan(radians: number): number; + +export function randomSeed(x: number): number; + +export function random(min: number, max?: number): number; + +export function delay(...args: any[]): any; + +export function autorun(...args: any[]): any; + +export function serverStart(...args: any[]): any; + +export function socketJSReqHandler(...args: any[]): any; + +export function addSocketListeners(...args: any[]): any; + +export function setGlobals(...args: any[]): any; + +// CONSTANTS +export const OUTPUT: 'out'; +export const INPUT: 'in'; +export const INPUT_PULLUP: 'in_pullup'; +export const ANALOG_OUTPUT: 'analog_out'; +export const HIGH: 1; +export const LOW: 0; +export const LSBFIRST: 1; +export const MSBFIRST: 0; +export const CHANGE: 'both'; +export const RISING: 'rising'; +export const FALLING: 'falling'; + +// INTERFACES - adjusted based on observation / testing +export interface PinModeObj { + gpio?: { + active: boolean, + direction: 'in' | 'out', + allocated: boolean, + }; + modes: string[]; + mux?: number; + name: string; + pin: string; + pinState: string; + pullup?: 'diabled' | 'pullup' | 'pulldown'; + pwm?: { + freq: number, + value: number, + }; + rx?: 'enabled' | 'disabled'; + slew?: 'fast' | 'slow'; +} + +export interface BoneObj { + i2c: i2cObj; + uarts: UartsObj; + getPinKeys(): string[]; + getPinObjects(): any; + naturalCompare(): any; +} + +export interface PinsObj { + [i: string]: PinInfo; +} + +export interface UartsObj { + [i: string]: UartInfo; +} + +export interface i2cObj { + [i: string]: i2cInfo; +} + +export interface PinInfo { + // Analog In + ain?: number; + // GPIO / LEDs / Analog In + eeprom?: number; + // GPIO / LEDs + gpio?: number; + key: string; + // LEDs + led?: string; + mux?: string; + muxRegOffset?: string; + // All + name: string; + options?: string[]; + // PWM + pwm?: PwmInfo; + scale?: number; + universalName?: string; +} + +export interface PwmInfo { + addr: string; + chip: string; + index: number; + module: string; + muxmode: number; + name: string; + path: string; + sysfs: number; +} + +export interface UartInfo { + devicetree?: string; + rx?: string; + tx?: string; +} + +export interface i2cInfo { + devicetree?: string; + path?: string; + scl?: string; + sda?: string; +} + +// TYPES - adjusted based on observation / testing +export type PinStateType = typeof HIGH | typeof LOW; +export type PinModeType = typeof ANALOG_OUTPUT | typeof INPUT | typeof INPUT_PULLUP | typeof OUTPUT; +export type InterruptType = typeof RISING | typeof FALLING | typeof CHANGE; +export type ErrorCb = (err: ErrorType) => void; +export type InterruptFn = (resp: { + pin: PinInfo, + value: number +}) => void; +export type AttachIntCb = (resp: { + pin: PinInfo, + attached: boolean +}) => void; +export type DetachIntCb = (resp: { + pin: PinInfo, + detached: boolean +}) => void; +export type PinModeCb = (resp: { + value: number, + err: VError +}) => void; +export type DigitalWriteCb = (resp: { + data: any, + err: VError +}) => void; +export type ErrorType = VError; From 7c46bb0d92344736e577e2c11dd1aebe17570d13 Mon Sep 17 00:00:00 2001 From: vaishnav Date: Tue, 10 Jul 2018 23:50:24 +0530 Subject: [PATCH 2/2] modifications to typescript definitions change function return value type to any removed getEeproms definition defined platform object structure removed unnecesary exports --- src/index.d.ts | 110 ++++++++++++++++++++++++++++--------------------- 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 6b09af0f..9d1400ca 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -12,70 +12,68 @@ export const bone: BoneObj; // FUNCTIONS - TODO obliterate all "any" types where possible! --- TWW // https://github.com/jadonk/bonescript#system -export function getPlatform(callback?: (error: ErrorType, platform: any) => void): any; +export function getPlatform(callback?: (error: ErrorType, platform: PlatformObj) => void): any; -export function getEeproms(callback?: (error: ErrorType, eeproms: any) => void): any; +export function echo(data: string, callback?: (error: ErrorType, data: string) => void): any; -export function echo(data: any, callback?: (error: ErrorType, data: any) => void): any; +export function readTextFile(filename: string, callback?: (error: ErrorType, data: string) => void): string; -export function readTextFile(filename: string, callback?: (error: ErrorType, data: any) => void): any; +export function writeTextFile(filename: string, data: any, callback?: (error: ErrorType) => void): any; -export function writeTextFile(filename: string, data: any, callback?: (error: ErrorType, data: any) => void): void; +export function writeCModule(filename: string, data: any, callback?: (error: ErrorType) => void): any; -export function writeCModule(filename: string, data: any, callback?: (error: ErrorType, data: any) => void): void; - -export function setDate(date: any, callback?: (error: ErrorType, data: any) => void): void; +export function setDate(date: string, callback?: (error: ErrorType) => void): any; export function analogRead(pin: string, callback?: (err: ErrorType, value: number) => void): any; -export function analogWrite(pin: string, value: number, freq?: number, callback?: ErrorCb): void; +export function analogWrite(pin: string, value: number, freq?: number, callback?: ErrorCb): any; export function attachInterrupt( pin: string, handler: InterruptFn, mode: InterruptType, callback?: AttachIntCb -): void; +): any; -export function detachInterrupt(pin: string, callback?: DetachIntCb): void; +export function detachInterrupt(pin: string, callback?: DetachIntCb): any; export function digitalRead(pin: string, callback?: (err: ErrorType, value: number) => void): any; -export function digitalWrite(pin: string, value: PinStateType, callback?: DigitalWriteCb): void; +export function digitalWrite(pin: string, value: PinStateType, callback?: DigitalWriteCb): any; export function pinMode( pin: string, - direction: any, - mux?: any, - pullup?: any, - slew?: any, + direction: 'in' | 'out' | 'analog_out' | 'in_pullup', + mux?: string, + pullup?: 'disabled' | 'pullup' | 'pulldown', + slew?: 'fast' | 'slow', callback?: PinModeCb -): void; +): any; export function getPinMode(pin: string, callback?: (err: ErrorType, mode: PinModeObj) => void): any; -export function shiftOut(dataPin: any, clockPin: any, bitOrder: any, val: any, callback?: ErrorCb): void; +export function shiftOut(dataPin: string, clockPin: string, bitOrder: BitOrderType, val: number, callback?: ErrorCb): any; // https://github.com/jadonk/bonescript#serial -export function serialOpen(port: any, options: any, callback?: ErrorCb): void; +export function serialOpen(port: any, options: any, callback?: ErrorCb): any; -export function serialWrite(port: any, data: any, callback?: ErrorCb): void; +export function serialWrite(port: any, data: any, callback?: ErrorCb): any; export const serialParsers: typeof SerialPort.parsers; -export function i2cOpen(port: any, address: any, options: any, callback?: ErrorCb): void; +export function i2cOpen(port: any, address: any, options: any, callback?: ErrorCb): any; -export function i2cScan(port: any, callback?: ErrorCb): void; +export function i2cScan(port: any, callback?: ErrorCb): any; -export function i2cWriteByte(port: any, byte: any, callback?: ErrorCb): void; +export function i2cWriteByte(port: any, byte: any, callback?: ErrorCb): any; -export function i2cWriteBytes(port: any, command: any, bytes: any, callback?: ErrorCb): void; +export function i2cWriteBytes(port: any, command: any, bytes: any, callback?: ErrorCb): any; -export function i2cReadByte(port: any, callback?: ErrorCb): void; +export function i2cReadByte(port: any, callback?: ErrorCb): any; -export function i2cReadBytes(port: any, command: any, length: any, callback?: ErrorCb): void; +export function i2cReadBytes(port: any, command: any, length: any, callback?: ErrorCb): any; -export function i2cStream(port: any, command: any, length: any, callback?: ErrorCb): void; +export function i2cStream(port: any, command: any, length: any, callback?: ErrorCb): any; // ffi export function loadCModule(filename: string, data: any, isMRAA?: boolean): any; @@ -113,7 +111,7 @@ export function cos(radians: number): number; export function tan(radians: number): number; -export function randomSeed(x: number): number; +export function randomSeed(x: number): void; export function random(min: number, max?: number): number; @@ -143,6 +141,25 @@ export const RISING: 'rising'; export const FALLING: 'falling'; // INTERFACES - adjusted based on observation / testing +interface PlatformObj { + name: string; + platform: BoneObj; + bonescript: string; + serialNumber?: string; + dogtag?: string; + os: { + hostname: string, + type: string, + arch: string, + release: string, + uptime: number, + loadavg: number[], + totalmem: number, + freemem: number, + networkInterfaces?: any + }; +} + export interface PinModeObj { gpio?: { active: boolean, @@ -154,7 +171,7 @@ export interface PinModeObj { name: string; pin: string; pinState: string; - pullup?: 'diabled' | 'pullup' | 'pulldown'; + pullup?: 'disabled' | 'pullup' | 'pulldown'; pwm?: { freq: number, value: number, @@ -163,7 +180,7 @@ export interface PinModeObj { slew?: 'fast' | 'slow'; } -export interface BoneObj { +interface BoneObj { i2c: i2cObj; uarts: UartsObj; getPinKeys(): string[]; @@ -171,15 +188,15 @@ export interface BoneObj { naturalCompare(): any; } -export interface PinsObj { +interface PinsObj { [i: string]: PinInfo; } -export interface UartsObj { +interface UartsObj { [i: string]: UartInfo; } -export interface i2cObj { +interface i2cObj { [i: string]: i2cInfo; } @@ -204,7 +221,7 @@ export interface PinInfo { universalName?: string; } -export interface PwmInfo { +interface PwmInfo { addr: string; chip: string; index: number; @@ -215,13 +232,13 @@ export interface PwmInfo { sysfs: number; } -export interface UartInfo { +interface UartInfo { devicetree?: string; rx?: string; tx?: string; } -export interface i2cInfo { +interface i2cInfo { devicetree?: string; path?: string; scl?: string; @@ -229,28 +246,29 @@ export interface i2cInfo { } // TYPES - adjusted based on observation / testing -export type PinStateType = typeof HIGH | typeof LOW; -export type PinModeType = typeof ANALOG_OUTPUT | typeof INPUT | typeof INPUT_PULLUP | typeof OUTPUT; -export type InterruptType = typeof RISING | typeof FALLING | typeof CHANGE; -export type ErrorCb = (err: ErrorType) => void; -export type InterruptFn = (resp: { +type PinStateType = typeof HIGH | typeof LOW; +type PinModeType = typeof ANALOG_OUTPUT | typeof INPUT | typeof INPUT_PULLUP | typeof OUTPUT; +type InterruptType = typeof RISING | typeof FALLING | typeof CHANGE; +type BitOrderType = typeof LSBFIRST | typeof MSBFIRST; +type ErrorCb = (err: ErrorType) => void; +type InterruptFn = (resp: { pin: PinInfo, value: number }) => void; -export type AttachIntCb = (resp: { +type AttachIntCb = (resp: { pin: PinInfo, attached: boolean }) => void; -export type DetachIntCb = (resp: { +type DetachIntCb = (resp: { pin: PinInfo, detached: boolean }) => void; -export type PinModeCb = (resp: { +type PinModeCb = (resp: { value: number, err: VError }) => void; -export type DigitalWriteCb = (resp: { +type DigitalWriteCb = (resp: { data: any, err: VError }) => void; -export type ErrorType = VError; +type ErrorType = VError;