From d7cdee2d0b5be53f85d5b5a4920b087eaf77db91 Mon Sep 17 00:00:00 2001 From: Nick Hehr Date: Mon, 19 Aug 2019 17:14:08 -0400 Subject: [PATCH] Update ESC API, include Collection class (#37572) --- types/johnny-five/index.d.ts | 58 +++++++++++++++++++++----- types/johnny-five/johnny-five-tests.ts | 22 +++++++++- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/types/johnny-five/index.d.ts b/types/johnny-five/index.d.ts index 745e025f8d..393ca16fb3 100644 --- a/types/johnny-five/index.d.ts +++ b/types/johnny-five/index.d.ts @@ -1,11 +1,13 @@ -// Type definitions for johnny-five +// Type definitions for johnny-five 1.3.0 // Project: https://github.com/rwaldron/johnny-five // Definitions by: Toshiya Nakakura // Zoltan Ujvary // Simon Colmer // XtrimSystems // Marcin ObiedziƄski +// Nicholas Hehr // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 /// @@ -157,6 +159,35 @@ export declare class Button { on(event: "release", cb: () => void): this; } +export interface CollectionPinOptions { + pins: Array; + [key: string]: any; +} + +export declare class Collection { + static installMethodForwarding(target: object, source: object): object; + + constructor(options: Array | CollectionPinOptions); + + type?: Base; + + add(...args: Array): number; + + each(callback: (item: Base, index: number) => void): this; + + forEach(callback: (item: Base, index: number) => void): this; + + includes(item: Base): boolean; + + indexOf(item: Base): number; + + map(callback: (item: Base, index: number) => void): Array; + + slice(begin?: number, end?: number): Collection; + + byId(id: any): Base | undefined; +} + export interface CompassOption { controller: string; gauss?: number; @@ -175,25 +206,32 @@ export declare class Compass { export interface ESCOption { pin: number | string; - range?: Array; - startAt?: number; - controller?: string; - device?: string; + pwmRange?: Array; + address?: string; + controller?: 'PCA9685' | 'DEFAULT'; + device?: 'FORWARD' | 'FORWARD_REVERSE' | 'FORWARD_REVERSE_BRAKE'; neutral?: number; } export declare class ESC { + static Collection: ESCs; + constructor(option: number | string | ESCOption); id: string; pin: number | string; - range: Array; + pwmRange: Array; readonly value: number; - speed(value: number): void; - min(): void; - max(): void; - stop(): void; + throttle(value: number): this; + brake(): this; +} + +export declare class ESCs extends Collection { + constructor(option: Array); + + throttle(value: number): this; + brake(): this; } export declare class Fn { diff --git a/types/johnny-five/johnny-five-tests.ts b/types/johnny-five/johnny-five-tests.ts index 9d2b117344..6b4e17200a 100644 --- a/types/johnny-five/johnny-five-tests.ts +++ b/types/johnny-five/johnny-five-tests.ts @@ -56,7 +56,27 @@ board.on('ready', function(){ var esc = new five.ESC(11); // Set to top speed. (this can be physically dangerous, you've been warned.) - esc.max(); + esc.throttle(100); + + // other options for creating an ESC instance + esc = new five.ESC({ + device: 'FORWARD_REVERSE', + pin: 11, + pwmRange: [800, 1800], + }); + + // set a percentage value between 0 and 100 + esc.throttle(25); + + // set a micro-second + esc.throttle(1500); + + // create a collection of ESC instances + var escs = new five.ESCs([11, 12]); + + escs.throttle(80); + + escs.forEach(item => item.brake()); var gyro = new five.Gyro({