From d453fe52fbac8e7bc1e9b2a6de7ac7f5d7a5c340 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Fri, 6 Dec 2019 11:31:56 -0800 Subject: [PATCH 01/62] Basic implementation of BasicMotor types --- src/basicmotor.ts | 39 +++++ src/boostmovehub.ts | 24 +-- src/colordistancesensor.ts | 12 ++ src/consts.ts | 4 +- src/controlplushub.ts | 26 +-- src/controlpluslargemotor.ts | 12 ++ src/device.ts | 45 +++++ src/duplotrainbase.ts | 12 +- src/hub.ts | 83 ++++++---- src/index-browser.ts | 24 ++- src/index-node.ts | 23 ++- src/interfaces.ts | 2 +- src/lpf2hub.ts | 155 ++++++++++-------- src/{nobledevice.ts => nobleabstraction.ts} | 4 +- src/poweredup-browser.ts | 6 +- src/poweredup-node.ts | 2 +- src/puphub.ts | 35 ++-- src/pupremote.ts | 4 +- src/simplemediumlinearmotor.ts | 12 ++ src/trainmotor.ts | 12 ++ src/{webbledevice.ts => webbleabstraction.ts} | 4 +- src/wedo2smarthub.ts | 91 ++++++---- 22 files changed, 431 insertions(+), 200 deletions(-) create mode 100644 src/basicmotor.ts create mode 100644 src/colordistancesensor.ts create mode 100644 src/controlpluslargemotor.ts create mode 100644 src/device.ts rename src/{nobledevice.ts => nobleabstraction.ts} (97%) create mode 100644 src/simplemediumlinearmotor.ts create mode 100644 src/trainmotor.ts rename src/{webbledevice.ts => webbleabstraction.ts} (97%) diff --git a/src/basicmotor.ts b/src/basicmotor.ts new file mode 100644 index 00000000..a9bd2cda --- /dev/null +++ b/src/basicmotor.ts @@ -0,0 +1,39 @@ +import { Device } from "./device"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class BasicMotor extends Device { + + + constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { + super(hub, portId, type); + } + + + /** + * Set the motor speed. + * @method BasicMotor#setSpeed + * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + * @returns {Promise} Resolved upon successful completion of command. + */ + public setSpeed (speed: number) { + return new Promise((resolve) => { + const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, speed]); + this.send(data); + return resolve(); + }); + } + + + /** + * Fully (hard) stop the motor. + * @method BasicMotor#brake + * @returns {Promise} Resolved upon successful completion of command. + */ + public brake () { + return this.setSpeed(127); + } + + +} diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index 93c1222d..784e0ede 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -7,7 +7,7 @@ import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("boostmovehub"); @@ -34,7 +34,7 @@ export class BoostMoveHub extends LPF2Hub { protected _currentPort = 0x3b; protected _voltagePort = 0x3c; - constructor (device: IBLEDevice, autoSubscribe: boolean = true) { + constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); this.type = Consts.HubType.BOOST_MOVE_HUB; this._ports = { @@ -102,17 +102,17 @@ export class BoostMoveHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); // @ts-ignore: The type of time is properly checked at the start }, time); @@ -135,14 +135,14 @@ export class BoostMoveHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); } } @@ -204,7 +204,7 @@ export class BoostMoveHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt32LE(angle, 4); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -241,7 +241,7 @@ export class BoostMoveHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data.writeInt32LE(pos, 4); } - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -265,7 +265,7 @@ export class BoostMoveHub extends LPF2Hub { } return new Promise((resolve) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -295,11 +295,11 @@ export class BoostMoveHub extends LPF2Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); diff --git a/src/colordistancesensor.ts b/src/colordistancesensor.ts new file mode 100644 index 00000000..1eecb5ce --- /dev/null +++ b/src/colordistancesensor.ts @@ -0,0 +1,12 @@ +import { Device } from "./device"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class ColorDistanceSensor extends Device { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR); + } + +} diff --git a/src/consts.ts b/src/consts.ts index dac8b225..abb79608 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -52,7 +52,7 @@ export const HubTypeNames = HubType; */ export enum DeviceType { UNKNOWN = 0, - BASIC_MOTOR = 1, + SIMPLE_MEDIUM_LINEAR_MOTOR = 1, TRAIN_MOTOR = 2, LED_LIGHTS = 8, VOLTAGE = 20, @@ -61,7 +61,7 @@ export enum DeviceType { RGB_LIGHT = 23, WEDO2_TILT = 34, WEDO2_DISTANCE = 35, - BOOST_DISTANCE = 37, + COLOR_DISTANCE_SENSOR = 37, BOOST_TACHO_MOTOR = 38, BOOST_MOVE_HUB_MOTOR = 39, BOOST_TILT = 40, diff --git a/src/controlplushub.ts b/src/controlplushub.ts index fbea7841..4a0abf10 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -6,7 +6,7 @@ import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("ControlPlusHub"); @@ -36,7 +36,7 @@ export class ControlPlusHub extends LPF2Hub { protected _voltageMaxRaw = 4095; protected _voltageMaxV = 9.615; - constructor (device: IBLEDevice, autoSubscribe: boolean = true) { + constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); this.type = Consts.HubType.CONTROL_PLUS_HUB; this._ports = { @@ -59,7 +59,7 @@ export class ControlPlusHub extends LPF2Hub { return new Promise(async (resolve, reject) => { debug("Connecting to Control+ Hub"); await super.connect(); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01])); // Temperature + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01])); // Temperature debug("Connect completed"); return resolve(); }); @@ -107,17 +107,17 @@ export class ControlPlusHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); // @ts-ignore: The type of time is properly checked at the start }, time); @@ -135,14 +135,14 @@ export class ControlPlusHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); } } @@ -204,7 +204,7 @@ export class ControlPlusHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt32LE(angle, 4); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -241,7 +241,7 @@ export class ControlPlusHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data.writeInt32LE(pos, 4); } - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -265,7 +265,7 @@ export class ControlPlusHub extends LPF2Hub { } return new Promise((resolve) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -295,11 +295,11 @@ export class ControlPlusHub extends LPF2Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); diff --git a/src/controlpluslargemotor.ts b/src/controlpluslargemotor.ts new file mode 100644 index 00000000..8fcccecc --- /dev/null +++ b/src/controlpluslargemotor.ts @@ -0,0 +1,12 @@ +import { BasicMotor } from "./basicmotor"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class ControlPlusLargeMotor extends BasicMotor { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR); + } + +} diff --git a/src/device.ts b/src/device.ts new file mode 100644 index 00000000..73ddd4d4 --- /dev/null +++ b/src/device.ts @@ -0,0 +1,45 @@ +import { EventEmitter } from "events"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class Device extends EventEmitter { + + private _hub: Hub; + private _portId: number; + private _connected: boolean = true; + private _type: number; + + constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { + super(); + this._hub = hub; + this._portId = portId; + this._type = type; + console.log(`New device on ${this._portId} - ${this._type}`); + } + + public get connected () { + return this._connected; + } + + public get hub () { + return this._hub; + } + + public get portId () { + return this._portId; + } + + public get port () { + return "A"; // TODO NK: Look up the port name from the relevant hub + } + + public get type () { + return this._type; + } + + public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { + this.hub.send(characteristic, data, callback); + } + +} diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index c52ca0d8..a343cdcd 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -6,7 +6,7 @@ import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("duplotrainbase"); @@ -36,7 +36,7 @@ export class DuploTrainBase extends LPF2Hub { protected _voltageMaxV = 6.4; protected _voltageMaxRaw = 3047; - constructor (device: IBLEDevice, autoSubscribe: boolean = true) { + constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); this.type = Consts.HubType.DUPLO_TRAIN_HUB; this._ports = { @@ -80,16 +80,16 @@ export class DuploTrainBase extends LPF2Hub { return new Promise((resolve, reject) => { if (time && typeof time === "number") { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); } else { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); } }); @@ -138,7 +138,7 @@ export class DuploTrainBase extends LPF2Hub { public playSound (sound: number) { return new Promise((resolve, reject) => { const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } diff --git a/src/hub.ts b/src/hub.ts index fb48dc32..f4fd9763 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -1,11 +1,12 @@ import { EventEmitter } from "events"; -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); +import { Device } from "./device"; const debug = Debug("hub"); @@ -20,6 +21,8 @@ export class Hub extends EventEmitter { public useSpeedMap: boolean = true; public type: Consts.HubType = Consts.HubType.UNKNOWN; + protected _attachedDevices: Device[] = []; + protected _ports: {[port: string]: Port} = {}; protected _virtualPorts: {[port: string]: Port} = {}; @@ -32,12 +35,12 @@ export class Hub extends EventEmitter { protected _current: number = 0; protected _rssi: number = -60; - protected _bleDevice: IBLEDevice; + protected _bleDevice: IBLEAbstraction; private _isConnecting = false; private _isConnected = false; - constructor (device: IBLEDevice, autoSubscribe: boolean = true) { + constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(); this.autoSubscribe = !!autoSubscribe; this._bleDevice = device; @@ -246,6 +249,13 @@ export class Hub extends EventEmitter { } + public send (uuid: string, message: Buffer, callback?: () => void) { + if (callback) { + callback(); + } + } + + // protected _getCharacteristic (uuid: string) { // return this._characteristics[uuid.replace(/-/g, "")]; // } @@ -277,34 +287,49 @@ export class Hub extends EventEmitter { } - protected _registerDeviceAttachment (port: Port, type: number) { + protected _registerDeviceAttachment (device: Device) { - if (port.connected) { - port.type = type; - if (this.autoSubscribe) { - this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00); - } - /** - * Emits when a motor or sensor is attached to the Hub. - * @event Hub#attach - * @param {string} port - * @param {DeviceType} type - */ - this.emit("attach", port.id, type); + const exists = this._attachedDevices.find((attachedDevice) => attachedDevice.portId === device.portId); + + if (exists) { + // TODO NK: Remove existing zombie device } else { - port.type = Consts.DeviceType.UNKNOWN; - debug(`Port ${port.id} disconnected`); - /** - * Emits when an attached motor or sensor is detached from the Hub. - * @event Hub#detach - * @param {string} port - */ - if (this._virtualPorts[port.id]) { - delete this._virtualPorts[port.id]; - } - this.emit("detach", port.id); + this._attachedDevices.push(device); } + /** + * Emits when a device is attached to the Hub. + * @event Hub#attach + * @param {Device} device + */ + this.emit("attach", device); + + // if (port.connected) { + // port.type = type; + // if (this.autoSubscribe) { + // this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00); + // } + // /** + // * Emits when a motor or sensor is attached to the Hub. + // * @event Hub#attach + // * @param {string} port + // * @param {DeviceType} type + // */ + // this.emit("attach", port.id, type); + // } else { + // port.type = Consts.DeviceType.UNKNOWN; + // debug(`Port ${port.id} disconnected`); + // /** + // * Emits when an attached motor or sensor is detached from the Hub. + // * @event Hub#detach + // * @param {string} port + // */ + // if (this._virtualPorts[port.id]) { + // delete this._virtualPorts[port.id]; + // } + // this.emit("detach", port.id); + // } + } @@ -388,7 +413,7 @@ export class Hub extends EventEmitter { private _getModeForDeviceType (type: Consts.DeviceType) { switch (type) { - case Consts.DeviceType.BASIC_MOTOR: + case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: return 0x02; case Consts.DeviceType.TRAIN_MOTOR: return 0x02; @@ -404,7 +429,7 @@ export class Hub extends EventEmitter { return 0x00; case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: return 0x00; - case Consts.DeviceType.BOOST_DISTANCE: + case Consts.DeviceType.COLOR_DISTANCE_SENSOR: return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08); case Consts.DeviceType.BOOST_TILT: return 0x04; diff --git a/src/index-browser.ts b/src/index-browser.ts index af7b1588..d1310d1e 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -1,15 +1,35 @@ import * as Consts from "./consts"; +import { PoweredUP } from "./poweredup-browser"; + import { BoostMoveHub } from "./boostmovehub"; import { ControlPlusHub } from "./controlplushub"; import { DuploTrainBase } from "./duplotrainbase"; import { Hub } from "./hub"; -import { PoweredUP } from "./poweredup-browser"; import { PUPHub } from "./puphub"; import { PUPRemote } from "./pupremote"; import { WeDo2SmartHub } from "./wedo2smarthub"; +import { ColorDistanceSensor } from "./colordistancesensor"; +import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { Device } from "./device"; + import { isWebBluetooth } from "./utils"; // @ts-ignore -window.PoweredUP = { PoweredUP, Hub, WeDo2SmartHub, BoostMoveHub, ControlPlusHub, PUPHub, PUPRemote, DuploTrainBase, Consts, isWebBluetooth }; +window.PoweredUP = { + PoweredUP, + Hub, + WeDo2SmartHub, + BoostMoveHub, + ControlPlusHub, + PUPHub, + PUPRemote, + DuploTrainBase, + Consts, + Device, + ColorDistanceSensor, + ControlPlusLargeMotor, + isWebBluetooth +}; + diff --git a/src/index-node.ts b/src/index-node.ts index 3d5c7ac1..88051381 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -1,15 +1,34 @@ import * as Consts from "./consts"; +import { PoweredUP } from "./poweredup-node"; + import { BoostMoveHub } from "./boostmovehub"; import { ControlPlusHub } from "./controlplushub"; import { DuploTrainBase } from "./duplotrainbase"; import { Hub } from "./hub"; -import { PoweredUP } from "./poweredup-node"; import { PUPHub } from "./puphub"; import { PUPRemote } from "./pupremote"; import { WeDo2SmartHub } from "./wedo2smarthub"; +import { ColorDistanceSensor } from "./colordistancesensor"; +import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { Device } from "./device"; + import { isWebBluetooth } from "./utils"; export default PoweredUP; -export { PoweredUP, Hub, WeDo2SmartHub, BoostMoveHub, ControlPlusHub, PUPHub, PUPRemote, DuploTrainBase, Consts, isWebBluetooth }; +export { + PoweredUP, + Hub, + WeDo2SmartHub, + BoostMoveHub, + ControlPlusHub, + PUPHub, + PUPRemote, + DuploTrainBase, + Consts, + Device, + ColorDistanceSensor, + ControlPlusLargeMotor, + isWebBluetooth +}; diff --git a/src/interfaces.ts b/src/interfaces.ts index efebe430..cdb1bce0 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,6 +1,6 @@ import { EventEmitter } from "events"; -export interface IBLEDevice extends EventEmitter { +export interface IBLEAbstraction extends EventEmitter { uuid: string; name: string; connecting: boolean; diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 3f9c658e..95936825 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -1,8 +1,10 @@ -import { Peripheral } from "@abandonware/noble"; - +import { Device } from "./device"; import { Hub } from "./hub"; import { Port } from "./port"; +import { ColorDistanceSensor } from "./colordistancesensor"; +import { ControlPlusLargeMotor } from "./controlpluslargemotor"; + import * as Consts from "./consts"; import { toBin, toHex } from "./utils"; @@ -46,21 +48,21 @@ export class LPF2Hub extends Hub { await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); if (this._voltagePort !== undefined) { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._voltagePort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._voltagePort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports } if (this._currentPort !== undefined) { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports } if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); } await this.sleep(100); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x04, 0x05])); // Request hardware version - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x05, 0x02])); // Activate RSSI updates - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x06, 0x02])); // Activate battery level reports - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x0d, 0x05])); // Request primary MAC address + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x04, 0x05])); // Request hardware version + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x05, 0x02])); // Activate RSSI updates + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x06, 0x02])); // Activate battery level reports + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x0d, 0x05])); // Request primary MAC address this.emit("connect"); resolve(); }); @@ -74,7 +76,7 @@ export class LPF2Hub extends Hub { */ public shutdown () { return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x02, 0x01]), () => { + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x02, 0x01]), () => { return resolve(); }); }); @@ -95,8 +97,8 @@ export class LPF2Hub extends Hub { let data = Buffer.from([0x01, 0x01, 0x01]); data = Buffer.concat([data, Buffer.from(name, "ascii")]); // Send this twice, as sometimes the first time doesn't take - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); this._name = name; return resolve(); }); @@ -112,12 +114,12 @@ export class LPF2Hub extends Hub { public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, this._ledPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); if (typeof color === "boolean") { color = 0; } data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x00, color]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -134,9 +136,9 @@ export class LPF2Hub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, this._ledPort, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x01, red, green, blue]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } @@ -144,28 +146,28 @@ export class LPF2Hub extends Hub { public sendRaw (message: Buffer) { return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, message, () => { + this.send(Consts.BLECharacteristic.LPF2_ALL, message, () => { return resolve(); }); }); } - protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); + public send (uuid: string, message: Buffer, callback?: () => void) { + message = Buffer.concat([Buffer.alloc(2), message]); + message[0] = message.length; + debug("Sent Message (LPF2_ALL)", message); + this._bleDevice.writeToCharacteristic(uuid, message, callback); } - protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); + protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); } - protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) { - message = Buffer.concat([Buffer.alloc(2), message]); - message[0] = message.length; - debug("Sent Message (LPF2_ALL)", message); - this._bleDevice.writeToCharacteristic(uuid, message, callback); + protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); } @@ -178,7 +180,7 @@ export class LPF2Hub extends Hub { Object.keys(this._ports).forEach((id) => { if (this._ports[id].type === type && this._ports[id].value !== portObj.value && !this._virtualPorts[`${portObj.value < this._ports[id].value ? portObj.id : this._ports[id].id}${portObj.value > this._ports[id].value ? portObj.id : this._ports[id].id}`]) { debug("Combining ports", portObj.value < this._ports[id].value ? portObj.id : id, portObj.value > this._ports[id].value ? portObj.id : id); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x61, 0x01, portObj.value < this._ports[id].value ? portObj.value : this._ports[id].value, portObj.value > this._ports[id].value ? portObj.value : this._ports[id].value])); + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x61, 0x01, portObj.value < this._ports[id].value ? portObj.value : this._ports[id].value, portObj.value > this._ports[id].value ? portObj.value : this._ports[id].value])); } }); } @@ -291,48 +293,73 @@ export class LPF2Hub extends Hub { private _parsePortMessage (data: Buffer) { - let port = this._getPortForPortNumber(data[3]); - const type = data[4] ? data.readUInt16LE(5) : 0; + const portId = data[3]; + const event = data[4]; + const deviceType = event ? data.readUInt16LE(5) : 0; - if (data[4] === 0x01 && modeInfoDebug.enabled) { - const typeName = Consts.DeviceTypeNames[data[5]] || "unknown"; - modeInfoDebug(`Port ${toHex(data[3])}, type ${toHex(type, 4)} (${typeName})`); - const hwVersion = LPF2Hub.decodeVersion(data.readInt32LE(7)); - const swVersion = LPF2Hub.decodeVersion(data.readInt32LE(11)); - modeInfoDebug(`Port ${toHex(data[3])}, hardware version ${hwVersion}, software version ${swVersion}`); - this._sendPortInformationRequest(data[3]); - } + if (event === 0x01) { - if (!port) { - if (data[4] === 0x02) { - const portA = this._getPortForPortNumber(data[7]); - const portB = this._getPortForPortNumber(data[8]); - if (portA && portB) { - this._virtualPorts[`${portA.id}${portB.id}`] = new Port(`${portA.id}${portB.id}`, data[3]); - port = this._getPortForPortNumber(data[3]); - if (port) { - port.connected = true; - this._registerDeviceAttachment(port, type); - } else { - return; - } - } else { - return; - } - } else { - return; + let device; + + switch (deviceType) { + case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: + device = new ControlPlusLargeMotor(this, portId); + break; + case Consts.DeviceType.COLOR_DISTANCE_SENSOR: + device = new ColorDistanceSensor(this, portId); + break; + default: + device = new Device(this, portId, deviceType); + break; } - } else { - port.connected = (data[4] === 0x01 || data[4] === 0x02) ? true : false; - this._registerDeviceAttachment(port, type); + + this._registerDeviceAttachment(device); + } + + + // let port = this._getPortForPortNumber(data[3]); + + // if (data[4] === 0x01 && modeInfoDebug.enabled) { + // const typeName = Consts.DeviceTypeNames[data[5]] || "unknown"; + // modeInfoDebug(`Port ${toHex(data[3])}, type ${toHex(deviceType, 4)} (${typeName})`); + // const hwVersion = LPF2Hub.decodeVersion(data.readInt32LE(7)); + // const swVersion = LPF2Hub.decodeVersion(data.readInt32LE(11)); + // modeInfoDebug(`Port ${toHex(data[3])}, hardware version ${hwVersion}, software version ${swVersion}`); + // this._sendPortInformationRequest(data[3]); + // } + + // if (!port) { + // if (data[4] === 0x02) { + // const portA = this._getPortForPortNumber(data[7]); + // const portB = this._getPortForPortNumber(data[8]); + // if (portA && portB) { + // this._virtualPorts[`${portA.id}${portB.id}`] = new Port(`${portA.id}${portB.id}`, data[3]); + // port = this._getPortForPortNumber(data[3]); + // if (port) { + // port.connected = true; + // this._registerDeviceAttachment(port, deviceType); + // } else { + // return; + // } + // } else { + // return; + // } + // } else { + // return; + // } + // } else { + // port.connected = (data[4] === 0x01 || data[4] === 0x02) ? true : false; + // this._registerDeviceAttachment(port, deviceType); + // } + } private _sendPortInformationRequest (port: number) { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x01])); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x02])); // Mode combinations + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x01])); + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x02])); // Mode combinations } @@ -363,7 +390,7 @@ export class LPF2Hub extends Hub { private _sendModeInformationRequest (port: number, mode: number, type: number) { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x22, port, mode, type])); + this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x22, port, mode, type])); } @@ -461,7 +488,7 @@ export class LPF2Hub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.DeviceType.BOOST_DISTANCE: { + case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { /** * Emits when a color sensor is activated. diff --git a/src/nobledevice.ts b/src/nobleabstraction.ts similarity index 97% rename from src/nobledevice.ts rename to src/nobleabstraction.ts index 5b30d300..d9a6f833 100644 --- a/src/nobledevice.ts +++ b/src/nobleabstraction.ts @@ -2,11 +2,11 @@ import { Characteristic, Peripheral, Service } from "@abandonware/noble"; import Debug = require("debug"); import { EventEmitter } from "events"; -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("bledevice"); -export class NobleDevice extends EventEmitter implements IBLEDevice { +export class NobleDevice extends EventEmitter implements IBLEAbstraction { private _noblePeripheral: Peripheral; diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index d6c4c6a1..67f4140d 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -4,7 +4,7 @@ import { DuploTrainBase } from "./duplotrainbase"; import { Hub } from "./hub"; import { PUPHub } from "./puphub"; import { PUPRemote } from "./pupremote"; -import { WebBLEDevice } from "./webbledevice"; +import { WebBLEDevice } from "./webbleabstraction"; import { WeDo2SmartHub } from "./wedo2smarthub"; import * as Consts from "./consts"; @@ -12,7 +12,7 @@ import * as Consts from "./consts"; import { EventEmitter } from "events"; import Debug = require("debug"); -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("poweredup"); @@ -118,7 +118,7 @@ export class PoweredUP extends EventEmitter { } - private _determineLPF2HubType (device: IBLEDevice): Promise { + private _determineLPF2HubType (device: IBLEAbstraction): Promise { return new Promise((resolve, reject) => { let buf: Buffer = Buffer.alloc(0); device.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, (data: Buffer) => { diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index fb9ea1d3..e9a8d821 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -4,7 +4,7 @@ import { BoostMoveHub } from "./boostmovehub"; import { ControlPlusHub } from "./controlplushub"; import { DuploTrainBase } from "./duplotrainbase"; import { Hub } from "./hub"; -import { NobleDevice } from "./nobledevice"; +import { NobleDevice } from "./nobleabstraction"; import { PUPHub } from "./puphub"; import { PUPRemote } from "./pupremote"; import { WeDo2SmartHub } from "./wedo2smarthub"; diff --git a/src/puphub.ts b/src/puphub.ts index a305511d..f718d5ed 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -7,7 +7,7 @@ import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("puphub"); @@ -34,7 +34,7 @@ export class PUPHub extends LPF2Hub { protected _currentPort = 0x3b; protected _voltagePort = 0x3c; - constructor (device: IBLEDevice, autoSubscribe: boolean = true) { + constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); this.type = Consts.HubType.POWERED_UP_HUB; this._ports = { @@ -99,17 +99,17 @@ export class PUPHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); // @ts-ignore: The type of time is properly checked at the start }, time); @@ -127,14 +127,14 @@ export class PUPHub extends LPF2Hub { // @ts-ignore: The type of speed is properly checked at the start data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; } else { // @ts-ignore: The type of speed is properly checked at the start const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); } } @@ -196,7 +196,7 @@ export class PUPHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); } data.writeUInt32LE(angle, 4); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -233,7 +233,7 @@ export class PUPHub extends LPF2Hub { data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data.writeInt32LE(pos, 4); } - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); portObj.finished = () => { return resolve(); }; @@ -257,23 +257,12 @@ export class PUPHub extends LPF2Hub { } return new Promise((resolve) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }); } - /** - * Fully (hard) stop the motor on a given port. - * @method PUPHub#brakeMotor - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command. - */ - public brakeMotor (port: string) { - return this.setMotorSpeed(port, 127); - } - - /** * Set the light brightness on a given port. * @method PUPHub#setLightBrightness @@ -287,11 +276,11 @@ export class PUPHub extends LPF2Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(Consts.BLECharacteristic.LPF2_ALL, data); return resolve(); }, time); portObj.setEventTimer(timeout); diff --git a/src/pupremote.ts b/src/pupremote.ts index d01c3f22..c6fcecf4 100644 --- a/src/pupremote.ts +++ b/src/pupremote.ts @@ -6,7 +6,7 @@ import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("pupremote"); @@ -37,7 +37,7 @@ export class PUPRemote extends LPF2Hub { protected _voltageMaxRaw = 3200; - constructor (device: IBLEDevice, autoSubscribe: boolean = true) { + constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); this.type = Consts.HubType.POWERED_UP_REMOTE; this._ports = { diff --git a/src/simplemediumlinearmotor.ts b/src/simplemediumlinearmotor.ts new file mode 100644 index 00000000..824b0f37 --- /dev/null +++ b/src/simplemediumlinearmotor.ts @@ -0,0 +1,12 @@ +import { BasicMotor } from "./basicmotor"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class SimpleMediumLinearMotor extends BasicMotor { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR); + } + +} diff --git a/src/trainmotor.ts b/src/trainmotor.ts new file mode 100644 index 00000000..6f590a61 --- /dev/null +++ b/src/trainmotor.ts @@ -0,0 +1,12 @@ +import { BasicMotor } from "./basicmotor"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class TrainMotor extends BasicMotor { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.TRAIN_MOTOR); + } + +} diff --git a/src/webbledevice.ts b/src/webbleabstraction.ts similarity index 97% rename from src/webbledevice.ts rename to src/webbleabstraction.ts index cf8a3379..0380191c 100644 --- a/src/webbledevice.ts +++ b/src/webbleabstraction.ts @@ -1,10 +1,10 @@ import Debug = require("debug"); import { EventEmitter } from "events"; -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; const debug = Debug("bledevice"); -export class WebBLEDevice extends EventEmitter implements IBLEDevice { +export class WebBLEDevice extends EventEmitter implements IBLEAbstraction { private _webBLEServer: any; diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index 1e7ecb7d..e08a99ac 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -1,12 +1,16 @@ import { Peripheral } from "@abandonware/noble"; +import { Device } from "./device"; import { Hub } from "./hub"; import { Port } from "./port"; +import { ColorDistanceSensor } from "./colordistancesensor"; +import { ControlPlusLargeMotor } from "./controlpluslargemotor"; + import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEDevice } from "./interfaces"; +import { IBLEAbstraction } from "./interfaces"; import { isWebBluetooth } from "./utils"; const debug = Debug("wedo2smarthub"); @@ -32,7 +36,7 @@ export class WeDo2SmartHub extends Hub { private _lastTiltY: number = 0; - constructor (device: IBLEDevice, autoSubscribe: boolean = true) { + constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); this.type = Consts.HubType.WEDO2_SMART_HUB; this._ports = { @@ -111,8 +115,8 @@ export class WeDo2SmartHub extends Hub { return new Promise((resolve, reject) => { const data = Buffer.from(name, "ascii"); // Send this twice, as sometimes the first time doesn't take - this._writeMessage(Consts.BLECharacteristic.WEDO2_NAME_ID, data); - this._writeMessage(Consts.BLECharacteristic.WEDO2_NAME_ID, data); + this.send(Consts.BLECharacteristic.WEDO2_NAME_ID, data); + this.send(Consts.BLECharacteristic.WEDO2_NAME_ID, data); this._name = name; return resolve(); }); @@ -128,12 +132,12 @@ export class WeDo2SmartHub extends Hub { public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); + this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); if (typeof color === "boolean") { color = 0; } data = Buffer.from([0x06, 0x04, 0x01, color]); - this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }); } @@ -146,7 +150,7 @@ export class WeDo2SmartHub extends Hub { */ public shutdown () { return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristic.WEDO2_DISCONNECT, Buffer.from([0x00]), () => { + this.send(Consts.BLECharacteristic.WEDO2_DISCONNECT, Buffer.from([0x00]), () => { return resolve(); }); }); @@ -164,9 +168,9 @@ export class WeDo2SmartHub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x02]); - this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); + this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }); } @@ -193,10 +197,10 @@ export class WeDo2SmartHub extends Hub { portObj.cancelEventTimer(); } return new Promise((resolve, reject) => { - this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); + this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); if (time && typeof time === "number") { const timeout = global.setTimeout(() => { - this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); + this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -252,7 +256,7 @@ export class WeDo2SmartHub extends Hub { const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]); data.writeUInt16LE(frequency, 3); data.writeUInt16LE(time, 5); - this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); global.setTimeout(resolve, time); }); } @@ -271,11 +275,11 @@ export class WeDo2SmartHub extends Hub { portObj.cancelEventTimer(); return new Promise((resolve, reject) => { const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); - this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); if (time) { const timeout = global.setTimeout(() => { const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); - this._writeMessage(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); return resolve(); }, time); portObj.setEventTimer(timeout); @@ -286,30 +290,21 @@ export class WeDo2SmartHub extends Hub { } - public sendRaw (message: Buffer, characteristic: string = Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE) { - return new Promise((resolve, reject) => { - this._writeMessage(characteristic, message, () => { - return resolve(); - }); - }); + public send (uuid: string, message: Buffer, callback?: () => void) { + if (debug.enabled) { + debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message); + } + this._bleDevice.writeToCharacteristic(uuid, message, callback); } protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); + this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this._writeMessage(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); - } - - - private _writeMessage (uuid: string, message: Buffer, callback?: () => void) { - if (debug.enabled) { - debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message); - } - this._bleDevice.writeToCharacteristic(uuid, message, callback); + this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); } @@ -346,14 +341,38 @@ export class WeDo2SmartHub extends Hub { debug("Received Message (WEDO2_PORT_TYPE)", data); - const port = this._getPortForPortNumber(data[0]); + const portId = data[0]; + const event = data[1]; + const deviceType = event ? data[3] : 0; + + if (event === 0x01) { + + let device; + + switch (deviceType) { + case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: + device = new ControlPlusLargeMotor(this, portId); + break; + case Consts.DeviceType.COLOR_DISTANCE_SENSOR: + device = new ColorDistanceSensor(this, portId); + break; + default: + device = new Device(this, portId); + break; + } + + this._registerDeviceAttachment(device); - if (!port) { - return; } - port.connected = data[1] === 1 ? true : false; - this._registerDeviceAttachment(port, data[3]); + // const port = this._getPortForPortNumber(data[0]); + + // if (!port) { + // return; + // } + + // port.connected = data[1] === 1 ? true : false; + // this._registerDeviceAttachment(port, data[3]); } @@ -408,7 +427,7 @@ export class WeDo2SmartHub extends Hub { this.emit("distance", port.id, distance * 10); break; } - case Consts.DeviceType.BOOST_DISTANCE: { + case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { const distance = data[2]; /** * Emits when a color sensor is activated. From 4af1d3d69bb3d333743f933ec0efe0a72e2239f9 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Fri, 6 Dec 2019 17:31:12 -0800 Subject: [PATCH 02/62] Added autosubscribe when adding eventlisteners --- examples/new_device_test.js | 43 +++ src/basicmotor.ts | 4 +- src/boostmovehub.ts | 511 +++++++++++++++++----------------- src/colordistancesensor.ts | 13 + src/controlplushub.ts | 440 +++++++++++++++--------------- src/device.ts | 17 +- src/duplotrainbase.ts | 83 +----- src/hub.ts | 336 ++++++++++++----------- src/lpf2hub.ts | 531 ++++++++++++++++++------------------ src/puphub.ts | 244 +---------------- src/pupremote.ts | 8 +- src/utils.ts | 21 +- src/wedo2smarthub.ts | 402 +++++++++++++-------------- 13 files changed, 1222 insertions(+), 1431 deletions(-) create mode 100644 examples/new_device_test.js diff --git a/examples/new_device_test.js b/examples/new_device_test.js new file mode 100644 index 00000000..3eb8cf8c --- /dev/null +++ b/examples/new_device_test.js @@ -0,0 +1,43 @@ +/* + * + * This demonstrates connecting multiple hubs to your laptop. Once connected, all the hubs LED lights will cycle through the same colors simultaneously. + * + */ + +const PoweredUP = require(".."); + +const poweredUP = new PoweredUP.PoweredUP(); +poweredUP.scan(); // Start scanning for hubs + +console.log("Looking for Hubs..."); + +poweredUP.on("discover", async (hub) => { // Wait to discover hubs + + await hub.connect(); // Connect to hub + console.log(`Connected to ${hub.name}!`); + + hub.on("attach", (device) => { + + if (device instanceof PoweredUP.ControlPlusLargeMotor) { + const motor = device; + motor.setSpeed(30); + } + + if (device instanceof PoweredUP.ColorDistanceSensor) { + const sensor = device; + sensor.on("distance", (distance) => { // Adding an event handler for distance automatically subscribes to distance notifications + console.log(`Distance ${distance}`); + }) + } + + device.on("detach", () => { + console.log(device.connected); + }) + + }); + + hub.on("disconnect", () => { + console.log("Hub disconnected"); + }) + +}); \ No newline at end of file diff --git a/src/basicmotor.ts b/src/basicmotor.ts index a9bd2cda..b0b3a93b 100644 --- a/src/basicmotor.ts +++ b/src/basicmotor.ts @@ -3,6 +3,8 @@ import { Hub } from "./hub"; import * as Consts from "./consts"; +import { mapSpeed } from "./utils"; + export class BasicMotor extends Device { @@ -19,7 +21,7 @@ export class BasicMotor extends Device { */ public setSpeed (speed: number) { return new Promise((resolve) => { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, speed]); + const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(speed)]); this.send(data); return resolve(); }); diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index 784e0ede..e363eb54 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -36,17 +36,14 @@ export class BoostMoveHub extends LPF2Hub { constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); - this.type = Consts.HubType.BOOST_MOVE_HUB; - this._ports = { - "A": new Port("A", 0), - "B": new Port("B", 1), - "C": new Port("C", 2), - "D": new Port("D", 3), - "TILT": new Port("TILT", 58) + this._type = Consts.HubType.BOOST_MOVE_HUB; + this._portNames = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "TILT": 58 }; - this.on("attach", (port, type) => { - this._combinePorts(port, type); - }); debug("Discovered Boost Move Hub"); } @@ -61,253 +58,253 @@ export class BoostMoveHub extends LPF2Hub { } - /** - * Set the motor speed on a given port. - * @method BoostMoveHub#setMotorSpeed - * @param {string} port - * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - */ - public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { - const portObj = this._portLookup(port); - if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - throw new Error(`Port ${portObj.id} can only accept a single speed`); - } - let cancelEventTimer = true; - if (typeof time === "boolean") { - if (time === true) { - cancelEventTimer = false; - } - time = undefined; - } - if (cancelEventTimer) { - portObj.cancelEventTimer(); - } - return new Promise((resolve, reject) => { - if (time && typeof time === "number") { - - if ( - portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - ) { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - } else { - // @ts-ignore: The type of speed is properly checked at the start - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - const timeout = global.setTimeout(() => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - // @ts-ignore: The type of time is properly checked at the start - }, time); - portObj.setEventTimer(timeout); - } - - } else { - - if ( - portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - ) { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - } else { - // @ts-ignore: The type of speed is properly checked at the start - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - } - - } - }); - } - - - /** - * Ramp the motor speed on a given port. - * @method BoostMoveHub#rampMotorSpeed - * @param {string} port - * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} time How long the ramp should last (in milliseconds). - * @returns {Promise} Resolved upon successful completion of command. - */ - public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time, portObj) - .on("changeSpeed", (speed) => { - this.setMotorSpeed(port, speed, true); - }) - .on("finished", resolve); - }); - } - - - /** - * Rotate a motor by a given angle. - * @method BoostMoveHub#setMotorAngle - * @param {string} port - * @param {number} angle How much the motor should be rotated (in degrees). - * @param {number | Array.} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor"); - } - if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - throw new Error(`Port ${portObj.id} can only accept a single speed`); - } - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - data.writeUInt32LE(angle, 4); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - }); - } - - - /** - * Tell motor to goto an absolute position - * @method BoostMoveHub#setAbsolutePosition - * @param {string} port - * @param {number} pos The position of the motor to go to - * @param {number | Array.} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public setAbsolutePosition (port: string, pos: number, speed: number = 100) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - } - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeInt32LE(pos, 4); - data.writeInt32LE(pos, 8); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeInt32LE(pos, 4); - } - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - }); - } - - - /** - * Reset the current motor position as absolute position zero - * @method BoostMoveHub#resetAbsolutePosition - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public resetAbsolutePosition (port: string) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - } - return new Promise((resolve) => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }); - } - - - /** - * Fully (hard) stop the motor on a given port. - * @method BoostMoveHub#brakeMotor - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command. - */ - public brakeMotor (port: string) { - return this.setMotorSpeed(port, 127); - } - - - /** - * Set the light brightness on a given port. - * @method BoostMoveHub#setLightBrightness - * @param {string} port - * @param {number} brightness Brightness value between 0-100 (0 is off) - * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. - */ - public setLightBrightness (port: string, brightness: number, time?: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - if (time) { - const timeout = global.setTimeout(() => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }, time); - portObj.setEventTimer(timeout); - } else { - return resolve(); - } - }); - } + // /** + // * Set the motor speed on a given port. + // * @method BoostMoveHub#setMotorSpeed + // * @param {string} port + // * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. + // * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. + // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. + // */ + // public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { + // const portObj = this._portLookup(port); + // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { + // throw new Error(`Port ${portObj.id} can only accept a single speed`); + // } + // let cancelEventTimer = true; + // if (typeof time === "boolean") { + // if (time === true) { + // cancelEventTimer = false; + // } + // time = undefined; + // } + // if (cancelEventTimer) { + // portObj.cancelEventTimer(); + // } + // return new Promise((resolve, reject) => { + // if (time && typeof time === "number") { + + // if ( + // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || + // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // ) { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // data.writeUInt16LE(time > 65535 ? 65535 : time, 4); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // const timeout = global.setTimeout(() => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // return resolve(); + // // @ts-ignore: The type of time is properly checked at the start + // }, time); + // portObj.setEventTimer(timeout); + // } + + // } else { + + // if ( + // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || + // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // ) { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // } + + // } + // }); + // } + + + // /** + // * Ramp the motor speed on a given port. + // * @method BoostMoveHub#rampMotorSpeed + // * @param {string} port + // * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + // * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + // * @param {number} time How long the ramp should last (in milliseconds). + // * @returns {Promise} Resolved upon successful completion of command. + // */ + // public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { + // const portObj = this._portLookup(port); + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // this._calculateRamp(fromSpeed, toSpeed, time, portObj) + // .on("changeSpeed", (speed) => { + // this.setMotorSpeed(port, speed, true); + // }) + // .on("finished", resolve); + // }); + // } + + + // /** + // * Rotate a motor by a given angle. + // * @method BoostMoveHub#setMotorAngle + // * @param {string} port + // * @param {number} angle How much the motor should be rotated (in degrees). + // * @param {number | Array.} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. + // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + // */ + // public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { + // const portObj = this._portLookup(port); + // if (!( + // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || + // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // )) { + // throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor"); + // } + // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { + // throw new Error(`Port ${portObj.id} can only accept a single speed`); + // } + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // data.writeUInt32LE(angle, 4); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // }); + // } + + + // /** + // * Tell motor to goto an absolute position + // * @method BoostMoveHub#setAbsolutePosition + // * @param {string} port + // * @param {number} pos The position of the motor to go to + // * @param {number | Array.} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) + // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + // */ + // public setAbsolutePosition (port: string, pos: number, speed: number = 100) { + // const portObj = this._portLookup(port); + // if (!( + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // )) { + // throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); + // } + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // data.writeInt32LE(pos, 4); + // data.writeInt32LE(pos, 8); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // data.writeInt32LE(pos, 4); + // } + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // }); + // } + + + // /** + // * Reset the current motor position as absolute position zero + // * @method BoostMoveHub#resetAbsolutePosition + // * @param {string} port + // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + // */ + // public resetAbsolutePosition (port: string) { + // const portObj = this._portLookup(port); + // if (!( + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // )) { + // throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); + // } + // return new Promise((resolve) => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // return resolve(); + // }); + // } + + + // /** + // * Fully (hard) stop the motor on a given port. + // * @method BoostMoveHub#brakeMotor + // * @param {string} port + // * @returns {Promise} Resolved upon successful completion of command. + // */ + // public brakeMotor (port: string) { + // return this.setMotorSpeed(port, 127); + // } + + + // /** + // * Set the light brightness on a given port. + // * @method BoostMoveHub#setLightBrightness + // * @param {string} port + // * @param {number} brightness Brightness value between 0-100 (0 is off) + // * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. + // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. + // */ + // public setLightBrightness (port: string, brightness: number, time?: number) { + // const portObj = this._portLookup(port); + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // if (time) { + // const timeout = global.setTimeout(() => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // return resolve(); + // }, time); + // portObj.setEventTimer(timeout); + // } else { + // return resolve(); + // } + // }); + // } protected _checkFirmware (version: string) { diff --git a/src/colordistancesensor.ts b/src/colordistancesensor.ts index 1eecb5ce..306b8925 100644 --- a/src/colordistancesensor.ts +++ b/src/colordistancesensor.ts @@ -7,6 +7,19 @@ export class ColorDistanceSensor extends Device { constructor (hub: Hub, portId: number) { super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR); + + this.on("newListener", (event) => { + switch (event) { + case "color": + this.subscribe(0x00); + break; + case "distance": + this.subscribe(0x01); + break; + } + + }); + } } diff --git a/src/controlplushub.ts b/src/controlplushub.ts index 4a0abf10..4d0a7b9d 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -38,19 +38,19 @@ export class ControlPlusHub extends LPF2Hub { constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); - this.type = Consts.HubType.CONTROL_PLUS_HUB; - this._ports = { - "A": new Port("A", 0), - "B": new Port("B", 1), - "C": new Port("C", 2), - "D": new Port("D", 3), - "ACCEL": new Port("ACCEL", 97), - "GYRO": new Port("GYRO", 98), - "TILT": new Port("TILT", 99) + this._type = Consts.HubType.CONTROL_PLUS_HUB; + this._portNames = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "ACCEL": 97, + "GYRO": 98, + "TILT": 99 }; - this.on("attach", (port, type) => { - this._combinePorts(port, type); - }); + // // this.on("attach", (port, type) => { + // // this._combinePorts(port, type); + // // }); debug("Discovered Control+ Hub"); } @@ -59,7 +59,7 @@ export class ControlPlusHub extends LPF2Hub { return new Promise(async (resolve, reject) => { debug("Connecting to Control+ Hub"); await super.connect(); - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01])); // Temperature + this.send(Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Temperature debug("Connect completed"); return resolve(); }); @@ -75,200 +75,200 @@ export class ControlPlusHub extends LPF2Hub { * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. */ public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { - const portObj = this._portLookup(port); - if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - throw new Error(`Port ${portObj.id} can only accept a single speed`); - } - let cancelEventTimer = true; - if (typeof time === "boolean") { - if (time === true) { - cancelEventTimer = false; - } - time = undefined; - } - if (cancelEventTimer) { - portObj.cancelEventTimer(); - } - return new Promise((resolve, reject) => { - if (time && typeof time === "number") { + // const portObj = this._portLookup(port); + // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { + // throw new Error(`Port ${portObj.id} can only accept a single speed`); + // } + // let cancelEventTimer = true; + // if (typeof time === "boolean") { + // if (time === true) { + // cancelEventTimer = false; + // } + // time = undefined; + // } + // if (cancelEventTimer) { + // portObj.cancelEventTimer(); + // } + // return new Promise((resolve, reject) => { + // if (time && typeof time === "number") { - if ( - portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - ) { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - } else { - // @ts-ignore: The type of speed is properly checked at the start - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - const timeout = global.setTimeout(() => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - // @ts-ignore: The type of time is properly checked at the start - }, time); - portObj.setEventTimer(timeout); - } + // if ( + // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || + // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // ) { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // data.writeUInt16LE(time > 65535 ? 65535 : time, 4); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // const timeout = global.setTimeout(() => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // return resolve(); + // // @ts-ignore: The type of time is properly checked at the start + // }, time); + // portObj.setEventTimer(timeout); + // } - } else { + // } else { - if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - } else { - // @ts-ignore: The type of speed is properly checked at the start - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - } + // if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // } - } - }); + // } + // }); } - /** - * Ramp the motor speed on a given port. - * @method ControlPlusHub#rampMotorSpeed - * @param {string} port - * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} time How long the ramp should last (in milliseconds). - * @returns {Promise} Resolved upon successful completion of command. - */ - public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time, portObj) - .on("changeSpeed", (speed) => { - this.setMotorSpeed(port, speed, true); - }) - .on("finished", resolve); - }); - } + // /** + // * Ramp the motor speed on a given port. + // * @method ControlPlusHub#rampMotorSpeed + // * @param {string} port + // * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + // * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + // * @param {number} time How long the ramp should last (in milliseconds). + // * @returns {Promise} Resolved upon successful completion of command. + // */ + // public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { + // const portObj = this._portLookup(port); + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // this._calculateRamp(fromSpeed, toSpeed, time, portObj) + // .on("changeSpeed", (speed) => { + // this.setMotorSpeed(port, speed, true); + // }) + // .on("finished", resolve); + // }); + // } - /** - * Rotate a motor by a given angle. - * @method ControlPlusHub#setMotorAngle - * @param {string} port - * @param {number} angle How much the motor should be rotated (in degrees). - * @param {number | Array.} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor"); - } - if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - throw new Error(`Port ${portObj.id} can only accept a single speed`); - } - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - data.writeUInt32LE(angle, 4); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - }); - } + // /** + // * Rotate a motor by a given angle. + // * @method ControlPlusHub#setMotorAngle + // * @param {string} port + // * @param {number} angle How much the motor should be rotated (in degrees). + // * @param {number | Array.} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. + // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + // */ + // public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { + // const portObj = this._portLookup(port); + // if (!( + // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || + // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // )) { + // throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor"); + // } + // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { + // throw new Error(`Port ${portObj.id} can only accept a single speed`); + // } + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // data.writeUInt32LE(angle, 4); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // }); + // } - /** - * Tell motor to goto an absolute position - * @method ControlPlusHub#setAbsolutePosition - * @param {string} port - * @param {number} pos The position of the motor to go to - * @param {number | Array.} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public setAbsolutePosition (port: string, pos: number, speed: number = 100) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - } - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeInt32LE(pos, 4); - data.writeInt32LE(pos, 8); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeInt32LE(pos, 4); - } - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - }); - } + // /** + // * Tell motor to goto an absolute position + // * @method ControlPlusHub#setAbsolutePosition + // * @param {string} port + // * @param {number} pos The position of the motor to go to + // * @param {number | Array.} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) + // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + // */ + // public setAbsolutePosition (port: string, pos: number, speed: number = 100) { + // const portObj = this._portLookup(port); + // if (!( + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // )) { + // throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); + // } + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // data.writeInt32LE(pos, 4); + // data.writeInt32LE(pos, 8); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // data.writeInt32LE(pos, 4); + // } + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // }); + // } - /** - * Reset the current motor position as absolute position zero - * @method ControlPlusHub#resetAbsolutePosition - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public resetAbsolutePosition (port: string) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - } - return new Promise((resolve) => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }); - } + // /** + // * Reset the current motor position as absolute position zero + // * @method ControlPlusHub#resetAbsolutePosition + // * @param {string} port + // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + // */ + // public resetAbsolutePosition (port: string) { + // const portObj = this._portLookup(port); + // if (!( + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // )) { + // throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); + // } + // return new Promise((resolve) => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // return resolve(); + // }); + // } /** @@ -282,32 +282,32 @@ export class ControlPlusHub extends LPF2Hub { } - /** - * Set the light brightness on a given port. - * @method ControlPlusHub#setLightBrightness - * @param {string} port - * @param {number} brightness Brightness value between 0-100 (0 is off) - * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. - */ - public setLightBrightness (port: string, brightness: number, time?: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - if (time) { - const timeout = global.setTimeout(() => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }, time); - portObj.setEventTimer(timeout); - } else { - return resolve(); - } - }); - } + // /** + // * Set the light brightness on a given port. + // * @method ControlPlusHub#setLightBrightness + // * @param {string} port + // * @param {number} brightness Brightness value between 0-100 (0 is off) + // * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. + // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. + // */ + // public setLightBrightness (port: string, brightness: number, time?: number) { + // const portObj = this._portLookup(port); + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // if (time) { + // const timeout = global.setTimeout(() => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // return resolve(); + // }, time); + // portObj.setEventTimer(timeout); + // } else { + // return resolve(); + // } + // }); + // } } diff --git a/src/device.ts b/src/device.ts index 73ddd4d4..8fe202eb 100644 --- a/src/device.ts +++ b/src/device.ts @@ -9,13 +9,20 @@ export class Device extends EventEmitter { private _portId: number; private _connected: boolean = true; private _type: number; + private _busy: boolean = false; + private _finished: (() => void) | null = null; constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { super(); this._hub = hub; this._portId = portId; this._type = type; - console.log(`New device on ${this._portId} - ${this._type}`); + this.hub.on("detach", (device) => { + if (device.portId === this.portId) { + this._connected = false; + this.emit("detach"); + } + }); } public get connected () { @@ -31,7 +38,7 @@ export class Device extends EventEmitter { } public get port () { - return "A"; // TODO NK: Look up the port name from the relevant hub + return this.hub.getPortNameForPortId(this.portId); } public get type () { @@ -39,7 +46,11 @@ export class Device extends EventEmitter { } public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { - this.hub.send(characteristic, data, callback); + this.hub.send(data, characteristic, callback); + } + + public subscribe (mode: number) { + this.send(Buffer.from([0x41, this.portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01])); } } diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index a343cdcd..596cbd32 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -38,11 +38,11 @@ export class DuploTrainBase extends LPF2Hub { constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); - this.type = Consts.HubType.DUPLO_TRAIN_HUB; - this._ports = { - "MOTOR": new Port("MOTOR", 0), - "COLOR": new Port("COLOR", 18), - "SPEEDOMETER": new Port("SPEEDOMETER", 19) + this._type = Consts.HubType.DUPLO_TRAIN_HUB; + this._portNames = { + "MOTOR": 0, + "COLOR": 18, + "SPEEDOMETER": 19 }; debug("Discovered Duplo Train Base"); } @@ -57,77 +57,6 @@ export class DuploTrainBase extends LPF2Hub { }); } - /** - * Set the motor speed on a given port. - * @method DuploTrainBase#setMotorSpeed - * @param {string} port - * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - */ - public setMotorSpeed (port: string, speed: number, time?: number | boolean) { - const portObj = this._portLookup(port); - let cancelEventTimer = true; - if (typeof time === "boolean") { - if (time === true) { - cancelEventTimer = false; - } - time = undefined; - } - if (cancelEventTimer) { - portObj.cancelEventTimer(); - } - return new Promise((resolve, reject) => { - if (time && typeof time === "number") { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - const timeout = global.setTimeout(() => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }, time); - portObj.setEventTimer(timeout); - } else { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - } - }); - } - - - /** - * Ramp the motor speed on a given port. - * @method DuploTrainBase#rampMotorSpeed - * @param {string} port - * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} time How long the ramp should last (in milliseconds). - * @returns {Promise} Resolved upon successful completion of command. - */ - public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time, portObj) - .on("changeSpeed", (speed) => { - this.setMotorSpeed(port, speed, true); - }) - .on("finished", resolve); - }); - } - - - /** - * Fully (hard) stop the motor on a given port. - * @method DuploTrainBase#brakeMotor - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command. - */ - public brakeMotor (port: string) { - return this.setMotorSpeed(port, 127); - } - /** * Play a built-in train sound. @@ -138,7 +67,7 @@ export class DuploTrainBase extends LPF2Hub { public playSound (sound: number) { return new Promise((resolve, reject) => { const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(data, Consts.BLECharacteristic.LPF2_ALL); return resolve(); }); } diff --git a/src/hub.ts b/src/hub.ts index f4fd9763..323f53db 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -19,12 +19,12 @@ export class Hub extends EventEmitter { public autoSubscribe: boolean = true; public useSpeedMap: boolean = true; - public type: Consts.HubType = Consts.HubType.UNKNOWN; + protected _type: Consts.HubType = Consts.HubType.UNKNOWN; protected _attachedDevices: Device[] = []; - protected _ports: {[port: string]: Port} = {}; - protected _virtualPorts: {[port: string]: Port} = {}; + protected _portNames: {[port: string]: number} = {}; + // protected _virtualPorts: {[port: string]: Port} = {}; protected _name: string = ""; protected _firmwareVersion: string = "0.0.00.0000"; @@ -63,6 +63,15 @@ export class Hub extends EventEmitter { } + /** + * @readonly + * @property {string} type Hub type + */ + public get type () { + return this._type; + } + + /** * @readonly * @property {string} firmwareVersion Firmware version of the hub @@ -160,44 +169,55 @@ export class Hub extends EventEmitter { * @method Hub#disconnect * @returns {Promise} Resolved upon successful disconnect. */ - public async disconnect () { - this._bleDevice.disconnect(); + public disconnect () { + return this._bleDevice.disconnect(); } - /** - * Subscribe to sensor notifications on a given port. - * @method Hub#subscribe - * @param {string} port - * @param {number} [mode] The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. - * @returns {Promise} Resolved upon successful issuance of command. - */ - public subscribe (port: string, mode?: number) { - return new Promise((resolve, reject) => { - let newMode = this._getModeForDeviceType(this._portLookup(port).type); - if (mode !== undefined) { - newMode = mode; + public getPortNameForPortId (portId: number) { + for (const port of Object.keys(this._portNames)) { + console.log(port); + if (this._portNames[port] === portId) { + return port; } - this._activatePortDevice(this._portLookup(port).value, this._portLookup(port).type, newMode, 0x00, () => { - return resolve(); - }); - }); + } + return; } - /** - * Unsubscribe to sensor notifications on a given port. - * @method Hub#unsubscribe - * @param {string} port - * @returns {Promise} Resolved upon successful issuance of command. - */ - public unsubscribe (port: string) { - return new Promise((resolve, reject) => { - const mode = this._getModeForDeviceType(this._portLookup(port).type); - this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => { - return resolve(); - }); - }); - } + + // /** + // * Subscribe to sensor notifications on a given port. + // * @method Hub#subscribe + // * @param {string} port + // * @param {number} [mode] The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. + // * @returns {Promise} Resolved upon successful issuance of command. + // */ + // public subscribe (port: string, mode?: number) { + // return new Promise((resolve, reject) => { + // let newMode = this._getModeForDeviceType(this._portLookup(port).type); + // if (mode !== undefined) { + // newMode = mode; + // } + // this._activatePortDevice(this._portLookup(port).value, this._portLookup(port).type, newMode, 0x00, () => { + // return resolve(); + // }); + // }); + // } + + // /** + // * Unsubscribe to sensor notifications on a given port. + // * @method Hub#unsubscribe + // * @param {string} port + // * @returns {Promise} Resolved upon successful issuance of command. + // */ + // public unsubscribe (port: string) { + // return new Promise((resolve, reject) => { + // const mode = this._getModeForDeviceType(this._portLookup(port).type); + // this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => { + // return resolve(); + // }); + // }); + // } /** @@ -228,28 +248,18 @@ export class Hub extends EventEmitter { } - /** - * Get the hub type. - * @method Hub#getHubType - * @returns {HubType} - */ - public getHubType () { - return this.type; - } - - - /** - * Get the device type for a given port. - * @method Hub#getPortDeviceType - * @param {string} port - * @returns {DeviceType} - */ - public getPortDeviceType (port: string) { - return this._portLookup(port).type; - } + // /** + // * Get the device type for a given port. + // * @method Hub#getPortDeviceType + // * @param {string} port + // * @returns {DeviceType} + // */ + // public getPortDeviceType (port: string) { + // return this._portLookup(port).type; + // } - public send (uuid: string, message: Buffer, callback?: () => void) { + public send (message: Buffer, uuid: string, callback?: () => void) { if (callback) { callback(); } @@ -273,26 +283,12 @@ export class Hub extends EventEmitter { // } - protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - if (callback) { - callback(); - } - } - - - protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - if (callback) { - callback(); - } - } + protected _attachDevice (device: Device) { - - protected _registerDeviceAttachment (device: Device) { - - const exists = this._attachedDevices.find((attachedDevice) => attachedDevice.portId === device.portId); + const exists = this._getDeviceByPortId(device.portId); if (exists) { - // TODO NK: Remove existing zombie device + this._attachedDevices.splice(this._attachedDevices.findIndex((attachedDevice) => attachedDevice.portId === device.portId), 1); } else { this._attachedDevices.push(device); } @@ -333,110 +329,126 @@ export class Hub extends EventEmitter { } - protected _getPortForPortNumber (num: number) { + protected _detachDevice (device: Device) { + this._attachedDevices.splice(this._attachedDevices.findIndex((attachedDevice) => attachedDevice.portId === device.portId), 1); + /** + * Emits when a device is detached from the Hub. + * @event Hub#attach + * @param {Device} device + */ + this.emit("detach", device); + } - for (const key of Object.keys(this._ports)) { - if (this._ports[key].value === num) { - return this._ports[key]; - } - } - for (const key of Object.keys(this._virtualPorts)) { - if (this._virtualPorts[key].value === num) { - return this._virtualPorts[key]; - } - } + protected _getDeviceByPortId (portId: number) { + return this._attachedDevices.find((attachedDevice) => attachedDevice.portId === portId); + } - return false; - } + // protected _getPortForPortNumber (num: number) { + // for (const key of Object.keys(this._ports)) { + // if (this._ports[key].value === num) { + // return this._ports[key]; + // } + // } - protected _mapSpeed (speed: number) { // Speed range of -100 to 100 is supported unless speed mapping is turned off, in which case, you're on your own! - if (!this.useSpeedMap) { - return speed; - } + // for (const key of Object.keys(this._virtualPorts)) { + // if (this._virtualPorts[key].value === num) { + // return this._virtualPorts[key]; + // } + // } - if (speed === 127) { - return 127; // Hard stop - } + // return false; - if (speed > 100) { - speed = 100; - } else if (speed < -100) { - speed = -100; - } + // } - return speed; - } + // protected _mapSpeed (speed: number) { // Speed range of -100 to 100 is supported unless speed mapping is turned off, in which case, you're on your own! + // if (!this.useSpeedMap) { + // return speed; + // } - protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number, port: Port) { - const emitter = new EventEmitter(); - const steps = Math.abs(toSpeed - fromSpeed); - let delay = time / steps; - let increment = 1; - if (delay < 50 && steps > 0) { - increment = 50 / delay; - delay = 50; - } - if (fromSpeed > toSpeed) { - increment = -increment; - } - let i = 0; - const interval = setInterval(() => { - let speed = Math.round(fromSpeed + (++i * increment)); - if (toSpeed > fromSpeed && speed > toSpeed) { - speed = toSpeed; - } else if (fromSpeed > toSpeed && speed < toSpeed) { - speed = toSpeed; - } - emitter.emit("changeSpeed", speed); - if (speed === toSpeed) { - clearInterval(interval); - emitter.emit("finished"); - } - }, delay); - port.setEventTimer(interval); - return emitter; - } + // if (speed === 127) { + // return 127; // Hard stop + // } + // if (speed > 100) { + // speed = 100; + // } else if (speed < -100) { + // speed = -100; + // } - protected _portLookup (portName: string) { - const portNameUpper = portName.toUpperCase(); - const port = this._ports[portNameUpper] || this._virtualPorts[portNameUpper]; - if (!port) { - throw new Error(`Port ${portNameUpper} does not exist on this Hub type`); - } - return port; - } + // return speed; + // } - private _getModeForDeviceType (type: Consts.DeviceType) { - switch (type) { - case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: - return 0x02; - case Consts.DeviceType.TRAIN_MOTOR: - return 0x02; - case Consts.DeviceType.BOOST_TACHO_MOTOR: - return 0x02; - case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: - return 0x02; - case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: - return 0x02; - case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: - return 0x02; - case Consts.DeviceType.CONTROL_PLUS_TILT: - return 0x00; - case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: - return 0x00; - case Consts.DeviceType.COLOR_DISTANCE_SENSOR: - return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08); - case Consts.DeviceType.BOOST_TILT: - return 0x04; - default: - return 0x00; - } - } + + // protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number, port: Port) { + // const emitter = new EventEmitter(); + // const steps = Math.abs(toSpeed - fromSpeed); + // let delay = time / steps; + // let increment = 1; + // if (delay < 50 && steps > 0) { + // increment = 50 / delay; + // delay = 50; + // } + // if (fromSpeed > toSpeed) { + // increment = -increment; + // } + // let i = 0; + // const interval = setInterval(() => { + // let speed = Math.round(fromSpeed + (++i * increment)); + // if (toSpeed > fromSpeed && speed > toSpeed) { + // speed = toSpeed; + // } else if (fromSpeed > toSpeed && speed < toSpeed) { + // speed = toSpeed; + // } + // emitter.emit("changeSpeed", speed); + // if (speed === toSpeed) { + // clearInterval(interval); + // emitter.emit("finished"); + // } + // }, delay); + // port.setEventTimer(interval); + // return emitter; + // } + + + // protected _portLookup (portName: string) { + // const portNameUpper = portName.toUpperCase(); + // const port = this._ports[portNameUpper] || this._virtualPorts[portNameUpper]; + // if (!port) { + // throw new Error(`Port ${portNameUpper} does not exist on this Hub type`); + // } + // return port; + // } + + // private _getModeForDeviceType (type: Consts.DeviceType) { + // switch (type) { + // case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: + // return 0x02; + // case Consts.DeviceType.TRAIN_MOTOR: + // return 0x02; + // case Consts.DeviceType.BOOST_TACHO_MOTOR: + // return 0x02; + // case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: + // return 0x02; + // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: + // return 0x02; + // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: + // return 0x02; + // case Consts.DeviceType.CONTROL_PLUS_TILT: + // return 0x00; + // case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: + // return 0x00; + // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: + // return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08); + // case Consts.DeviceType.BOOST_TILT: + // return 0x04; + // default: + // return 0x00; + // } + // } } diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 95936825..7adcc86a 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -48,21 +48,21 @@ export class LPF2Hub extends Hub { await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); if (this._voltagePort !== undefined) { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._voltagePort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports + this.send(Buffer.from([0x41, this._voltagePort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Activate voltage reports } if (this._currentPort !== undefined) { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + this.send(Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Activate current reports } - if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); + if (this._type === Consts.HubType.DUPLO_TRAIN_HUB) { + this.send(Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); } await this.sleep(100); - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x04, 0x05])); // Request hardware version - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x05, 0x02])); // Activate RSSI updates - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x06, 0x02])); // Activate battery level reports - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x0d, 0x05])); // Request primary MAC address + this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports + this.send(Buffer.from([0x01, 0x03, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request firmware version + this.send(Buffer.from([0x01, 0x04, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request hardware version + this.send(Buffer.from([0x01, 0x05, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate RSSI updates + this.send(Buffer.from([0x01, 0x06, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate battery level reports + this.send(Buffer.from([0x01, 0x0d, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request primary MAC address this.emit("connect"); resolve(); }); @@ -76,7 +76,7 @@ export class LPF2Hub extends Hub { */ public shutdown () { return new Promise((resolve, reject) => { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x02, 0x01]), () => { + this.send(Buffer.from([0x02, 0x01]), Consts.BLECharacteristic.LPF2_ALL, () => { return resolve(); }); }); @@ -97,8 +97,8 @@ export class LPF2Hub extends Hub { let data = Buffer.from([0x01, 0x01, 0x01]); data = Buffer.concat([data, Buffer.from(name, "ascii")]); // Send this twice, as sometimes the first time doesn't take - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(data, Consts.BLECharacteristic.LPF2_ALL); + this.send(data, Consts.BLECharacteristic.LPF2_ALL); this._name = name; return resolve(); }); @@ -114,12 +114,12 @@ export class LPF2Hub extends Hub { public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, this._ledPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(data, Consts.BLECharacteristic.LPF2_ALL); if (typeof color === "boolean") { color = 0; } data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x00, color]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(data, Consts.BLECharacteristic.LPF2_ALL); return resolve(); }); } @@ -136,9 +136,9 @@ export class LPF2Hub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x41, this._ledPort, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(data, Consts.BLECharacteristic.LPF2_ALL); data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x01, red, green, blue]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); + this.send(data, Consts.BLECharacteristic.LPF2_ALL); return resolve(); }); } @@ -146,14 +146,14 @@ export class LPF2Hub extends Hub { public sendRaw (message: Buffer) { return new Promise((resolve, reject) => { - this.send(Consts.BLECharacteristic.LPF2_ALL, message, () => { + this.send(message, Consts.BLECharacteristic.LPF2_ALL, () => { return resolve(); }); }); } - public send (uuid: string, message: Buffer, callback?: () => void) { + public send (message: Buffer, uuid: string, callback?: () => void) { message = Buffer.concat([Buffer.alloc(2), message]); message[0] = message.length; debug("Sent Message (LPF2_ALL)", message); @@ -161,30 +161,30 @@ export class LPF2Hub extends Hub { } - protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); - } + // protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { + // this.send(Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL, callback); + // } - protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); - } + // protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { + // this.send(Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.LPF2_ALL, callback); + // } - protected _combinePorts (port: string, type: number) { - if (!this._ports[port]) { - return; - } - const portObj = this._portLookup(port); - if (portObj) { - Object.keys(this._ports).forEach((id) => { - if (this._ports[id].type === type && this._ports[id].value !== portObj.value && !this._virtualPorts[`${portObj.value < this._ports[id].value ? portObj.id : this._ports[id].id}${portObj.value > this._ports[id].value ? portObj.id : this._ports[id].id}`]) { - debug("Combining ports", portObj.value < this._ports[id].value ? portObj.id : id, portObj.value > this._ports[id].value ? portObj.id : id); - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x61, 0x01, portObj.value < this._ports[id].value ? portObj.value : this._ports[id].value, portObj.value > this._ports[id].value ? portObj.value : this._ports[id].value])); - } - }); - } - } + // protected _combinePorts (port: string, type: number) { + // if (!this._ports[port]) { + // return; + // } + // const portObj = this._portLookup(port); + // if (portObj) { + // Object.keys(this._ports).forEach((id) => { + // if (this._ports[id].type === type && this._ports[id].value !== portObj.value && !this._virtualPorts[`${portObj.value < this._ports[id].value ? portObj.id : this._ports[id].id}${portObj.value > this._ports[id].value ? portObj.id : this._ports[id].id}`]) { + // debug("Combining ports", portObj.value < this._ports[id].value ? portObj.id : id, portObj.value > this._ports[id].value ? portObj.id : id); + // this.send(Buffer.from([0x61, 0x01, portObj.value < this._ports[id].value ? portObj.value : this._ports[id].value, portObj.value > this._ports[id].value ? portObj.value : this._ports[id].value]), Consts.BLECharacteristic.LPF2_ALL); + // } + // }); + // } + // } protected _checkFirmware (version: string) { @@ -297,6 +297,7 @@ export class LPF2Hub extends Hub { const event = data[4]; const deviceType = event ? data.readUInt16LE(5) : 0; + // Handle device attachments if (event === 0x01) { let device; @@ -313,8 +314,14 @@ export class LPF2Hub extends Hub { break; } - this._registerDeviceAttachment(device); + this._attachDevice(device); + // Handle device detachments + } else if (event === 0x00) { + const device = this._getDeviceByPortId(portId); + if (device) { + this._detachDevice(device); + } } @@ -358,8 +365,8 @@ export class LPF2Hub extends Hub { private _sendPortInformationRequest (port: number) { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x01])); - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x02])); // Mode combinations + this.send(Buffer.from([0x21, port, 0x01]), Consts.BLECharacteristic.LPF2_ALL); + this.send(Buffer.from([0x21, port, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Mode combinations } @@ -390,7 +397,7 @@ export class LPF2Hub extends Hub { private _sendModeInformationRequest (port: number, mode: number, type: number) { - this.send(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x22, port, mode, type])); + this.send(Buffer.from([0x22, port, mode, type]), Consts.BLECharacteristic.LPF2_ALL); } @@ -426,233 +433,233 @@ export class LPF2Hub extends Hub { private _parsePortAction (data: Buffer) { - const port = this._getPortForPortNumber(data[3]); + // const port = this._getPortForPortNumber(data[3]); - if (!port) { - return; - } + // if (!port) { + // return; + // } - if (data[4] === 0x0a) { - port.busy = false; - if (port.finished) { - port.finished(); - port.finished = null; - } - } + // if (data[4] === 0x0a) { + // port.busy = false; + // if (port.finished) { + // port.finished(); + // port.finished = null; + // } + // } } private _parseSensorMessage (data: Buffer) { - if (data[3] === this._voltagePort) { - const voltageRaw = data.readUInt16LE(4); - this._voltage = voltageRaw * this._voltageMaxV / this._voltageMaxRaw; - return; - } else if (data[3] === this._currentPort) { - const currentRaw = data.readUInt16LE(4); - this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; - return; - } - - if ((data[3] === 0x3d && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Control+ CPU Temperature - /** - * Emits when a change is detected on a temperature sensor. Measured in degrees centigrade. - * @event LPF2Hub#temp - * @param {string} port For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature. - * @param {number} temp - */ - this.emit("temp", "CPU", ((data.readInt16LE(4) / 900) * 90).toFixed(2)); - return; - } - - const port = this._getPortForPortNumber(data[3]); - - if (!port) { - return; - } - - if (port && port.connected) { - switch (port.type) { - case Consts.DeviceType.WEDO2_DISTANCE: { - let distance = data[4]; - if (data[5] === 1) { - distance = data[4] + 255; - } - /** - * Emits when a distance sensor is activated. - * @event LPF2Hub#distance - * @param {string} port - * @param {number} distance Distance, in millimeters. - */ - this.emit("distance", port.id, distance * 10); - break; - } - case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { - - /** - * Emits when a color sensor is activated. - * @event LPF2Hub#color - * @param {string} port - * @param {Color} color - */ - if (data[4] <= 10) { - this.emit("color", port.id, data[4]); - } - - let distance = data[5]; - const partial = data[7]; - - if (partial > 0) { - distance += 1.0 / partial; - } - - distance = Math.floor(distance * 25.4) - 20; - - this.emit("distance", port.id, distance); - - /** - * A combined color and distance event, emits when the sensor is activated. - * @event LPF2Hub#colorAndDistance - * @param {string} port - * @param {Color} color - * @param {number} distance Distance, in millimeters. - */ - if (data[4] <= 10) { - this.emit("colorAndDistance", port.id, data[4], distance); - } - break; - } - case Consts.DeviceType.WEDO2_TILT: { - const tiltX = data.readInt8(4); - const tiltY = data.readInt8(5); - this._lastTiltX = tiltX; - this._lastTiltY = tiltY; - /** - * Emits when a tilt sensor is activated. - * @event LPF2Hub#tilt - * @param {string} port If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used. - * @param {number} x - * @param {number} y - * @param {number} z (Only available when using a Control+ Hub) - */ - this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ); - break; - } - case Consts.DeviceType.BOOST_TACHO_MOTOR: { - const rotation = data.readInt32LE(4); - /** - * Emits when a rotation sensor is activated. - * @event LPF2Hub#rotate - * @param {string} port - * @param {number} rotation - */ - this.emit("rotate", port.id, rotation); - break; - } - case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: { - const rotation = data.readInt32LE(4); - this.emit("rotate", port.id, rotation); - break; - } - case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: { - const rotation = data.readInt32LE(4); - this.emit("rotate", port.id, rotation); - break; - } - case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: { - const rotation = data.readInt32LE(4); - this.emit("rotate", port.id, rotation); - break; - } - case Consts.DeviceType.CONTROL_PLUS_TILT: { - const tiltZ = data.readInt16LE(4); - const tiltY = data.readInt16LE(6); - const tiltX = data.readInt16LE(8); - this._lastTiltX = tiltX; - this._lastTiltY = tiltY; - this._lastTiltZ = tiltZ; - this.emit("tilt", "TILT", this._lastTiltX, this._lastTiltY, this._lastTiltZ); - break; - } - case Consts.DeviceType.CONTROL_PLUS_GYRO: { - const gyroX = Math.round(data.readInt16LE(4) * 7 / 400); - const gyroY = Math.round(data.readInt16LE(6) * 7 / 400); - const gyroZ = Math.round(data.readInt16LE(8) * 7 / 400); - /** - * Emits when gyroscope detects movement. Measured in DPS - degrees per second. - * @event LPF2Hub#gyro - * @param {string} port - * @param {number} x - * @param {number} y - * @param {number} z - */ - this.emit("gyro", "GYRO", gyroX, gyroY, gyroZ); - break; - } - case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: { - const accelX = Math.round(data.readInt16LE(4) / 4.096); - const accelY = Math.round(data.readInt16LE(6) / 4.096); - const accelZ = Math.round(data.readInt16LE(8) / 4.096); - /** - * Emits when accelerometer detects movement. Measured in mG. - * @event LPF2Hub#accel - * @param {string} port - * @param {number} x - * @param {number} y - * @param {number} z - */ - this.emit("accel", "ACCEL", accelX, accelY, accelZ); - break; - } - case Consts.DeviceType.BOOST_TILT: { - const tiltX = data.readInt8(4); - const tiltY = data.readInt8(5); - this._lastTiltX = tiltX; - this._lastTiltY = tiltY; - this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ); - break; - } - case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: { - switch (data[4]) { - case 0x01: { - this.emit("button", port.id, Consts.ButtonState.UP); - break; - } - case 0xff: { - this.emit("button", port.id, Consts.ButtonState.DOWN); - break; - } - case 0x7f: { - this.emit("button", port.id, Consts.ButtonState.STOP); - break; - } - case 0x00: { - this.emit("button", port.id, Consts.ButtonState.RELEASED); - break; - } - } - break; - } - case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: { - if (data[4] <= 10) { - this.emit("color", port.id, data[4]); - } - break; - } - case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER: { - /** - * Emits on a speed change. - * @event LPF2Hub#speed - * @param {string} port - * @param {number} speed - */ - const speed = data.readInt16LE(4); - this.emit("speed", port.id, speed); - break; - } - } - } + // if (data[3] === this._voltagePort) { + // const voltageRaw = data.readUInt16LE(4); + // this._voltage = voltageRaw * this._voltageMaxV / this._voltageMaxRaw; + // return; + // } else if (data[3] === this._currentPort) { + // const currentRaw = data.readUInt16LE(4); + // this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; + // return; + // } + + // if ((data[3] === 0x3d && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Control+ CPU Temperature + // /** + // * Emits when a change is detected on a temperature sensor. Measured in degrees centigrade. + // * @event LPF2Hub#temp + // * @param {string} port For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature. + // * @param {number} temp + // */ + // this.emit("temp", "CPU", ((data.readInt16LE(4) / 900) * 90).toFixed(2)); + // return; + // } + + // const port = this._getPortForPortNumber(data[3]); + + // if (!port) { + // return; + // } + + // if (port && port.connected) { + // switch (port.type) { + // case Consts.DeviceType.WEDO2_DISTANCE: { + // let distance = data[4]; + // if (data[5] === 1) { + // distance = data[4] + 255; + // } + // /** + // * Emits when a distance sensor is activated. + // * @event LPF2Hub#distance + // * @param {string} port + // * @param {number} distance Distance, in millimeters. + // */ + // this.emit("distance", port.id, distance * 10); + // break; + // } + // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { + + // /** + // * Emits when a color sensor is activated. + // * @event LPF2Hub#color + // * @param {string} port + // * @param {Color} color + // */ + // if (data[4] <= 10) { + // this.emit("color", port.id, data[4]); + // } + + // let distance = data[5]; + // const partial = data[7]; + + // if (partial > 0) { + // distance += 1.0 / partial; + // } + + // distance = Math.floor(distance * 25.4) - 20; + + // this.emit("distance", port.id, distance); + + // /** + // * A combined color and distance event, emits when the sensor is activated. + // * @event LPF2Hub#colorAndDistance + // * @param {string} port + // * @param {Color} color + // * @param {number} distance Distance, in millimeters. + // */ + // if (data[4] <= 10) { + // this.emit("colorAndDistance", port.id, data[4], distance); + // } + // break; + // } + // case Consts.DeviceType.WEDO2_TILT: { + // const tiltX = data.readInt8(4); + // const tiltY = data.readInt8(5); + // this._lastTiltX = tiltX; + // this._lastTiltY = tiltY; + // /** + // * Emits when a tilt sensor is activated. + // * @event LPF2Hub#tilt + // * @param {string} port If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used. + // * @param {number} x + // * @param {number} y + // * @param {number} z (Only available when using a Control+ Hub) + // */ + // this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ); + // break; + // } + // case Consts.DeviceType.BOOST_TACHO_MOTOR: { + // const rotation = data.readInt32LE(4); + // /** + // * Emits when a rotation sensor is activated. + // * @event LPF2Hub#rotate + // * @param {string} port + // * @param {number} rotation + // */ + // this.emit("rotate", port.id, rotation); + // break; + // } + // case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: { + // const rotation = data.readInt32LE(4); + // this.emit("rotate", port.id, rotation); + // break; + // } + // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: { + // const rotation = data.readInt32LE(4); + // this.emit("rotate", port.id, rotation); + // break; + // } + // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: { + // const rotation = data.readInt32LE(4); + // this.emit("rotate", port.id, rotation); + // break; + // } + // case Consts.DeviceType.CONTROL_PLUS_TILT: { + // const tiltZ = data.readInt16LE(4); + // const tiltY = data.readInt16LE(6); + // const tiltX = data.readInt16LE(8); + // this._lastTiltX = tiltX; + // this._lastTiltY = tiltY; + // this._lastTiltZ = tiltZ; + // this.emit("tilt", "TILT", this._lastTiltX, this._lastTiltY, this._lastTiltZ); + // break; + // } + // case Consts.DeviceType.CONTROL_PLUS_GYRO: { + // const gyroX = Math.round(data.readInt16LE(4) * 7 / 400); + // const gyroY = Math.round(data.readInt16LE(6) * 7 / 400); + // const gyroZ = Math.round(data.readInt16LE(8) * 7 / 400); + // /** + // * Emits when gyroscope detects movement. Measured in DPS - degrees per second. + // * @event LPF2Hub#gyro + // * @param {string} port + // * @param {number} x + // * @param {number} y + // * @param {number} z + // */ + // this.emit("gyro", "GYRO", gyroX, gyroY, gyroZ); + // break; + // } + // case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: { + // const accelX = Math.round(data.readInt16LE(4) / 4.096); + // const accelY = Math.round(data.readInt16LE(6) / 4.096); + // const accelZ = Math.round(data.readInt16LE(8) / 4.096); + // /** + // * Emits when accelerometer detects movement. Measured in mG. + // * @event LPF2Hub#accel + // * @param {string} port + // * @param {number} x + // * @param {number} y + // * @param {number} z + // */ + // this.emit("accel", "ACCEL", accelX, accelY, accelZ); + // break; + // } + // case Consts.DeviceType.BOOST_TILT: { + // const tiltX = data.readInt8(4); + // const tiltY = data.readInt8(5); + // this._lastTiltX = tiltX; + // this._lastTiltY = tiltY; + // this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ); + // break; + // } + // case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: { + // switch (data[4]) { + // case 0x01: { + // this.emit("button", port.id, Consts.ButtonState.UP); + // break; + // } + // case 0xff: { + // this.emit("button", port.id, Consts.ButtonState.DOWN); + // break; + // } + // case 0x7f: { + // this.emit("button", port.id, Consts.ButtonState.STOP); + // break; + // } + // case 0x00: { + // this.emit("button", port.id, Consts.ButtonState.RELEASED); + // break; + // } + // } + // break; + // } + // case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: { + // if (data[4] <= 10) { + // this.emit("color", port.id, data[4]); + // } + // break; + // } + // case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER: { + // /** + // * Emits on a speed change. + // * @event LPF2Hub#speed + // * @param {string} port + // * @param {number} speed + // */ + // const speed = data.readInt16LE(4); + // this.emit("speed", port.id, speed); + // break; + // } + // } + // } } diff --git a/src/puphub.ts b/src/puphub.ts index f718d5ed..8902bf94 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -36,14 +36,11 @@ export class PUPHub extends LPF2Hub { constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); - this.type = Consts.HubType.POWERED_UP_HUB; - this._ports = { - "A": new Port("A", 0), - "B": new Port("B", 1) + this._type = Consts.HubType.POWERED_UP_HUB; + this._portNames = { + "A": 0, + "B": 1 }; - this.on("attach", (port, type) => { - this._combinePorts(port, type); - }); debug("Discovered Powered UP Hub"); } @@ -58,239 +55,6 @@ export class PUPHub extends LPF2Hub { } - /** - * Set the motor speed on a given port. - * @method PUPHub#setMotorSpeed - * @param {string} port - * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - */ - public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { - const portObj = this._portLookup(port); - if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - throw new Error(`Port ${portObj.id} can only accept a single speed`); - } - let cancelEventTimer = true; - if (typeof time === "boolean") { - if (time === true) { - cancelEventTimer = false; - } - time = undefined; - } - if (cancelEventTimer) { - portObj.cancelEventTimer(); - } - return new Promise((resolve, reject) => { - if (time && typeof time === "number") { - - if ( - portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - ) { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - } else { - // @ts-ignore: The type of speed is properly checked at the start - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - const timeout = global.setTimeout(() => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - // @ts-ignore: The type of time is properly checked at the start - }, time); - portObj.setEventTimer(timeout); - } - - } else { - - if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - } else { - // @ts-ignore: The type of speed is properly checked at the start - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - } - - } - }); - } - - - /** - * Ramp the motor speed on a given port. - * @method PUPHub#rampMotorSpeed - * @param {string} port - * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} time How long the ramp should last (in milliseconds). - * @returns {Promise} Resolved upon successful completion of command. - */ - public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time, portObj) - .on("changeSpeed", (speed) => { - this.setMotorSpeed(port, speed, true); - }) - .on("finished", resolve); - }); - } - - - /** - * Rotate a motor by a given angle. - * @method PUPHub#setMotorAngle - * @param {string} port - * @param {number} angle How much the motor should be rotated (in degrees). - * @param {number | Array.} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor"); - } - if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - throw new Error(`Port ${portObj.id} can only accept a single speed`); - } - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - } - data.writeUInt32LE(angle, 4); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - }); - } - - - /** - * Tell motor to goto an absolute position - * @method PUPHub#setAbsolutePosition - * @param {string} port - * @param {number} pos The position of the motor to go to - * @param {number | Array.} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public setAbsolutePosition (port: string, pos: number, speed: number = 100) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - } - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - portObj.busy = true; - let data = null; - if (this._virtualPorts[portObj.id]) { - data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeInt32LE(pos, 4); - data.writeInt32LE(pos, 8); - } else { - // @ts-ignore: The type of speed is properly checked at the start - data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeInt32LE(pos, 4); - } - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - portObj.finished = () => { - return resolve(); - }; - }); - } - - - /** - * Reset the current motor position as absolute position zero - * @method PUPHub#resetAbsolutePosition - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - */ - public resetAbsolutePosition (port: string) { - const portObj = this._portLookup(port); - if (!( - portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - )) { - throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - } - return new Promise((resolve) => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }); - } - - - /** - * Set the light brightness on a given port. - * @method PUPHub#setLightBrightness - * @param {string} port - * @param {number} brightness Brightness value between 0-100 (0 is off) - * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. - */ - public setLightBrightness (port: string, brightness: number, time?: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - if (time) { - const timeout = global.setTimeout(() => { - const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - this.send(Consts.BLECharacteristic.LPF2_ALL, data); - return resolve(); - }, time); - portObj.setEventTimer(timeout); - } else { - return resolve(); - } - }); - } - - protected _checkFirmware (version: string) { if (compareVersion("1.1.00.0004", version) === 1) { throw new Error(`Your Powered Up Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`); diff --git a/src/pupremote.ts b/src/pupremote.ts index c6fcecf4..2a25bb11 100644 --- a/src/pupremote.ts +++ b/src/pupremote.ts @@ -39,10 +39,10 @@ export class PUPRemote extends LPF2Hub { constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); - this.type = Consts.HubType.POWERED_UP_REMOTE; - this._ports = { - "LEFT": new Port("LEFT", 0), - "RIGHT": new Port("RIGHT", 1) + this._type = Consts.HubType.POWERED_UP_REMOTE; + this._portNames = { + "LEFT": 0, + "RIGHT": 1 }; debug("Discovered Powered UP Remote"); } diff --git a/src/utils.ts b/src/utils.ts index 83650aa1..e4632148 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,9 +1,22 @@ // @ts-ignore export const isWebBluetooth = (typeof navigator !== "undefined" && navigator && navigator.bluetooth); -export function toHex (value: number, length: number = 2) { +export const toHex = (value: number, length: number = 2) => { return value.toString(16).padStart(length, "0"); -} -export function toBin (value: number, length: number = 8) { +}; + +export const toBin = (value: number, length: number = 8) => { return value.toString(2).padStart(length, "0"); -} +}; + +export const mapSpeed = (speed: number) => { + if (speed === 127) { + return 127; + } + if (speed > 100) { + speed = 100; + } else if (speed < -100) { + speed = -100; + } + return speed; +}; diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index e08a99ac..4142e362 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -38,10 +38,10 @@ export class WeDo2SmartHub extends Hub { constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { super(device, autoSubscribe); - this.type = Consts.HubType.WEDO2_SMART_HUB; - this._ports = { - "A": new Port("A", 1), - "B": new Port("B", 2) + this._type = Consts.HubType.WEDO2_SMART_HUB; + this._portNames = { + "A": 1, + "B": 2 }; debug("Discovered WeDo 2.0 Smart Hub"); } @@ -115,8 +115,8 @@ export class WeDo2SmartHub extends Hub { return new Promise((resolve, reject) => { const data = Buffer.from(name, "ascii"); // Send this twice, as sometimes the first time doesn't take - this.send(Consts.BLECharacteristic.WEDO2_NAME_ID, data); - this.send(Consts.BLECharacteristic.WEDO2_NAME_ID, data); + this.send(data, Consts.BLECharacteristic.WEDO2_NAME_ID); + this.send(data, Consts.BLECharacteristic.WEDO2_NAME_ID); this._name = name; return resolve(); }); @@ -132,12 +132,12 @@ export class WeDo2SmartHub extends Hub { public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); + this.send(data, Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); if (typeof color === "boolean") { color = 0; } data = Buffer.from([0x06, 0x04, 0x01, color]); - this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); return resolve(); }); } @@ -150,7 +150,7 @@ export class WeDo2SmartHub extends Hub { */ public shutdown () { return new Promise((resolve, reject) => { - this.send(Consts.BLECharacteristic.WEDO2_DISCONNECT, Buffer.from([0x00]), () => { + this.send(Buffer.from([0x00]), Consts.BLECharacteristic.WEDO2_DISCONNECT, () => { return resolve(); }); }); @@ -168,80 +168,80 @@ export class WeDo2SmartHub extends Hub { public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { let data = Buffer.from([0x06, 0x17, 0x01, 0x02]); - this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, data); + this.send(data, Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); return resolve(); }); } - /** - * Set the motor speed on a given port. - * @method WeDo2SmartHub#setMotorSpeed - * @param {string} port - * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - */ - public setMotorSpeed (port: string, speed: number, time?: number | boolean) { - const portObj = this._portLookup(port); - let cancelEventTimer = true; - if (typeof time === "boolean") { - if (time === true) { - cancelEventTimer = false; - } - time = undefined; - } - if (cancelEventTimer) { - portObj.cancelEventTimer(); - } - return new Promise((resolve, reject) => { - this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)])); - if (time && typeof time === "number") { - const timeout = global.setTimeout(() => { - this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([portObj.value, 0x01, 0x02, 0x00])); - return resolve(); - }, time); - portObj.setEventTimer(timeout); - } else { - return resolve(); - } - }); - } - - - /** - * Ramp the motor speed on a given port. - * @method WeDo2SmartHub#rampMotorSpeed - * @param {string} port - * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @param {number} time How long the ramp should last (in milliseconds). - * @returns {Promise} Resolved upon successful completion of command. - */ - public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - this._calculateRamp(fromSpeed, toSpeed, time, portObj) - .on("changeSpeed", (speed) => { - this.setMotorSpeed(port, speed, true); - }) - .on("finished", resolve); - }); - } - - - /** - * Fully (hard) stop the motor on a given port. - * @method WeDo2SmartHub#brakeMotor - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command. - */ - public brakeMotor (port: string) { - return this.setMotorSpeed(port, 127); - } + // /** + // * Set the motor speed on a given port. + // * @method WeDo2SmartHub#setMotorSpeed + // * @param {string} port + // * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + // * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. + // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. + // */ + // public setMotorSpeed (port: string, speed: number, time?: number | boolean) { + // const portObj = this._portLookup(port); + // let cancelEventTimer = true; + // if (typeof time === "boolean") { + // if (time === true) { + // cancelEventTimer = false; + // } + // time = undefined; + // } + // if (cancelEventTimer) { + // portObj.cancelEventTimer(); + // } + // return new Promise((resolve, reject) => { + // this.send(Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + // if (time && typeof time === "number") { + // const timeout = global.setTimeout(() => { + // this.send(Buffer.from([portObj.value, 0x01, 0x02, 0x00]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + // return resolve(); + // }, time); + // portObj.setEventTimer(timeout); + // } else { + // return resolve(); + // } + // }); + // } + + + // /** + // * Ramp the motor speed on a given port. + // * @method WeDo2SmartHub#rampMotorSpeed + // * @param {string} port + // * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + // * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + // * @param {number} time How long the ramp should last (in milliseconds). + // * @returns {Promise} Resolved upon successful completion of command. + // */ + // public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { + // const portObj = this._portLookup(port); + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // this._calculateRamp(fromSpeed, toSpeed, time, portObj) + // .on("changeSpeed", (speed) => { + // this.setMotorSpeed(port, speed, true); + // }) + // .on("finished", resolve); + // }); + // } + + + // /** + // * Fully (hard) stop the motor on a given port. + // * @method WeDo2SmartHub#brakeMotor + // * @param {string} port + // * @returns {Promise} Resolved upon successful completion of command. + // */ + // public brakeMotor (port: string) { + // return this.setMotorSpeed(port, 127); + // } /** @@ -256,41 +256,41 @@ export class WeDo2SmartHub extends Hub { const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]); data.writeUInt16LE(frequency, 3); data.writeUInt16LE(time, 5); - this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); + this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); global.setTimeout(resolve, time); }); } - /** - * Set the light brightness on a given port. - * @method WeDo2SmartHub#setLightBrightness - * @param {string} port - * @param {number} brightness Brightness value between 0-100 (0 is off) - * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. - */ - public setLightBrightness (port: string, brightness: number, time?: number) { - const portObj = this._portLookup(port); - portObj.cancelEventTimer(); - return new Promise((resolve, reject) => { - const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); - this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); - if (time) { - const timeout = global.setTimeout(() => { - const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); - this.send(Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE, data); - return resolve(); - }, time); - portObj.setEventTimer(timeout); - } else { - return resolve(); - } - }); - } - - - public send (uuid: string, message: Buffer, callback?: () => void) { + // /** + // * Set the light brightness on a given port. + // * @method WeDo2SmartHub#setLightBrightness + // * @param {string} port + // * @param {number} brightness Brightness value between 0-100 (0 is off) + // * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. + // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. + // */ + // public setLightBrightness (port: string, brightness: number, time?: number) { + // const portObj = this._portLookup(port); + // portObj.cancelEventTimer(); + // return new Promise((resolve, reject) => { + // const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); + // this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + // if (time) { + // const timeout = global.setTimeout(() => { + // const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); + // this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + // return resolve(); + // }, time); + // portObj.setEventTimer(timeout); + // } else { + // return resolve(); + // } + // }); + // } + + + public send (message: Buffer, uuid: string, callback?: () => void) { if (debug.enabled) { debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message); } @@ -299,12 +299,12 @@ export class WeDo2SmartHub extends Hub { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); + this.send(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this.send(Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); + this.send(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, callback); } @@ -361,7 +361,7 @@ export class WeDo2SmartHub extends Hub { break; } - this._registerDeviceAttachment(device); + this._attachDevice(device); } @@ -379,101 +379,101 @@ export class WeDo2SmartHub extends Hub { private _parseSensorMessage (data: Buffer) { - debug("Received Message (WEDO2_SENSOR_VALUE)", data); - - if (data[0] === 0x01) { - /** - * Emits when a button is pressed. - * @event WeDo2SmartHub#button - * @param {string} button - * @param {ButtonState} state - */ - this.emit("button", "GREEN", Consts.ButtonState.PRESSED); - return; - } else if (data[0] === 0x00) { - this.emit("button", "GREEN", Consts.ButtonState.RELEASED); - return; - } + // debug("Received Message (WEDO2_SENSOR_VALUE)", data); - // Voltage - if (data[1] === 0x03) { - const voltage = data.readInt16LE(2); - this._voltage = voltage / 40; - // Current - } else if (data[1] === 0x04) { - const current = data.readInt16LE(2); - this._current = current / 1000; - } + // if (data[0] === 0x01) { + // /** + // * Emits when a button is pressed. + // * @event WeDo2SmartHub#button + // * @param {string} button + // * @param {ButtonState} state + // */ + // this.emit("button", "GREEN", Consts.ButtonState.PRESSED); + // return; + // } else if (data[0] === 0x00) { + // this.emit("button", "GREEN", Consts.ButtonState.RELEASED); + // return; + // } - const port = this._getPortForPortNumber(data[1]); + // // Voltage + // if (data[1] === 0x03) { + // const voltage = data.readInt16LE(2); + // this._voltage = voltage / 40; + // // Current + // } else if (data[1] === 0x04) { + // const current = data.readInt16LE(2); + // this._current = current / 1000; + // } - if (!port) { - return; - } + // const port = this._getPortForPortNumber(data[1]); - if (port && port.connected) { - switch (port.type) { - case Consts.DeviceType.WEDO2_DISTANCE: { - let distance = data[2]; - if (data[3] === 1) { - distance = data[2] + 255; - } - /** - * Emits when a distance sensor is activated. - * @event WeDo2SmartHub#distance - * @param {string} port - * @param {number} distance Distance, in millimeters. - */ - this.emit("distance", port.id, distance * 10); - break; - } - case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { - const distance = data[2]; - /** - * Emits when a color sensor is activated. - * @event WeDo2SmartHub#color - * @param {string} port - * @param {Color} color - */ - this.emit("color", port.id, distance); - break; - } - case Consts.DeviceType.WEDO2_TILT: { - this._lastTiltX = data.readInt8(2); - this._lastTiltY = data.readInt8(3); - /** - * Emits when a tilt sensor is activated. - * @event WeDo2SmartHub#tilt - * @param {string} port - * @param {number} x - * @param {number} y - */ - this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); - break; - } - case Consts.DeviceType.BOOST_TACHO_MOTOR: { - const rotation = data.readInt32LE(2); - /** - * Emits when a rotation sensor is activated. - * @event WeDo2SmartHub#rotate - * @param {string} port - * @param {number} rotation - */ - this.emit("rotate", port.id, rotation); - break; - } - case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: { - const rotation = data.readInt32LE(2); - this.emit("rotate", port.id, rotation); - break; - } - case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: { - const rotation = data.readInt32LE(2); - this.emit("rotate", port.id, rotation); - break; - } - } - } + // if (!port) { + // return; + // } + + // if (port && port.connected) { + // switch (port.type) { + // case Consts.DeviceType.WEDO2_DISTANCE: { + // let distance = data[2]; + // if (data[3] === 1) { + // distance = data[2] + 255; + // } + // /** + // * Emits when a distance sensor is activated. + // * @event WeDo2SmartHub#distance + // * @param {string} port + // * @param {number} distance Distance, in millimeters. + // */ + // this.emit("distance", port.id, distance * 10); + // break; + // } + // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { + // const distance = data[2]; + // /** + // * Emits when a color sensor is activated. + // * @event WeDo2SmartHub#color + // * @param {string} port + // * @param {Color} color + // */ + // this.emit("color", port.id, distance); + // break; + // } + // case Consts.DeviceType.WEDO2_TILT: { + // this._lastTiltX = data.readInt8(2); + // this._lastTiltY = data.readInt8(3); + // /** + // * Emits when a tilt sensor is activated. + // * @event WeDo2SmartHub#tilt + // * @param {string} port + // * @param {number} x + // * @param {number} y + // */ + // this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); + // break; + // } + // case Consts.DeviceType.BOOST_TACHO_MOTOR: { + // const rotation = data.readInt32LE(2); + // /** + // * Emits when a rotation sensor is activated. + // * @event WeDo2SmartHub#rotate + // * @param {string} port + // * @param {number} rotation + // */ + // this.emit("rotate", port.id, rotation); + // break; + // } + // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: { + // const rotation = data.readInt32LE(2); + // this.emit("rotate", port.id, rotation); + // break; + // } + // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: { + // const rotation = data.readInt32LE(2); + // this.emit("rotate", port.id, rotation); + // break; + // } + // } + // } } From 8ffc60b92406f68e5c3637abccc2ff47e1d349e0 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sat, 7 Dec 2019 13:19:16 -0800 Subject: [PATCH 03/62] attachedDevices now a map --- src/colordistancesensor.ts | 17 +++++++++-------- src/device.ts | 7 ++++++- src/hub.ts | 15 ++++----------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/colordistancesensor.ts b/src/colordistancesensor.ts index 306b8925..5506677d 100644 --- a/src/colordistancesensor.ts +++ b/src/colordistancesensor.ts @@ -9,15 +9,16 @@ export class ColorDistanceSensor extends Device { super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR); this.on("newListener", (event) => { - switch (event) { - case "color": - this.subscribe(0x00); - break; - case "distance": - this.subscribe(0x01); - break; + if (this.autoSubscribe) { + switch (event) { + case "color": + this.subscribe(0x00); + break; + case "distance": + this.subscribe(0x01); + break; + } } - }); } diff --git a/src/device.ts b/src/device.ts index 8fe202eb..4ed700eb 100644 --- a/src/device.ts +++ b/src/device.ts @@ -5,6 +5,8 @@ import * as Consts from "./consts"; export class Device extends EventEmitter { + public autoSubscribe: boolean = true; + private _hub: Hub; private _portId: number; private _connected: boolean = true; @@ -18,7 +20,7 @@ export class Device extends EventEmitter { this._portId = portId; this._type = type; this.hub.on("detach", (device) => { - if (device.portId === this.portId) { + if (device === this) { this._connected = false; this.emit("detach"); } @@ -46,6 +48,9 @@ export class Device extends EventEmitter { } public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { + if (!this.connected) { + throw new Error("Device is not connected"); + } this.hub.send(data, characteristic, callback); } diff --git a/src/hub.ts b/src/hub.ts index 323f53db..36fe7359 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -21,7 +21,7 @@ export class Hub extends EventEmitter { public useSpeedMap: boolean = true; protected _type: Consts.HubType = Consts.HubType.UNKNOWN; - protected _attachedDevices: Device[] = []; + protected _attachedDevices: {[portId: number]: Device} = {}; protected _portNames: {[port: string]: number} = {}; // protected _virtualPorts: {[port: string]: Port} = {}; @@ -176,7 +176,6 @@ export class Hub extends EventEmitter { public getPortNameForPortId (portId: number) { for (const port of Object.keys(this._portNames)) { - console.log(port); if (this._portNames[port] === portId) { return port; } @@ -285,13 +284,7 @@ export class Hub extends EventEmitter { protected _attachDevice (device: Device) { - const exists = this._getDeviceByPortId(device.portId); - - if (exists) { - this._attachedDevices.splice(this._attachedDevices.findIndex((attachedDevice) => attachedDevice.portId === device.portId), 1); - } else { - this._attachedDevices.push(device); - } + this._attachedDevices[device.portId] = device; /** * Emits when a device is attached to the Hub. @@ -330,7 +323,7 @@ export class Hub extends EventEmitter { protected _detachDevice (device: Device) { - this._attachedDevices.splice(this._attachedDevices.findIndex((attachedDevice) => attachedDevice.portId === device.portId), 1); + delete this._attachedDevices[device.portId]; /** * Emits when a device is detached from the Hub. * @event Hub#attach @@ -341,7 +334,7 @@ export class Hub extends EventEmitter { protected _getDeviceByPortId (portId: number) { - return this._attachedDevices.find((attachedDevice) => attachedDevice.portId === portId); + return this._attachedDevices[portId]; } From 93a1369e8488be4f4b1558c9f3aecec46858b53a Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sun, 8 Dec 2019 18:07:46 -0800 Subject: [PATCH 04/62] Basic code for passing messages through to the device --- src/colordistancesensor.ts | 4 ++++ src/device.ts | 12 +++++++++--- src/lpf2hub.ts | 9 ++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/colordistancesensor.ts b/src/colordistancesensor.ts index 5506677d..e1633be1 100644 --- a/src/colordistancesensor.ts +++ b/src/colordistancesensor.ts @@ -23,4 +23,8 @@ export class ColorDistanceSensor extends Device { } + public receive (message: Buffer) { + // NK TODO + } + } diff --git a/src/device.ts b/src/device.ts index 4ed700eb..1579c747 100644 --- a/src/device.ts +++ b/src/device.ts @@ -19,12 +19,14 @@ export class Device extends EventEmitter { this._hub = hub; this._portId = portId; this._type = type; - this.hub.on("detach", (device) => { - if (device === this) { + const detachListener = (device: Device) => { + if (device.portId === this.portId) { this._connected = false; + this.hub.removeListener("detach", detachListener); this.emit("detach"); } - }); + }; + this.hub.on("detach", detachListener); } public get connected () { @@ -58,4 +60,8 @@ export class Device extends EventEmitter { this.send(Buffer.from([0x41, this.portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01])); } + public receive (message: Buffer) { + this.emit("receive", message); + } + } diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 7adcc86a..d9452227 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -450,7 +450,14 @@ export class LPF2Hub extends Hub { } - private _parseSensorMessage (data: Buffer) { + private _parseSensorMessage (message: Buffer) { + + const portId = message[3]; + const device = this._getDeviceByPortId(portId); + + if (device) { + device.receive(message); + } // if (data[3] === this._voltagePort) { // const voltageRaw = data.readUInt16LE(4); From d382c3d9f24f96427424dcd566fc49e9f141cad5 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sun, 8 Dec 2019 22:06:26 -0800 Subject: [PATCH 05/62] Basic ColorDistanceSensor and TachoMotor handling --- src/colordistancesensor.ts | 54 ++++++++++++++++++++++++++++++++--- src/controlpluslargemotor.ts | 3 +- src/device.ts | 7 ++++- src/tachomotor.ts | 55 ++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 src/tachomotor.ts diff --git a/src/colordistancesensor.ts b/src/colordistancesensor.ts index e1633be1..2d2fc6d8 100644 --- a/src/colordistancesensor.ts +++ b/src/colordistancesensor.ts @@ -12,19 +12,65 @@ export class ColorDistanceSensor extends Device { if (this.autoSubscribe) { switch (event) { case "color": - this.subscribe(0x00); + this.subscribe(0x08); break; case "distance": - this.subscribe(0x01); + this.subscribe(0x08); + break; + case "colorAndDistance": + this.subscribe(0x08); break; } } }); - } public receive (message: Buffer) { - // NK TODO + const mode = this._mode; + + switch (mode) { + case 0x08: + /** + * Emits when a color sensor is activated. + * @event ColorDistanceSensor#color + * @param {string} port + * @param {Color} color + */ + if (message[4] <= 10) { + const color = message[4]; + this.emit("color", color); + } + + let distance = message[5]; + const partial = message[7]; + + if (partial > 0) { + distance += 1.0 / partial; + } + + distance = Math.floor(distance * 25.4) - 20; + + /** + * Emits when a distance sensor is activated. + * @event ColorDistanceSensor#distance + * @param {string} port + * @param {number} distance Distance, in millimeters. + */ + this.emit("distance", distance); + + /** + * A combined color and distance event, emits when the sensor is activated. + * @event ColorDistanceSensor#colorAndDistance + * @param {string} port + * @param {Color} color + * @param {number} distance Distance, in millimeters. + */ + if (message[4] <= 10) { + const color = message[4]; + this.emit("colorAndDistance", color, distance); + } + break; + } } } diff --git a/src/controlpluslargemotor.ts b/src/controlpluslargemotor.ts index 8fcccecc..66077b75 100644 --- a/src/controlpluslargemotor.ts +++ b/src/controlpluslargemotor.ts @@ -2,8 +2,9 @@ import { BasicMotor } from "./basicmotor"; import { Hub } from "./hub"; import * as Consts from "./consts"; +import { TachoMotor } from "./tachomotor"; -export class ControlPlusLargeMotor extends BasicMotor { +export class ControlPlusLargeMotor extends TachoMotor { constructor (hub: Hub, portId: number) { super(hub, portId, Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR); diff --git a/src/device.ts b/src/device.ts index 1579c747..bbf87608 100644 --- a/src/device.ts +++ b/src/device.ts @@ -7,6 +7,8 @@ export class Device extends EventEmitter { public autoSubscribe: boolean = true; + protected _mode: number = 0x00; + private _hub: Hub; private _portId: number; private _connected: boolean = true; @@ -57,7 +59,10 @@ export class Device extends EventEmitter { } public subscribe (mode: number) { - this.send(Buffer.from([0x41, this.portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01])); + if (mode !== this._mode) { + this._mode = mode; + this.send(Buffer.from([0x41, this.portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01])); + } } public receive (message: Buffer) { diff --git a/src/tachomotor.ts b/src/tachomotor.ts new file mode 100644 index 00000000..32240cd4 --- /dev/null +++ b/src/tachomotor.ts @@ -0,0 +1,55 @@ +import { BasicMotor } from "./basicmotor"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; +import { mapSpeed } from "./utils"; + +export class TachoMotor extends BasicMotor { + + constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { + super(hub, portId, type); + + this.on("newListener", (event) => { + if (this.autoSubscribe) { + switch (event) { + case "rotate": + this.subscribe(0x02); + break; + } + } + }); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case 0x02: + const rotation = message.readInt32LE(4); + /** + * Emits when a rotation sensor is activated. + * @event TachoMotor#rotate + * @param {number} rotation + */ + this.emit("rotate", rotation); + break; + } + } + + /** + * Rotate a motor by a given angle. + * @method TachoMotor#setMotorAngle + * @param {number} angle How much the motor should be rotated (in degrees). + * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. + * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + */ + public rotateByAngle (port: string, angle: number, speed: number = 100) { + return new Promise((resolve) => { + const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + message.writeUInt32LE(angle, 4); + this.send(message); + return resolve(); + }); + } + +} From a02249f6968edf509cfd6bf52f4da52a62100ff1 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sun, 8 Dec 2019 22:57:44 -0800 Subject: [PATCH 06/62] Implemented basic lights, device busy-ness --- examples/new_device_test.js | 22 +++++++++-- examples/winter_village_train.js | 60 +++++++++++++++++++++++++++++ src/basicmotor.ts | 10 ++--- src/boostmovehub.ts | 4 +- src/controlplushub.ts | 4 +- src/device.ts | 12 +++++- src/lights.ts | 29 ++++++++++++++ src/lpf2hub.ts | 65 ++++++++++++++++---------------- src/puphub.ts | 4 +- src/pupremote.ts | 4 +- src/tachomotor.ts | 11 ++++-- src/utils.ts | 9 +++++ src/wedo2smarthub.ts | 15 +++++--- 13 files changed, 189 insertions(+), 60 deletions(-) create mode 100644 examples/winter_village_train.js create mode 100644 src/lights.ts diff --git a/examples/new_device_test.js b/examples/new_device_test.js index 3eb8cf8c..c291333f 100644 --- a/examples/new_device_test.js +++ b/examples/new_device_test.js @@ -16,22 +16,36 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs await hub.connect(); // Connect to hub console.log(`Connected to ${hub.name}!`); - hub.on("attach", (device) => { + hub.on("attach", async (device) => { + + console.log(`Attached device ${device.type} to ${device.port}`) if (device instanceof PoweredUP.ControlPlusLargeMotor) { const motor = device; - motor.setSpeed(30); + + motor.on("rotate", (angle) => { + console.log(`Rotate ${angle}`); + }); + + await motor.rotateByAngle(9000, 50); + await motor.rotateByAngle(9000, -50); + await motor.rotateByAngle(9000, 50); + await motor.rotateByAngle(9000, -50); + motor.power(100); } if (device instanceof PoweredUP.ColorDistanceSensor) { const sensor = device; sensor.on("distance", (distance) => { // Adding an event handler for distance automatically subscribes to distance notifications console.log(`Distance ${distance}`); - }) + }); + sensor.on("color", (color) => { + console.log(`Color ${color}`); + }); } device.on("detach", () => { - console.log(device.connected); + console.log(`Detached device ${device.type} from ${device.port}`) }) }); diff --git a/examples/winter_village_train.js b/examples/winter_village_train.js new file mode 100644 index 00000000..7e27f403 --- /dev/null +++ b/examples/winter_village_train.js @@ -0,0 +1,60 @@ +/* + * + * This runs the train under our Christmas tree. It uses the 10254 Winter Holiday Train retrofitted with a Powered UP hub and train motor. + * It also uses a WeDo 2.0 hub with Powered UP distance sensor to detect the train approaching the station and slow it down. + * + * Note that if you want to use this yourself you don't need to use a WeDo 2.0 hub, you can use any hub that can accept a distance or color/distance sensor. + * + * The maximum speed of the train is set to a constant 50. A further improvement can be made by scaling the speed up according to the current battery voltage, + * so the speed doesn't slow as the battery starts dying. + * + */ + +const PoweredUP = require(".."); + +const poweredUP = new PoweredUP.PoweredUP(); +poweredUP.scan(); // Start scanning for hubs + +console.log("Looking for Hubs..."); + +let train = null; +let sensor = null; + +let ramping = false; + +poweredUP.on("discover", async (hub) => { // Wait to discover hubs + + if (hub.name === "NK_Winter_Train") { + await hub.connect(); // Connect to hub + console.log(`Connected to train!`); + train = hub; + } else if (hub.name === "NK_Winter_Sensor") { + await hub.connect(); // Connect to hub + console.log(`Connected to sensor!`); + sensor = hub; + + sensor.on("distance", (_, distance) => { + if (distance < 5 && !ramping) { + await stopTrain(); + } + }); + + } + + if (train && sensor) { + console.log("Train and sensor connected, starting!"); + await startTrain(); + } + +}); + +const startTrain = async () => { + ramping = true; + await train.rampMotorSpeed("A", 0, 50, 2000); + ramping = false; +} + +const stopTrain = async () => { + ramping = true; + await train.rampMotorSpeed("A", 50, 0, 2000); +} \ No newline at end of file diff --git a/src/basicmotor.ts b/src/basicmotor.ts index b0b3a93b..4330416a 100644 --- a/src/basicmotor.ts +++ b/src/basicmotor.ts @@ -15,13 +15,13 @@ export class BasicMotor extends Device { /** * Set the motor speed. - * @method BasicMotor#setSpeed - * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + * @method BasicMotor#power + * @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. * @returns {Promise} Resolved upon successful completion of command. */ - public setSpeed (speed: number) { + public power (power: number) { return new Promise((resolve) => { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(speed)]); + const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]); this.send(data); return resolve(); }); @@ -34,7 +34,7 @@ export class BasicMotor extends Device { * @returns {Promise} Resolved upon successful completion of command. */ public brake () { - return this.setSpeed(127); + return this.power(127); } diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index e363eb54..e36eee37 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -1,13 +1,13 @@ import { Peripheral } from "@abandonware/noble"; import compareVersion from "compare-versions"; +import { IBLEAbstraction } from "./interfaces"; + import { LPF2Hub } from "./lpf2hub"; -import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEAbstraction } from "./interfaces"; const debug = Debug("boostmovehub"); diff --git a/src/controlplushub.ts b/src/controlplushub.ts index 4d0a7b9d..8d8cdafd 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -1,12 +1,12 @@ import { Peripheral } from "@abandonware/noble"; +import { IBLEAbstraction } from "./interfaces"; + import { LPF2Hub } from "./lpf2hub"; -import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEAbstraction } from "./interfaces"; const debug = Debug("ControlPlusHub"); diff --git a/src/device.ts b/src/device.ts index bbf87608..b49e5299 100644 --- a/src/device.ts +++ b/src/device.ts @@ -8,13 +8,13 @@ export class Device extends EventEmitter { public autoSubscribe: boolean = true; protected _mode: number = 0x00; + protected _busy: boolean = false; + protected _finished: (() => void) | undefined; private _hub: Hub; private _portId: number; private _connected: boolean = true; private _type: number; - private _busy: boolean = false; - private _finished: (() => void) | null = null; constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { super(); @@ -69,4 +69,12 @@ export class Device extends EventEmitter { this.emit("receive", message); } + public finish () { + this._busy = false; + if (this._finished) { + this._finished(); + this._finished = undefined; + } + } + } diff --git a/src/lights.ts b/src/lights.ts new file mode 100644 index 00000000..411d8643 --- /dev/null +++ b/src/lights.ts @@ -0,0 +1,29 @@ +import { Device } from "./device"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class Lights extends Device { + + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.LED_LIGHTS); + } + + + /** + * Set the light brightness. + * @method Light#brightness + * @param {number} brightness Brightness value between 0-100 (0 is off) + * @returns {Promise} Resolved upon successful completion of command. + */ + public brightness (brightness: number) { + return new Promise((resolve) => { + const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); + this.send(data); + return resolve(); + }); + } + + +} diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index d9452227..8f569ee3 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -1,12 +1,13 @@ import { Device } from "./device"; import { Hub } from "./hub"; -import { Port } from "./port"; import { ColorDistanceSensor } from "./colordistancesensor"; import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { Lights } from "./lights"; import * as Consts from "./consts"; -import { toBin, toHex } from "./utils"; + +import { decodeMACAddress, decodeVersion, toBin, toHex } from "./utils"; import Debug = require("debug"); const debug = Debug("lpf2hub"); @@ -18,14 +19,6 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo"); * @extends Hub */ export class LPF2Hub extends Hub { - private static decodeVersion(v: number) { - const t = v.toString(16).padStart(8, "0"); - return [t[0], t[1], t.substring(2, 4), t.substring(4)].join("."); - } - - private static decodeMACAddress(v: Uint8Array) { - return Array.from(v).map((n) => toHex(n, 2)).join(":"); - } protected _ledPort: number = 0x32; protected _voltagePort: number | undefined; @@ -265,12 +258,12 @@ export class LPF2Hub extends Hub { // Firmware version } else if (data[3] === 0x03) { - this._firmwareVersion = LPF2Hub.decodeVersion(data.readInt32LE(5)); + this._firmwareVersion = decodeVersion(data.readInt32LE(5)); this._checkFirmware(this._firmwareVersion); // Hardware version } else if (data[3] === 0x04) { - this._hardwareVersion = LPF2Hub.decodeVersion(data.readInt32LE(5)); + this._hardwareVersion = decodeVersion(data.readInt32LE(5)); // RSSI update } else if (data[3] === 0x05) { @@ -282,7 +275,7 @@ export class LPF2Hub extends Hub { // primary MAC Address } else if (data[3] === 0x0d) { - this._primaryMACAddress = LPF2Hub.decodeMACAddress(data.slice(5)); + this._primaryMACAddress = decodeMACAddress(data.slice(5)); // Battery level reports } else if (data[3] === 0x06) { @@ -291,11 +284,11 @@ export class LPF2Hub extends Hub { } - private _parsePortMessage (data: Buffer) { + private _parsePortMessage (message: Buffer) { - const portId = data[3]; - const event = data[4]; - const deviceType = event ? data.readUInt16LE(5) : 0; + const portId = message[3]; + const event = message[4]; + const deviceType = event ? message.readUInt16LE(5) : 0; // Handle device attachments if (event === 0x01) { @@ -303,6 +296,9 @@ export class LPF2Hub extends Hub { let device; switch (deviceType) { + case Consts.DeviceType.LED_LIGHTS: + device = new Lights(this, portId); + break; case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: device = new ControlPlusLargeMotor(this, portId); break; @@ -314,6 +310,15 @@ export class LPF2Hub extends Hub { break; } + if (modeInfoDebug.enabled) { + const deviceTypeName = Consts.DeviceTypeNames[message[5]] || "Unknown"; + modeInfoDebug(`Port ${toHex(portId)}, type ${toHex(deviceType, 4)} (${deviceTypeName})`); + const hwVersion = decodeVersion(message.readInt32LE(7)); + const swVersion = decodeVersion(message.readInt32LE(11)); + modeInfoDebug(`Port ${toHex(portId)}, hardware version ${hwVersion}, software version ${swVersion}`); + this._sendPortInformationRequest(portId); + } + this._attachDevice(device); // Handle device detachments @@ -324,19 +329,8 @@ export class LPF2Hub extends Hub { } } - - // let port = this._getPortForPortNumber(data[3]); - // if (data[4] === 0x01 && modeInfoDebug.enabled) { - // const typeName = Consts.DeviceTypeNames[data[5]] || "unknown"; - // modeInfoDebug(`Port ${toHex(data[3])}, type ${toHex(deviceType, 4)} (${typeName})`); - // const hwVersion = LPF2Hub.decodeVersion(data.readInt32LE(7)); - // const swVersion = LPF2Hub.decodeVersion(data.readInt32LE(11)); - // modeInfoDebug(`Port ${toHex(data[3])}, hardware version ${hwVersion}, software version ${swVersion}`); - // this._sendPortInformationRequest(data[3]); - // } - // if (!port) { // if (data[4] === 0x02) { // const portA = this._getPortForPortNumber(data[7]); @@ -356,9 +350,6 @@ export class LPF2Hub extends Hub { // } else { // return; // } - // } else { - // port.connected = (data[4] === 0x01 || data[4] === 0x02) ? true : false; - // this._registerDeviceAttachment(port, deviceType); // } } @@ -431,7 +422,17 @@ export class LPF2Hub extends Hub { } - private _parsePortAction (data: Buffer) { + private _parsePortAction (message: Buffer) { + + const portId = message[3]; + const device = this._getDeviceByPortId(portId); + + if (device) { + const finished = (message[4] === 0x0a); + if (finished) { + device.finish(); + } + } // const port = this._getPortForPortNumber(data[3]); diff --git a/src/puphub.ts b/src/puphub.ts index 8902bf94..4c916431 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -1,13 +1,13 @@ import { Peripheral } from "@abandonware/noble"; import compareVersion from "compare-versions"; +import { IBLEAbstraction } from "./interfaces"; + import { LPF2Hub } from "./lpf2hub"; -import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEAbstraction } from "./interfaces"; const debug = Debug("puphub"); diff --git a/src/pupremote.ts b/src/pupremote.ts index 2a25bb11..3f8b3721 100644 --- a/src/pupremote.ts +++ b/src/pupremote.ts @@ -1,12 +1,12 @@ import { Peripheral } from "@abandonware/noble"; +import { IBLEAbstraction } from "./interfaces"; + import { LPF2Hub } from "./lpf2hub"; -import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { IBLEAbstraction } from "./interfaces"; const debug = Debug("pupremote"); diff --git a/src/tachomotor.ts b/src/tachomotor.ts index 32240cd4..624a1e7b 100644 --- a/src/tachomotor.ts +++ b/src/tachomotor.ts @@ -40,15 +40,18 @@ export class TachoMotor extends BasicMotor { * Rotate a motor by a given angle. * @method TachoMotor#setMotorAngle * @param {number} angle How much the motor should be rotated (in degrees). - * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. + * @param {number} [power=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public rotateByAngle (port: string, angle: number, speed: number = 100) { + public rotateByAngle (angle: number, power: number = 100) { return new Promise((resolve) => { - const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + this._busy = true; + const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(power), 0x64, 0x7f, 0x03]); message.writeUInt32LE(angle, 4); this.send(message); - return resolve(); + this._finished = () => { + return resolve(); + }; }); } diff --git a/src/utils.ts b/src/utils.ts index e4632148..4bff8d57 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -20,3 +20,12 @@ export const mapSpeed = (speed: number) => { } return speed; }; + +export const decodeVersion = (version: number) => { + const parts = version.toString(16).padStart(8, "0"); + return [parts[0], parts[1], parts.substring(2, 4), parts.substring(4)].join("."); +}; + +export const decodeMACAddress = (address: Uint8Array) => { + return Array.from(address).map((part) => toHex(part, 2)).join(":"); +}; diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index 4142e362..bd6ba7e4 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -1,17 +1,19 @@ import { Peripheral } from "@abandonware/noble"; +import { IBLEAbstraction } from "./interfaces"; + import { Device } from "./device"; import { Hub } from "./hub"; -import { Port } from "./port"; import { ColorDistanceSensor } from "./colordistancesensor"; import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { Lights } from "./lights"; import * as Consts from "./consts"; -import Debug = require("debug"); -import { IBLEAbstraction } from "./interfaces"; import { isWebBluetooth } from "./utils"; + +import Debug = require("debug"); const debug = Debug("wedo2smarthub"); @@ -350,6 +352,9 @@ export class WeDo2SmartHub extends Hub { let device; switch (deviceType) { + case Consts.DeviceType.LED_LIGHTS: + device = new Lights(this, portId); + break; case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: device = new ControlPlusLargeMotor(this, portId); break; @@ -357,9 +362,9 @@ export class WeDo2SmartHub extends Hub { device = new ColorDistanceSensor(this, portId); break; default: - device = new Device(this, portId); + device = new Device(this, portId, deviceType); break; - } + } this._attachDevice(device); From f9cea9a6bf40d3f51efacaf17fb397158a7e47e5 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sun, 8 Dec 2019 22:59:25 -0800 Subject: [PATCH 07/62] Yay, port object is no longer a thing --- src/duplotrainbase.ts | 1 - src/hub.ts | 1 - src/port.ts | 33 --------------------------------- 3 files changed, 35 deletions(-) delete mode 100644 src/port.ts diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index 596cbd32..fd24fc3e 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -1,7 +1,6 @@ import { Peripheral } from "@abandonware/noble"; import { LPF2Hub } from "./lpf2hub"; -import { Port } from "./port"; import * as Consts from "./consts"; diff --git a/src/hub.ts b/src/hub.ts index 36fe7359..76899f74 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -1,7 +1,6 @@ import { EventEmitter } from "events"; import { IBLEAbstraction } from "./interfaces"; -import { Port } from "./port"; import * as Consts from "./consts"; diff --git a/src/port.ts b/src/port.ts deleted file mode 100644 index 0a279cb2..00000000 --- a/src/port.ts +++ /dev/null @@ -1,33 +0,0 @@ -import * as Consts from "./consts"; - - -export class Port { - - - public id: string; - public value: number; - public type: Consts.DeviceType; - public connected: boolean = false; - public busy: boolean = false; - public finished: (() => void) | null = null; - - private _eventTimer: NodeJS.Timer | null = null; - - constructor (id: string, value: number) { - this.id = id; - this.value = value; - this.type = Consts.DeviceType.UNKNOWN; - } - - public cancelEventTimer () { - if (this._eventTimer) { - clearTimeout(this._eventTimer); - this._eventTimer = null; - } - } - - public setEventTimer (timer: NodeJS.Timer) { - this._eventTimer = timer; - } - -} From 7fdc754084eeb9dc3024d36cc8bc7680d86207fe Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 10 Dec 2019 07:23:33 +0900 Subject: [PATCH 08/62] Start of hub refactor --- src/basicmotor.ts | 2 +- src/boostmovehub.ts | 5 +- src/controlplushub.ts | 5 +- src/device.ts | 6 +- src/duplotrainbase.ts | 5 +- src/hub.ts | 149 ++------------------------------------- src/lpf2hub.ts | 93 +++++++++++------------- src/poweredup-browser.ts | 15 ++-- src/poweredup-node.ts | 15 ++-- src/puphub.ts | 5 +- src/pupremote.ts | 5 +- src/tachomotor.ts | 2 +- src/wedo2smarthub.ts | 5 +- 13 files changed, 78 insertions(+), 234 deletions(-) diff --git a/src/basicmotor.ts b/src/basicmotor.ts index 4330416a..288387c7 100644 --- a/src/basicmotor.ts +++ b/src/basicmotor.ts @@ -8,7 +8,7 @@ import { mapSpeed } from "./utils"; export class BasicMotor extends Device { - constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { + constructor (hub: Hub, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(hub, portId, type); } diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index e36eee37..013b9c9d 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -34,9 +34,8 @@ export class BoostMoveHub extends LPF2Hub { protected _currentPort = 0x3b; protected _voltagePort = 0x3c; - constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { - super(device, autoSubscribe); - this._type = Consts.HubType.BOOST_MOVE_HUB; + constructor (device: IBLEAbstraction) { + super(device, Consts.HubType.BOOST_MOVE_HUB); this._portNames = { "A": 0, "B": 1, diff --git a/src/controlplushub.ts b/src/controlplushub.ts index 8d8cdafd..c23ea516 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -36,9 +36,8 @@ export class ControlPlusHub extends LPF2Hub { protected _voltageMaxRaw = 4095; protected _voltageMaxV = 9.615; - constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { - super(device, autoSubscribe); - this._type = Consts.HubType.CONTROL_PLUS_HUB; + constructor (device: IBLEAbstraction) { + super(device, Consts.HubType.CONTROL_PLUS_HUB); this._portNames = { "A": 0, "B": 1, diff --git a/src/device.ts b/src/device.ts index b49e5299..86dd1ec1 100644 --- a/src/device.ts +++ b/src/device.ts @@ -14,9 +14,9 @@ export class Device extends EventEmitter { private _hub: Hub; private _portId: number; private _connected: boolean = true; - private _type: number; + private _type: Consts.DeviceType; - constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { + constructor (hub: Hub, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(); this._hub = hub; this._portId = portId; @@ -61,7 +61,7 @@ export class Device extends EventEmitter { public subscribe (mode: number) { if (mode !== this._mode) { this._mode = mode; - this.send(Buffer.from([0x41, this.portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01])); + this.hub.subscribe(this.portId, mode); } } diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index fd24fc3e..ece154bc 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -35,9 +35,8 @@ export class DuploTrainBase extends LPF2Hub { protected _voltageMaxV = 6.4; protected _voltageMaxRaw = 3047; - constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { - super(device, autoSubscribe); - this._type = Consts.HubType.DUPLO_TRAIN_HUB; + constructor (device: IBLEAbstraction) { + super(device, Consts.HubType.DUPLO_TRAIN_HUB); this._portNames = { "MOTOR": 0, "COLOR": 18, diff --git a/src/hub.ts b/src/hub.ts index 76899f74..dfdd4b47 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -15,10 +15,7 @@ const debug = Debug("hub"); */ export class Hub extends EventEmitter { - - public autoSubscribe: boolean = true; public useSpeedMap: boolean = true; - protected _type: Consts.HubType = Consts.HubType.UNKNOWN; protected _attachedDevices: {[portId: number]: Device} = {}; @@ -39,9 +36,11 @@ export class Hub extends EventEmitter { private _isConnecting = false; private _isConnected = false; - constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { + private _type: Consts.HubType; + + constructor (device: IBLEAbstraction, type: Consts.HubType = Consts.HubType.UNKNOWN) { super(); - this.autoSubscribe = !!autoSubscribe; + this._type = type; this._bleDevice = device; device.on("disconnect", () => { /** @@ -183,41 +182,6 @@ export class Hub extends EventEmitter { } - // /** - // * Subscribe to sensor notifications on a given port. - // * @method Hub#subscribe - // * @param {string} port - // * @param {number} [mode] The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen. - // * @returns {Promise} Resolved upon successful issuance of command. - // */ - // public subscribe (port: string, mode?: number) { - // return new Promise((resolve, reject) => { - // let newMode = this._getModeForDeviceType(this._portLookup(port).type); - // if (mode !== undefined) { - // newMode = mode; - // } - // this._activatePortDevice(this._portLookup(port).value, this._portLookup(port).type, newMode, 0x00, () => { - // return resolve(); - // }); - // }); - // } - - // /** - // * Unsubscribe to sensor notifications on a given port. - // * @method Hub#unsubscribe - // * @param {string} port - // * @returns {Promise} Resolved upon successful issuance of command. - // */ - // public unsubscribe (port: string) { - // return new Promise((resolve, reject) => { - // const mode = this._getModeForDeviceType(this._portLookup(port).type); - // this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => { - // return resolve(); - // }); - // }); - // } - - /** * Sleep a given amount of time. * @@ -246,17 +210,6 @@ export class Hub extends EventEmitter { } - // /** - // * Get the device type for a given port. - // * @method Hub#getPortDeviceType - // * @param {string} port - // * @returns {DeviceType} - // */ - // public getPortDeviceType (port: string) { - // return this._portLookup(port).type; - // } - - public send (message: Buffer, uuid: string, callback?: () => void) { if (callback) { callback(); @@ -264,60 +217,19 @@ export class Hub extends EventEmitter { } - // protected _getCharacteristic (uuid: string) { - // return this._characteristics[uuid.replace(/-/g, "")]; - // } - - - // protected _subscribeToCharacteristic (characteristic: Characteristic, callback: (data: Buffer) => void) { - // characteristic.on("data", (data: Buffer) => { - // return callback(data); - // }); - // characteristic.subscribe((err) => { - // if (err) { - // this.emit("error", err); - // } - // }); - // } + public subscribe (portId: number, mode: number) { + // NK Do nothing here + } protected _attachDevice (device: Device) { - this._attachedDevices[device.portId] = device; - /** * Emits when a device is attached to the Hub. * @event Hub#attach * @param {Device} device */ this.emit("attach", device); - - // if (port.connected) { - // port.type = type; - // if (this.autoSubscribe) { - // this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00); - // } - // /** - // * Emits when a motor or sensor is attached to the Hub. - // * @event Hub#attach - // * @param {string} port - // * @param {DeviceType} type - // */ - // this.emit("attach", port.id, type); - // } else { - // port.type = Consts.DeviceType.UNKNOWN; - // debug(`Port ${port.id} disconnected`); - // /** - // * Emits when an attached motor or sensor is detached from the Hub. - // * @event Hub#detach - // * @param {string} port - // */ - // if (this._virtualPorts[port.id]) { - // delete this._virtualPorts[port.id]; - // } - // this.emit("detach", port.id); - // } - } @@ -337,44 +249,6 @@ export class Hub extends EventEmitter { } - // protected _getPortForPortNumber (num: number) { - - // for (const key of Object.keys(this._ports)) { - // if (this._ports[key].value === num) { - // return this._ports[key]; - // } - // } - - // for (const key of Object.keys(this._virtualPorts)) { - // if (this._virtualPorts[key].value === num) { - // return this._virtualPorts[key]; - // } - // } - - // return false; - - // } - - - // protected _mapSpeed (speed: number) { // Speed range of -100 to 100 is supported unless speed mapping is turned off, in which case, you're on your own! - // if (!this.useSpeedMap) { - // return speed; - // } - - // if (speed === 127) { - // return 127; // Hard stop - // } - - // if (speed > 100) { - // speed = 100; - // } else if (speed < -100) { - // speed = -100; - // } - - // return speed; - // } - - // protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number, port: Port) { // const emitter = new EventEmitter(); // const steps = Math.abs(toSpeed - fromSpeed); @@ -406,15 +280,6 @@ export class Hub extends EventEmitter { // } - // protected _portLookup (portName: string) { - // const portNameUpper = portName.toUpperCase(); - // const port = this._ports[portNameUpper] || this._virtualPorts[portNameUpper]; - // if (!port) { - // throw new Error(`Port ${portNameUpper} does not exist on this Hub type`); - // } - // return port; - // } - // private _getModeForDeviceType (type: Consts.DeviceType) { // switch (type) { // case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 8f569ee3..b596585f 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -46,7 +46,7 @@ export class LPF2Hub extends Hub { if (this._currentPort !== undefined) { this.send(Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Activate current reports } - if (this._type === Consts.HubType.DUPLO_TRAIN_HUB) { + if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { this.send(Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); } await this.sleep(100); @@ -137,15 +137,6 @@ export class LPF2Hub extends Hub { } - public sendRaw (message: Buffer) { - return new Promise((resolve, reject) => { - this.send(message, Consts.BLECharacteristic.LPF2_ALL, () => { - return resolve(); - }); - }); - } - - public send (message: Buffer, uuid: string, callback?: () => void) { message = Buffer.concat([Buffer.alloc(2), message]); message[0] = message.length; @@ -154,14 +145,14 @@ export class LPF2Hub extends Hub { } - // protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - // this.send(Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL, callback); - // } + public subscribe (portId: number, mode: number) { + this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); + } - // protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - // this.send(Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.LPF2_ALL, callback); - // } + public unsubscribe (portId: number, mode: number) { + this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.LPF2_ALL); + } // protected _combinePorts (port: string, type: number) { @@ -238,11 +229,11 @@ export class LPF2Hub extends Hub { } - private _parseDeviceInfo (data: Buffer) { + private _parseDeviceInfo (message: Buffer) { // Button press reports - if (data[3] === 0x02) { - if (data[5] === 1) { + if (message[3] === 0x02) { + if (message[5] === 1) { /** * Emits when a button is pressed. * @event LPF2Hub#button @@ -251,35 +242,35 @@ export class LPF2Hub extends Hub { */ this.emit("button", "GREEN", Consts.ButtonState.PRESSED); return; - } else if (data[5] === 0) { + } else if (message[5] === 0) { this.emit("button", "GREEN", Consts.ButtonState.RELEASED); return; } // Firmware version - } else if (data[3] === 0x03) { - this._firmwareVersion = decodeVersion(data.readInt32LE(5)); + } else if (message[3] === 0x03) { + this._firmwareVersion = decodeVersion(message.readInt32LE(5)); this._checkFirmware(this._firmwareVersion); // Hardware version - } else if (data[3] === 0x04) { - this._hardwareVersion = decodeVersion(data.readInt32LE(5)); + } else if (message[3] === 0x04) { + this._hardwareVersion = decodeVersion(message.readInt32LE(5)); // RSSI update - } else if (data[3] === 0x05) { - const rssi = data.readInt8(5); + } else if (message[3] === 0x05) { + const rssi = message.readInt8(5); if (rssi !== 0) { this._rssi = rssi; this.emit("rssiChange", this._rssi); } // primary MAC Address - } else if (data[3] === 0x0d) { - this._primaryMACAddress = decodeMACAddress(data.slice(5)); + } else if (message[3] === 0x0d) { + this._primaryMACAddress = decodeMACAddress(message.slice(5)); // Battery level reports - } else if (data[3] === 0x06) { - this._batteryLevel = data[5]; + } else if (message[3] === 0x06) { + this._batteryLevel = message[5]; } } @@ -361,19 +352,19 @@ export class LPF2Hub extends Hub { } - private _parsePortInformationResponse (data: Buffer) { - const port = data[3]; - if (data[4] === 2) { + private _parsePortInformationResponse (message: Buffer) { + const port = message[3]; + if (message[4] === 2) { const modeCombinationMasks: number[] = []; - for (let i = 5; i < data.length; i += 2) { - modeCombinationMasks.push(data.readUInt16LE(i)); + for (let i = 5; i < message.length; i += 2) { + modeCombinationMasks.push(message.readUInt16LE(i)); } modeInfoDebug(`Port ${toHex(port)}, mode combinations [${modeCombinationMasks.map((c) => toBin(c, 0)).join(", ")}]`); return; } - const count = data[6]; - const input = toBin(data.readUInt16LE(7), count); - const output = toBin(data.readUInt16LE(9), count); + const count = message[6]; + const input = toBin(message.readUInt16LE(7), count); + const output = toBin(message.readUInt16LE(9), count); modeInfoDebug(`Port ${toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`); for (let i = 0; i < count; i++) { @@ -392,31 +383,31 @@ export class LPF2Hub extends Hub { } - private _parseModeInformationResponse (data: Buffer) { - const port = toHex(data[3]); - const mode = data[4]; - const type = data[5]; + private _parseModeInformationResponse (message: Buffer) { + const port = toHex(message[3]); + const mode = message[4]; + const type = message[5]; switch (type) { case 0x00: // Mode Name - modeInfoDebug(`Port ${port}, mode ${mode}, name ${data.slice(6, data.length).toString()}`); + modeInfoDebug(`Port ${port}, mode ${mode}, name ${message.slice(6, message.length).toString()}`); break; case 0x01: // RAW Range - modeInfoDebug(`Port ${port}, mode ${mode}, RAW min ${data.readFloatLE(6)}, max ${data.readFloatLE(10)}`); + modeInfoDebug(`Port ${port}, mode ${mode}, RAW min ${message.readFloatLE(6)}, max ${message.readFloatLE(10)}`); break; case 0x02: // PCT Range - modeInfoDebug(`Port ${port}, mode ${mode}, PCT min ${data.readFloatLE(6)}, max ${data.readFloatLE(10)}`); + modeInfoDebug(`Port ${port}, mode ${mode}, PCT min ${message.readFloatLE(6)}, max ${message.readFloatLE(10)}`); break; case 0x03: // SI Range - modeInfoDebug(`Port ${port}, mode ${mode}, SI min ${data.readFloatLE(6)}, max ${data.readFloatLE(10)}`); + modeInfoDebug(`Port ${port}, mode ${mode}, SI min ${message.readFloatLE(6)}, max ${message.readFloatLE(10)}`); break; case 0x04: // SI Symbol - modeInfoDebug(`Port ${port}, mode ${mode}, SI symbol ${data.slice(6, data.length).toString()}`); + modeInfoDebug(`Port ${port}, mode ${mode}, SI symbol ${message.slice(6, message.length).toString()}`); break; case 0x80: // Value Format - const numValues = data[6]; - const dataType = ["8bit", "16bit", "32bit", "float"][data[7]]; - const totalFigures = data[8]; - const decimals = data[9]; + const numValues = message[6]; + const dataType = ["8bit", "16bit", "32bit", "float"][message[7]]; + const totalFigures = message[8]; + const decimals = message[9]; modeInfoDebug(`Port ${port}, mode ${mode}, Value ${numValues} x ${dataType}, Decimal format ${totalFigures}.${decimals}`); } } diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 67f4140d..5fab9be7 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -23,9 +23,6 @@ const debug = Debug("poweredup"); export class PoweredUP extends EventEmitter { - public autoSubscribe: boolean = true; - - private _connectedHubs: {[uuid: string]: Hub} = {}; @@ -184,22 +181,22 @@ export class PoweredUP extends EventEmitter { switch (hubType) { case Consts.HubType.WEDO2_SMART_HUB: - hub = new WeDo2SmartHub(device, this.autoSubscribe); + hub = new WeDo2SmartHub(device); break; case Consts.HubType.BOOST_MOVE_HUB: - hub = new BoostMoveHub(device, this.autoSubscribe); + hub = new BoostMoveHub(device); break; case Consts.HubType.POWERED_UP_HUB: - hub = new PUPHub(device, this.autoSubscribe); + hub = new PUPHub(device); break; case Consts.HubType.POWERED_UP_REMOTE: - hub = new PUPRemote(device, this.autoSubscribe); + hub = new PUPRemote(device); break; case Consts.HubType.DUPLO_TRAIN_HUB: - hub = new DuploTrainBase(device, this.autoSubscribe); + hub = new DuploTrainBase(device); break; case Consts.HubType.CONTROL_PLUS_HUB: - hub = new ControlPlusHub(device, this.autoSubscribe); + hub = new ControlPlusHub(device); break; default: return; diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index e9a8d821..88a9eb39 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -44,9 +44,6 @@ noble.on("stateChange", (state: string) => { export class PoweredUP extends EventEmitter { - public autoSubscribe: boolean = true; - - private _connectedHubs: {[uuid: string]: Hub} = {}; @@ -144,17 +141,17 @@ export class PoweredUP extends EventEmitter { let hub: Hub; if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { - hub = new WeDo2SmartHub(device, this.autoSubscribe); + hub = new WeDo2SmartHub(device); } else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) { - hub = new BoostMoveHub(device, this.autoSubscribe); + hub = new BoostMoveHub(device); } else if (await PUPHub.IsPUPHub(peripheral)) { - hub = new PUPHub(device, this.autoSubscribe); + hub = new PUPHub(device); } else if (await PUPRemote.IsPUPRemote(peripheral)) { - hub = new PUPRemote(device, this.autoSubscribe); + hub = new PUPRemote(device); } else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) { - hub = new DuploTrainBase(device, this.autoSubscribe); + hub = new DuploTrainBase(device); } else if (await ControlPlusHub.IsControlPlusHub(peripheral)) { - hub = new ControlPlusHub(device, this.autoSubscribe); + hub = new ControlPlusHub(device); } else { return; } diff --git a/src/puphub.ts b/src/puphub.ts index 4c916431..21349989 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -34,9 +34,8 @@ export class PUPHub extends LPF2Hub { protected _currentPort = 0x3b; protected _voltagePort = 0x3c; - constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { - super(device, autoSubscribe); - this._type = Consts.HubType.POWERED_UP_HUB; + constructor (device: IBLEAbstraction) { + super(device, Consts.HubType.POWERED_UP_HUB); this._portNames = { "A": 0, "B": 1 diff --git a/src/pupremote.ts b/src/pupremote.ts index 3f8b3721..24886c75 100644 --- a/src/pupremote.ts +++ b/src/pupremote.ts @@ -37,9 +37,8 @@ export class PUPRemote extends LPF2Hub { protected _voltageMaxRaw = 3200; - constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { - super(device, autoSubscribe); - this._type = Consts.HubType.POWERED_UP_REMOTE; + constructor (device: IBLEAbstraction) { + super(device, Consts.HubType.POWERED_UP_REMOTE); this._portNames = { "LEFT": 0, "RIGHT": 1 diff --git a/src/tachomotor.ts b/src/tachomotor.ts index 624a1e7b..f12fd895 100644 --- a/src/tachomotor.ts +++ b/src/tachomotor.ts @@ -6,7 +6,7 @@ import { mapSpeed } from "./utils"; export class TachoMotor extends BasicMotor { - constructor (hub: Hub, portId: number, type: number = Consts.DeviceType.UNKNOWN) { + constructor (hub: Hub, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(hub, portId, type); this.on("newListener", (event) => { diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index bd6ba7e4..1b22944b 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -38,9 +38,8 @@ export class WeDo2SmartHub extends Hub { private _lastTiltY: number = 0; - constructor (device: IBLEAbstraction, autoSubscribe: boolean = true) { - super(device, autoSubscribe); - this._type = Consts.HubType.WEDO2_SMART_HUB; + constructor (device: IBLEAbstraction) { + super(device, Consts.HubType.WEDO2_SMART_HUB); this._portNames = { "A": 1, "B": 2 From 5c03e5c3b65cdf96acfb4b54289bcf4cc226de76 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 10 Dec 2019 08:15:21 +0900 Subject: [PATCH 09/62] More refactoring --- examples/new_device_test.js | 8 +-- src/basicmotor.ts | 14 +++-- src/device.ts | 11 +++- src/hub.ts | 4 -- src/lights.ts | 12 +++- src/lpf2hub.ts | 6 +- src/wedo2smarthub.ts | 120 +++++++----------------------------- 7 files changed, 55 insertions(+), 120 deletions(-) diff --git a/examples/new_device_test.js b/examples/new_device_test.js index c291333f..91a36ec2 100644 --- a/examples/new_device_test.js +++ b/examples/new_device_test.js @@ -20,6 +20,10 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs console.log(`Attached device ${device.type} to ${device.port}`) + device.on("detach", () => { + console.log(`Detached device ${device.type} from ${device.port}`); + }); + if (device instanceof PoweredUP.ControlPlusLargeMotor) { const motor = device; @@ -44,10 +48,6 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs }); } - device.on("detach", () => { - console.log(`Detached device ${device.type} from ${device.port}`) - }) - }); hub.on("disconnect", () => { diff --git a/src/basicmotor.ts b/src/basicmotor.ts index 288387c7..fc06356a 100644 --- a/src/basicmotor.ts +++ b/src/basicmotor.ts @@ -1,5 +1,6 @@ import { Device } from "./device"; import { Hub } from "./hub"; +import { WeDo2SmartHub } from "./wedo2smarthub"; import * as Consts from "./consts"; @@ -19,10 +20,15 @@ export class BasicMotor extends Device { * @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. * @returns {Promise} Resolved upon successful completion of command. */ - public power (power: number) { + public setPower (power: number) { return new Promise((resolve) => { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]); - this.send(data); + if (this.hub instanceof WeDo2SmartHub) { + const data = Buffer.from([this.portId, 0x01, 0x02, mapSpeed(power)]); + this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + } else { + const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]); + this.send(data); + } return resolve(); }); } @@ -34,7 +40,7 @@ export class BasicMotor extends Device { * @returns {Promise} Resolved upon successful completion of command. */ public brake () { - return this.power(127); + return this.setPower(127); } diff --git a/src/device.ts b/src/device.ts index 86dd1ec1..7494a751 100644 --- a/src/device.ts +++ b/src/device.ts @@ -52,13 +52,12 @@ export class Device extends EventEmitter { } public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { - if (!this.connected) { - throw new Error("Device is not connected"); - } + this._ensureConnected(); this.hub.send(data, characteristic, callback); } public subscribe (mode: number) { + this._ensureConnected(); if (mode !== this._mode) { this._mode = mode; this.hub.subscribe(this.portId, mode); @@ -77,4 +76,10 @@ export class Device extends EventEmitter { } } + private _ensureConnected () { + if (!this.connected) { + throw new Error("Device is not connected"); + } + } + } diff --git a/src/hub.ts b/src/hub.ts index dfdd4b47..27028bda 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -33,9 +33,6 @@ export class Hub extends EventEmitter { protected _bleDevice: IBLEAbstraction; - private _isConnecting = false; - private _isConnected = false; - private _type: Consts.HubType; constructor (device: IBLEAbstraction, type: Consts.HubType = Consts.HubType.UNKNOWN) { @@ -154,7 +151,6 @@ export class Hub extends EventEmitter { } else if (this._bleDevice.connected) { return connectReject("Already connected"); } - this._isConnecting = true; await this._bleDevice.connect(); return connectResolve(); }); diff --git a/src/lights.ts b/src/lights.ts index 411d8643..8386d82b 100644 --- a/src/lights.ts +++ b/src/lights.ts @@ -1,5 +1,6 @@ import { Device } from "./device"; import { Hub } from "./hub"; +import { WeDo2SmartHub } from "./wedo2smarthub"; import * as Consts from "./consts"; @@ -17,10 +18,15 @@ export class Lights extends Device { * @param {number} brightness Brightness value between 0-100 (0 is off) * @returns {Promise} Resolved upon successful completion of command. */ - public brightness (brightness: number) { + public setBrightness (brightness: number) { return new Promise((resolve) => { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); - this.send(data); + if (this.hub instanceof WeDo2SmartHub) { + const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); + this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + } else { + const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); + this.send(data); + } return resolve(); }); } diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index b596585f..e29fe616 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -41,13 +41,13 @@ export class LPF2Hub extends Hub { await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); if (this._voltagePort !== undefined) { - this.send(Buffer.from([0x41, this._voltagePort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Activate voltage reports + this.subscribe(this._voltagePort, 0x00); // Activate voltage reports } if (this._currentPort !== undefined) { - this.send(Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Activate current reports + this.subscribe(this._currentPort, 0x00); // Activate currrent reports } if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { - this.send(Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); + this.subscribe(0x01, 0x01); } await this.sleep(100); this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index 1b22944b..7d53a1d1 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -103,6 +103,20 @@ export class WeDo2SmartHub extends Hub { } + /** + * Shutdown the Hub. + * @method WeDo2SmartHub#shutdown + * @returns {Promise} Resolved upon successful disconnect. + */ + public shutdown () { + return new Promise((resolve, reject) => { + this.send(Buffer.from([0x00]), Consts.BLECharacteristic.WEDO2_DISCONNECT, () => { + return resolve(); + }); + }); + } + + /** * Set the name of the Hub. * @method WeDo2SmartHub#setName @@ -144,20 +158,6 @@ export class WeDo2SmartHub extends Hub { } - /** - * Shutdown the Hub. - * @method WeDo2SmartHub#shutdown - * @returns {Promise} Resolved upon successful disconnect. - */ - public shutdown () { - return new Promise((resolve, reject) => { - this.send(Buffer.from([0x00]), Consts.BLECharacteristic.WEDO2_DISCONNECT, () => { - return resolve(); - }); - }); - } - - /** * Set the color of the LED on the Hub via RGB values. * @method WeDo2SmartHub#setLEDRGB @@ -177,41 +177,6 @@ export class WeDo2SmartHub extends Hub { } - // /** - // * Set the motor speed on a given port. - // * @method WeDo2SmartHub#setMotorSpeed - // * @param {string} port - // * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - // * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - // */ - // public setMotorSpeed (port: string, speed: number, time?: number | boolean) { - // const portObj = this._portLookup(port); - // let cancelEventTimer = true; - // if (typeof time === "boolean") { - // if (time === true) { - // cancelEventTimer = false; - // } - // time = undefined; - // } - // if (cancelEventTimer) { - // portObj.cancelEventTimer(); - // } - // return new Promise((resolve, reject) => { - // this.send(Buffer.from([portObj.value, 0x01, 0x02, this._mapSpeed(speed)]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - // if (time && typeof time === "number") { - // const timeout = global.setTimeout(() => { - // this.send(Buffer.from([portObj.value, 0x01, 0x02, 0x00]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - // return resolve(); - // }, time); - // portObj.setEventTimer(timeout); - // } else { - // return resolve(); - // } - // }); - // } - - // /** // * Ramp the motor speed on a given port. // * @method WeDo2SmartHub#rampMotorSpeed @@ -234,17 +199,6 @@ export class WeDo2SmartHub extends Hub { // } - // /** - // * Fully (hard) stop the motor on a given port. - // * @method WeDo2SmartHub#brakeMotor - // * @param {string} port - // * @returns {Promise} Resolved upon successful completion of command. - // */ - // public brakeMotor (port: string) { - // return this.setMotorSpeed(port, 127); - // } - - /** * Play a tone on the Hub's in-built buzzer * @method WeDo2SmartHub#playTone @@ -263,34 +217,6 @@ export class WeDo2SmartHub extends Hub { } - // /** - // * Set the light brightness on a given port. - // * @method WeDo2SmartHub#setLightBrightness - // * @param {string} port - // * @param {number} brightness Brightness value between 0-100 (0 is off) - // * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. - // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. - // */ - // public setLightBrightness (port: string, brightness: number, time?: number) { - // const portObj = this._portLookup(port); - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // const data = Buffer.from([portObj.value, 0x01, 0x02, brightness]); - // this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - // if (time) { - // const timeout = global.setTimeout(() => { - // const data = Buffer.from([portObj.value, 0x01, 0x02, 0x00]); - // this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - // return resolve(); - // }, time); - // portObj.setEventTimer(timeout); - // } else { - // return resolve(); - // } - // }); - // } - - public send (message: Buffer, uuid: string, callback?: () => void) { if (debug.enabled) { debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message); @@ -339,7 +265,6 @@ export class WeDo2SmartHub extends Hub { private _parsePortMessage (data: Buffer) { - debug("Received Message (WEDO2_PORT_TYPE)", data); const portId = data[0]; @@ -368,20 +293,17 @@ export class WeDo2SmartHub extends Hub { this._attachDevice(device); } + } - // const port = this._getPortForPortNumber(data[0]); - - // if (!port) { - // return; - // } - - // port.connected = data[1] === 1 ? true : false; - // this._registerDeviceAttachment(port, data[3]); - } + private _parseSensorMessage (message: Buffer) { + const portId = message[1]; + const device = this._getDeviceByPortId(portId); - private _parseSensorMessage (data: Buffer) { + if (device) { + device.receive(message); + } // debug("Received Message (WEDO2_SENSOR_VALUE)", data); From 1793eb05e0acae5304a997760ea472ef10ac935c Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 10 Dec 2019 08:21:56 +0900 Subject: [PATCH 10/62] Removing commented code --- src/boostmovehub.ts | 101 ------------------ src/controlplushub.ts | 237 ++++++++++++++---------------------------- src/duplotrainbase.ts | 1 + src/lpf2hub.ts | 3 - 4 files changed, 80 insertions(+), 262 deletions(-) diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index 013b9c9d..5bcaea6c 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -146,68 +146,6 @@ export class BoostMoveHub extends LPF2Hub { // } - // /** - // * Ramp the motor speed on a given port. - // * @method BoostMoveHub#rampMotorSpeed - // * @param {string} port - // * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - // * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - // * @param {number} time How long the ramp should last (in milliseconds). - // * @returns {Promise} Resolved upon successful completion of command. - // */ - // public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - // const portObj = this._portLookup(port); - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // this._calculateRamp(fromSpeed, toSpeed, time, portObj) - // .on("changeSpeed", (speed) => { - // this.setMotorSpeed(port, speed, true); - // }) - // .on("finished", resolve); - // }); - // } - - - // /** - // * Rotate a motor by a given angle. - // * @method BoostMoveHub#setMotorAngle - // * @param {string} port - // * @param {number} angle How much the motor should be rotated (in degrees). - // * @param {number | Array.} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - // */ - // public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { - // const portObj = this._portLookup(port); - // if (!( - // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // )) { - // throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor"); - // } - // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - // throw new Error(`Port ${portObj.id} can only accept a single speed`); - // } - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // data.writeUInt32LE(angle, 4); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // }); - // } - - // /** // * Tell motor to goto an absolute position // * @method BoostMoveHub#setAbsolutePosition @@ -267,45 +205,6 @@ export class BoostMoveHub extends LPF2Hub { // } - // /** - // * Fully (hard) stop the motor on a given port. - // * @method BoostMoveHub#brakeMotor - // * @param {string} port - // * @returns {Promise} Resolved upon successful completion of command. - // */ - // public brakeMotor (port: string) { - // return this.setMotorSpeed(port, 127); - // } - - - // /** - // * Set the light brightness on a given port. - // * @method BoostMoveHub#setLightBrightness - // * @param {string} port - // * @param {number} brightness Brightness value between 0-100 (0 is off) - // * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. - // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. - // */ - // public setLightBrightness (port: string, brightness: number, time?: number) { - // const portObj = this._portLookup(port); - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // if (time) { - // const timeout = global.setTimeout(() => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // return resolve(); - // }, time); - // portObj.setEventTimer(timeout); - // } else { - // return resolve(); - // } - // }); - // } - - protected _checkFirmware (version: string) { if (compareVersion("2.0.00.0017", version) === 1) { throw new Error(`Your Boost Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`); diff --git a/src/controlplushub.ts b/src/controlplushub.ts index c23ea516..b2125798 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -65,88 +65,88 @@ export class ControlPlusHub extends LPF2Hub { } - /** - * Set the motor speed on a given port. - * @method ControlPlusHub#setMotorSpeed - * @param {string} port - * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - */ - public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { - // const portObj = this._portLookup(port); - // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - // throw new Error(`Port ${portObj.id} can only accept a single speed`); - // } - // let cancelEventTimer = true; - // if (typeof time === "boolean") { - // if (time === true) { - // cancelEventTimer = false; - // } - // time = undefined; - // } - // if (cancelEventTimer) { - // portObj.cancelEventTimer(); - // } - // return new Promise((resolve, reject) => { - // if (time && typeof time === "number") { - - // if ( - // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // ) { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // const timeout = global.setTimeout(() => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // return resolve(); - // // @ts-ignore: The type of time is properly checked at the start - // }, time); - // portObj.setEventTimer(timeout); - // } + // /** + // * Set the motor speed on a given port. + // * @method ControlPlusHub#setMotorSpeed + // * @param {string} port + // * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. + // * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. + // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. + // */ + // public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { + // const portObj = this._portLookup(port); + // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { + // throw new Error(`Port ${portObj.id} can only accept a single speed`); + // } + // let cancelEventTimer = true; + // if (typeof time === "boolean") { + // if (time === true) { + // cancelEventTimer = false; + // } + // time = undefined; + // } + // if (cancelEventTimer) { + // portObj.cancelEventTimer(); + // } + // return new Promise((resolve, reject) => { + // if (time && typeof time === "number") { + + // if ( + // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || + // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || + // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR + // ) { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // data.writeUInt16LE(time > 65535 ? 65535 : time, 4); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // const timeout = global.setTimeout(() => { + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // return resolve(); + // // @ts-ignore: The type of time is properly checked at the start + // }, time); + // portObj.setEventTimer(timeout); + // } - // } else { + // } else { - // if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // } + // if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { + // portObj.busy = true; + // let data = null; + // if (this._virtualPorts[portObj.id]) { + // data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + // } + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // portObj.finished = () => { + // return resolve(); + // }; + // } else { + // // @ts-ignore: The type of speed is properly checked at the start + // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); + // this.send(data, Consts.BLECharacteristic.LPF2_ALL); + // } - // } - // }); - } + // } + // }); + // } // /** @@ -171,46 +171,6 @@ export class ControlPlusHub extends LPF2Hub { // } - // /** - // * Rotate a motor by a given angle. - // * @method ControlPlusHub#setMotorAngle - // * @param {string} port - // * @param {number} angle How much the motor should be rotated (in degrees). - // * @param {number | Array.} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - // */ - // public setMotorAngle (port: string, angle: number, speed: number | [number, number] = 100) { - // const portObj = this._portLookup(port); - // if (!( - // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // )) { - // throw new Error("Angle rotation is only available when using a Boost Tacho Motor, Boost Move Hub Motor, Control+ Medium Motor, or Control+ Large Motor"); - // } - // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - // throw new Error(`Port ${portObj.id} can only accept a single speed`); - // } - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // data.writeUInt32LE(angle, 4); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // }); - // } - - // /** // * Tell motor to goto an absolute position // * @method ControlPlusHub#setAbsolutePosition @@ -270,43 +230,4 @@ export class ControlPlusHub extends LPF2Hub { // } - /** - * Fully (hard) stop the motor on a given port. - * @method ControlPlusHub#brakeMotor - * @param {string} port - * @returns {Promise} Resolved upon successful completion of command. - */ - public brakeMotor (port: string) { - return this.setMotorSpeed(port, 127); - } - - - // /** - // * Set the light brightness on a given port. - // * @method ControlPlusHub#setLightBrightness - // * @param {string} port - // * @param {number} brightness Brightness value between 0-100 (0 is off) - // * @param {number} [time] How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely. - // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the light is turned off. - // */ - // public setLightBrightness (port: string, brightness: number, time?: number) { - // const portObj = this._portLookup(port); - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, brightness]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // if (time) { - // const timeout = global.setTimeout(() => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // return resolve(); - // }, time); - // portObj.setEventTimer(timeout); - // } else { - // return resolve(); - // } - // }); - // } - - } diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index ece154bc..a28e1f46 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -50,6 +50,7 @@ export class DuploTrainBase extends LPF2Hub { return new Promise(async (resolve, reject) => { debug("Connecting to Duplo Train Base"); await super.connect(); + this.subscribe(0x01, 0x01); debug("Connect completed"); return resolve(); }); diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index e29fe616..49a55327 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -46,9 +46,6 @@ export class LPF2Hub extends Hub { if (this._currentPort !== undefined) { this.subscribe(this._currentPort, 0x00); // Activate currrent reports } - if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { - this.subscribe(0x01, 0x01); - } await this.sleep(100); this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports this.send(Buffer.from([0x01, 0x03, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request firmware version From 6beeda5df79eed8b7bdc97ada6c2003e2c86185e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 11 Dec 2019 17:20:35 +0900 Subject: [PATCH 11/62] Implemented methods for retrieving hubs and devices --- src/hub.ts | 20 ++++++++++++++++++++ src/poweredup-browser.ts | 33 ++++++++++++++++++++++----------- src/poweredup-node.ts | 33 ++++++++++++++++++++++----------- 3 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/hub.ts b/src/hub.ts index 27028bda..cc905e08 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -168,6 +168,26 @@ export class Hub extends EventEmitter { } + public getDeviceAtPort (portName: string) { + const portId = this._portNames[portName]; + if (portId) { + return this._attachedDevices[portId]; + } else { + throw new Error(`Port ${portName} does not exist on this hub type`); + } + } + + + public getDevices () { + return Object.values(this._attachedDevices); + } + + + public getDevicesByType (deviceType: number) { + return this.getDevices().filter((device) => device.type === deviceType); + } + + public getPortNameForPortId (portId: number) { for (const port of Object.keys(this._portNames)) { if (this._portNames[port] === portId) { diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 5fab9be7..3fd5144a 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -74,44 +74,55 @@ export class PoweredUP extends EventEmitter { /** * Retrieve a list of Powered UP Hubs. - * @method PoweredUP#getConnectedHubs + * @method PoweredUP#getHubs * @returns {Hub[]} */ - public getConnectedHubs () { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]); + public getHubs () { + return Object.values(this._connectedHubs); } /** * Retrieve a Powered UP Hub by UUID. - * @method PoweredUP#getConnectedHubByUUID + * @method PoweredUP#getHubByUUID * @param {string} uuid * @returns {Hub | null} */ - public getConnectedHubByUUID (uuid: string) { + public getHubByUUID (uuid: string) { return this._connectedHubs[uuid]; } /** * Retrieve a Powered UP Hub by primary MAC address. - * @method PoweredUP#getConnectedHubByPrimaryMACAddress + * @method PoweredUP#getHubByPrimaryMACAddress * @param {string} address * @returns {Hub} */ - public getConnectedHubByPrimaryMACAddress (address: string) { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.primaryMACAddress === address)[0]; + public getHubByPrimaryMACAddress (address: string) { + return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0]; } /** * Retrieve a list of Powered UP Hub by name. - * @method PoweredUP#getConnectedHubsByName + * @method PoweredUP#getHubsByName * @param {string} name * @returns {Hub[]} */ - public getConnectedHubsByName (name: string) { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name); + public getHubsByName (name: string) { + return Object.values(this._connectedHubs).filter((hub) => hub.name === name); + } + + + /** + * Retrieve a list of Powered UP Hub by type. + * @method PoweredUP#getHubsByType + * @param {string} name + * @returns {Hub[]} + */ + public getHubsByType (hubType: number) { + return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType); } diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index 88a9eb39..e6fe53de 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -92,44 +92,55 @@ export class PoweredUP extends EventEmitter { /** * Retrieve a list of Powered UP Hubs. - * @method PoweredUP#getConnectedHubs + * @method PoweredUP#getHubs * @returns {Hub[]} */ - public getConnectedHubs () { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]); + public getHubs () { + return Object.values(this._connectedHubs); } /** * Retrieve a Powered UP Hub by UUID. - * @method PoweredUP#getConnectedHubByUUID + * @method PoweredUP#getHubByUUID * @param {string} uuid * @returns {Hub | null} */ - public getConnectedHubByUUID (uuid: string) { + public getHubByUUID (uuid: string) { return this._connectedHubs[uuid]; } /** * Retrieve a Powered UP Hub by primary MAC address. - * @method PoweredUP#getConnectedHubByPrimaryMACAddress + * @method PoweredUP#getHubByPrimaryMACAddress * @param {string} address * @returns {Hub} */ - public getConnectedHubByPrimaryMACAddress (address: string) { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.primaryMACAddress === address)[0]; + public getHubByPrimaryMACAddress (address: string) { + return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0]; } /** * Retrieve a list of Powered UP Hub by name. - * @method PoweredUP#getConnectedHubsByName + * @method PoweredUP#getHubsByName * @param {string} name * @returns {Hub[]} */ - public getConnectedHubsByName (name: string) { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name); + public getHubsByName (name: string) { + return Object.values(this._connectedHubs).filter((hub) => hub.name === name); + } + + + /** + * Retrieve a list of Powered UP Hub by type. + * @method PoweredUP#getHubsByType + * @param {string} name + * @returns {Hub[]} + */ + public getHubsByType (hubType: number) { + return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType); } From 6a7c4899b445f6affca09e8ced55e1c80c29e395 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sun, 15 Dec 2019 14:07:43 -0800 Subject: [PATCH 12/62] Added more motor types --- examples/new_device_test.js | 54 +++++++++++++++++++++++++++++---- src/MoveHubMediumLinearMotor.ts | 12 ++++++++ src/consts.ts | 12 ++++---- src/controlpluslargemotor.ts | 1 - src/controlplusxlargemotor.ts | 12 ++++++++ src/index-browser.ts | 14 ++++++++- src/index-node.ts | 14 ++++++++- src/lpf2hub.ts | 20 ++++++++++++ src/mediumlinearmotor.ts | 12 ++++++++ src/tachomotor.ts | 2 +- src/wedo2smarthub.ts | 19 +++++++++--- 11 files changed, 152 insertions(+), 20 deletions(-) create mode 100644 src/MoveHubMediumLinearMotor.ts create mode 100644 src/controlplusxlargemotor.ts create mode 100644 src/mediumlinearmotor.ts diff --git a/examples/new_device_test.js b/examples/new_device_test.js index 91a36ec2..99f35e67 100644 --- a/examples/new_device_test.js +++ b/examples/new_device_test.js @@ -24,18 +24,60 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs console.log(`Detached device ${device.type} from ${device.port}`); }); - if (device instanceof PoweredUP.ControlPlusLargeMotor) { + if ( + // device instanceof PoweredUP.MoveHubMediumLinearMotor || + device instanceof PoweredUP.MediumLinearMotor || + device instanceof PoweredUP.ControlPlusLargeMotor || + device instanceof PoweredUP.ControlPlusXLargeMotor + ) { const motor = device; motor.on("rotate", (angle) => { console.log(`Rotate ${angle}`); }); - await motor.rotateByAngle(9000, 50); - await motor.rotateByAngle(9000, -50); - await motor.rotateByAngle(9000, 50); - await motor.rotateByAngle(9000, -50); - motor.power(100); + await motor.rotateByAngle(900, 50); + await motor.rotateByAngle(900, -50); + await motor.rotateByAngle(900, 50); + await motor.rotateByAngle(900, -50); + motor.setPower(20); + } + + if ( + device instanceof PoweredUP.SimpleMediumLinearMotor || + device instanceof PoweredUP.TrainMotor + ) { + const motor = device; + + motor.setPower(20); + await hub.sleep(1000); + motor.setPower(40); + await hub.sleep(1000); + motor.setPower(60); + await hub.sleep(1000); + motor.setPower(80); + await hub.sleep(1000); + motor.setPower(100); + await hub.sleep(1000); + motor.setPower(60); + await hub.sleep(1000); + motor.setPower(20); + } + + if ( + device instanceof PoweredUP.Lights + ) { + const lights = device; + + lights.setBrightness(100); + await hub.sleep(1000); + lights.setBrightness(0); + await hub.sleep(1000); + lights.setBrightness(100); + await hub.sleep(1000); + lights.setBrightness(0); + await hub.sleep(1000); + lights.setBrightness(100); } if (device instanceof PoweredUP.ColorDistanceSensor) { diff --git a/src/MoveHubMediumLinearMotor.ts b/src/MoveHubMediumLinearMotor.ts new file mode 100644 index 00000000..ffb07a74 --- /dev/null +++ b/src/MoveHubMediumLinearMotor.ts @@ -0,0 +1,12 @@ +import { Hub } from "./hub"; + +import * as Consts from "./consts"; +import { TachoMotor } from "./tachomotor"; + +export class MoveHubMediumLinearMotor extends TachoMotor { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR); + } + +} diff --git a/src/consts.ts b/src/consts.ts index abb79608..21895083 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -26,7 +26,7 @@ export const HubTypeNames = HubType; /** * @typedef DeviceType * @property {number} UNKNOWN 0 - * @property {number} BASIC_MOTOR 1 + * @property {number} SIMPLE_MEDIUM_LINEAR_MOTOR 1 * @property {number} TRAIN_MOTOR 2 * @property {number} LED_LIGHTS 8 * @property {number} VOLTAGE 20 @@ -35,9 +35,9 @@ export const HubTypeNames = HubType; * @property {number} RGB_LIGHT 23 * @property {number} WEDO2_TILT 34 * @property {number} WEDO2_DISTANCE 35 - * @property {number} BOOST_DISTANCE 37 - * @property {number} BOOST_TACHO_MOTOR 38 - * @property {number} BOOST_MOVE_HUB_MOTOR 39 + * @property {number} COLOR_DISTANCE_SENSOR 37 + * @property {number} MEDIUM_LINEAR_MOTOR 38 + * @property {number} MOVE_HUB_MEDIUM_LINEAR_MOTOR 39 * @property {number} BOOST_TILT 40 * @property {number} DUPLO_TRAIN_BASE_MOTOR 41 * @property {number} DUPLO_TRAIN_BASE_SPEAKER 42 @@ -62,8 +62,8 @@ export enum DeviceType { WEDO2_TILT = 34, WEDO2_DISTANCE = 35, COLOR_DISTANCE_SENSOR = 37, - BOOST_TACHO_MOTOR = 38, - BOOST_MOVE_HUB_MOTOR = 39, + MEDIUM_LINEAR_MOTOR = 38, + MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39, BOOST_TILT = 40, DUPLO_TRAIN_BASE_MOTOR = 41, DUPLO_TRAIN_BASE_SPEAKER = 42, diff --git a/src/controlpluslargemotor.ts b/src/controlpluslargemotor.ts index 66077b75..8ba05675 100644 --- a/src/controlpluslargemotor.ts +++ b/src/controlpluslargemotor.ts @@ -1,4 +1,3 @@ -import { BasicMotor } from "./basicmotor"; import { Hub } from "./hub"; import * as Consts from "./consts"; diff --git a/src/controlplusxlargemotor.ts b/src/controlplusxlargemotor.ts new file mode 100644 index 00000000..c83bcde8 --- /dev/null +++ b/src/controlplusxlargemotor.ts @@ -0,0 +1,12 @@ +import { Hub } from "./hub"; + +import * as Consts from "./consts"; +import { TachoMotor } from "./tachomotor"; + +export class ControlPlusXLargeMotor extends TachoMotor { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR); + } + +} diff --git a/src/index-browser.ts b/src/index-browser.ts index d1310d1e..032e422d 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -12,7 +12,13 @@ import { WeDo2SmartHub } from "./wedo2smarthub"; import { ColorDistanceSensor } from "./colordistancesensor"; import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; import { Device } from "./device"; +import { Lights } from "./lights"; +import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -27,9 +33,15 @@ window.PoweredUP = { PUPRemote, DuploTrainBase, Consts, - Device, ColorDistanceSensor, ControlPlusLargeMotor, + ControlPlusXLargeMotor, + Device, + Lights, + MediumLinearMotor, + MoveHubMediumLinearMotor, + SimpleMediumLinearMotor, + TrainMotor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index 88051381..ccaed225 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -12,7 +12,13 @@ import { WeDo2SmartHub } from "./wedo2smarthub"; import { ColorDistanceSensor } from "./colordistancesensor"; import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; import { Device } from "./device"; +import { Lights } from "./lights"; +import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -27,8 +33,14 @@ export { PUPRemote, DuploTrainBase, Consts, - Device, ColorDistanceSensor, ControlPlusLargeMotor, + ControlPlusXLargeMotor, + Device, + Lights, + MediumLinearMotor, + MoveHubMediumLinearMotor, + SimpleMediumLinearMotor, + TrainMotor, isWebBluetooth }; diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 49a55327..f1d330a5 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -3,7 +3,12 @@ import { Hub } from "./hub"; import { ColorDistanceSensor } from "./colordistancesensor"; import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; import { Lights } from "./lights"; +import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TrainMotor } from "./trainmotor"; import * as Consts from "./consts"; @@ -287,9 +292,24 @@ export class LPF2Hub extends Hub { case Consts.DeviceType.LED_LIGHTS: device = new Lights(this, portId); break; + case Consts.DeviceType.TRAIN_MOTOR: + device = new TrainMotor(this, portId); + break; + case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: + device = new SimpleMediumLinearMotor(this, portId); + break; + case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: + device = new MoveHubMediumLinearMotor(this, portId); + break; + case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: + device = new MediumLinearMotor(this, portId); + break; case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: device = new ControlPlusLargeMotor(this, portId); break; + case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: + device = new ControlPlusXLargeMotor(this, portId); + break; case Consts.DeviceType.COLOR_DISTANCE_SENSOR: device = new ColorDistanceSensor(this, portId); break; diff --git a/src/mediumlinearmotor.ts b/src/mediumlinearmotor.ts new file mode 100644 index 00000000..53680a6c --- /dev/null +++ b/src/mediumlinearmotor.ts @@ -0,0 +1,12 @@ +import { Hub } from "./hub"; + +import * as Consts from "./consts"; +import { TachoMotor } from "./tachomotor"; + +export class MediumLinearMotor extends TachoMotor { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.MEDIUM_LINEAR_MOTOR); + } + +} diff --git a/src/tachomotor.ts b/src/tachomotor.ts index f12fd895..3fee32ed 100644 --- a/src/tachomotor.ts +++ b/src/tachomotor.ts @@ -38,7 +38,7 @@ export class TachoMotor extends BasicMotor { /** * Rotate a motor by a given angle. - * @method TachoMotor#setMotorAngle + * @method TachoMotor#rotateByAngle * @param {number} angle How much the motor should be rotated (in degrees). * @param {number} [power=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index 7d53a1d1..7eef8446 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -7,7 +7,12 @@ import { Hub } from "./hub"; import { ColorDistanceSensor } from "./colordistancesensor"; import { ControlPlusLargeMotor } from "./controlpluslargemotor"; +import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; import { Lights } from "./lights"; +import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TrainMotor } from "./trainmotor"; import * as Consts from "./consts"; @@ -279,16 +284,22 @@ export class WeDo2SmartHub extends Hub { case Consts.DeviceType.LED_LIGHTS: device = new Lights(this, portId); break; - case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: - device = new ControlPlusLargeMotor(this, portId); - break; + // case Consts.DeviceType.BOOST_TACHO_MOTOR: + // device = new BoostTachoMotor(this, portId); + // break; + // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: + // device = new ControlPlusLargeMotor(this, portId); + // break; + // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: + // device = new ControlPlusXLargeMotor(this, portId); + // break; case Consts.DeviceType.COLOR_DISTANCE_SENSOR: device = new ColorDistanceSensor(this, portId); break; default: device = new Device(this, portId, deviceType); break; - } + } this._attachDevice(device); From 198d637ef9d13cdb7b73ca8278f58aa57b97ffd4 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 08:42:40 -0800 Subject: [PATCH 13/62] Renamed devices --- examples/new_device_test.js | 4 ++-- src/consts.ts | 6 +++--- src/index-browser.ts | 14 ++++++------- src/index-node.ts | 14 ++++++------- src/{lights.ts => light.ts} | 4 ++-- src/lpf2hub.ts | 20 +++++++++---------- ...rgemotor.ts => techniclargelinearmotor.ts} | 4 ++-- ...gemotor.ts => technicxlargelinearmotor.ts} | 4 ++-- src/wedo2smarthub.ts | 12 +++++------ 9 files changed, 41 insertions(+), 41 deletions(-) rename src/{lights.ts => light.ts} (90%) rename src/{controlpluslargemotor.ts => techniclargelinearmotor.ts} (55%) rename src/{controlplusxlargemotor.ts => technicxlargelinearmotor.ts} (55%) diff --git a/examples/new_device_test.js b/examples/new_device_test.js index 99f35e67..fb05ed0d 100644 --- a/examples/new_device_test.js +++ b/examples/new_device_test.js @@ -27,8 +27,8 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs if ( // device instanceof PoweredUP.MoveHubMediumLinearMotor || device instanceof PoweredUP.MediumLinearMotor || - device instanceof PoweredUP.ControlPlusLargeMotor || - device instanceof PoweredUP.ControlPlusXLargeMotor + device instanceof PoweredUP.TechnicLargeLinearMotor || + device instanceof PoweredUP.TechnicXLargeLinearMotor ) { const motor = device; diff --git a/src/consts.ts b/src/consts.ts index 21895083..11f7bb74 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -54,7 +54,7 @@ export enum DeviceType { UNKNOWN = 0, SIMPLE_MEDIUM_LINEAR_MOTOR = 1, TRAIN_MOTOR = 2, - LED_LIGHTS = 8, + LIGHT = 8, VOLTAGE = 20, CURRENT = 21, PIEZO_TONE = 22, @@ -69,8 +69,8 @@ export enum DeviceType { DUPLO_TRAIN_BASE_SPEAKER = 42, DUPLO_TRAIN_BASE_COLOR = 43, DUPLO_TRAIN_BASE_SPEEDOMETER = 44, - CONTROL_PLUS_LARGE_MOTOR = 46, - CONTROL_PLUS_XLARGE_MOTOR = 47, + TECHNIC_LARGE_LINEAR_MOTOR = 46, + TECHNIC_XLARGE_LINEAR_MOTOR = 47, CONTROL_PLUS_GEST = 54, POWERED_UP_REMOTE_BUTTON = 55, RSSI = 56, diff --git a/src/index-browser.ts b/src/index-browser.ts index 032e422d..0eafb357 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -11,13 +11,13 @@ import { PUPRemote } from "./pupremote"; import { WeDo2SmartHub } from "./wedo2smarthub"; import { ColorDistanceSensor } from "./colordistancesensor"; -import { ControlPlusLargeMotor } from "./controlpluslargemotor"; -import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; import { Device } from "./device"; -import { Lights } from "./lights"; +import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -34,13 +34,13 @@ window.PoweredUP = { DuploTrainBase, Consts, ColorDistanceSensor, - ControlPlusLargeMotor, - ControlPlusXLargeMotor, Device, - Lights, + Light, MediumLinearMotor, MoveHubMediumLinearMotor, SimpleMediumLinearMotor, + TechnicLargeLinearMotor, + TechnicXLargeLinearMotor, TrainMotor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index ccaed225..ffe988d1 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -11,13 +11,13 @@ import { PUPRemote } from "./pupremote"; import { WeDo2SmartHub } from "./wedo2smarthub"; import { ColorDistanceSensor } from "./colordistancesensor"; -import { ControlPlusLargeMotor } from "./controlpluslargemotor"; -import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; import { Device } from "./device"; -import { Lights } from "./lights"; +import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -34,13 +34,13 @@ export { DuploTrainBase, Consts, ColorDistanceSensor, - ControlPlusLargeMotor, - ControlPlusXLargeMotor, Device, - Lights, + Light, MediumLinearMotor, MoveHubMediumLinearMotor, SimpleMediumLinearMotor, + TechnicLargeLinearMotor, + TechnicXLargeLinearMotor, TrainMotor, isWebBluetooth }; diff --git a/src/lights.ts b/src/light.ts similarity index 90% rename from src/lights.ts rename to src/light.ts index 8386d82b..fda7104a 100644 --- a/src/lights.ts +++ b/src/light.ts @@ -4,11 +4,11 @@ import { WeDo2SmartHub } from "./wedo2smarthub"; import * as Consts from "./consts"; -export class Lights extends Device { +export class Light extends Device { constructor (hub: Hub, portId: number) { - super(hub, portId, Consts.DeviceType.LED_LIGHTS); + super(hub, portId, Consts.DeviceType.LIGHT); } diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index f1d330a5..b573bb3f 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -2,12 +2,12 @@ import { Device } from "./device"; import { Hub } from "./hub"; import { ColorDistanceSensor } from "./colordistancesensor"; -import { ControlPlusLargeMotor } from "./controlpluslargemotor"; -import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; -import { Lights } from "./lights"; +import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; import { TrainMotor } from "./trainmotor"; import * as Consts from "./consts"; @@ -289,8 +289,8 @@ export class LPF2Hub extends Hub { let device; switch (deviceType) { - case Consts.DeviceType.LED_LIGHTS: - device = new Lights(this, portId); + case Consts.DeviceType.LIGHT: + device = new Light(this, portId); break; case Consts.DeviceType.TRAIN_MOTOR: device = new TrainMotor(this, portId); @@ -304,11 +304,11 @@ export class LPF2Hub extends Hub { case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: device = new MediumLinearMotor(this, portId); break; - case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: - device = new ControlPlusLargeMotor(this, portId); + case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR: + device = new TechnicLargeLinearMotor(this, portId); break; - case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: - device = new ControlPlusXLargeMotor(this, portId); + case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR: + device = new TechnicXLargeLinearMotor(this, portId); break; case Consts.DeviceType.COLOR_DISTANCE_SENSOR: device = new ColorDistanceSensor(this, portId); diff --git a/src/controlpluslargemotor.ts b/src/techniclargelinearmotor.ts similarity index 55% rename from src/controlpluslargemotor.ts rename to src/techniclargelinearmotor.ts index 8ba05675..5f65f088 100644 --- a/src/controlpluslargemotor.ts +++ b/src/techniclargelinearmotor.ts @@ -3,10 +3,10 @@ import { Hub } from "./hub"; import * as Consts from "./consts"; import { TachoMotor } from "./tachomotor"; -export class ControlPlusLargeMotor extends TachoMotor { +export class TechnicLargeLinearMotor extends TachoMotor { constructor (hub: Hub, portId: number) { - super(hub, portId, Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR); + super(hub, portId, Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR); } } diff --git a/src/controlplusxlargemotor.ts b/src/technicxlargelinearmotor.ts similarity index 55% rename from src/controlplusxlargemotor.ts rename to src/technicxlargelinearmotor.ts index c83bcde8..4a24ccca 100644 --- a/src/controlplusxlargemotor.ts +++ b/src/technicxlargelinearmotor.ts @@ -3,10 +3,10 @@ import { Hub } from "./hub"; import * as Consts from "./consts"; import { TachoMotor } from "./tachomotor"; -export class ControlPlusXLargeMotor extends TachoMotor { +export class TechnicXLargeLinearMotor extends TachoMotor { constructor (hub: Hub, portId: number) { - super(hub, portId, Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR); + super(hub, portId, Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR); } } diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index 7eef8446..dc812843 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -6,12 +6,12 @@ import { Device } from "./device"; import { Hub } from "./hub"; import { ColorDistanceSensor } from "./colordistancesensor"; -import { ControlPlusLargeMotor } from "./controlpluslargemotor"; -import { ControlPlusXLargeMotor } from "./controlplusxlargemotor"; -import { Lights } from "./lights"; +import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; import { TrainMotor } from "./trainmotor"; import * as Consts from "./consts"; @@ -281,8 +281,8 @@ export class WeDo2SmartHub extends Hub { let device; switch (deviceType) { - case Consts.DeviceType.LED_LIGHTS: - device = new Lights(this, portId); + case Consts.DeviceType.LIGHT: + device = new Light(this, portId); break; // case Consts.DeviceType.BOOST_TACHO_MOTOR: // device = new BoostTachoMotor(this, portId); From 31ac93275c96c67a5c774795a05e35045599ef98 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 11:48:09 -0800 Subject: [PATCH 14/62] Motion and tilt sensor --- examples/new_device_test.js | 18 ++++++++++++++-- src/consts.ts | 4 ++-- src/device.ts | 2 +- src/index-browser.ts | 4 ++++ src/index-node.ts | 4 ++++ src/lpf2hub.ts | 8 ++++++++ src/motionsensor.ts | 41 +++++++++++++++++++++++++++++++++++++ src/tiltsensor.ts | 40 ++++++++++++++++++++++++++++++++++++ 8 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 src/motionsensor.ts create mode 100644 src/tiltsensor.ts diff --git a/examples/new_device_test.js b/examples/new_device_test.js index fb05ed0d..6364a2be 100644 --- a/examples/new_device_test.js +++ b/examples/new_device_test.js @@ -65,7 +65,7 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs } if ( - device instanceof PoweredUP.Lights + device instanceof PoweredUP.Light ) { const lights = device; @@ -82,7 +82,7 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs if (device instanceof PoweredUP.ColorDistanceSensor) { const sensor = device; - sensor.on("distance", (distance) => { // Adding an event handler for distance automatically subscribes to distance notifications + sensor.on("distance", (distance) => { console.log(`Distance ${distance}`); }); sensor.on("color", (color) => { @@ -90,6 +90,20 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs }); } + if (device instanceof PoweredUP.MotionSensor) { + const sensor = device; + sensor.on("distance", (distance) => { + console.log(`Distance ${distance}`); + }); + } + + if (device instanceof PoweredUP.TiltSensor) { + const sensor = device; + sensor.on("tilt", (x, y) => { + console.log(`Tilt ${x} ${y}`); + }); + } + }); hub.on("disconnect", () => { diff --git a/src/consts.ts b/src/consts.ts index 11f7bb74..3ab80cba 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -59,8 +59,8 @@ export enum DeviceType { CURRENT = 21, PIEZO_TONE = 22, RGB_LIGHT = 23, - WEDO2_TILT = 34, - WEDO2_DISTANCE = 35, + TILT_SENSOR = 34, + MOTION_SENSOR = 35, COLOR_DISTANCE_SENSOR = 37, MEDIUM_LINEAR_MOTOR = 38, MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39, diff --git a/src/device.ts b/src/device.ts index 7494a751..8d95041f 100644 --- a/src/device.ts +++ b/src/device.ts @@ -7,7 +7,7 @@ export class Device extends EventEmitter { public autoSubscribe: boolean = true; - protected _mode: number = 0x00; + protected _mode: number | undefined; protected _busy: boolean = false; protected _finished: (() => void) | undefined; diff --git a/src/index-browser.ts b/src/index-browser.ts index 0eafb357..595ab5f7 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -14,10 +14,12 @@ import { ColorDistanceSensor } from "./colordistancesensor"; import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MotionSensor } from "./motionsensor"; import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; +import { TiltSensor } from "./tiltsensor"; import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -37,10 +39,12 @@ window.PoweredUP = { Device, Light, MediumLinearMotor, + MotionSensor, MoveHubMediumLinearMotor, SimpleMediumLinearMotor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, + TiltSensor, TrainMotor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index ffe988d1..48566c0b 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -14,10 +14,12 @@ import { ColorDistanceSensor } from "./colordistancesensor"; import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MotionSensor } from "./motionsensor"; import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; +import { TiltSensor } from "./tiltsensor"; import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -37,10 +39,12 @@ export { Device, Light, MediumLinearMotor, + MotionSensor, MoveHubMediumLinearMotor, SimpleMediumLinearMotor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, + TiltSensor, TrainMotor, isWebBluetooth }; diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index b573bb3f..bfd167ab 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -4,10 +4,12 @@ import { Hub } from "./hub"; import { ColorDistanceSensor } from "./colordistancesensor"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MotionSensor } from "./motionsensor"; import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; +import { TiltSensor } from "./tiltsensor"; import { TrainMotor } from "./trainmotor"; import * as Consts from "./consts"; @@ -301,6 +303,12 @@ export class LPF2Hub extends Hub { case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: device = new MoveHubMediumLinearMotor(this, portId); break; + case Consts.DeviceType.MOTION_SENSOR: + device = new MotionSensor(this, portId); + break; + case Consts.DeviceType.TILT_SENSOR: + device = new TiltSensor(this, portId); + break; case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: device = new MediumLinearMotor(this, portId); break; diff --git a/src/motionsensor.ts b/src/motionsensor.ts new file mode 100644 index 00000000..b68a0b01 --- /dev/null +++ b/src/motionsensor.ts @@ -0,0 +1,41 @@ +import { Device } from "./device"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class MotionSensor extends Device { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.MOTION_SENSOR); + + this.on("newListener", (event) => { + if (this.autoSubscribe) { + switch (event) { + case "distance": + this.subscribe(0x00); + break; + } + } + }); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case 0x00: + let distance = message[4]; + if (message[5] === 1) { + distance = message[4] + 255; + } + /** + * Emits when a distance sensor is activated. + * @event MotionSensor#distance + * @param {number} distance Distance, in millimeters. + */ + this.emit("distance", distance * 10); + break; + } + } + +} diff --git a/src/tiltsensor.ts b/src/tiltsensor.ts new file mode 100644 index 00000000..169c88ad --- /dev/null +++ b/src/tiltsensor.ts @@ -0,0 +1,40 @@ +import { Device } from "./device"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class TiltSensor extends Device { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.TILT_SENSOR); + + this.on("newListener", (event) => { + if (this.autoSubscribe) { + switch (event) { + case "tilt": + this.subscribe(0x00); + break; + } + } + }); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case 0x00: + const tiltX = message.readInt8(4); + const tiltY = message.readInt8(5); + /** + * Emits when a tilt sensor is activated. + * @event LPF2Hub#tilt + * @param {number} x + * @param {number} y + */ + this.emit("tilt", tiltX, tiltY); + break; + } + } + +} From 2a67242f347dde4a52a523e58f97d46dd5d13a8e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 12:13:41 -0800 Subject: [PATCH 15/62] Removed some commented code from hubs --- src/boostmovehub.ts | 148 ------------------------------------------ src/controlplushub.ts | 109 ------------------------------- 2 files changed, 257 deletions(-) diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index 5bcaea6c..4ef2f3b8 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -57,154 +57,6 @@ export class BoostMoveHub extends LPF2Hub { } - // /** - // * Set the motor speed on a given port. - // * @method BoostMoveHub#setMotorSpeed - // * @param {string} port - // * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - // * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - // */ - // public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { - // const portObj = this._portLookup(port); - // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - // throw new Error(`Port ${portObj.id} can only accept a single speed`); - // } - // let cancelEventTimer = true; - // if (typeof time === "boolean") { - // if (time === true) { - // cancelEventTimer = false; - // } - // time = undefined; - // } - // if (cancelEventTimer) { - // portObj.cancelEventTimer(); - // } - // return new Promise((resolve, reject) => { - // if (time && typeof time === "number") { - - // if ( - // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // ) { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // const timeout = global.setTimeout(() => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // return resolve(); - // // @ts-ignore: The type of time is properly checked at the start - // }, time); - // portObj.setEventTimer(timeout); - // } - - // } else { - - // if ( - // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // ) { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // } - - // } - // }); - // } - - - // /** - // * Tell motor to goto an absolute position - // * @method BoostMoveHub#setAbsolutePosition - // * @param {string} port - // * @param {number} pos The position of the motor to go to - // * @param {number | Array.} [speed=100] A value between 1 - 100 should be set (Direction does not apply when going to absolute position) - // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - // */ - // public setAbsolutePosition (port: string, pos: number, speed: number = 100) { - // const portObj = this._portLookup(port); - // if (!( - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // )) { - // throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - // } - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // data.writeInt32LE(pos, 4); - // data.writeInt32LE(pos, 8); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // data.writeInt32LE(pos, 4); - // } - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // }); - // } - - - // /** - // * Reset the current motor position as absolute position zero - // * @method BoostMoveHub#resetAbsolutePosition - // * @param {string} port - // * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). - // */ - // public resetAbsolutePosition (port: string) { - // const portObj = this._portLookup(port); - // if (!( - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // )) { - // throw new Error("Absolute positioning is only available when using a Control+ Medium Motor, or Control+ Large Motor"); - // } - // return new Promise((resolve) => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // return resolve(); - // }); - // } - - protected _checkFirmware (version: string) { if (compareVersion("2.0.00.0017", version) === 1) { throw new Error(`Your Boost Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`); diff --git a/src/controlplushub.ts b/src/controlplushub.ts index b2125798..5c481729 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -47,9 +47,6 @@ export class ControlPlusHub extends LPF2Hub { "GYRO": 98, "TILT": 99 }; - // // this.on("attach", (port, type) => { - // // this._combinePorts(port, type); - // // }); debug("Discovered Control+ Hub"); } @@ -65,112 +62,6 @@ export class ControlPlusHub extends LPF2Hub { } - // /** - // * Set the motor speed on a given port. - // * @method ControlPlusHub#setMotorSpeed - // * @param {string} port - // * @param {number | Array.} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds. - // * @param {number} [time] How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely. - // * @returns {Promise} Resolved upon successful completion of command. If time is specified, this is once the motor is finished. - // */ - // public setMotorSpeed (port: string, speed: number | [number, number], time?: number | boolean) { - // const portObj = this._portLookup(port); - // if (!this._virtualPorts[portObj.id] && speed instanceof Array) { - // throw new Error(`Port ${portObj.id} can only accept a single speed`); - // } - // let cancelEventTimer = true; - // if (typeof time === "boolean") { - // if (time === true) { - // cancelEventTimer = false; - // } - // time = undefined; - // } - // if (cancelEventTimer) { - // portObj.cancelEventTimer(); - // } - // return new Promise((resolve, reject) => { - // if (time && typeof time === "number") { - - // if ( - // portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || - // portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR || - // portObj.type === Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR - // ) { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // data.writeUInt16LE(time > 65535 ? 65535 : time, 4); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // const timeout = global.setTimeout(() => { - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // return resolve(); - // // @ts-ignore: The type of time is properly checked at the start - // }, time); - // portObj.setEventTimer(timeout); - // } - - // } else { - - // if (portObj.type === Consts.DeviceType.BOOST_TACHO_MOTOR || portObj.type === Consts.DeviceType.BOOST_MOVE_HUB_MOTOR) { - // portObj.busy = true; - // let data = null; - // if (this._virtualPorts[portObj.id]) { - // data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed instanceof Array ? speed[0] : speed), this._mapSpeed(speed instanceof Array ? speed[1] : speed), 0x64, 0x7f, 0x03]); - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - // } - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // portObj.finished = () => { - // return resolve(); - // }; - // } else { - // // @ts-ignore: The type of speed is properly checked at the start - // const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); - // this.send(data, Consts.BLECharacteristic.LPF2_ALL); - // } - - // } - // }); - // } - - - // /** - // * Ramp the motor speed on a given port. - // * @method ControlPlusHub#rampMotorSpeed - // * @param {string} port - // * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - // * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - // * @param {number} time How long the ramp should last (in milliseconds). - // * @returns {Promise} Resolved upon successful completion of command. - // */ - // public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - // const portObj = this._portLookup(port); - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // this._calculateRamp(fromSpeed, toSpeed, time, portObj) - // .on("changeSpeed", (speed) => { - // this.setMotorSpeed(port, speed, true); - // }) - // .on("finished", resolve); - // }); - // } - - // /** // * Tell motor to goto an absolute position // * @method ControlPlusHub#setAbsolutePosition From 023b141c4d2d77a6ac40226df258805a8bd8111e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 17:07:43 -0800 Subject: [PATCH 16/62] Better handling of device initialisation, implemented more modes --- examples/new_device_test.js | 35 ++++++++-- src/MoveHubMediumLinearMotor.ts | 7 +- src/basicmotor.ts | 9 +-- src/colordistancesensor.ts | 22 +++++- src/device.ts | 9 +-- src/duplotrainbase.ts | 2 +- src/hub.ts | 117 +++++++++++++++----------------- src/interfaces.ts | 9 +++ src/light.ts | 9 +-- src/lpf2hub.ts | 57 ++-------------- src/mediumlinearmotor.ts | 7 +- src/motionsensor.ts | 12 ++-- src/simplemediumlinearmotor.ts | 5 +- src/tachomotor.ts | 12 +++- src/techniclargelinearmotor.ts | 7 +- src/technicxlargelinearmotor.ts | 7 +- src/tiltsensor.ts | 10 +-- src/trainmotor.ts | 5 +- src/wedo2smarthub.ts | 116 ++++++++----------------------- 19 files changed, 205 insertions(+), 252 deletions(-) diff --git a/examples/new_device_test.js b/examples/new_device_test.js index 6364a2be..188dde69 100644 --- a/examples/new_device_test.js +++ b/examples/new_device_test.js @@ -24,12 +24,37 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs console.log(`Detached device ${device.type} from ${device.port}`); }); - if ( + if (( + device instanceof PoweredUP.SimpleMediumLinearMotor || + device instanceof PoweredUP.TrainMotor || + device instanceof PoweredUP.MediumLinearMotor || + device instanceof PoweredUP.TechnicLargeLinearMotor || + device instanceof PoweredUP.TechnicXLargeLinearMotor + ) && hub.type === PoweredUP.Consts.HubType.WEDO2_SMART_HUB) { + const motor = device; + + motor.on("rotate", (angle) => { + console.log(`Rotate ${angle}`); + }); + + motor.setPower(40); + await hub.sleep(2000); + motor.setPower(0); + await hub.sleep(2000); + motor.setPower(-40); + await hub.sleep(2000); + motor.setPower(0); + await hub.sleep(2000); + motor.setPower(20); + + } + + if (( // device instanceof PoweredUP.MoveHubMediumLinearMotor || device instanceof PoweredUP.MediumLinearMotor || device instanceof PoweredUP.TechnicLargeLinearMotor || device instanceof PoweredUP.TechnicXLargeLinearMotor - ) { + ) && hub.type !== PoweredUP.Consts.HubType.WEDO2_SMART_HUB) { const motor = device; motor.on("rotate", (angle) => { @@ -85,9 +110,9 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs sensor.on("distance", (distance) => { console.log(`Distance ${distance}`); }); - sensor.on("color", (color) => { - console.log(`Color ${color}`); - }); + // sensor.on("color", (color) => { + // console.log(`Color ${color}`); + // }); } if (device instanceof PoweredUP.MotionSensor) { diff --git a/src/MoveHubMediumLinearMotor.ts b/src/MoveHubMediumLinearMotor.ts index ffb07a74..c0889fc9 100644 --- a/src/MoveHubMediumLinearMotor.ts +++ b/src/MoveHubMediumLinearMotor.ts @@ -1,11 +1,12 @@ -import { Hub } from "./hub"; +import { TachoMotor } from "./tachomotor"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; -import { TachoMotor } from "./tachomotor"; export class MoveHubMediumLinearMotor extends TachoMotor { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR); } diff --git a/src/basicmotor.ts b/src/basicmotor.ts index fc06356a..2d6a5ea2 100644 --- a/src/basicmotor.ts +++ b/src/basicmotor.ts @@ -1,6 +1,6 @@ import { Device } from "./device"; -import { Hub } from "./hub"; -import { WeDo2SmartHub } from "./wedo2smarthub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; @@ -9,7 +9,7 @@ import { mapSpeed } from "./utils"; export class BasicMotor extends Device { - constructor (hub: Hub, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { + constructor (hub: IDeviceInterface, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(hub, portId, type); } @@ -22,7 +22,8 @@ export class BasicMotor extends Device { */ public setPower (power: number) { return new Promise((resolve) => { - if (this.hub instanceof WeDo2SmartHub) { + const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); + if (isWeDo2) { const data = Buffer.from([this.portId, 0x01, 0x02, mapSpeed(power)]); this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); } else { diff --git a/src/colordistancesensor.ts b/src/colordistancesensor.ts index 2d2fc6d8..05a66ccd 100644 --- a/src/colordistancesensor.ts +++ b/src/colordistancesensor.ts @@ -1,18 +1,26 @@ import { Device } from "./device"; -import { Hub } from "./hub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; export class ColorDistanceSensor extends Device { - constructor (hub: Hub, portId: number) { + private _isWeDo2: boolean; + + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR); + this._isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); this.on("newListener", (event) => { if (this.autoSubscribe) { switch (event) { case "color": - this.subscribe(0x08); + if (this._isWeDo2) { + this.subscribe(0x00); + } else { + this.subscribe(0x08); + } break; case "distance": this.subscribe(0x08); @@ -28,7 +36,15 @@ export class ColorDistanceSensor extends Device { public receive (message: Buffer) { const mode = this._mode; + console.log(message); + switch (mode) { + case 0x00: + if (this._isWeDo2 && message[2] <= 10) { + const color = message[2]; + this.emit("color", color); + } + break; case 0x08: /** * Emits when a color sensor is activated. diff --git a/src/device.ts b/src/device.ts index 8d95041f..f5dc0176 100644 --- a/src/device.ts +++ b/src/device.ts @@ -1,5 +1,6 @@ import { EventEmitter } from "events"; -import { Hub } from "./hub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; @@ -11,12 +12,12 @@ export class Device extends EventEmitter { protected _busy: boolean = false; protected _finished: (() => void) | undefined; - private _hub: Hub; + private _hub: IDeviceInterface; private _portId: number; private _connected: boolean = true; private _type: Consts.DeviceType; - constructor (hub: Hub, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { + constructor (hub: IDeviceInterface, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(); this._hub = hub; this._portId = portId; @@ -60,7 +61,7 @@ export class Device extends EventEmitter { this._ensureConnected(); if (mode !== this._mode) { this._mode = mode; - this.hub.subscribe(this.portId, mode); + this.hub.subscribe(this.portId, this.type, mode); } } diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index a28e1f46..294e6502 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -50,7 +50,7 @@ export class DuploTrainBase extends LPF2Hub { return new Promise(async (resolve, reject) => { debug("Connecting to Duplo Train Base"); await super.connect(); - this.subscribe(0x01, 0x01); + // this.subscribe(0x01, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER, 0x01); debug("Connect completed"); return resolve(); }); diff --git a/src/hub.ts b/src/hub.ts index cc905e08..fb4185b5 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -2,10 +2,21 @@ import { EventEmitter } from "events"; import { IBLEAbstraction } from "./interfaces"; +import { ColorDistanceSensor } from "./colordistancesensor"; +import { Device } from "./device"; +import { Light } from "./light"; +import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MotionSensor } from "./motionsensor"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; +import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; +import { TiltSensor } from "./tiltsensor"; +import { TrainMotor } from "./trainmotor"; + import * as Consts from "./consts"; import Debug = require("debug"); -import { Device } from "./device"; const debug = Debug("hub"); @@ -233,7 +244,7 @@ export class Hub extends EventEmitter { } - public subscribe (portId: number, mode: number) { + public subscribe (portId: number, deviceType: number, mode: number) { // NK Do nothing here } @@ -260,68 +271,52 @@ export class Hub extends EventEmitter { } - protected _getDeviceByPortId (portId: number) { - return this._attachedDevices[portId]; + protected _createDevice (deviceType: number, portId: number) { + let device; + + switch (deviceType) { + case Consts.DeviceType.LIGHT: + device = new Light(this, portId); + break; + case Consts.DeviceType.TRAIN_MOTOR: + device = new TrainMotor(this, portId); + break; + case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: + device = new SimpleMediumLinearMotor(this, portId); + break; + case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: + device = new MoveHubMediumLinearMotor(this, portId); + break; + case Consts.DeviceType.MOTION_SENSOR: + device = new MotionSensor(this, portId); + break; + case Consts.DeviceType.TILT_SENSOR: + device = new TiltSensor(this, portId); + break; + case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: + device = new MediumLinearMotor(this, portId); + break; + case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR: + device = new TechnicLargeLinearMotor(this, portId); + break; + case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR: + device = new TechnicXLargeLinearMotor(this, portId); + break; + case Consts.DeviceType.COLOR_DISTANCE_SENSOR: + device = new ColorDistanceSensor(this, portId); + break; + default: + device = new Device(this, portId, deviceType); + break; + } + + return device; } - // protected _calculateRamp (fromSpeed: number, toSpeed: number, time: number, port: Port) { - // const emitter = new EventEmitter(); - // const steps = Math.abs(toSpeed - fromSpeed); - // let delay = time / steps; - // let increment = 1; - // if (delay < 50 && steps > 0) { - // increment = 50 / delay; - // delay = 50; - // } - // if (fromSpeed > toSpeed) { - // increment = -increment; - // } - // let i = 0; - // const interval = setInterval(() => { - // let speed = Math.round(fromSpeed + (++i * increment)); - // if (toSpeed > fromSpeed && speed > toSpeed) { - // speed = toSpeed; - // } else if (fromSpeed > toSpeed && speed < toSpeed) { - // speed = toSpeed; - // } - // emitter.emit("changeSpeed", speed); - // if (speed === toSpeed) { - // clearInterval(interval); - // emitter.emit("finished"); - // } - // }, delay); - // port.setEventTimer(interval); - // return emitter; - // } - - - // private _getModeForDeviceType (type: Consts.DeviceType) { - // switch (type) { - // case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: - // return 0x02; - // case Consts.DeviceType.TRAIN_MOTOR: - // return 0x02; - // case Consts.DeviceType.BOOST_TACHO_MOTOR: - // return 0x02; - // case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: - // return 0x02; - // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: - // return 0x02; - // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: - // return 0x02; - // case Consts.DeviceType.CONTROL_PLUS_TILT: - // return 0x00; - // case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: - // return 0x00; - // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: - // return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08); - // case Consts.DeviceType.BOOST_TILT: - // return 0x04; - // default: - // return 0x00; - // } - // } + protected _getDeviceByPortId (portId: number) { + return this._attachedDevices[portId]; + } } diff --git a/src/interfaces.ts b/src/interfaces.ts index cdb1bce0..ea7111c7 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,5 +1,7 @@ import { EventEmitter } from "events"; +import * as Consts from "./consts"; + export interface IBLEAbstraction extends EventEmitter { uuid: string; name: string; @@ -13,3 +15,10 @@ export interface IBLEAbstraction extends EventEmitter { readFromCharacteristic: (uuid: string, callback: (err: string | null, data: Buffer | null) => void) => void; writeToCharacteristic: (uuid: string, data: Buffer, callback?: () => void) => void; } + +export interface IDeviceInterface extends EventEmitter { + type: Consts.HubType; + getPortNameForPortId: (portId: number) => string | undefined; + send: (message: Buffer, uuid: string, callback?: () => void) => void; + subscribe: (portId: number, deviceType: number, mode: number) => void; +} diff --git a/src/light.ts b/src/light.ts index fda7104a..67a242e6 100644 --- a/src/light.ts +++ b/src/light.ts @@ -1,13 +1,13 @@ import { Device } from "./device"; -import { Hub } from "./hub"; -import { WeDo2SmartHub } from "./wedo2smarthub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; export class Light extends Device { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.LIGHT); } @@ -20,7 +20,8 @@ export class Light extends Device { */ public setBrightness (brightness: number) { return new Promise((resolve) => { - if (this.hub instanceof WeDo2SmartHub) { + const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); + if (isWeDo2) { const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); } else { diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index bfd167ab..6905e16c 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -1,17 +1,5 @@ -import { Device } from "./device"; import { Hub } from "./hub"; -import { ColorDistanceSensor } from "./colordistancesensor"; -import { Light } from "./light"; -import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; -import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; -import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; -import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; -import { TiltSensor } from "./tiltsensor"; -import { TrainMotor } from "./trainmotor"; - import * as Consts from "./consts"; import { decodeMACAddress, decodeVersion, toBin, toHex } from "./utils"; @@ -48,10 +36,10 @@ export class LPF2Hub extends Hub { await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); if (this._voltagePort !== undefined) { - this.subscribe(this._voltagePort, 0x00); // Activate voltage reports + this.subscribe(this._voltagePort, Consts.DeviceType.VOLTAGE, 0x00); // Activate voltage reports } if (this._currentPort !== undefined) { - this.subscribe(this._currentPort, 0x00); // Activate currrent reports + this.subscribe(this._currentPort, Consts.DeviceType.CURRENT, 0x00); // Activate currrent reports } await this.sleep(100); this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports @@ -149,7 +137,7 @@ export class LPF2Hub extends Hub { } - public subscribe (portId: number, mode: number) { + public subscribe (portId: number, deviceType: number, mode: number) { this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); } @@ -288,44 +276,6 @@ export class LPF2Hub extends Hub { // Handle device attachments if (event === 0x01) { - let device; - - switch (deviceType) { - case Consts.DeviceType.LIGHT: - device = new Light(this, portId); - break; - case Consts.DeviceType.TRAIN_MOTOR: - device = new TrainMotor(this, portId); - break; - case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: - device = new SimpleMediumLinearMotor(this, portId); - break; - case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: - device = new MoveHubMediumLinearMotor(this, portId); - break; - case Consts.DeviceType.MOTION_SENSOR: - device = new MotionSensor(this, portId); - break; - case Consts.DeviceType.TILT_SENSOR: - device = new TiltSensor(this, portId); - break; - case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: - device = new MediumLinearMotor(this, portId); - break; - case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR: - device = new TechnicLargeLinearMotor(this, portId); - break; - case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR: - device = new TechnicXLargeLinearMotor(this, portId); - break; - case Consts.DeviceType.COLOR_DISTANCE_SENSOR: - device = new ColorDistanceSensor(this, portId); - break; - default: - device = new Device(this, portId, deviceType); - break; - } - if (modeInfoDebug.enabled) { const deviceTypeName = Consts.DeviceTypeNames[message[5]] || "Unknown"; modeInfoDebug(`Port ${toHex(portId)}, type ${toHex(deviceType, 4)} (${deviceTypeName})`); @@ -335,6 +285,7 @@ export class LPF2Hub extends Hub { this._sendPortInformationRequest(portId); } + const device = this._createDevice(deviceType, portId); this._attachDevice(device); // Handle device detachments diff --git a/src/mediumlinearmotor.ts b/src/mediumlinearmotor.ts index 53680a6c..b3af724d 100644 --- a/src/mediumlinearmotor.ts +++ b/src/mediumlinearmotor.ts @@ -1,11 +1,12 @@ -import { Hub } from "./hub"; +import { TachoMotor } from "./tachomotor"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; -import { TachoMotor } from "./tachomotor"; export class MediumLinearMotor extends TachoMotor { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.MEDIUM_LINEAR_MOTOR); } diff --git a/src/motionsensor.ts b/src/motionsensor.ts index b68a0b01..e47d7642 100644 --- a/src/motionsensor.ts +++ b/src/motionsensor.ts @@ -1,11 +1,12 @@ import { Device } from "./device"; -import { Hub } from "./hub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; export class MotionSensor extends Device { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.MOTION_SENSOR); this.on("newListener", (event) => { @@ -21,12 +22,13 @@ export class MotionSensor extends Device { public receive (message: Buffer) { const mode = this._mode; + const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { case 0x00: - let distance = message[4]; - if (message[5] === 1) { - distance = message[4] + 255; + let distance = message[isWeDo2 ? 2 : 4]; + if (message[isWeDo2 ? 3 : 5] === 1) { + distance = distance + 255; } /** * Emits when a distance sensor is activated. diff --git a/src/simplemediumlinearmotor.ts b/src/simplemediumlinearmotor.ts index 824b0f37..41ca50b5 100644 --- a/src/simplemediumlinearmotor.ts +++ b/src/simplemediumlinearmotor.ts @@ -1,11 +1,12 @@ import { BasicMotor } from "./basicmotor"; -import { Hub } from "./hub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; export class SimpleMediumLinearMotor extends BasicMotor { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR); } diff --git a/src/tachomotor.ts b/src/tachomotor.ts index 3fee32ed..00cbe28b 100644 --- a/src/tachomotor.ts +++ b/src/tachomotor.ts @@ -1,12 +1,13 @@ import { BasicMotor } from "./basicmotor"; -import { Hub } from "./hub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; import { mapSpeed } from "./utils"; export class TachoMotor extends BasicMotor { - constructor (hub: Hub, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { + constructor (hub: IDeviceInterface, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(hub, portId, type); this.on("newListener", (event) => { @@ -22,10 +23,11 @@ export class TachoMotor extends BasicMotor { public receive (message: Buffer) { const mode = this._mode; + const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { case 0x02: - const rotation = message.readInt32LE(4); + const rotation = message.readInt32LE(isWeDo2 ? 2 : 4); /** * Emits when a rotation sensor is activated. * @event TachoMotor#rotate @@ -44,6 +46,10 @@ export class TachoMotor extends BasicMotor { * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ public rotateByAngle (angle: number, power: number = 100) { + const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); + if (isWeDo2) { + throw new Error("Rotating by angle is not available on the WeDo 2.0 Smart Hub"); + } return new Promise((resolve) => { this._busy = true; const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(power), 0x64, 0x7f, 0x03]); diff --git a/src/techniclargelinearmotor.ts b/src/techniclargelinearmotor.ts index 5f65f088..d68199e6 100644 --- a/src/techniclargelinearmotor.ts +++ b/src/techniclargelinearmotor.ts @@ -1,11 +1,12 @@ -import { Hub } from "./hub"; +import { TachoMotor } from "./tachomotor"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; -import { TachoMotor } from "./tachomotor"; export class TechnicLargeLinearMotor extends TachoMotor { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR); } diff --git a/src/technicxlargelinearmotor.ts b/src/technicxlargelinearmotor.ts index 4a24ccca..9974ce25 100644 --- a/src/technicxlargelinearmotor.ts +++ b/src/technicxlargelinearmotor.ts @@ -1,11 +1,12 @@ -import { Hub } from "./hub"; +import { TachoMotor } from "./tachomotor"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; -import { TachoMotor } from "./tachomotor"; export class TechnicXLargeLinearMotor extends TachoMotor { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR); } diff --git a/src/tiltsensor.ts b/src/tiltsensor.ts index 169c88ad..e6f8a2a9 100644 --- a/src/tiltsensor.ts +++ b/src/tiltsensor.ts @@ -1,11 +1,12 @@ import { Device } from "./device"; -import { Hub } from "./hub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; export class TiltSensor extends Device { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.TILT_SENSOR); this.on("newListener", (event) => { @@ -21,11 +22,12 @@ export class TiltSensor extends Device { public receive (message: Buffer) { const mode = this._mode; + const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { case 0x00: - const tiltX = message.readInt8(4); - const tiltY = message.readInt8(5); + const tiltX = message.readInt8(isWeDo2 ? 2 : 4); + const tiltY = message.readInt8(isWeDo2 ? 3 : 5); /** * Emits when a tilt sensor is activated. * @event LPF2Hub#tilt diff --git a/src/trainmotor.ts b/src/trainmotor.ts index 6f590a61..ca591b2e 100644 --- a/src/trainmotor.ts +++ b/src/trainmotor.ts @@ -1,11 +1,12 @@ import { BasicMotor } from "./basicmotor"; -import { Hub } from "./hub"; + +import { IDeviceInterface } from "./interfaces"; import * as Consts from "./consts"; export class TrainMotor extends BasicMotor { - constructor (hub: Hub, portId: number) { + constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, Consts.DeviceType.TRAIN_MOTOR); } diff --git a/src/wedo2smarthub.ts b/src/wedo2smarthub.ts index dc812843..a08fdce8 100644 --- a/src/wedo2smarthub.ts +++ b/src/wedo2smarthub.ts @@ -2,18 +2,8 @@ import { Peripheral } from "@abandonware/noble"; import { IBLEAbstraction } from "./interfaces"; -import { Device } from "./device"; import { Hub } from "./hub"; -import { ColorDistanceSensor } from "./colordistancesensor"; -import { Light } from "./light"; -import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; -import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; -import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; -import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; -import { TrainMotor } from "./trainmotor"; - import * as Consts from "./consts"; import { isWebBluetooth } from "./utils"; @@ -67,8 +57,8 @@ export class WeDo2SmartHub extends Hub { await this._bleDevice.discoverCharacteristicsForService("battery_service"); await this._bleDevice.discoverCharacteristicsForService("device_information"); } - this._activatePortDevice(0x03, 0x15, 0x00, 0x00); // Activate voltage reports - this._activatePortDevice(0x04, 0x14, 0x00, 0x00); // Activate current reports + this.subscribe(0x03, 0x15, 0x00); // Activate voltage reports + this.subscribe(0x04, 0x14, 0x00); // Activate current reports debug("Connect completed"); this.emit("connect"); resolve(); @@ -230,8 +220,8 @@ export class WeDo2SmartHub extends Hub { } - protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this.send(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, callback); + public subscribe (portId: number, deviceType: number, mode: number) { + this.send(Buffer.from([0x01, 0x02, portId, deviceType, mode, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); } @@ -277,47 +267,43 @@ export class WeDo2SmartHub extends Hub { const deviceType = event ? data[3] : 0; if (event === 0x01) { - - let device; - - switch (deviceType) { - case Consts.DeviceType.LIGHT: - device = new Light(this, portId); - break; - // case Consts.DeviceType.BOOST_TACHO_MOTOR: - // device = new BoostTachoMotor(this, portId); - // break; - // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: - // device = new ControlPlusLargeMotor(this, portId); - // break; - // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: - // device = new ControlPlusXLargeMotor(this, portId); - // break; - case Consts.DeviceType.COLOR_DISTANCE_SENSOR: - device = new ColorDistanceSensor(this, portId); - break; - default: - device = new Device(this, portId, deviceType); - break; - } - + const device = this._createDevice(deviceType, portId); this._attachDevice(device); - + } else if (event === 0x00) { + const device = this._getDeviceByPortId(portId); + if (device) { + this._detachDevice(device); + } } } private _parseSensorMessage (message: Buffer) { + debug("Received Message (WEDO2_SENSOR_VALUE)", message); + + if (message[0] === 0x01) { + /** + * Emits when a button is pressed. + * @event WeDo2SmartHub#button + * @param {string} button + * @param {ButtonState} state + */ + this.emit("button", "GREEN", Consts.ButtonState.PRESSED); + return; + } else if (message[0] === 0x00) { + this.emit("button", "GREEN", Consts.ButtonState.RELEASED); + return; + } + const portId = message[1]; const device = this._getDeviceByPortId(portId); if (device) { + console.log(portId, device.type); device.receive(message); } - // debug("Received Message (WEDO2_SENSOR_VALUE)", data); - // if (data[0] === 0x01) { // /** // * Emits when a button is pressed. @@ -350,20 +336,6 @@ export class WeDo2SmartHub extends Hub { // if (port && port.connected) { // switch (port.type) { - // case Consts.DeviceType.WEDO2_DISTANCE: { - // let distance = data[2]; - // if (data[3] === 1) { - // distance = data[2] + 255; - // } - // /** - // * Emits when a distance sensor is activated. - // * @event WeDo2SmartHub#distance - // * @param {string} port - // * @param {number} distance Distance, in millimeters. - // */ - // this.emit("distance", port.id, distance * 10); - // break; - // } // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { // const distance = data[2]; // /** @@ -375,40 +347,6 @@ export class WeDo2SmartHub extends Hub { // this.emit("color", port.id, distance); // break; // } - // case Consts.DeviceType.WEDO2_TILT: { - // this._lastTiltX = data.readInt8(2); - // this._lastTiltY = data.readInt8(3); - // /** - // * Emits when a tilt sensor is activated. - // * @event WeDo2SmartHub#tilt - // * @param {string} port - // * @param {number} x - // * @param {number} y - // */ - // this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); - // break; - // } - // case Consts.DeviceType.BOOST_TACHO_MOTOR: { - // const rotation = data.readInt32LE(2); - // /** - // * Emits when a rotation sensor is activated. - // * @event WeDo2SmartHub#rotate - // * @param {string} port - // * @param {number} rotation - // */ - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: { - // const rotation = data.readInt32LE(2); - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: { - // const rotation = data.readInt32LE(2); - // this.emit("rotate", port.id, rotation); - // break; - // } // } // } From 37be7f85b64552fe01259e6a45b8073ce74b1d18 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 17:10:50 -0800 Subject: [PATCH 17/62] Rename --- ...MoveHubMediumLinearMotor.ts => MoveHubMediumLinearMotor2.ts} | 0 src/hub.ts | 2 +- src/index-browser.ts | 2 +- src/index-node.ts | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename src/{MoveHubMediumLinearMotor.ts => MoveHubMediumLinearMotor2.ts} (100%) diff --git a/src/MoveHubMediumLinearMotor.ts b/src/MoveHubMediumLinearMotor2.ts similarity index 100% rename from src/MoveHubMediumLinearMotor.ts rename to src/MoveHubMediumLinearMotor2.ts diff --git a/src/hub.ts b/src/hub.ts index fb4185b5..6e9e7469 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -7,7 +7,7 @@ import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; +import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor2"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; diff --git a/src/index-browser.ts b/src/index-browser.ts index 595ab5f7..a032b5b8 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -15,7 +15,7 @@ import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; +import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor2"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; diff --git a/src/index-node.ts b/src/index-node.ts index 48566c0b..0e498c31 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -15,7 +15,7 @@ import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; +import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor2"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; From 406e9e99bc8078d4efc2f9b36473facf872eadde Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 17:11:30 -0800 Subject: [PATCH 18/62] Rename again --- src/hub.ts | 2 +- src/index-browser.ts | 2 +- src/index-node.ts | 2 +- ...MoveHubMediumLinearMotor2.ts => movehubmediumlinearmotor.ts} | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename src/{MoveHubMediumLinearMotor2.ts => movehubmediumlinearmotor.ts} (100%) diff --git a/src/hub.ts b/src/hub.ts index 6e9e7469..fb4185b5 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -7,7 +7,7 @@ import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor2"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; diff --git a/src/index-browser.ts b/src/index-browser.ts index a032b5b8..595ab5f7 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -15,7 +15,7 @@ import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor2"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; diff --git a/src/index-node.ts b/src/index-node.ts index 0e498c31..48566c0b 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -15,7 +15,7 @@ import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./MoveHubMediumLinearMotor2"; +import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; diff --git a/src/MoveHubMediumLinearMotor2.ts b/src/movehubmediumlinearmotor.ts similarity index 100% rename from src/MoveHubMediumLinearMotor2.ts rename to src/movehubmediumlinearmotor.ts From 225f331811955f824ba607015534a2de3d80ccce Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 17:31:22 -0800 Subject: [PATCH 19/62] Better directory structure --- package.json | 4 +-- src/{ => devices}/basicmotor.ts | 6 ++-- src/{ => devices}/colordistancesensor.ts | 4 +-- src/{ => devices}/device.ts | 4 +-- src/{ => devices}/light.ts | 4 +-- src/{ => devices}/mediumlinearmotor.ts | 4 +-- src/{ => devices}/motionsensor.ts | 4 +-- src/{ => devices}/movehubmediumlinearmotor.ts | 4 +-- src/{ => devices}/simplemediumlinearmotor.ts | 4 +-- src/{ => devices}/tachomotor.ts | 6 ++-- src/{ => devices}/techniclargelinearmotor.ts | 4 +-- src/{ => devices}/technicxlargelinearmotor.ts | 4 +-- src/{ => devices}/tiltsensor.ts | 4 +-- src/{ => devices}/trainmotor.ts | 4 +-- src/{ => hubs}/boostmovehub.ts | 4 +-- src/{ => hubs}/controlplushub.ts | 4 +-- src/{ => hubs}/duplotrainbase.ts | 4 +-- src/{ => hubs}/hub.ts | 32 ++++++++--------- src/{ => hubs}/lpf2hub.ts | 4 +-- src/{ => hubs}/puphub.ts | 4 +-- src/{ => hubs}/pupremote.ts | 4 +-- src/{ => hubs}/wedo2smarthub.ts | 6 ++-- src/index-browser.ts | 36 +++++++++---------- src/index-node.ts | 36 +++++++++---------- src/poweredup-browser.ts | 15 ++++---- src/poweredup-node.ts | 15 ++++---- 26 files changed, 112 insertions(+), 112 deletions(-) rename src/{ => devices}/basicmotor.ts (91%) rename src/{ => devices}/colordistancesensor.ts (97%) rename src/{ => devices}/device.ts (96%) rename src/{ => devices}/light.ts (91%) rename src/{ => devices}/mediumlinearmotor.ts (72%) rename src/{ => devices}/motionsensor.ts (93%) rename src/{ => devices}/movehubmediumlinearmotor.ts (74%) rename src/{ => devices}/simplemediumlinearmotor.ts (73%) rename src/{ => devices}/tachomotor.ts (94%) rename src/{ => devices}/techniclargelinearmotor.ts (73%) rename src/{ => devices}/technicxlargelinearmotor.ts (74%) rename src/{ => devices}/tiltsensor.ts (92%) rename src/{ => devices}/trainmotor.ts (71%) rename src/{ => hubs}/boostmovehub.ts (95%) rename src/{ => hubs}/controlplushub.ts (98%) rename src/{ => hubs}/duplotrainbase.ts (96%) rename src/{ => hubs}/hub.ts (90%) rename src/{ => hubs}/lpf2hub.ts (99%) rename src/{ => hubs}/puphub.ts (95%) rename src/{ => hubs}/pupremote.ts (94%) rename src/{ => hubs}/wedo2smarthub.ts (99%) diff --git a/package.json b/package.json index 7872f7f0..473850b1 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "main": "dist/node/index-node.js", "types": "dist/node/index-node.d.ts", "scripts": { - "build:node": "tslint -c tslint.json \"./src/*.ts\" && tsc", - "build:browser": "tslint -c tslint.json \"./src/*.ts\" && webpack --mode=production", + "build:node": "tslint -c tslint.json \"./src/**/*.ts\" && tsc", + "build:browser": "tslint -c tslint.json \"./src/**/*.ts\" && webpack --mode=production", "build:all": "tslint -c tslint.json \"./src/*.ts\" && tsc && webpack --mode=production", "docs": "jsdoc -d docs -c jsdoc.conf.json -t ./node_modules/ink-docstrap/template -R README.md dist/node/consts.js dist/node/poweredup-node.js dist/node/lpf2hub.js dist/node/wedo2smarthub.js dist/node/boostmovehub.js dist/node/puphub.js dist/node/pupremote.js dist/node/duplotrainbase.js dist/node/controlplushub.js dist/node/hub.js dist/node/consts.js", "all": "npm run build:all && npm run docs", diff --git a/src/basicmotor.ts b/src/devices/basicmotor.ts similarity index 91% rename from src/basicmotor.ts rename to src/devices/basicmotor.ts index 2d6a5ea2..aeec7f18 100644 --- a/src/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -1,10 +1,10 @@ import { Device } from "./device"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; -import { mapSpeed } from "./utils"; +import { mapSpeed } from "../utils"; export class BasicMotor extends Device { diff --git a/src/colordistancesensor.ts b/src/devices/colordistancesensor.ts similarity index 97% rename from src/colordistancesensor.ts rename to src/devices/colordistancesensor.ts index 05a66ccd..7589759d 100644 --- a/src/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -1,8 +1,8 @@ import { Device } from "./device"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class ColorDistanceSensor extends Device { diff --git a/src/device.ts b/src/devices/device.ts similarity index 96% rename from src/device.ts rename to src/devices/device.ts index f5dc0176..d7673176 100644 --- a/src/device.ts +++ b/src/devices/device.ts @@ -1,8 +1,8 @@ import { EventEmitter } from "events"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class Device extends EventEmitter { diff --git a/src/light.ts b/src/devices/light.ts similarity index 91% rename from src/light.ts rename to src/devices/light.ts index 67a242e6..8e5b3cb5 100644 --- a/src/light.ts +++ b/src/devices/light.ts @@ -1,8 +1,8 @@ import { Device } from "./device"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class Light extends Device { diff --git a/src/mediumlinearmotor.ts b/src/devices/mediumlinearmotor.ts similarity index 72% rename from src/mediumlinearmotor.ts rename to src/devices/mediumlinearmotor.ts index b3af724d..0e810006 100644 --- a/src/mediumlinearmotor.ts +++ b/src/devices/mediumlinearmotor.ts @@ -1,8 +1,8 @@ import { TachoMotor } from "./tachomotor"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class MediumLinearMotor extends TachoMotor { diff --git a/src/motionsensor.ts b/src/devices/motionsensor.ts similarity index 93% rename from src/motionsensor.ts rename to src/devices/motionsensor.ts index e47d7642..0bb17179 100644 --- a/src/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -1,8 +1,8 @@ import { Device } from "./device"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class MotionSensor extends Device { diff --git a/src/movehubmediumlinearmotor.ts b/src/devices/movehubmediumlinearmotor.ts similarity index 74% rename from src/movehubmediumlinearmotor.ts rename to src/devices/movehubmediumlinearmotor.ts index c0889fc9..cd4ec768 100644 --- a/src/movehubmediumlinearmotor.ts +++ b/src/devices/movehubmediumlinearmotor.ts @@ -1,8 +1,8 @@ import { TachoMotor } from "./tachomotor"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class MoveHubMediumLinearMotor extends TachoMotor { diff --git a/src/simplemediumlinearmotor.ts b/src/devices/simplemediumlinearmotor.ts similarity index 73% rename from src/simplemediumlinearmotor.ts rename to src/devices/simplemediumlinearmotor.ts index 41ca50b5..e7d99bf5 100644 --- a/src/simplemediumlinearmotor.ts +++ b/src/devices/simplemediumlinearmotor.ts @@ -1,8 +1,8 @@ import { BasicMotor } from "./basicmotor"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class SimpleMediumLinearMotor extends BasicMotor { diff --git a/src/tachomotor.ts b/src/devices/tachomotor.ts similarity index 94% rename from src/tachomotor.ts rename to src/devices/tachomotor.ts index 00cbe28b..cae01c6f 100644 --- a/src/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -1,9 +1,9 @@ import { BasicMotor } from "./basicmotor"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; -import { mapSpeed } from "./utils"; +import * as Consts from "../consts"; +import { mapSpeed } from "../utils"; export class TachoMotor extends BasicMotor { diff --git a/src/techniclargelinearmotor.ts b/src/devices/techniclargelinearmotor.ts similarity index 73% rename from src/techniclargelinearmotor.ts rename to src/devices/techniclargelinearmotor.ts index d68199e6..bcff3509 100644 --- a/src/techniclargelinearmotor.ts +++ b/src/devices/techniclargelinearmotor.ts @@ -1,8 +1,8 @@ import { TachoMotor } from "./tachomotor"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class TechnicLargeLinearMotor extends TachoMotor { diff --git a/src/technicxlargelinearmotor.ts b/src/devices/technicxlargelinearmotor.ts similarity index 74% rename from src/technicxlargelinearmotor.ts rename to src/devices/technicxlargelinearmotor.ts index 9974ce25..fe230b5d 100644 --- a/src/technicxlargelinearmotor.ts +++ b/src/devices/technicxlargelinearmotor.ts @@ -1,8 +1,8 @@ import { TachoMotor } from "./tachomotor"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class TechnicXLargeLinearMotor extends TachoMotor { diff --git a/src/tiltsensor.ts b/src/devices/tiltsensor.ts similarity index 92% rename from src/tiltsensor.ts rename to src/devices/tiltsensor.ts index e6f8a2a9..a0e76762 100644 --- a/src/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -1,8 +1,8 @@ import { Device } from "./device"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class TiltSensor extends Device { diff --git a/src/trainmotor.ts b/src/devices/trainmotor.ts similarity index 71% rename from src/trainmotor.ts rename to src/devices/trainmotor.ts index ca591b2e..39d1e06b 100644 --- a/src/trainmotor.ts +++ b/src/devices/trainmotor.ts @@ -1,8 +1,8 @@ import { BasicMotor } from "./basicmotor"; -import { IDeviceInterface } from "./interfaces"; +import { IDeviceInterface } from "../interfaces"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; export class TrainMotor extends BasicMotor { diff --git a/src/boostmovehub.ts b/src/hubs/boostmovehub.ts similarity index 95% rename from src/boostmovehub.ts rename to src/hubs/boostmovehub.ts index 4ef2f3b8..0870f873 100644 --- a/src/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -1,11 +1,11 @@ import { Peripheral } from "@abandonware/noble"; import compareVersion from "compare-versions"; -import { IBLEAbstraction } from "./interfaces"; +import { IBLEAbstraction } from "../interfaces"; import { LPF2Hub } from "./lpf2hub"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; import Debug = require("debug"); const debug = Debug("boostmovehub"); diff --git a/src/controlplushub.ts b/src/hubs/controlplushub.ts similarity index 98% rename from src/controlplushub.ts rename to src/hubs/controlplushub.ts index 5c481729..9eb3c7ec 100644 --- a/src/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -1,10 +1,10 @@ import { Peripheral } from "@abandonware/noble"; -import { IBLEAbstraction } from "./interfaces"; +import { IBLEAbstraction } from "../interfaces"; import { LPF2Hub } from "./lpf2hub"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; import Debug = require("debug"); const debug = Debug("ControlPlusHub"); diff --git a/src/duplotrainbase.ts b/src/hubs/duplotrainbase.ts similarity index 96% rename from src/duplotrainbase.ts rename to src/hubs/duplotrainbase.ts index 294e6502..c8a01fb2 100644 --- a/src/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -2,10 +2,10 @@ import { Peripheral } from "@abandonware/noble"; import { LPF2Hub } from "./lpf2hub"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; import Debug = require("debug"); -import { IBLEAbstraction } from "./interfaces"; +import { IBLEAbstraction } from "../interfaces"; const debug = Debug("duplotrainbase"); diff --git a/src/hub.ts b/src/hubs/hub.ts similarity index 90% rename from src/hub.ts rename to src/hubs/hub.ts index fb4185b5..dce1ec37 100644 --- a/src/hub.ts +++ b/src/hubs/hub.ts @@ -1,20 +1,20 @@ import { EventEmitter } from "events"; -import { IBLEAbstraction } from "./interfaces"; - -import { ColorDistanceSensor } from "./colordistancesensor"; -import { Device } from "./device"; -import { Light } from "./light"; -import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; -import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; -import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; -import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; -import { TiltSensor } from "./tiltsensor"; -import { TrainMotor } from "./trainmotor"; - -import * as Consts from "./consts"; +import { IBLEAbstraction } from "../interfaces"; + +import { ColorDistanceSensor } from "../devices/colordistancesensor"; +import { Device } from "../devices/device"; +import { Light } from "../devices/light"; +import { MediumLinearMotor } from "../devices/mediumlinearmotor"; +import { MotionSensor } from "../devices/motionsensor"; +import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor"; +import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor"; +import { TiltSensor } from "../devices/tiltsensor"; +import { TrainMotor } from "../devices/trainmotor"; + +import * as Consts from "../consts"; import Debug = require("debug"); const debug = Debug("hub"); @@ -26,8 +26,6 @@ const debug = Debug("hub"); */ export class Hub extends EventEmitter { - public useSpeedMap: boolean = true; - protected _attachedDevices: {[portId: number]: Device} = {}; protected _portNames: {[port: string]: number} = {}; diff --git a/src/lpf2hub.ts b/src/hubs/lpf2hub.ts similarity index 99% rename from src/lpf2hub.ts rename to src/hubs/lpf2hub.ts index 6905e16c..200fcc49 100644 --- a/src/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -1,8 +1,8 @@ import { Hub } from "./hub"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; -import { decodeMACAddress, decodeVersion, toBin, toHex } from "./utils"; +import { decodeMACAddress, decodeVersion, toBin, toHex } from "../utils"; import Debug = require("debug"); const debug = Debug("lpf2hub"); diff --git a/src/puphub.ts b/src/hubs/puphub.ts similarity index 95% rename from src/puphub.ts rename to src/hubs/puphub.ts index 21349989..43dd1453 100644 --- a/src/puphub.ts +++ b/src/hubs/puphub.ts @@ -1,11 +1,11 @@ import { Peripheral } from "@abandonware/noble"; import compareVersion from "compare-versions"; -import { IBLEAbstraction } from "./interfaces"; +import { IBLEAbstraction } from "../interfaces"; import { LPF2Hub } from "./lpf2hub"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; import Debug = require("debug"); const debug = Debug("puphub"); diff --git a/src/pupremote.ts b/src/hubs/pupremote.ts similarity index 94% rename from src/pupremote.ts rename to src/hubs/pupremote.ts index 24886c75..2ce066ef 100644 --- a/src/pupremote.ts +++ b/src/hubs/pupremote.ts @@ -1,10 +1,10 @@ import { Peripheral } from "@abandonware/noble"; -import { IBLEAbstraction } from "./interfaces"; +import { IBLEAbstraction } from "../interfaces"; import { LPF2Hub } from "./lpf2hub"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; import Debug = require("debug"); const debug = Debug("pupremote"); diff --git a/src/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts similarity index 99% rename from src/wedo2smarthub.ts rename to src/hubs/wedo2smarthub.ts index a08fdce8..10bb776d 100644 --- a/src/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -1,12 +1,12 @@ import { Peripheral } from "@abandonware/noble"; -import { IBLEAbstraction } from "./interfaces"; +import { IBLEAbstraction } from "../interfaces"; import { Hub } from "./hub"; -import * as Consts from "./consts"; +import * as Consts from "../consts"; -import { isWebBluetooth } from "./utils"; +import { isWebBluetooth } from "../utils"; import Debug = require("debug"); const debug = Debug("wedo2smarthub"); diff --git a/src/index-browser.ts b/src/index-browser.ts index 595ab5f7..3eb7e07c 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -2,25 +2,25 @@ import * as Consts from "./consts"; import { PoweredUP } from "./poweredup-browser"; -import { BoostMoveHub } from "./boostmovehub"; -import { ControlPlusHub } from "./controlplushub"; -import { DuploTrainBase } from "./duplotrainbase"; -import { Hub } from "./hub"; -import { PUPHub } from "./puphub"; -import { PUPRemote } from "./pupremote"; -import { WeDo2SmartHub } from "./wedo2smarthub"; +import { BoostMoveHub } from "./hubs/boostmovehub"; +import { ControlPlusHub } from "./hubs/controlplushub"; +import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { Hub } from "./hubs/hub"; +import { PUPHub } from "./hubs/puphub"; +import { PUPRemote } from "./hubs/pupremote"; +import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; -import { ColorDistanceSensor } from "./colordistancesensor"; -import { Device } from "./device"; -import { Light } from "./light"; -import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; -import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; -import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; -import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; -import { TiltSensor } from "./tiltsensor"; -import { TrainMotor } from "./trainmotor"; +import { ColorDistanceSensor } from "./devices/colordistancesensor"; +import { Device } from "./devices/device"; +import { Light } from "./devices/light"; +import { MediumLinearMotor } from "./devices/mediumlinearmotor"; +import { MotionSensor } from "./devices/motionsensor"; +import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; +import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; +import { TiltSensor } from "./devices/tiltsensor"; +import { TrainMotor } from "./devices/trainmotor"; import { isWebBluetooth } from "./utils"; diff --git a/src/index-node.ts b/src/index-node.ts index 48566c0b..b4f4eeb5 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -2,25 +2,25 @@ import * as Consts from "./consts"; import { PoweredUP } from "./poweredup-node"; -import { BoostMoveHub } from "./boostmovehub"; -import { ControlPlusHub } from "./controlplushub"; -import { DuploTrainBase } from "./duplotrainbase"; -import { Hub } from "./hub"; -import { PUPHub } from "./puphub"; -import { PUPRemote } from "./pupremote"; -import { WeDo2SmartHub } from "./wedo2smarthub"; +import { BoostMoveHub } from "./hubs/boostmovehub"; +import { ControlPlusHub } from "./hubs/controlplushub"; +import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { Hub } from "./hubs/hub"; +import { PUPHub } from "./hubs/puphub"; +import { PUPRemote } from "./hubs/pupremote"; +import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; -import { ColorDistanceSensor } from "./colordistancesensor"; -import { Device } from "./device"; -import { Light } from "./light"; -import { MediumLinearMotor } from "./mediumlinearmotor"; -import { MotionSensor } from "./motionsensor"; -import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; -import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; -import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; -import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; -import { TiltSensor } from "./tiltsensor"; -import { TrainMotor } from "./trainmotor"; +import { ColorDistanceSensor } from "./devices/colordistancesensor"; +import { Device } from "./devices/device"; +import { Light } from "./devices/light"; +import { MediumLinearMotor } from "./devices/mediumlinearmotor"; +import { MotionSensor } from "./devices/motionsensor"; +import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; +import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; +import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; +import { TiltSensor } from "./devices/tiltsensor"; +import { TrainMotor } from "./devices/trainmotor"; import { isWebBluetooth } from "./utils"; diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 3fd5144a..1156fdec 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -1,11 +1,12 @@ -import { BoostMoveHub } from "./boostmovehub"; -import { ControlPlusHub } from "./controlplushub"; -import { DuploTrainBase } from "./duplotrainbase"; -import { Hub } from "./hub"; -import { PUPHub } from "./puphub"; -import { PUPRemote } from "./pupremote"; import { WebBLEDevice } from "./webbleabstraction"; -import { WeDo2SmartHub } from "./wedo2smarthub"; + +import { BoostMoveHub } from "./hubs/boostmovehub"; +import { ControlPlusHub } from "./hubs/controlplushub"; +import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { Hub } from "./hubs/hub"; +import { PUPHub } from "./hubs/puphub"; +import { PUPRemote } from "./hubs/pupremote"; +import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import * as Consts from "./consts"; diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index e6fe53de..d5fd57c9 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -1,13 +1,14 @@ import { Peripheral } from "@abandonware/noble"; -import { BoostMoveHub } from "./boostmovehub"; -import { ControlPlusHub } from "./controlplushub"; -import { DuploTrainBase } from "./duplotrainbase"; -import { Hub } from "./hub"; import { NobleDevice } from "./nobleabstraction"; -import { PUPHub } from "./puphub"; -import { PUPRemote } from "./pupremote"; -import { WeDo2SmartHub } from "./wedo2smarthub"; + +import { BoostMoveHub } from "./hubs/boostmovehub"; +import { ControlPlusHub } from "./hubs/controlplushub"; +import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { Hub } from "./hubs/hub"; +import { PUPHub } from "./hubs/puphub"; +import { PUPRemote } from "./hubs/pupremote"; +import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import * as Consts from "./consts"; From df70d8a8a4bdbb664fc4777173a88b226afb45e4 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 21:19:56 -0800 Subject: [PATCH 20/62] Renamed devices --- src/devices/colordistancesensor.ts | 22 +++++++++++++--------- src/devices/motionsensor.ts | 10 ++++++++-- src/devices/tachomotor.ts | 12 +++++++++--- src/devices/tiltsensor.ts | 10 ++++++++-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 7589759d..84018e6e 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -16,17 +16,13 @@ export class ColorDistanceSensor extends Device { if (this.autoSubscribe) { switch (event) { case "color": - if (this._isWeDo2) { - this.subscribe(0x00); - } else { - this.subscribe(0x08); - } + this.subscribe(ColorDistanceSensor.Mode.COLOR); break; case "distance": - this.subscribe(0x08); + this.subscribe(ColorDistanceSensor.Mode.DISTANCE); break; case "colorAndDistance": - this.subscribe(0x08); + this.subscribe(ColorDistanceSensor.Mode.COLOR_AND_DISTANCE); break; } } @@ -39,13 +35,13 @@ export class ColorDistanceSensor extends Device { console.log(message); switch (mode) { - case 0x00: + case ColorDistanceSensor.Mode.COLOR: if (this._isWeDo2 && message[2] <= 10) { const color = message[2]; this.emit("color", color); } break; - case 0x08: + case ColorDistanceSensor.Mode.COLOR_AND_DISTANCE: /** * Emits when a color sensor is activated. * @event ColorDistanceSensor#color @@ -90,3 +86,11 @@ export class ColorDistanceSensor extends Device { } } + +export namespace ColorDistanceSensor { + export enum Mode { + COLOR = 0x00, + DISTANCE = 0x01, + COLOR_AND_DISTANCE = 0x08 + } +} \ No newline at end of file diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index 0bb17179..0c44e571 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -13,7 +13,7 @@ export class MotionSensor extends Device { if (this.autoSubscribe) { switch (event) { case "distance": - this.subscribe(0x00); + this.subscribe(MotionSensor.Mode.DISTANCE); break; } } @@ -25,7 +25,7 @@ export class MotionSensor extends Device { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { - case 0x00: + case MotionSensor.Mode.DISTANCE: let distance = message[isWeDo2 ? 2 : 4]; if (message[isWeDo2 ? 3 : 5] === 1) { distance = distance + 255; @@ -41,3 +41,9 @@ export class MotionSensor extends Device { } } + +export namespace MotionSensor { + export enum Mode { + DISTANCE = 0x00 + } +} \ No newline at end of file diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index cae01c6f..72a6f00a 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -14,7 +14,7 @@ export class TachoMotor extends BasicMotor { if (this.autoSubscribe) { switch (event) { case "rotate": - this.subscribe(0x02); + this.subscribe(TachoMotor.Mode.ROTATION); break; } } @@ -26,7 +26,7 @@ export class TachoMotor extends BasicMotor { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { - case 0x02: + case TachoMotor.Mode.ROTATION: const rotation = message.readInt32LE(isWeDo2 ? 2 : 4); /** * Emits when a rotation sensor is activated. @@ -48,7 +48,7 @@ export class TachoMotor extends BasicMotor { public rotateByAngle (angle: number, power: number = 100) { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); if (isWeDo2) { - throw new Error("Rotating by angle is not available on the WeDo 2.0 Smart Hub"); + throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub"); } return new Promise((resolve) => { this._busy = true; @@ -62,3 +62,9 @@ export class TachoMotor extends BasicMotor { } } + +export namespace TachoMotor { + export enum Mode { + ROTATION = 0x02 + } +} \ No newline at end of file diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index a0e76762..4d5a9642 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -13,7 +13,7 @@ export class TiltSensor extends Device { if (this.autoSubscribe) { switch (event) { case "tilt": - this.subscribe(0x00); + this.subscribe(TiltSensor.Mode.TILT); break; } } @@ -25,7 +25,7 @@ export class TiltSensor extends Device { const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { - case 0x00: + case TiltSensor.Mode.TILT: const tiltX = message.readInt8(isWeDo2 ? 2 : 4); const tiltY = message.readInt8(isWeDo2 ? 3 : 5); /** @@ -40,3 +40,9 @@ export class TiltSensor extends Device { } } + +export namespace TiltSensor { + export enum Mode { + TILT = 0x00 + } +} \ No newline at end of file From 04b79ade4736368c46e5d6bc7391c58af4a01f9b Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 21:21:25 -0800 Subject: [PATCH 21/62] Renamed devices --- src/devices/colordistancesensor.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 84018e6e..936109b8 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -36,12 +36,17 @@ export class ColorDistanceSensor extends Device { switch (mode) { case ColorDistanceSensor.Mode.COLOR: - if (this._isWeDo2 && message[2] <= 10) { - const color = message[2]; + if (message[this._isWeDo2 ? 2 : 4] <= 10) { + const color = message[this._isWeDo2 ? 2 : 4]; this.emit("color", color); } break; + case ColorDistanceSensor.Mode.COLOR_AND_DISTANCE: + if (this._isWeDo2) { + break; + } + /** * Emits when a color sensor is activated. * @event ColorDistanceSensor#color @@ -82,6 +87,7 @@ export class ColorDistanceSensor extends Device { this.emit("colorAndDistance", color, distance); } break; + } } From 388bce170c1a43d7317941dcc6fcdc507a24f6aa Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 17 Dec 2019 14:03:37 -0800 Subject: [PATCH 22/62] Mapping of events to modes --- src/devices/basicmotor.ts | 7 +-- src/devices/colordistancesensor.ts | 74 +++++++++++-------------- src/devices/device.ts | 23 +++++++- src/devices/light.ts | 5 +- src/devices/mediumlinearmotor.ts | 2 +- src/devices/motionsensor.ts | 19 ++----- src/devices/movehubmediumlinearmotor.ts | 2 +- src/devices/simplemediumlinearmotor.ts | 2 +- src/devices/tachomotor.ts | 22 ++------ src/devices/techniclargelinearmotor.ts | 2 +- src/devices/technicxlargelinearmotor.ts | 2 +- src/devices/tiltsensor.ts | 19 ++----- src/devices/trainmotor.ts | 2 +- src/hubs/hub.ts | 2 +- src/hubs/wedo2smarthub.ts | 1 - 15 files changed, 81 insertions(+), 103 deletions(-) diff --git a/src/devices/basicmotor.ts b/src/devices/basicmotor.ts index aeec7f18..ad203d78 100644 --- a/src/devices/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -9,8 +9,8 @@ import { mapSpeed } from "../utils"; export class BasicMotor extends Device { - constructor (hub: IDeviceInterface, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { - super(hub, portId, type); + constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { + super(hub, portId, modeMap, type); } @@ -22,8 +22,7 @@ export class BasicMotor extends Device { */ public setPower (power: number) { return new Promise((resolve) => { - const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); - if (isWeDo2) { + if (this.isWeDo2SmartHub) { const data = Buffer.from([this.portId, 0x01, 0x02, mapSpeed(power)]); this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); } else { diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 936109b8..3ef43d2e 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -6,56 +6,52 @@ import * as Consts from "../consts"; export class ColorDistanceSensor extends Device { - private _isWeDo2: boolean; - constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.COLOR_DISTANCE_SENSOR); - this._isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); - - this.on("newListener", (event) => { - if (this.autoSubscribe) { - switch (event) { - case "color": - this.subscribe(ColorDistanceSensor.Mode.COLOR); - break; - case "distance": - this.subscribe(ColorDistanceSensor.Mode.DISTANCE); - break; - case "colorAndDistance": - this.subscribe(ColorDistanceSensor.Mode.COLOR_AND_DISTANCE); - break; - } - } - }); + super(hub, portId, { + "color": ColorDistanceSensor.Mode.COLOR, + "distance": ColorDistanceSensor.Mode.DISTANCE, + "colorAndDistance": ColorDistanceSensor.Mode.COLOR_AND_DISTANCE + }, Consts.DeviceType.COLOR_DISTANCE_SENSOR); } public receive (message: Buffer) { const mode = this._mode; - console.log(message); - switch (mode) { case ColorDistanceSensor.Mode.COLOR: - if (message[this._isWeDo2 ? 2 : 4] <= 10) { - const color = message[this._isWeDo2 ? 2 : 4]; + if (message[this.isWeDo2SmartHub ? 2 : 4] <= 10) { + const color = message[this.isWeDo2SmartHub ? 2 : 4]; + + /** + * Emits when a color sensor is activated. + * @event ColorDistanceSensor#color + * @param {string} port + * @param {Color} color + */ this.emit("color", color); } break; - case ColorDistanceSensor.Mode.COLOR_AND_DISTANCE: - if (this._isWeDo2) { + case ColorDistanceSensor.Mode.DISTANCE: + if (this.isWeDo2SmartHub) { break; } - - /** - * Emits when a color sensor is activated. - * @event ColorDistanceSensor#color - * @param {string} port - * @param {Color} color - */ if (message[4] <= 10) { - const color = message[4]; - this.emit("color", color); + const distance = Math.floor(message[4] * 25.4) - 20; + + /** + * Emits when a distance sensor is activated. + * @event ColorDistanceSensor#distance + * @param {string} port + * @param {number} distance Distance, in millimeters. + */ + this.emit("distance", distance); + } + break; + + case ColorDistanceSensor.Mode.COLOR_AND_DISTANCE: + if (this.isWeDo2SmartHub) { + break; } let distance = message[5]; @@ -67,14 +63,6 @@ export class ColorDistanceSensor extends Device { distance = Math.floor(distance * 25.4) - 20; - /** - * Emits when a distance sensor is activated. - * @event ColorDistanceSensor#distance - * @param {string} port - * @param {number} distance Distance, in millimeters. - */ - this.emit("distance", distance); - /** * A combined color and distance event, emits when the sensor is activated. * @event ColorDistanceSensor#colorAndDistance diff --git a/src/devices/device.ts b/src/devices/device.ts index d7673176..91f11aa0 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -16,12 +16,18 @@ export class Device extends EventEmitter { private _portId: number; private _connected: boolean = true; private _type: Consts.DeviceType; + private _modeMap: {[event: string]: number} = {}; - constructor (hub: IDeviceInterface, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { + private _isWeDo2SmartHub: boolean; + + constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(); this._hub = hub; this._portId = portId; this._type = type; + this._modeMap = modeMap; + this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); + const detachListener = (device: Device) => { if (device.portId === this.portId) { this._connected = false; @@ -30,6 +36,17 @@ export class Device extends EventEmitter { } }; this.hub.on("detach", detachListener); + + this.on("newListener", (event) => { + if (event === "detach") { + return; + } + if (this.autoSubscribe) { + if (this._modeMap[event] !== undefined) { + this.subscribe(this._modeMap[event]); + } + } + }); } public get connected () { @@ -52,6 +69,10 @@ export class Device extends EventEmitter { return this._type; } + protected get isWeDo2SmartHub () { + return this._isWeDo2SmartHub; + } + public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { this._ensureConnected(); this.hub.send(data, characteristic, callback); diff --git a/src/devices/light.ts b/src/devices/light.ts index 8e5b3cb5..5021615d 100644 --- a/src/devices/light.ts +++ b/src/devices/light.ts @@ -8,7 +8,7 @@ export class Light extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.LIGHT); + super(hub, portId, {}, Consts.DeviceType.LIGHT); } @@ -20,8 +20,7 @@ export class Light extends Device { */ public setBrightness (brightness: number) { return new Promise((resolve) => { - const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); - if (isWeDo2) { + if (this.isWeDo2SmartHub) { const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); } else { diff --git a/src/devices/mediumlinearmotor.ts b/src/devices/mediumlinearmotor.ts index 0e810006..48152ebf 100644 --- a/src/devices/mediumlinearmotor.ts +++ b/src/devices/mediumlinearmotor.ts @@ -7,7 +7,7 @@ import * as Consts from "../consts"; export class MediumLinearMotor extends TachoMotor { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.MEDIUM_LINEAR_MOTOR); + super(hub, portId, {}, Consts.DeviceType.MEDIUM_LINEAR_MOTOR); } } diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index 0c44e571..611e1d98 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -7,27 +7,18 @@ import * as Consts from "../consts"; export class MotionSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.MOTION_SENSOR); - - this.on("newListener", (event) => { - if (this.autoSubscribe) { - switch (event) { - case "distance": - this.subscribe(MotionSensor.Mode.DISTANCE); - break; - } - } - }); + super(hub, portId, { + "distance": MotionSensor.Mode.DISTANCE + }, Consts.DeviceType.MOTION_SENSOR); } public receive (message: Buffer) { const mode = this._mode; - const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { case MotionSensor.Mode.DISTANCE: - let distance = message[isWeDo2 ? 2 : 4]; - if (message[isWeDo2 ? 3 : 5] === 1) { + let distance = message[this.isWeDo2SmartHub ? 2 : 4]; + if (message[this.isWeDo2SmartHub ? 3 : 5] === 1) { distance = distance + 255; } /** diff --git a/src/devices/movehubmediumlinearmotor.ts b/src/devices/movehubmediumlinearmotor.ts index cd4ec768..8b379660 100644 --- a/src/devices/movehubmediumlinearmotor.ts +++ b/src/devices/movehubmediumlinearmotor.ts @@ -7,7 +7,7 @@ import * as Consts from "../consts"; export class MoveHubMediumLinearMotor extends TachoMotor { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR); + super(hub, portId, {}, Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR); } } diff --git a/src/devices/simplemediumlinearmotor.ts b/src/devices/simplemediumlinearmotor.ts index e7d99bf5..d41f21af 100644 --- a/src/devices/simplemediumlinearmotor.ts +++ b/src/devices/simplemediumlinearmotor.ts @@ -7,7 +7,7 @@ import * as Consts from "../consts"; export class SimpleMediumLinearMotor extends BasicMotor { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR); + super(hub, portId, {}, Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR); } } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 72a6f00a..a448bed1 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -7,27 +7,18 @@ import { mapSpeed } from "../utils"; export class TachoMotor extends BasicMotor { - constructor (hub: IDeviceInterface, portId: number, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { - super(hub, portId, type); - - this.on("newListener", (event) => { - if (this.autoSubscribe) { - switch (event) { - case "rotate": - this.subscribe(TachoMotor.Mode.ROTATION); - break; - } - } - }); + constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { + super(hub, portId, Object.assign({}, modeMap, { + "rotate": TachoMotor.Mode.ROTATION + }), type); } public receive (message: Buffer) { const mode = this._mode; - const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { case TachoMotor.Mode.ROTATION: - const rotation = message.readInt32LE(isWeDo2 ? 2 : 4); + const rotation = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4); /** * Emits when a rotation sensor is activated. * @event TachoMotor#rotate @@ -46,8 +37,7 @@ export class TachoMotor extends BasicMotor { * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ public rotateByAngle (angle: number, power: number = 100) { - const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); - if (isWeDo2) { + if (this.isWeDo2SmartHub) { throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub"); } return new Promise((resolve) => { diff --git a/src/devices/techniclargelinearmotor.ts b/src/devices/techniclargelinearmotor.ts index bcff3509..103b3a00 100644 --- a/src/devices/techniclargelinearmotor.ts +++ b/src/devices/techniclargelinearmotor.ts @@ -7,7 +7,7 @@ import * as Consts from "../consts"; export class TechnicLargeLinearMotor extends TachoMotor { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR); + super(hub, portId, {}, Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR); } } diff --git a/src/devices/technicxlargelinearmotor.ts b/src/devices/technicxlargelinearmotor.ts index fe230b5d..53b4c0ac 100644 --- a/src/devices/technicxlargelinearmotor.ts +++ b/src/devices/technicxlargelinearmotor.ts @@ -7,7 +7,7 @@ import * as Consts from "../consts"; export class TechnicXLargeLinearMotor extends TachoMotor { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR); + super(hub, portId, {}, Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR); } } diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index 4d5a9642..7fcfd7a8 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -7,27 +7,18 @@ import * as Consts from "../consts"; export class TiltSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.TILT_SENSOR); - - this.on("newListener", (event) => { - if (this.autoSubscribe) { - switch (event) { - case "tilt": - this.subscribe(TiltSensor.Mode.TILT); - break; - } - } - }); + super(hub, portId, { + "tilt": TiltSensor.Mode.TILT + }, Consts.DeviceType.TILT_SENSOR); } public receive (message: Buffer) { const mode = this._mode; - const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); switch (mode) { case TiltSensor.Mode.TILT: - const tiltX = message.readInt8(isWeDo2 ? 2 : 4); - const tiltY = message.readInt8(isWeDo2 ? 3 : 5); + const tiltX = message.readInt8(this.isWeDo2SmartHub ? 2 : 4); + const tiltY = message.readInt8(this.isWeDo2SmartHub ? 3 : 5); /** * Emits when a tilt sensor is activated. * @event LPF2Hub#tilt diff --git a/src/devices/trainmotor.ts b/src/devices/trainmotor.ts index 39d1e06b..dc2996f3 100644 --- a/src/devices/trainmotor.ts +++ b/src/devices/trainmotor.ts @@ -7,7 +7,7 @@ import * as Consts from "../consts"; export class TrainMotor extends BasicMotor { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, Consts.DeviceType.TRAIN_MOTOR); + super(hub, portId, {}, Consts.DeviceType.TRAIN_MOTOR); } } diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index dce1ec37..fb723523 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -304,7 +304,7 @@ export class Hub extends EventEmitter { device = new ColorDistanceSensor(this, portId); break; default: - device = new Device(this, portId, deviceType); + device = new Device(this, portId, undefined, deviceType); break; } diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index 10bb776d..4f8560ce 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -300,7 +300,6 @@ export class WeDo2SmartHub extends Hub { const device = this._getDeviceByPortId(portId); if (device) { - console.log(portId, device.type); device.receive(message); } From 36c34a9743e0f74a733a92e85b877bae3e5ee83a Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 17 Dec 2019 14:29:14 -0800 Subject: [PATCH 23/62] Exported device ModeMap --- src/devices/colordistancesensor.ts | 14 +++++++++----- src/devices/motionsensor.ts | 10 +++++++--- src/devices/tachomotor.ts | 10 +++++++--- src/devices/tiltsensor.ts | 10 +++++++--- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 3ef43d2e..cbf5de87 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -7,11 +7,7 @@ import * as Consts from "../consts"; export class ColorDistanceSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, { - "color": ColorDistanceSensor.Mode.COLOR, - "distance": ColorDistanceSensor.Mode.DISTANCE, - "colorAndDistance": ColorDistanceSensor.Mode.COLOR_AND_DISTANCE - }, Consts.DeviceType.COLOR_DISTANCE_SENSOR); + super(hub, portId, ColorDistanceSensor.ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR); } public receive (message: Buffer) { @@ -82,9 +78,17 @@ export class ColorDistanceSensor extends Device { } export namespace ColorDistanceSensor { + export enum Mode { COLOR = 0x00, DISTANCE = 0x01, COLOR_AND_DISTANCE = 0x08 } + + export const ModeMap: {[event: string]: number} = { + "color": ColorDistanceSensor.Mode.COLOR, + "distance": ColorDistanceSensor.Mode.DISTANCE, + "colorAndDistance": ColorDistanceSensor.Mode.COLOR_AND_DISTANCE + } + } \ No newline at end of file diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index 611e1d98..6d547baf 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -7,9 +7,7 @@ import * as Consts from "../consts"; export class MotionSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, { - "distance": MotionSensor.Mode.DISTANCE - }, Consts.DeviceType.MOTION_SENSOR); + super(hub, portId, MotionSensor.ModeMap, Consts.DeviceType.MOTION_SENSOR); } public receive (message: Buffer) { @@ -34,7 +32,13 @@ export class MotionSensor extends Device { } export namespace MotionSensor { + export enum Mode { DISTANCE = 0x00 } + + export const ModeMap: {[event: string]: number} = { + "distance": MotionSensor.Mode.DISTANCE + } + } \ No newline at end of file diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index a448bed1..5f0aff3f 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -8,9 +8,7 @@ import { mapSpeed } from "../utils"; export class TachoMotor extends BasicMotor { constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { - super(hub, portId, Object.assign({}, modeMap, { - "rotate": TachoMotor.Mode.ROTATION - }), type); + super(hub, portId, Object.assign({}, modeMap, TachoMotor.ModeMap), type); } public receive (message: Buffer) { @@ -54,7 +52,13 @@ export class TachoMotor extends BasicMotor { } export namespace TachoMotor { + export enum Mode { ROTATION = 0x02 } + + export const ModeMap: {[event: string]: number} = { + "rotate": TachoMotor.Mode.ROTATION + } + } \ No newline at end of file diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index 7fcfd7a8..7e9e45d7 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -7,9 +7,7 @@ import * as Consts from "../consts"; export class TiltSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, { - "tilt": TiltSensor.Mode.TILT - }, Consts.DeviceType.TILT_SENSOR); + super(hub, portId, TiltSensor.ModeMap, Consts.DeviceType.TILT_SENSOR); } public receive (message: Buffer) { @@ -33,7 +31,13 @@ export class TiltSensor extends Device { } export namespace TiltSensor { + export enum Mode { TILT = 0x00 } + + export const ModeMap: {[event: string]: number} = { + "tilt": TiltSensor.Mode.TILT + } + } \ No newline at end of file From ffd3cceab8d8348bda2b1c33184fa35254b44956 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 17 Dec 2019 14:44:48 -0800 Subject: [PATCH 24/62] Exported hub PortMap --- src/hubs/boostmovehub.ts | 21 +++++++++++++-------- src/hubs/controlplushub.ts | 25 +++++++++++++++---------- src/hubs/duplotrainbase.ts | 17 +++++++++++------ src/hubs/hub.ts | 21 +++++++++++++++------ src/hubs/puphub.ts | 15 ++++++++++----- src/hubs/pupremote.ts | 15 ++++++++++----- src/hubs/wedo2smarthub.ts | 15 ++++++++++----- 7 files changed, 84 insertions(+), 45 deletions(-) diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 0870f873..1ad7572d 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -35,14 +35,7 @@ export class BoostMoveHub extends LPF2Hub { protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.BOOST_MOVE_HUB); - this._portNames = { - "A": 0, - "B": 1, - "C": 2, - "D": 3, - "TILT": 58 - }; + super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); debug("Discovered Boost Move Hub"); } @@ -65,3 +58,15 @@ export class BoostMoveHub extends LPF2Hub { } + +export namespace BoostMoveHub { + + export const PortMap: {[port: string]: number} = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "TILT": 58 + } + +} \ No newline at end of file diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index 9eb3c7ec..54a849bc 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -37,16 +37,7 @@ export class ControlPlusHub extends LPF2Hub { protected _voltageMaxV = 9.615; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.CONTROL_PLUS_HUB); - this._portNames = { - "A": 0, - "B": 1, - "C": 2, - "D": 3, - "ACCEL": 97, - "GYRO": 98, - "TILT": 99 - }; + super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB); debug("Discovered Control+ Hub"); } @@ -122,3 +113,17 @@ export class ControlPlusHub extends LPF2Hub { } + +export namespace ControlPlusHub { + + export const PortMap: {[port: string]: number} = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "ACCEL": 97, + "GYRO": 98, + "TILT": 99 + } + +} \ No newline at end of file diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index c8a01fb2..3d3399b0 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -36,12 +36,7 @@ export class DuploTrainBase extends LPF2Hub { protected _voltageMaxRaw = 3047; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.DUPLO_TRAIN_HUB); - this._portNames = { - "MOTOR": 0, - "COLOR": 18, - "SPEEDOMETER": 19 - }; + super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB); debug("Discovered Duplo Train Base"); } @@ -73,3 +68,13 @@ export class DuploTrainBase extends LPF2Hub { } + +export namespace DuploTrainBase { + + export const PortMap: {[port: string]: number} = { + "MOTOR": 0, + "COLOR": 18, + "SPEEDOMETER": 19 + } + +} \ No newline at end of file diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index fb723523..04d7d442 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -27,8 +27,6 @@ const debug = Debug("hub"); export class Hub extends EventEmitter { protected _attachedDevices: {[portId: number]: Device} = {}; - - protected _portNames: {[port: string]: number} = {}; // protected _virtualPorts: {[port: string]: Port} = {}; protected _name: string = ""; @@ -43,11 +41,13 @@ export class Hub extends EventEmitter { protected _bleDevice: IBLEAbstraction; private _type: Consts.HubType; + private _portMap: {[port: string]: number} = {}; - constructor (device: IBLEAbstraction, type: Consts.HubType = Consts.HubType.UNKNOWN) { + constructor (device: IBLEAbstraction, portMap: {[port: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { super(); this._type = type; this._bleDevice = device; + this._portMap = portMap; device.on("disconnect", () => { /** * Emits when the hub is disconnected. @@ -76,6 +76,15 @@ export class Hub extends EventEmitter { } + /** + * @readonly + * @property {string[]} ports Array of port names + */ + public get ports () { + return Object.keys(this._portMap); + } + + /** * @readonly * @property {string} firmwareVersion Firmware version of the hub @@ -178,7 +187,7 @@ export class Hub extends EventEmitter { public getDeviceAtPort (portName: string) { - const portId = this._portNames[portName]; + const portId = this._portMap[portName]; if (portId) { return this._attachedDevices[portId]; } else { @@ -198,8 +207,8 @@ export class Hub extends EventEmitter { public getPortNameForPortId (portId: number) { - for (const port of Object.keys(this._portNames)) { - if (this._portNames[port] === portId) { + for (const port of Object.keys(this._portMap)) { + if (this._portMap[port] === portId) { return port; } } diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts index 43dd1453..dfb4673b 100644 --- a/src/hubs/puphub.ts +++ b/src/hubs/puphub.ts @@ -35,11 +35,7 @@ export class PUPHub extends LPF2Hub { protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.POWERED_UP_HUB); - this._portNames = { - "A": 0, - "B": 1 - }; + super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB); debug("Discovered Powered UP Hub"); } @@ -62,3 +58,12 @@ export class PUPHub extends LPF2Hub { } + +export namespace PUPHub { + + export const PortMap: {[port: string]: number} = { + "A": 0, + "B": 1 + } + +} \ No newline at end of file diff --git a/src/hubs/pupremote.ts b/src/hubs/pupremote.ts index 2ce066ef..64b95502 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/pupremote.ts @@ -38,11 +38,7 @@ export class PUPRemote extends LPF2Hub { constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.POWERED_UP_REMOTE); - this._portNames = { - "LEFT": 0, - "RIGHT": 1 - }; + super(device, PUPRemote.PortMap, Consts.HubType.POWERED_UP_REMOTE); debug("Discovered Powered UP Remote"); } @@ -58,3 +54,12 @@ export class PUPRemote extends LPF2Hub { } + +export namespace PUPRemote { + + export const PortMap: {[port: string]: number} = { + "LEFT": 0, + "RIGHT": 1 + } + +} \ No newline at end of file diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index 4f8560ce..ce085e7f 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -34,11 +34,7 @@ export class WeDo2SmartHub extends Hub { constructor (device: IBLEAbstraction) { - super(device, Consts.HubType.WEDO2_SMART_HUB); - this._portNames = { - "A": 1, - "B": 2 - }; + super(device, WeDo2SmartHub.PortMap, Consts.HubType.WEDO2_SMART_HUB); debug("Discovered WeDo 2.0 Smart Hub"); } @@ -353,3 +349,12 @@ export class WeDo2SmartHub extends Hub { } + +export namespace WeDo2SmartHub { + + export const PortMap: {[port: string]: number} = { + "A": 1, + "B": 2 + } + +} \ No newline at end of file From 364089b703dd026f7f047d84b7a1429fd0e49c3c Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Tue, 17 Dec 2019 17:23:27 -0800 Subject: [PATCH 25/62] Power to speed when rotating by angle --- src/devices/tachomotor.ts | 6 +++--- src/hubs/lpf2hub.ts | 14 -------------- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 5f0aff3f..3ae54f6b 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -31,16 +31,16 @@ export class TachoMotor extends BasicMotor { * Rotate a motor by a given angle. * @method TachoMotor#rotateByAngle * @param {number} angle How much the motor should be rotated (in degrees). - * @param {number} [power=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. + * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public rotateByAngle (angle: number, power: number = 100) { + public rotateByAngle (angle: number, speed: number = 100) { if (this.isWeDo2SmartHub) { throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub"); } return new Promise((resolve) => { this._busy = true; - const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(power), 0x64, 0x7f, 0x03]); + const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); message.writeUInt32LE(angle, 4); this.send(message); this._finished = () => { diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 200fcc49..70bcdc1f 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -401,20 +401,6 @@ export class LPF2Hub extends Hub { } } - // const port = this._getPortForPortNumber(data[3]); - - // if (!port) { - // return; - // } - - // if (data[4] === 0x0a) { - // port.busy = false; - // if (port.finished) { - // port.finished(); - // port.finished = null; - // } - // } - } From de4feb0b897e7528e65cc778f23df989db7a467a Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 10:24:39 -0800 Subject: [PATCH 26/62] Added functions to wait for port attachments --- src/devices/colordistancesensor.ts | 3 - src/devices/device.ts | 2 +- src/hubs/boostmovehub.ts | 2 +- src/hubs/controlplushub.ts | 2 +- src/hubs/duplotrainbase.ts | 2 +- src/hubs/hub.ts | 40 ++++++++++++- src/hubs/lpf2hub.ts | 91 ------------------------------ src/hubs/puphub.ts | 2 +- src/hubs/pupremote.ts | 2 +- src/hubs/wedo2smarthub.ts | 2 +- 10 files changed, 44 insertions(+), 104 deletions(-) diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index cbf5de87..f952e637 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -21,7 +21,6 @@ export class ColorDistanceSensor extends Device { /** * Emits when a color sensor is activated. * @event ColorDistanceSensor#color - * @param {string} port * @param {Color} color */ this.emit("color", color); @@ -38,7 +37,6 @@ export class ColorDistanceSensor extends Device { /** * Emits when a distance sensor is activated. * @event ColorDistanceSensor#distance - * @param {string} port * @param {number} distance Distance, in millimeters. */ this.emit("distance", distance); @@ -62,7 +60,6 @@ export class ColorDistanceSensor extends Device { /** * A combined color and distance event, emits when the sensor is activated. * @event ColorDistanceSensor#colorAndDistance - * @param {string} port * @param {Color} color * @param {number} distance Distance, in millimeters. */ diff --git a/src/devices/device.ts b/src/devices/device.ts index 91f11aa0..20664d21 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -61,7 +61,7 @@ export class Device extends EventEmitter { return this._portId; } - public get port () { + public get portName () { return this.hub.getPortNameForPortId(this.portId); } diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 1ad7572d..0a88b849 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -61,7 +61,7 @@ export class BoostMoveHub extends LPF2Hub { export namespace BoostMoveHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 0, "B": 1, "C": 2, diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index 54a849bc..b088d1d8 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -116,7 +116,7 @@ export class ControlPlusHub extends LPF2Hub { export namespace ControlPlusHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 0, "B": 1, "C": 2, diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index 3d3399b0..fd8d5294 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -71,7 +71,7 @@ export class DuploTrainBase extends LPF2Hub { export namespace DuploTrainBase { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "MOTOR": 0, "COLOR": 18, "SPEEDOMETER": 19 diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 04d7d442..a063d97d 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -27,7 +27,7 @@ const debug = Debug("hub"); export class Hub extends EventEmitter { protected _attachedDevices: {[portId: number]: Device} = {}; - // protected _virtualPorts: {[port: string]: Port} = {}; + // protected _virtualPorts: {[portName: string]: Port} = {}; protected _name: string = ""; protected _firmwareVersion: string = "0.0.00.0000"; @@ -41,9 +41,10 @@ export class Hub extends EventEmitter { protected _bleDevice: IBLEAbstraction; private _type: Consts.HubType; - private _portMap: {[port: string]: number} = {}; + private _portMap: {[portName: string]: number} = {}; + private _attachCallbacks: ((device: Device) => void)[] = []; - constructor (device: IBLEAbstraction, portMap: {[port: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { + constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { super(); this._type = type; this._bleDevice = device; @@ -196,6 +197,21 @@ export class Hub extends EventEmitter { } + public waitForDeviceAtPort (portName: string) { + return new Promise((resolve) => { + const existingDevice = this.getDeviceAtPort(portName); + if (existingDevice) { + return resolve(existingDevice); + } + this._attachCallbacks.push((device) => { + if (device.portName === portName) { + return resolve(device); + } + }); + }); + } + + public getDevices () { return Object.values(this._attachedDevices); } @@ -206,6 +222,21 @@ export class Hub extends EventEmitter { } + public waitForDeviceByType (deviceType: number) { + return new Promise((resolve) => { + const existingDevices = this.getDevicesByType(deviceType); + if (existingDevices.length >= 1) { + return resolve(existingDevices[0]); + } + this._attachCallbacks.push((device) => { + if (device.type === deviceType) { + return resolve(device); + } + }) + }); + } + + public getPortNameForPortId (portId: number) { for (const port of Object.keys(this._portMap)) { if (this._portMap[port] === portId) { @@ -264,6 +295,9 @@ export class Hub extends EventEmitter { * @param {Device} device */ this.emit("attach", device); + this._attachCallbacks.forEach((callback) => { + callback(device); + }); } diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 70bcdc1f..4975dd93 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -442,97 +442,6 @@ export class LPF2Hub extends Hub { // if (port && port.connected) { // switch (port.type) { - // case Consts.DeviceType.WEDO2_DISTANCE: { - // let distance = data[4]; - // if (data[5] === 1) { - // distance = data[4] + 255; - // } - // /** - // * Emits when a distance sensor is activated. - // * @event LPF2Hub#distance - // * @param {string} port - // * @param {number} distance Distance, in millimeters. - // */ - // this.emit("distance", port.id, distance * 10); - // break; - // } - // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { - - // /** - // * Emits when a color sensor is activated. - // * @event LPF2Hub#color - // * @param {string} port - // * @param {Color} color - // */ - // if (data[4] <= 10) { - // this.emit("color", port.id, data[4]); - // } - - // let distance = data[5]; - // const partial = data[7]; - - // if (partial > 0) { - // distance += 1.0 / partial; - // } - - // distance = Math.floor(distance * 25.4) - 20; - - // this.emit("distance", port.id, distance); - - // /** - // * A combined color and distance event, emits when the sensor is activated. - // * @event LPF2Hub#colorAndDistance - // * @param {string} port - // * @param {Color} color - // * @param {number} distance Distance, in millimeters. - // */ - // if (data[4] <= 10) { - // this.emit("colorAndDistance", port.id, data[4], distance); - // } - // break; - // } - // case Consts.DeviceType.WEDO2_TILT: { - // const tiltX = data.readInt8(4); - // const tiltY = data.readInt8(5); - // this._lastTiltX = tiltX; - // this._lastTiltY = tiltY; - // /** - // * Emits when a tilt sensor is activated. - // * @event LPF2Hub#tilt - // * @param {string} port If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used. - // * @param {number} x - // * @param {number} y - // * @param {number} z (Only available when using a Control+ Hub) - // */ - // this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ); - // break; - // } - // case Consts.DeviceType.BOOST_TACHO_MOTOR: { - // const rotation = data.readInt32LE(4); - // /** - // * Emits when a rotation sensor is activated. - // * @event LPF2Hub#rotate - // * @param {string} port - // * @param {number} rotation - // */ - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR: { - // const rotation = data.readInt32LE(4); - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: { - // const rotation = data.readInt32LE(4); - // this.emit("rotate", port.id, rotation); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: { - // const rotation = data.readInt32LE(4); - // this.emit("rotate", port.id, rotation); - // break; - // } // case Consts.DeviceType.CONTROL_PLUS_TILT: { // const tiltZ = data.readInt16LE(4); // const tiltY = data.readInt16LE(6); diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts index dfb4673b..cd66c53a 100644 --- a/src/hubs/puphub.ts +++ b/src/hubs/puphub.ts @@ -61,7 +61,7 @@ export class PUPHub extends LPF2Hub { export namespace PUPHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 0, "B": 1 } diff --git a/src/hubs/pupremote.ts b/src/hubs/pupremote.ts index 64b95502..96b93bf0 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/pupremote.ts @@ -57,7 +57,7 @@ export class PUPRemote extends LPF2Hub { export namespace PUPRemote { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "LEFT": 0, "RIGHT": 1 } diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index ce085e7f..7cd607a2 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -352,7 +352,7 @@ export class WeDo2SmartHub extends Hub { export namespace WeDo2SmartHub { - export const PortMap: {[port: string]: number} = { + export const PortMap: {[portName: string]: number} = { "A": 1, "B": 2 } From c1b8697a566fd6fb6bc6ed673c71f71c620215df Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 11:45:12 -0800 Subject: [PATCH 27/62] VoltageSensor --- src/consts.ts | 4 +-- src/devices/voltagesensor.ts | 62 ++++++++++++++++++++++++++++++++++++ src/hubs/boostmovehub.ts | 1 - src/hubs/controlplushub.ts | 3 -- src/hubs/duplotrainbase.ts | 3 -- src/hubs/hub.ts | 14 +++----- src/hubs/lpf2hub.ts | 14 ++------ src/hubs/puphub.ts | 1 - src/hubs/pupremote.ts | 3 -- src/index-browser.ts | 2 ++ src/index-node.ts | 2 ++ 11 files changed, 74 insertions(+), 35 deletions(-) create mode 100644 src/devices/voltagesensor.ts diff --git a/src/consts.ts b/src/consts.ts index 3ab80cba..88c56bdd 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -55,8 +55,8 @@ export enum DeviceType { SIMPLE_MEDIUM_LINEAR_MOTOR = 1, TRAIN_MOTOR = 2, LIGHT = 8, - VOLTAGE = 20, - CURRENT = 21, + VOLTAGE_SENSOR = 20, + CURRENT_SENSOR = 21, PIEZO_TONE = 22, RGB_LIGHT = 23, TILT_SENSOR = 34, diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts new file mode 100644 index 00000000..a1a85cac --- /dev/null +++ b/src/devices/voltagesensor.ts @@ -0,0 +1,62 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class VoltageSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, VoltageSensor.ModeMap, Consts.DeviceType.VOLTAGE_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case VoltageSensor.Mode.VOLTAGE: + let maxVoltageValue = VoltageSensor.MaxVoltageValue[this.hub.type]; + if (maxVoltageValue === undefined) { + maxVoltageValue = VoltageSensor.MaxVoltageValue[Consts.HubType.UNKNOWN]; + } + let maxVoltageRaw = VoltageSensor.MaxVoltageRaw[this.hub.type]; + if (maxVoltageRaw === undefined) { + maxVoltageRaw = VoltageSensor.MaxVoltageRaw[Consts.HubType.UNKNOWN]; + } + const voltage = message.readUInt16LE(4) * maxVoltageValue / maxVoltageRaw; + /** + * Emits when a voltage change is detected. + * @event VoltageSensor#voltage + * @param {number} voltage + */ + this.emit("voltage", voltage); + break; + } + } + +} + +export namespace VoltageSensor { + + export enum Mode { + VOLTAGE = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "voltage": VoltageSensor.Mode.VOLTAGE + } + + export const MaxVoltageValue: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 9.615, + [Consts.HubType.DUPLO_TRAIN_HUB]: 6.4, + [Consts.HubType.POWERED_UP_REMOTE]: 6.4, + } + + export const MaxVoltageRaw: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 3893, + [Consts.HubType.DUPLO_TRAIN_HUB]: 3047, + [Consts.HubType.POWERED_UP_REMOTE]: 3200, + [Consts.HubType.CONTROL_PLUS_HUB]: 4095, + } + +} \ No newline at end of file diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 0a88b849..66282a3b 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -32,7 +32,6 @@ export class BoostMoveHub extends LPF2Hub { } protected _currentPort = 0x3b; - protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index b088d1d8..29985ba9 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -32,9 +32,6 @@ export class ControlPlusHub extends LPF2Hub { protected _currentPort = 0x3b; protected _currentMaxMA = 4175; - protected _voltagePort = 0x3c; - protected _voltageMaxRaw = 4095; - protected _voltageMaxV = 9.615; constructor (device: IBLEAbstraction) { super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB); diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index fd8d5294..98eda910 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -31,9 +31,6 @@ export class DuploTrainBase extends LPF2Hub { protected _ledPort = 0x11; - protected _voltagePort = 0x14; - protected _voltageMaxV = 6.4; - protected _voltageMaxRaw = 3047; constructor (device: IBLEAbstraction) { super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB); diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index a063d97d..b4311254 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -13,6 +13,7 @@ import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor"; import { TiltSensor } from "../devices/tiltsensor"; import { TrainMotor } from "../devices/trainmotor"; +import { VoltageSensor } from "../devices/voltagesensor"; import * as Consts from "../consts"; @@ -34,7 +35,6 @@ export class Hub extends EventEmitter { protected _hardwareVersion: string = "0.0.00.0000"; protected _primaryMACAddress: string = "00:00:00:00:00:00"; protected _batteryLevel: number = 100; - protected _voltage: number = 0; protected _current: number = 0; protected _rssi: number = -60; @@ -140,15 +140,6 @@ export class Hub extends EventEmitter { } - /** - * @readonly - * @property {number} voltage Voltage of the hub (Volts) - */ - public get voltage () { - return this._voltage; - } - - /** * @readonly * @property {number} current Current usage of the hub (Milliamps) @@ -346,6 +337,9 @@ export class Hub extends EventEmitter { case Consts.DeviceType.COLOR_DISTANCE_SENSOR: device = new ColorDistanceSensor(this, portId); break; + case Consts.DeviceType.VOLTAGE_SENSOR: + device = new VoltageSensor(this, portId); + break; default: device = new Device(this, portId, undefined, deviceType); break; diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 4975dd93..f76ecebe 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -16,9 +16,6 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo"); export class LPF2Hub extends Hub { protected _ledPort: number = 0x32; - protected _voltagePort: number | undefined; - protected _voltageMaxV: number = 9.6; - protected _voltageMaxRaw: number = 3893; protected _currentPort: number | undefined; protected _currentMaxMA: number = 2444; protected _currentMaxRaw: number = 4095; @@ -35,11 +32,8 @@ export class LPF2Hub extends Hub { await super.connect(); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); - if (this._voltagePort !== undefined) { - this.subscribe(this._voltagePort, Consts.DeviceType.VOLTAGE, 0x00); // Activate voltage reports - } if (this._currentPort !== undefined) { - this.subscribe(this._currentPort, Consts.DeviceType.CURRENT, 0x00); // Activate currrent reports + this.subscribe(this._currentPort, Consts.DeviceType.CURRENT_SENSOR, 0x00); // Activate currrent reports } await this.sleep(100); this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports @@ -413,11 +407,7 @@ export class LPF2Hub extends Hub { device.receive(message); } - // if (data[3] === this._voltagePort) { - // const voltageRaw = data.readUInt16LE(4); - // this._voltage = voltageRaw * this._voltageMaxV / this._voltageMaxRaw; - // return; - // } else if (data[3] === this._currentPort) { + // if (data[3] === this._currentPort) { // const currentRaw = data.readUInt16LE(4); // this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; // return; diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts index cd66c53a..c3768561 100644 --- a/src/hubs/puphub.ts +++ b/src/hubs/puphub.ts @@ -32,7 +32,6 @@ export class PUPHub extends LPF2Hub { } protected _currentPort = 0x3b; - protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB); diff --git a/src/hubs/pupremote.ts b/src/hubs/pupremote.ts index 96b93bf0..7197f16c 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/pupremote.ts @@ -32,9 +32,6 @@ export class PUPRemote extends LPF2Hub { protected _ledPort = 0x34; - protected _voltagePort = 0x3b; - protected _voltageMaxV = 6.4; - protected _voltageMaxRaw = 3200; constructor (device: IBLEAbstraction) { diff --git a/src/index-browser.ts b/src/index-browser.ts index 3eb7e07c..f6aeeee4 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -21,6 +21,7 @@ import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; import { TiltSensor } from "./devices/tiltsensor"; import { TrainMotor } from "./devices/trainmotor"; +import { VoltageSensor } from "./devices/voltagesensor"; import { isWebBluetooth } from "./utils"; @@ -46,6 +47,7 @@ window.PoweredUP = { TechnicXLargeLinearMotor, TiltSensor, TrainMotor, + VoltageSensor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index b4f4eeb5..729b2eb0 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -21,6 +21,7 @@ import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; import { TiltSensor } from "./devices/tiltsensor"; import { TrainMotor } from "./devices/trainmotor"; +import { VoltageSensor } from "./devices/voltagesensor"; import { isWebBluetooth } from "./utils"; @@ -46,5 +47,6 @@ export { TechnicXLargeLinearMotor, TiltSensor, TrainMotor, + VoltageSensor, isWebBluetooth }; From 2e06a17e5d232872c749e184ba652a0371c02e37 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 12:04:39 -0800 Subject: [PATCH 28/62] CurrentSensor --- src/devices/currentsensor.ts | 58 ++++++++++++++++++++++++++++++++++++ src/hubs/boostmovehub.ts | 2 -- src/hubs/controlplushub.ts | 3 -- src/hubs/hub.ts | 14 +++------ src/hubs/lpf2hub.ts | 12 -------- src/index-browser.ts | 2 ++ src/index-node.ts | 2 ++ 7 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 src/devices/currentsensor.ts diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts new file mode 100644 index 00000000..b078454b --- /dev/null +++ b/src/devices/currentsensor.ts @@ -0,0 +1,58 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class CurrentSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, CurrentSensor.ModeMap, Consts.DeviceType.CURRENT_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case CurrentSensor.Mode.CURRENT: + let maxCurrentValue = CurrentSensor.MaxCurrentValue[this.hub.type]; + if (maxCurrentValue === undefined) { + maxCurrentValue = CurrentSensor.MaxCurrentValue[Consts.HubType.UNKNOWN]; + } + let maxCurrentRaw = CurrentSensor.MaxCurrentRaw[this.hub.type]; + if (maxCurrentRaw === undefined) { + maxCurrentRaw = CurrentSensor.MaxCurrentRaw[Consts.HubType.UNKNOWN]; + } + const current = message.readUInt16LE(4) * maxCurrentValue / maxCurrentRaw; + /** + * Emits when a current change is detected. + * @event CurrentSensor#current + * @param {number} current + */ + this.emit("current", current); + break; + } + } + +} + +export namespace CurrentSensor { + + export enum Mode { + CURRENT = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "current": CurrentSensor.Mode.CURRENT + } + + export const MaxCurrentValue: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 2444, + [Consts.HubType.CONTROL_PLUS_HUB]: 4175, + } + + export const MaxCurrentRaw: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 4095, + } + +} \ No newline at end of file diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 66282a3b..644b2cf2 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -31,8 +31,6 @@ export class BoostMoveHub extends LPF2Hub { ); } - protected _currentPort = 0x3b; - constructor (device: IBLEAbstraction) { super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); debug("Discovered Boost Move Hub"); diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index 29985ba9..43929ee4 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -30,9 +30,6 @@ export class ControlPlusHub extends LPF2Hub { ); } - protected _currentPort = 0x3b; - protected _currentMaxMA = 4175; - constructor (device: IBLEAbstraction) { super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB); debug("Discovered Control+ Hub"); diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index b4311254..6032eda2 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -3,6 +3,7 @@ import { EventEmitter } from "events"; import { IBLEAbstraction } from "../interfaces"; import { ColorDistanceSensor } from "../devices/colordistancesensor"; +import { CurrentSensor } from "../devices/currentsensor"; import { Device } from "../devices/device"; import { Light } from "../devices/light"; import { MediumLinearMotor } from "../devices/mediumlinearmotor"; @@ -35,7 +36,6 @@ export class Hub extends EventEmitter { protected _hardwareVersion: string = "0.0.00.0000"; protected _primaryMACAddress: string = "00:00:00:00:00:00"; protected _batteryLevel: number = 100; - protected _current: number = 0; protected _rssi: number = -60; protected _bleDevice: IBLEAbstraction; @@ -140,15 +140,6 @@ export class Hub extends EventEmitter { } - /** - * @readonly - * @property {number} current Current usage of the hub (Milliamps) - */ - public get current () { - return this._current; - } - - /** * Connect to the Hub. * @method Hub#connect @@ -340,6 +331,9 @@ export class Hub extends EventEmitter { case Consts.DeviceType.VOLTAGE_SENSOR: device = new VoltageSensor(this, portId); break; + case Consts.DeviceType.CURRENT_SENSOR: + device = new CurrentSensor(this, portId); + break; default: device = new Device(this, portId, undefined, deviceType); break; diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index f76ecebe..aecce975 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -16,9 +16,6 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo"); export class LPF2Hub extends Hub { protected _ledPort: number = 0x32; - protected _currentPort: number | undefined; - protected _currentMaxMA: number = 2444; - protected _currentMaxRaw: number = 4095; private _lastTiltX: number = 0; private _lastTiltY: number = 0; @@ -32,9 +29,6 @@ export class LPF2Hub extends Hub { await super.connect(); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); - if (this._currentPort !== undefined) { - this.subscribe(this._currentPort, Consts.DeviceType.CURRENT_SENSOR, 0x00); // Activate currrent reports - } await this.sleep(100); this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports this.send(Buffer.from([0x01, 0x03, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request firmware version @@ -407,12 +401,6 @@ export class LPF2Hub extends Hub { device.receive(message); } - // if (data[3] === this._currentPort) { - // const currentRaw = data.readUInt16LE(4); - // this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; - // return; - // } - // if ((data[3] === 0x3d && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Control+ CPU Temperature // /** // * Emits when a change is detected on a temperature sensor. Measured in degrees centigrade. diff --git a/src/index-browser.ts b/src/index-browser.ts index f6aeeee4..6018cdf9 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -11,6 +11,7 @@ import { PUPRemote } from "./hubs/pupremote"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; +import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; @@ -48,6 +49,7 @@ window.PoweredUP = { TiltSensor, TrainMotor, VoltageSensor, + CurrentSensor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index 729b2eb0..0db196c4 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -11,6 +11,7 @@ import { PUPRemote } from "./hubs/pupremote"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; +import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; @@ -48,5 +49,6 @@ export { TiltSensor, TrainMotor, VoltageSensor, + CurrentSensor, isWebBluetooth }; From 6d23179be6dc1c7abb8a29c0a229d7feae546bb3 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 13:21:32 -0800 Subject: [PATCH 29/62] PUPRemoteButton --- src/consts.ts | 2 +- src/devices/pupremotebutton.ts | 48 ++++++++++++++++++++++++++++++++++ src/hubs/hub.ts | 4 +++ src/index-browser.ts | 2 ++ src/index-node.ts | 2 ++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/devices/pupremotebutton.ts diff --git a/src/consts.ts b/src/consts.ts index 88c56bdd..b86a3795 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -72,7 +72,7 @@ export enum DeviceType { TECHNIC_LARGE_LINEAR_MOTOR = 46, TECHNIC_XLARGE_LINEAR_MOTOR = 47, CONTROL_PLUS_GEST = 54, - POWERED_UP_REMOTE_BUTTON = 55, + PUP_REMOTE_BUTTON = 55, RSSI = 56, CONTROL_PLUS_ACCELEROMETER = 57, CONTROL_PLUS_GYRO = 58, diff --git a/src/devices/pupremotebutton.ts b/src/devices/pupremotebutton.ts new file mode 100644 index 00000000..0f7719fd --- /dev/null +++ b/src/devices/pupremotebutton.ts @@ -0,0 +1,48 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class PUPRemoteButton extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, PUPRemoteButton.ModeMap, Consts.DeviceType.PUP_REMOTE_BUTTON); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case PUPRemoteButton.Mode.BUTTON_EVENTS: + /** + * Emits when a button on the remote is pressed or released. + * @event PUPRemoteButton#button + * @param {number} event + */ + const event = message[4]; + this.emit("button", event); + break; + } + } + +} + +export namespace PUPRemoteButton { + + export enum Mode { + BUTTON_EVENTS = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "button": PUPRemoteButton.Mode.BUTTON_EVENTS + } + + export const ButtonState: {[state: string]: number} = { + "UP": 0x01, + "DOWN": 0xff, + "STOP": 0x7f, + "RELEASED": 0x00, + } + +} \ No newline at end of file diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 6032eda2..99a77330 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -9,6 +9,7 @@ import { Light } from "../devices/light"; import { MediumLinearMotor } from "../devices/mediumlinearmotor"; import { MotionSensor } from "../devices/motionsensor"; import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor"; +import { PUPRemoteButton } from "../devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor"; @@ -334,6 +335,9 @@ export class Hub extends EventEmitter { case Consts.DeviceType.CURRENT_SENSOR: device = new CurrentSensor(this, portId); break; + case Consts.DeviceType.PUP_REMOTE_BUTTON: + device = new PUPRemoteButton(this, portId); + break; default: device = new Device(this, portId, undefined, deviceType); break; diff --git a/src/index-browser.ts b/src/index-browser.ts index 6018cdf9..3e99c92f 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -17,6 +17,7 @@ import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; +import { PUPRemoteButton } from "./devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; @@ -43,6 +44,7 @@ window.PoweredUP = { MediumLinearMotor, MotionSensor, MoveHubMediumLinearMotor, + PUPRemoteButton, SimpleMediumLinearMotor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, diff --git a/src/index-node.ts b/src/index-node.ts index 0db196c4..497c00c1 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -17,6 +17,7 @@ import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; +import { PUPRemoteButton } from "./devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; @@ -43,6 +44,7 @@ export { MediumLinearMotor, MotionSensor, MoveHubMediumLinearMotor, + PUPRemoteButton, SimpleMediumLinearMotor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, From 195c0d5123c717e8e07edd5658668e65527ea714 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 15:20:20 -0800 Subject: [PATCH 30/62] HubLED and MoveHubTiltSensor --- examples/leds.js | 7 +-- src/consts.ts | 4 +- src/devices/device.ts | 4 ++ src/devices/hubled.ts | 84 ++++++++++++++++++++++++++++++++ src/devices/movehubtiltsensor.ts | 43 ++++++++++++++++ src/hubs/hub.ts | 8 +++ src/hubs/lpf2hub.ts | 68 -------------------------- src/index-browser.ts | 4 ++ src/index-node.ts | 4 ++ 9 files changed, 153 insertions(+), 73 deletions(-) create mode 100644 src/devices/hubled.ts create mode 100644 src/devices/movehubtiltsensor.ts diff --git a/examples/leds.js b/examples/leds.js index 1e19c064..99167a22 100644 --- a/examples/leds.js +++ b/examples/leds.js @@ -25,9 +25,10 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs let color = 1; setInterval(() => { - const hubs = poweredUP.getConnectedHubs(); // Get an array of all connected hubs - hubs.forEach((hub) => { - hub.setLEDColor(color); // Set the color + const hubs = poweredUP.getHubs(); // Get an array of all connected hubs + hubs.forEach(async (hub) => { + const led = await hub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED); + led.setColor(color); // Set the color }) color++; if (color > 10) { diff --git a/src/consts.ts b/src/consts.ts index b86a3795..8af9de73 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -58,13 +58,13 @@ export enum DeviceType { VOLTAGE_SENSOR = 20, CURRENT_SENSOR = 21, PIEZO_TONE = 22, - RGB_LIGHT = 23, + HUB_LED = 23, TILT_SENSOR = 34, MOTION_SENSOR = 35, COLOR_DISTANCE_SENSOR = 37, MEDIUM_LINEAR_MOTOR = 38, MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39, - BOOST_TILT = 40, + MOVE_HUB_TILT_SENSOR = 40, DUPLO_TRAIN_BASE_MOTOR = 41, DUPLO_TRAIN_BASE_SPEAKER = 42, DUPLO_TRAIN_BASE_COLOR = 43, diff --git a/src/devices/device.ts b/src/devices/device.ts index 20664d21..dc988150 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -69,6 +69,10 @@ export class Device extends EventEmitter { return this._type; } + public get mode () { + return this._mode; + } + protected get isWeDo2SmartHub () { return this._isWeDo2SmartHub; } diff --git a/src/devices/hubled.ts b/src/devices/hubled.ts new file mode 100644 index 00000000..b7c26054 --- /dev/null +++ b/src/devices/hubled.ts @@ -0,0 +1,84 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class HubLED extends Device { + + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, {}, Consts.DeviceType.HUB_LED); + } + + + /** + * Set the color of the LED on the Hub via a color value. + * @method HubLED#setColor + * @param {Color} color + * @returns {Promise} Resolved upon successful issuance of command. + */ + public setColor (color: number | boolean) { + return new Promise((resolve, reject) => { + if (this.mode !== HubLED.Mode.COLOR) { + this.subscribe(HubLED.Mode.COLOR); + } + if (typeof color === "boolean") { + color = 0; + } + this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, color])); + return resolve(); + }); + } + + + /** + * Set the color of the LED on the Hub via RGB values. + * @method HubLED#setRGB + * @param {number} red + * @param {number} green + * @param {number} blue + * @returns {Promise} Resolved upon successful issuance of command. + */ + public setRGB (red: number, green: number, blue: number) { + return new Promise((resolve, reject) => { + if (this.mode !== HubLED.Mode.RGB) { + this.subscribe(HubLED.Mode.RGB); + } + this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x01, red, green, blue])); + return resolve(); + }); + } + + + + /** + * Set the light brightness. + * @method Light#brightness + * @param {number} brightness Brightness value between 0-100 (0 is off) + * @returns {Promise} Resolved upon successful completion of command. + */ + public setBrightness (brightness: number) { + return new Promise((resolve) => { + if (this.isWeDo2SmartHub) { + const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); + this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + } else { + const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); + this.send(data); + } + return resolve(); + }); + } + + +} + +export namespace HubLED { + + export enum Mode { + COLOR = 0x00, + RGB = 0x01 + } + +} \ No newline at end of file diff --git a/src/devices/movehubtiltsensor.ts b/src/devices/movehubtiltsensor.ts new file mode 100644 index 00000000..7e65fe18 --- /dev/null +++ b/src/devices/movehubtiltsensor.ts @@ -0,0 +1,43 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class MoveHubTiltSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, MoveHubTiltSensor.ModeMap, Consts.DeviceType.MOVE_HUB_TILT_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case MoveHubTiltSensor.Mode.TILT: + /** + * Emits when a tilt sensor is activated. + * @event MoveHubTiltSensor#tilt + * @param {number} x + * @param {number} y + */ + const tiltX = -(message.readInt8(4)); + const tiltY = message.readInt8(5); + this.emit("tilt", tiltX, tiltY); + break; + } + } + +} + +export namespace MoveHubTiltSensor { + + export enum Mode { + TILT = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "tilt": MoveHubTiltSensor.Mode.TILT + } + +} \ No newline at end of file diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 99a77330..832f42f9 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -5,10 +5,12 @@ import { IBLEAbstraction } from "../interfaces"; import { ColorDistanceSensor } from "../devices/colordistancesensor"; import { CurrentSensor } from "../devices/currentsensor"; import { Device } from "../devices/device"; +import { HubLED } from "../devices/hubled"; import { Light } from "../devices/light"; import { MediumLinearMotor } from "../devices/mediumlinearmotor"; import { MotionSensor } from "../devices/motionsensor"; import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor"; +import { MoveHubTiltSensor } from "../devices/movehubtiltsensor"; import { PUPRemoteButton } from "../devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; @@ -317,6 +319,9 @@ export class Hub extends EventEmitter { case Consts.DeviceType.TILT_SENSOR: device = new TiltSensor(this, portId); break; + case Consts.DeviceType.MOVE_HUB_TILT_SENSOR: + device = new MoveHubTiltSensor(this, portId); + break; case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: device = new MediumLinearMotor(this, portId); break; @@ -338,6 +343,9 @@ export class Hub extends EventEmitter { case Consts.DeviceType.PUP_REMOTE_BUTTON: device = new PUPRemoteButton(this, portId); break; + case Consts.DeviceType.HUB_LED: + device = new HubLED(this, portId); + break; default: device = new Device(this, portId, undefined, deviceType); break; diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index aecce975..03937a3f 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -78,45 +78,6 @@ export class LPF2Hub extends Hub { } - /** - * Set the color of the LED on the Hub via a color value. - * @method LPF2Hub#setLEDColor - * @param {Color} color - * @returns {Promise} Resolved upon successful issuance of command. - */ - public setLEDColor (color: number | boolean) { - return new Promise((resolve, reject) => { - let data = Buffer.from([0x41, this._ledPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); - this.send(data, Consts.BLECharacteristic.LPF2_ALL); - if (typeof color === "boolean") { - color = 0; - } - data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x00, color]); - this.send(data, Consts.BLECharacteristic.LPF2_ALL); - return resolve(); - }); - } - - - /** - * Set the color of the LED on the Hub via RGB values. - * @method LPF2Hub#setLEDRGB - * @param {number} red - * @param {number} green - * @param {number} blue - * @returns {Promise} Resolved upon successful issuance of command. - */ - public setLEDRGB (red: number, green: number, blue: number) { - return new Promise((resolve, reject) => { - let data = Buffer.from([0x41, this._ledPort, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); - this.send(data, Consts.BLECharacteristic.LPF2_ALL); - data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x01, red, green, blue]); - this.send(data, Consts.BLECharacteristic.LPF2_ALL); - return resolve(); - }); - } - - public send (message: Buffer, uuid: string, callback?: () => void) { message = Buffer.concat([Buffer.alloc(2), message]); message[0] = message.length; @@ -460,35 +421,6 @@ export class LPF2Hub extends Hub { // this.emit("accel", "ACCEL", accelX, accelY, accelZ); // break; // } - // case Consts.DeviceType.BOOST_TILT: { - // const tiltX = data.readInt8(4); - // const tiltY = data.readInt8(5); - // this._lastTiltX = tiltX; - // this._lastTiltY = tiltY; - // this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ); - // break; - // } - // case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: { - // switch (data[4]) { - // case 0x01: { - // this.emit("button", port.id, Consts.ButtonState.UP); - // break; - // } - // case 0xff: { - // this.emit("button", port.id, Consts.ButtonState.DOWN); - // break; - // } - // case 0x7f: { - // this.emit("button", port.id, Consts.ButtonState.STOP); - // break; - // } - // case 0x00: { - // this.emit("button", port.id, Consts.ButtonState.RELEASED); - // break; - // } - // } - // break; - // } // case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: { // if (data[4] <= 10) { // this.emit("color", port.id, data[4]); diff --git a/src/index-browser.ts b/src/index-browser.ts index 3e99c92f..78cd6b65 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -13,10 +13,12 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; +import { HubLED } from "./devices/hubled"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; +import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; import { PUPRemoteButton } from "./devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; @@ -40,10 +42,12 @@ window.PoweredUP = { Consts, ColorDistanceSensor, Device, + HubLED, Light, MediumLinearMotor, MotionSensor, MoveHubMediumLinearMotor, + MoveHubTiltSensor, PUPRemoteButton, SimpleMediumLinearMotor, TechnicLargeLinearMotor, diff --git a/src/index-node.ts b/src/index-node.ts index 497c00c1..0147263e 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -13,10 +13,12 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; +import { HubLED } from "./devices/hubled"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; +import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; import { PUPRemoteButton } from "./devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; @@ -40,10 +42,12 @@ export { Consts, ColorDistanceSensor, Device, + HubLED, Light, MediumLinearMotor, MotionSensor, MoveHubMediumLinearMotor, + MoveHubTiltSensor, PUPRemoteButton, SimpleMediumLinearMotor, TechnicLargeLinearMotor, From 6e77d697fce1e308f774284959007085dba13e9d Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 16:51:32 -0800 Subject: [PATCH 31/62] Control+ (Technic Medium Hub) sensors --- src/consts.ts | 6 +-- src/devices/movehubtiltsensor.ts | 4 +- .../technicmediumhubaccelerometersensor.ts | 46 +++++++++++++++++++ src/devices/technicmediumhubgyrosensor.ts | 45 ++++++++++++++++++ src/devices/technicmediumhubtiltsensor.ts | 45 ++++++++++++++++++ src/hubs/hub.ts | 12 +++++ src/index-browser.ts | 6 +++ src/index-node.ts | 6 +++ 8 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 src/devices/technicmediumhubaccelerometersensor.ts create mode 100644 src/devices/technicmediumhubgyrosensor.ts create mode 100644 src/devices/technicmediumhubtiltsensor.ts diff --git a/src/consts.ts b/src/consts.ts index 8af9de73..015a6d1f 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -74,9 +74,9 @@ export enum DeviceType { CONTROL_PLUS_GEST = 54, PUP_REMOTE_BUTTON = 55, RSSI = 56, - CONTROL_PLUS_ACCELEROMETER = 57, - CONTROL_PLUS_GYRO = 58, - CONTROL_PLUS_TILT = 59, + TECHNIC_MEDIUM_HUB_ACCELEROMETER = 57, + TECHNIC_MEDIUM_HUB_GYRO_SENSOR = 58, + TECHNIC_MEDIUM_HUB_TILT_SENSOR = 59, TEMPERATURE = 60, } diff --git a/src/devices/movehubtiltsensor.ts b/src/devices/movehubtiltsensor.ts index 7e65fe18..70192e59 100644 --- a/src/devices/movehubtiltsensor.ts +++ b/src/devices/movehubtiltsensor.ts @@ -21,9 +21,9 @@ export class MoveHubTiltSensor extends Device { * @param {number} x * @param {number} y */ - const tiltX = -(message.readInt8(4)); + const tiltX = message.readInt8(4); const tiltY = message.readInt8(5); - this.emit("tilt", tiltX, tiltY); + this.emit("tilt", -tiltX, tiltY); break; } } diff --git a/src/devices/technicmediumhubaccelerometersensor.ts b/src/devices/technicmediumhubaccelerometersensor.ts new file mode 100644 index 00000000..51b0a03b --- /dev/null +++ b/src/devices/technicmediumhubaccelerometersensor.ts @@ -0,0 +1,46 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class TechnicMediumHubAccelerometerSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, TechnicMediumHubAccelerometerSensor.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case TechnicMediumHubAccelerometerSensor.Mode.ACCEL: + /** + * Emits when accelerometer detects movement. Measured in mG. + * @event LPF2Hub#accel + * @param {string} port + * @param {number} x + * @param {number} y + * @param {number} z + */ + const accelX = Math.round(message.readInt16LE(4) / 4.096); + const accelY = Math.round(message.readInt16LE(6) / 4.096); + const accelZ = Math.round(message.readInt16LE(8) / 4.096); + this.emit("accel", accelX, accelY, accelZ); + break; + } + } + +} + +export namespace TechnicMediumHubAccelerometerSensor { + + export enum Mode { + ACCEL = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "accel": TechnicMediumHubAccelerometerSensor.Mode.ACCEL + } + +} \ No newline at end of file diff --git a/src/devices/technicmediumhubgyrosensor.ts b/src/devices/technicmediumhubgyrosensor.ts new file mode 100644 index 00000000..3151e429 --- /dev/null +++ b/src/devices/technicmediumhubgyrosensor.ts @@ -0,0 +1,45 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class TechnicMediumHubGyroSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, TechnicMediumHubGyroSensor.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case TechnicMediumHubGyroSensor.Mode.GYRO: + /** + * Emits when gyroscope detects movement. Measured in DPS - degrees per second. + * @event TechnicMediumHubGyroSensor#gyro + * @param {number} x + * @param {number} y + * @param {number} z + */ + const gyroX = Math.round(message.readInt16LE(4) * 7 / 400); + const gyroY = Math.round(message.readInt16LE(6) * 7 / 400); + const gyroZ = Math.round(message.readInt16LE(8) * 7 / 400); + this.emit("gyro", gyroX, gyroY, -gyroZ); + break; + } + } + +} + +export namespace TechnicMediumHubGyroSensor { + + export enum Mode { + GYRO = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "gyro": TechnicMediumHubGyroSensor.Mode.GYRO + } + +} \ No newline at end of file diff --git a/src/devices/technicmediumhubtiltsensor.ts b/src/devices/technicmediumhubtiltsensor.ts new file mode 100644 index 00000000..f5cc6579 --- /dev/null +++ b/src/devices/technicmediumhubtiltsensor.ts @@ -0,0 +1,45 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class TechnicMediumHubTiltSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, TechnicMediumHubTiltSensor.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case TechnicMediumHubTiltSensor.Mode.TILT: + /** + * Emits when a tilt sensor is activated. + * @event TechnicMediumHubTiltSensor#tilt + * @param {number} x + * @param {number} y + * @param {number} z + */ + const tiltZ = message.readInt16LE(4); + const tiltY = message.readInt16LE(6); + const tiltX = message.readInt16LE(8); + this.emit("tilt", tiltX, tiltY, -tiltZ); + break; + } + } + +} + +export namespace TechnicMediumHubTiltSensor { + + export enum Mode { + TILT = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "tilt": TechnicMediumHubTiltSensor.Mode.TILT + } + +} \ No newline at end of file diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 832f42f9..3596ed2d 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -14,6 +14,9 @@ import { MoveHubTiltSensor } from "../devices/movehubtiltsensor"; import { PUPRemoteButton } from "../devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; +import { TechnicMediumHubAccelerometerSensor } from "../devices/technicmediumhubaccelerometersensor"; +import { TechnicMediumHubGyroSensor } from "../devices/technicmediumhubgyrosensor"; +import { TechnicMediumHubTiltSensor } from "../devices/technicmediumhubtiltsensor"; import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor"; import { TiltSensor } from "../devices/tiltsensor"; import { TrainMotor } from "../devices/trainmotor"; @@ -322,6 +325,15 @@ export class Hub extends EventEmitter { case Consts.DeviceType.MOVE_HUB_TILT_SENSOR: device = new MoveHubTiltSensor(this, portId); break; + case Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR: + device = new TechnicMediumHubTiltSensor(this, portId); + break; + case Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR: + device = new TechnicMediumHubGyroSensor(this, portId); + break; + case Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER: + device = new TechnicMediumHubAccelerometerSensor(this, portId); + break; case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: device = new MediumLinearMotor(this, portId); break; diff --git a/src/index-browser.ts b/src/index-browser.ts index 78cd6b65..707e687c 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -22,6 +22,9 @@ import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; import { PUPRemoteButton } from "./devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; +import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor"; +import { TechnicMediumHubGyroSensor } from "./devices/technicmediumhubgyrosensor"; +import { TechnicMediumHubTiltSensor } from "./devices/technicmediumhubtiltsensor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; import { TiltSensor } from "./devices/tiltsensor"; import { TrainMotor } from "./devices/trainmotor"; @@ -50,6 +53,9 @@ window.PoweredUP = { MoveHubTiltSensor, PUPRemoteButton, SimpleMediumLinearMotor, + TechnicMediumHubAccelerometerSensor, + TechnicMediumHubGyroSensor, + TechnicMediumHubTiltSensor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, TiltSensor, diff --git a/src/index-node.ts b/src/index-node.ts index 0147263e..a01c76ee 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -22,6 +22,9 @@ import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; import { PUPRemoteButton } from "./devices/pupremotebutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; +import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor"; +import { TechnicMediumHubGyroSensor } from "./devices/technicmediumhubgyrosensor"; +import { TechnicMediumHubTiltSensor } from "./devices/technicmediumhubtiltsensor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; import { TiltSensor } from "./devices/tiltsensor"; import { TrainMotor } from "./devices/trainmotor"; @@ -50,6 +53,9 @@ export { MoveHubTiltSensor, PUPRemoteButton, SimpleMediumLinearMotor, + TechnicMediumHubAccelerometerSensor, + TechnicMediumHubGyroSensor, + TechnicMediumHubTiltSensor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, TiltSensor, From a5a9b2e880f8ad68c98e4ba30c71555407470d77 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 17:13:21 -0800 Subject: [PATCH 32/62] Control+ (Technic Medium Hub) devices, renamed hubs to be more inline with official naming convention --- src/consts.ts | 22 +- src/devices/currentsensor.ts | 2 +- ...remotebutton.ts => remotecontrolbutton.ts} | 12 +- src/devices/voltagesensor.ts | 10 +- src/hubs/basehub.ts | 375 ++++++++++++++++++ src/hubs/duplotrainbase.ts | 10 +- src/hubs/hub.ts | 375 ++---------------- src/hubs/lpf2hub.ts | 52 +-- src/hubs/{boostmovehub.ts => movehub.ts} | 27 +- src/hubs/puphub.ts | 68 ---- src/hubs/{pupremote.ts => remotecontrol.ts} | 21 +- ...{controlplushub.ts => technicmediumhub.ts} | 18 +- src/hubs/wedo2smarthub.ts | 6 +- src/index-browser.ts | 22 +- src/index-node.ts | 22 +- src/poweredup-browser.ts | 62 +-- src/poweredup-node.ts | 40 +- 17 files changed, 546 insertions(+), 598 deletions(-) rename src/devices/{pupremotebutton.ts => remotecontrolbutton.ts} (71%) create mode 100644 src/hubs/basehub.ts rename src/hubs/{boostmovehub.ts => movehub.ts} (63%) delete mode 100644 src/hubs/puphub.ts rename src/hubs/{pupremote.ts => remotecontrol.ts} (72%) rename src/hubs/{controlplushub.ts => technicmediumhub.ts} (90%) diff --git a/src/consts.ts b/src/consts.ts index 015a6d1f..a08450bb 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -2,7 +2,7 @@ * @typedef HubType * @property {number} UNKNOWN 0 * @property {number} WEDO2_SMART_HUB 1 - * @property {number} BOOST_MOVE_HUB 2 + * @property {number} MOVE_HUB 2 * @property {number} POWERED_UP_HUB 3 * @property {number} POWERED_UP_REMOTE 4 * @property {number} DUPLO_TRAIN_HUB 5 @@ -11,11 +11,11 @@ export enum HubType { UNKNOWN = 0, WEDO2_SMART_HUB = 1, - BOOST_MOVE_HUB = 2, - POWERED_UP_HUB = 3, - POWERED_UP_REMOTE = 4, - DUPLO_TRAIN_HUB = 5, - CONTROL_PLUS_HUB = 6 + MOVE_HUB = 2, + HUB = 3, + REMOTE_CONTROL = 4, + DUPLO_TRAIN_BASE = 5, + TECHNIC_MEDIUM_HUB = 6 } @@ -155,11 +155,11 @@ export enum DuploTrainBaseSound { export enum BLEManufacturerData { - DUPLO_TRAIN_HUB_ID = 32, - BOOST_MOVE_HUB_ID = 64, - POWERED_UP_HUB_ID = 65, - POWERED_UP_REMOTE_ID = 66, - CONTROL_PLUS_LARGE_HUB = 128 + DUPLO_TRAIN_BASE_ID = 32, + MOVE_HUB_ID = 64, + HUB_ID = 65, + REMOTE_CONTROL_ID = 66, + TECHNIC_MEDIUM_HUB = 128 } diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index b078454b..efa11c87 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -48,7 +48,7 @@ export namespace CurrentSensor { export const MaxCurrentValue: {[hubType: number]: number} = { [Consts.HubType.UNKNOWN]: 2444, - [Consts.HubType.CONTROL_PLUS_HUB]: 4175, + [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4175, } export const MaxCurrentRaw: {[hubType: number]: number} = { diff --git a/src/devices/pupremotebutton.ts b/src/devices/remotecontrolbutton.ts similarity index 71% rename from src/devices/pupremotebutton.ts rename to src/devices/remotecontrolbutton.ts index 0f7719fd..c2377c89 100644 --- a/src/devices/pupremotebutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -4,20 +4,20 @@ import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; -export class PUPRemoteButton extends Device { +export class RemoteControlButton extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, PUPRemoteButton.ModeMap, Consts.DeviceType.PUP_REMOTE_BUTTON); + super(hub, portId, RemoteControlButton.ModeMap, Consts.DeviceType.PUP_REMOTE_BUTTON); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case PUPRemoteButton.Mode.BUTTON_EVENTS: + case RemoteControlButton.Mode.BUTTON_EVENTS: /** * Emits when a button on the remote is pressed or released. - * @event PUPRemoteButton#button + * @event RemoteControlButton#button * @param {number} event */ const event = message[4]; @@ -28,14 +28,14 @@ export class PUPRemoteButton extends Device { } -export namespace PUPRemoteButton { +export namespace RemoteControlButton { export enum Mode { BUTTON_EVENTS = 0x00 } export const ModeMap: {[event: string]: number} = { - "button": PUPRemoteButton.Mode.BUTTON_EVENTS + "button": RemoteControlButton.Mode.BUTTON_EVENTS } export const ButtonState: {[state: string]: number} = { diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index a1a85cac..aadf7c5e 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -48,15 +48,15 @@ export namespace VoltageSensor { export const MaxVoltageValue: {[hubType: number]: number} = { [Consts.HubType.UNKNOWN]: 9.615, - [Consts.HubType.DUPLO_TRAIN_HUB]: 6.4, - [Consts.HubType.POWERED_UP_REMOTE]: 6.4, + [Consts.HubType.DUPLO_TRAIN_BASE]: 6.4, + [Consts.HubType.REMOTE_CONTROL]: 6.4, } export const MaxVoltageRaw: {[hubType: number]: number} = { [Consts.HubType.UNKNOWN]: 3893, - [Consts.HubType.DUPLO_TRAIN_HUB]: 3047, - [Consts.HubType.POWERED_UP_REMOTE]: 3200, - [Consts.HubType.CONTROL_PLUS_HUB]: 4095, + [Consts.HubType.DUPLO_TRAIN_BASE]: 3047, + [Consts.HubType.REMOTE_CONTROL]: 3200, + [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4095, } } \ No newline at end of file diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts new file mode 100644 index 00000000..7f029e34 --- /dev/null +++ b/src/hubs/basehub.ts @@ -0,0 +1,375 @@ +import { EventEmitter } from "events"; + +import { IBLEAbstraction } from "../interfaces"; + +import { ColorDistanceSensor } from "../devices/colordistancesensor"; +import { CurrentSensor } from "../devices/currentsensor"; +import { Device } from "../devices/device"; +import { HubLED } from "../devices/hubled"; +import { Light } from "../devices/light"; +import { MediumLinearMotor } from "../devices/mediumlinearmotor"; +import { MotionSensor } from "../devices/motionsensor"; +import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor"; +import { MoveHubTiltSensor } from "../devices/movehubtiltsensor"; +import { RemoteControlButton } from "../devices/remotecontrolbutton"; +import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; +import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; +import { TechnicMediumHubAccelerometerSensor } from "../devices/technicmediumhubaccelerometersensor"; +import { TechnicMediumHubGyroSensor } from "../devices/technicmediumhubgyrosensor"; +import { TechnicMediumHubTiltSensor } from "../devices/technicmediumhubtiltsensor"; +import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor"; +import { TiltSensor } from "../devices/tiltsensor"; +import { TrainMotor } from "../devices/trainmotor"; +import { VoltageSensor } from "../devices/voltagesensor"; + +import * as Consts from "../consts"; + +import Debug = require("debug"); +const debug = Debug("basehub"); + + +/** + * @class BaseHub + * @extends EventEmitter + */ +export class BaseHub extends EventEmitter { + + protected _attachedDevices: {[portId: number]: Device} = {}; + // protected _virtualPorts: {[portName: string]: Port} = {}; + + protected _name: string = ""; + protected _firmwareVersion: string = "0.0.00.0000"; + protected _hardwareVersion: string = "0.0.00.0000"; + protected _primaryMACAddress: string = "00:00:00:00:00:00"; + protected _batteryLevel: number = 100; + protected _rssi: number = -60; + + protected _bleDevice: IBLEAbstraction; + + private _type: Consts.HubType; + private _portMap: {[portName: string]: number} = {}; + private _attachCallbacks: ((device: Device) => void)[] = []; + + constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { + super(); + this._type = type; + this._bleDevice = device; + this._portMap = portMap; + device.on("disconnect", () => { + /** + * Emits when the hub is disconnected. + * @event Hub#disconnect + */ + this.emit("disconnect"); + }); + } + + + /** + * @readonly + * @property {string} name Name of the hub + */ + public get name () { + return this._bleDevice.name; + } + + + /** + * @readonly + * @property {string} type Hub type + */ + public get type () { + return this._type; + } + + + /** + * @readonly + * @property {string[]} ports Array of port names + */ + public get ports () { + return Object.keys(this._portMap); + } + + + /** + * @readonly + * @property {string} firmwareVersion Firmware version of the hub + */ + public get firmwareVersion () { + return this._firmwareVersion; + } + + + /** + * @readonly + * @property {string} firmwareVersion Hardware version of the hub + */ + public get hardwareVersion () { + return this._hardwareVersion; + } + + + /** + * @readonly + * @property {string} primaryMACAddress Primary MAC address of the hub + */ + public get primaryMACAddress () { + return this._primaryMACAddress; + } + + + /** + * @readonly + * @property {string} uuid UUID of the hub + */ + public get uuid () { + return this._bleDevice.uuid; + } + + + /** + * @readonly + * @property {number} batteryLevel Battery level of the hub (Percentage between 0-100) + */ + public get batteryLevel () { + return this._batteryLevel; + } + + + /** + * @readonly + * @property {number} rssi Signal strength of the hub + */ + public get rssi () { + return this._rssi; + } + + + /** + * Connect to the Hub. + * @method Hub#connect + * @returns {Promise} Resolved upon successful connect. + */ + public connect () { + return new Promise(async (connectResolve, connectReject) => { + if (this._bleDevice.connecting) { + return connectReject("Already connecting"); + } else if (this._bleDevice.connected) { + return connectReject("Already connected"); + } + await this._bleDevice.connect(); + return connectResolve(); + }); + + } + + + /** + * Disconnect the Hub. + * @method Hub#disconnect + * @returns {Promise} Resolved upon successful disconnect. + */ + public disconnect () { + return this._bleDevice.disconnect(); + } + + + public getDeviceAtPort (portName: string) { + const portId = this._portMap[portName]; + if (portId) { + return this._attachedDevices[portId]; + } else { + throw new Error(`Port ${portName} does not exist on this hub type`); + } + } + + + public waitForDeviceAtPort (portName: string) { + return new Promise((resolve) => { + const existingDevice = this.getDeviceAtPort(portName); + if (existingDevice) { + return resolve(existingDevice); + } + this._attachCallbacks.push((device) => { + if (device.portName === portName) { + return resolve(device); + } + }); + }); + } + + + public getDevices () { + return Object.values(this._attachedDevices); + } + + + public getDevicesByType (deviceType: number) { + return this.getDevices().filter((device) => device.type === deviceType); + } + + + public waitForDeviceByType (deviceType: number) { + return new Promise((resolve) => { + const existingDevices = this.getDevicesByType(deviceType); + if (existingDevices.length >= 1) { + return resolve(existingDevices[0]); + } + this._attachCallbacks.push((device) => { + if (device.type === deviceType) { + return resolve(device); + } + }) + }); + } + + + public getPortNameForPortId (portId: number) { + for (const port of Object.keys(this._portMap)) { + if (this._portMap[port] === portId) { + return port; + } + } + return; + } + + + /** + * Sleep a given amount of time. + * + * This is a helper method to make it easier to add delays into a chain of commands. + * @method Hub#sleep + * @param {number} delay How long to sleep (in milliseconds). + * @returns {Promise} Resolved after the delay is finished. + */ + public sleep (delay: number) { + return new Promise((resolve) => { + global.setTimeout(resolve, delay); + }); + } + + + /** + * Wait until a given list of concurrently running commands are complete. + * + * This is a helper method to make it easier to wait for concurrent commands to complete. + * @method Hub#wait + * @param {Array>} commands Array of executing commands. + * @returns {Promise} Resolved after the commands are finished. + */ + public wait (commands: Array>) { + return Promise.all(commands); + } + + + public send (message: Buffer, uuid: string, callback?: () => void) { + if (callback) { + callback(); + } + } + + + public subscribe (portId: number, deviceType: number, mode: number) { + // NK Do nothing here + } + + + protected _attachDevice (device: Device) { + this._attachedDevices[device.portId] = device; + /** + * Emits when a device is attached to the Hub. + * @event Hub#attach + * @param {Device} device + */ + this.emit("attach", device); + this._attachCallbacks.forEach((callback) => { + callback(device); + }); + } + + + protected _detachDevice (device: Device) { + delete this._attachedDevices[device.portId]; + /** + * Emits when a device is detached from the Hub. + * @event Hub#attach + * @param {Device} device + */ + this.emit("detach", device); + } + + + protected _createDevice (deviceType: number, portId: number) { + let device; + + switch (deviceType) { + case Consts.DeviceType.LIGHT: + device = new Light(this, portId); + break; + case Consts.DeviceType.TRAIN_MOTOR: + device = new TrainMotor(this, portId); + break; + case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: + device = new SimpleMediumLinearMotor(this, portId); + break; + case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: + device = new MoveHubMediumLinearMotor(this, portId); + break; + case Consts.DeviceType.MOTION_SENSOR: + device = new MotionSensor(this, portId); + break; + case Consts.DeviceType.TILT_SENSOR: + device = new TiltSensor(this, portId); + break; + case Consts.DeviceType.MOVE_HUB_TILT_SENSOR: + device = new MoveHubTiltSensor(this, portId); + break; + case Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR: + device = new TechnicMediumHubTiltSensor(this, portId); + break; + case Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR: + device = new TechnicMediumHubGyroSensor(this, portId); + break; + case Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER: + device = new TechnicMediumHubAccelerometerSensor(this, portId); + break; + case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: + device = new MediumLinearMotor(this, portId); + break; + case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR: + device = new TechnicLargeLinearMotor(this, portId); + break; + case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR: + device = new TechnicXLargeLinearMotor(this, portId); + break; + case Consts.DeviceType.COLOR_DISTANCE_SENSOR: + device = new ColorDistanceSensor(this, portId); + break; + case Consts.DeviceType.VOLTAGE_SENSOR: + device = new VoltageSensor(this, portId); + break; + case Consts.DeviceType.CURRENT_SENSOR: + device = new CurrentSensor(this, portId); + break; + case Consts.DeviceType.PUP_REMOTE_BUTTON: + device = new RemoteControlButton(this, portId); + break; + case Consts.DeviceType.HUB_LED: + device = new HubLED(this, portId); + break; + default: + device = new Device(this, portId, undefined, deviceType); + break; + } + + return device; + } + + + protected _getDeviceByPortId (portId: number) { + return this._attachedDevices[portId]; + } + + +} diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index 98eda910..727dd421 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -13,7 +13,7 @@ const debug = Debug("duplotrainbase"); * The DuploTrainBase is emitted if the discovered device is a Duplo Train Base. * @class DuploTrainBase * @extends LPF2Hub - * @extends Hub + * @extends BaseHub */ export class DuploTrainBase extends LPF2Hub { @@ -25,15 +25,13 @@ export class DuploTrainBase extends LPF2Hub { peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData && peripheral.advertisement.manufacturerData.length > 3 && - peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID ); } - - - protected _ledPort = 0x11; + constructor (device: IBLEAbstraction) { - super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB); + super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_BASE); debug("Discovered Duplo Train Base"); } diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 3596ed2d..0e567d4c 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -1,26 +1,9 @@ -import { EventEmitter } from "events"; +import { Peripheral } from "@abandonware/noble"; +import compareVersion from "compare-versions"; import { IBLEAbstraction } from "../interfaces"; -import { ColorDistanceSensor } from "../devices/colordistancesensor"; -import { CurrentSensor } from "../devices/currentsensor"; -import { Device } from "../devices/device"; -import { HubLED } from "../devices/hubled"; -import { Light } from "../devices/light"; -import { MediumLinearMotor } from "../devices/mediumlinearmotor"; -import { MotionSensor } from "../devices/motionsensor"; -import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor"; -import { MoveHubTiltSensor } from "../devices/movehubtiltsensor"; -import { PUPRemoteButton } from "../devices/pupremotebutton"; -import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; -import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; -import { TechnicMediumHubAccelerometerSensor } from "../devices/technicmediumhubaccelerometersensor"; -import { TechnicMediumHubGyroSensor } from "../devices/technicmediumhubgyrosensor"; -import { TechnicMediumHubTiltSensor } from "../devices/technicmediumhubtiltsensor"; -import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor"; -import { TiltSensor } from "../devices/tiltsensor"; -import { TrainMotor } from "../devices/trainmotor"; -import { VoltageSensor } from "../devices/voltagesensor"; +import { LPF2Hub } from "./lpf2hub"; import * as Consts from "../consts"; @@ -29,347 +12,57 @@ const debug = Debug("hub"); /** + * The Hub is emitted if the discovered device is a Hub. * @class Hub - * @extends EventEmitter + * @extends LPF2Hub + * @extends BaseHub */ -export class Hub extends EventEmitter { +export class Hub extends LPF2Hub { - protected _attachedDevices: {[portId: number]: Device} = {}; - // protected _virtualPorts: {[portName: string]: Port} = {}; - protected _name: string = ""; - protected _firmwareVersion: string = "0.0.00.0000"; - protected _hardwareVersion: string = "0.0.00.0000"; - protected _primaryMACAddress: string = "00:00:00:00:00:00"; - protected _batteryLevel: number = 100; - protected _rssi: number = -60; - - protected _bleDevice: IBLEAbstraction; - - private _type: Consts.HubType; - private _portMap: {[portName: string]: number} = {}; - private _attachCallbacks: ((device: Device) => void)[] = []; - - constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { - super(); - this._type = type; - this._bleDevice = device; - this._portMap = portMap; - device.on("disconnect", () => { - /** - * Emits when the hub is disconnected. - * @event Hub#disconnect - */ - this.emit("disconnect"); - }); - } - - - /** - * @readonly - * @property {string} name Name of the hub - */ - public get name () { - return this._bleDevice.name; - } - - - /** - * @readonly - * @property {string} type Hub type - */ - public get type () { - return this._type; + public static IsHub (peripheral: Peripheral) { + return ( + peripheral.advertisement && + peripheral.advertisement.serviceUuids && + peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && + peripheral.advertisement.manufacturerData && + peripheral.advertisement.manufacturerData.length > 3 && + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.HUB_ID + ); } + protected _currentPort = 0x3b; - /** - * @readonly - * @property {string[]} ports Array of port names - */ - public get ports () { - return Object.keys(this._portMap); + constructor (device: IBLEAbstraction) { + super(device, Hub.PortMap, Consts.HubType.HUB); + debug("Discovered Powered UP Hub"); } - /** - * @readonly - * @property {string} firmwareVersion Firmware version of the hub - */ - public get firmwareVersion () { - return this._firmwareVersion; - } - - - /** - * @readonly - * @property {string} firmwareVersion Hardware version of the hub - */ - public get hardwareVersion () { - return this._hardwareVersion; - } - - - /** - * @readonly - * @property {string} primaryMACAddress Primary MAC address of the hub - */ - public get primaryMACAddress () { - return this._primaryMACAddress; - } - - - /** - * @readonly - * @property {string} uuid UUID of the hub - */ - public get uuid () { - return this._bleDevice.uuid; - } - - - /** - * @readonly - * @property {number} batteryLevel Battery level of the hub (Percentage between 0-100) - */ - public get batteryLevel () { - return this._batteryLevel; - } - - - /** - * @readonly - * @property {number} rssi Signal strength of the hub - */ - public get rssi () { - return this._rssi; - } - - - /** - * Connect to the Hub. - * @method Hub#connect - * @returns {Promise} Resolved upon successful connect. - */ public connect () { - return new Promise(async (connectResolve, connectReject) => { - if (this._bleDevice.connecting) { - return connectReject("Already connecting"); - } else if (this._bleDevice.connected) { - return connectReject("Already connected"); - } - await this._bleDevice.connect(); - return connectResolve(); - }); - - } - - - /** - * Disconnect the Hub. - * @method Hub#disconnect - * @returns {Promise} Resolved upon successful disconnect. - */ - public disconnect () { - return this._bleDevice.disconnect(); - } - - - public getDeviceAtPort (portName: string) { - const portId = this._portMap[portName]; - if (portId) { - return this._attachedDevices[portId]; - } else { - throw new Error(`Port ${portName} does not exist on this hub type`); - } - } - - - public waitForDeviceAtPort (portName: string) { - return new Promise((resolve) => { - const existingDevice = this.getDeviceAtPort(portName); - if (existingDevice) { - return resolve(existingDevice); - } - this._attachCallbacks.push((device) => { - if (device.portName === portName) { - return resolve(device); - } - }); - }); - } - - - public getDevices () { - return Object.values(this._attachedDevices); - } - - - public getDevicesByType (deviceType: number) { - return this.getDevices().filter((device) => device.type === deviceType); - } - - - public waitForDeviceByType (deviceType: number) { - return new Promise((resolve) => { - const existingDevices = this.getDevicesByType(deviceType); - if (existingDevices.length >= 1) { - return resolve(existingDevices[0]); - } - this._attachCallbacks.push((device) => { - if (device.type === deviceType) { - return resolve(device); - } - }) + return new Promise(async (resolve, reject) => { + debug("Connecting to Powered UP Hub"); + await super.connect(); + debug("Connect completed"); + return resolve(); }); } - public getPortNameForPortId (portId: number) { - for (const port of Object.keys(this._portMap)) { - if (this._portMap[port] === portId) { - return port; - } + protected _checkFirmware (version: string) { + if (compareVersion("1.1.00.0004", version) === 1) { + throw new Error(`Your Powered Up Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`); } - return; - } - - - /** - * Sleep a given amount of time. - * - * This is a helper method to make it easier to add delays into a chain of commands. - * @method Hub#sleep - * @param {number} delay How long to sleep (in milliseconds). - * @returns {Promise} Resolved after the delay is finished. - */ - public sleep (delay: number) { - return new Promise((resolve) => { - global.setTimeout(resolve, delay); - }); } - /** - * Wait until a given list of concurrently running commands are complete. - * - * This is a helper method to make it easier to wait for concurrent commands to complete. - * @method Hub#wait - * @param {Array>} commands Array of executing commands. - * @returns {Promise} Resolved after the commands are finished. - */ - public wait (commands: Array>) { - return Promise.all(commands); - } - - - public send (message: Buffer, uuid: string, callback?: () => void) { - if (callback) { - callback(); - } - } - - - public subscribe (portId: number, deviceType: number, mode: number) { - // NK Do nothing here - } - - - protected _attachDevice (device: Device) { - this._attachedDevices[device.portId] = device; - /** - * Emits when a device is attached to the Hub. - * @event Hub#attach - * @param {Device} device - */ - this.emit("attach", device); - this._attachCallbacks.forEach((callback) => { - callback(device); - }); - } - - - protected _detachDevice (device: Device) { - delete this._attachedDevices[device.portId]; - /** - * Emits when a device is detached from the Hub. - * @event Hub#attach - * @param {Device} device - */ - this.emit("detach", device); - } - - - protected _createDevice (deviceType: number, portId: number) { - let device; - - switch (deviceType) { - case Consts.DeviceType.LIGHT: - device = new Light(this, portId); - break; - case Consts.DeviceType.TRAIN_MOTOR: - device = new TrainMotor(this, portId); - break; - case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: - device = new SimpleMediumLinearMotor(this, portId); - break; - case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: - device = new MoveHubMediumLinearMotor(this, portId); - break; - case Consts.DeviceType.MOTION_SENSOR: - device = new MotionSensor(this, portId); - break; - case Consts.DeviceType.TILT_SENSOR: - device = new TiltSensor(this, portId); - break; - case Consts.DeviceType.MOVE_HUB_TILT_SENSOR: - device = new MoveHubTiltSensor(this, portId); - break; - case Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR: - device = new TechnicMediumHubTiltSensor(this, portId); - break; - case Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR: - device = new TechnicMediumHubGyroSensor(this, portId); - break; - case Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER: - device = new TechnicMediumHubAccelerometerSensor(this, portId); - break; - case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: - device = new MediumLinearMotor(this, portId); - break; - case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR: - device = new TechnicLargeLinearMotor(this, portId); - break; - case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR: - device = new TechnicXLargeLinearMotor(this, portId); - break; - case Consts.DeviceType.COLOR_DISTANCE_SENSOR: - device = new ColorDistanceSensor(this, portId); - break; - case Consts.DeviceType.VOLTAGE_SENSOR: - device = new VoltageSensor(this, portId); - break; - case Consts.DeviceType.CURRENT_SENSOR: - device = new CurrentSensor(this, portId); - break; - case Consts.DeviceType.PUP_REMOTE_BUTTON: - device = new PUPRemoteButton(this, portId); - break; - case Consts.DeviceType.HUB_LED: - device = new HubLED(this, portId); - break; - default: - device = new Device(this, portId, undefined, deviceType); - break; - } - - return device; - } +} +export namespace Hub { - protected _getDeviceByPortId (portId: number) { - return this._attachedDevices[portId]; + export const PortMap: {[portName: string]: number} = { + "A": 0, + "B": 1 } - -} +} \ No newline at end of file diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 03937a3f..0ece6943 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -1,4 +1,4 @@ -import { Hub } from "./hub"; +import { BaseHub } from "./basehub"; import * as Consts from "../consts"; @@ -11,15 +11,9 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo"); /** * @class LPF2Hub - * @extends Hub + * @extends BaseHub */ -export class LPF2Hub extends Hub { - - protected _ledPort: number = 0x32; - - private _lastTiltX: number = 0; - private _lastTiltY: number = 0; - private _lastTiltZ: number = 0; +export class LPF2Hub extends BaseHub { private _messageBuffer: Buffer = Buffer.alloc(0); @@ -381,46 +375,6 @@ export class LPF2Hub extends Hub { // if (port && port.connected) { // switch (port.type) { - // case Consts.DeviceType.CONTROL_PLUS_TILT: { - // const tiltZ = data.readInt16LE(4); - // const tiltY = data.readInt16LE(6); - // const tiltX = data.readInt16LE(8); - // this._lastTiltX = tiltX; - // this._lastTiltY = tiltY; - // this._lastTiltZ = tiltZ; - // this.emit("tilt", "TILT", this._lastTiltX, this._lastTiltY, this._lastTiltZ); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_GYRO: { - // const gyroX = Math.round(data.readInt16LE(4) * 7 / 400); - // const gyroY = Math.round(data.readInt16LE(6) * 7 / 400); - // const gyroZ = Math.round(data.readInt16LE(8) * 7 / 400); - // /** - // * Emits when gyroscope detects movement. Measured in DPS - degrees per second. - // * @event LPF2Hub#gyro - // * @param {string} port - // * @param {number} x - // * @param {number} y - // * @param {number} z - // */ - // this.emit("gyro", "GYRO", gyroX, gyroY, gyroZ); - // break; - // } - // case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: { - // const accelX = Math.round(data.readInt16LE(4) / 4.096); - // const accelY = Math.round(data.readInt16LE(6) / 4.096); - // const accelZ = Math.round(data.readInt16LE(8) / 4.096); - // /** - // * Emits when accelerometer detects movement. Measured in mG. - // * @event LPF2Hub#accel - // * @param {string} port - // * @param {number} x - // * @param {number} y - // * @param {number} z - // */ - // this.emit("accel", "ACCEL", accelX, accelY, accelZ); - // break; - // } // case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: { // if (data[4] <= 10) { // this.emit("color", port.id, data[4]); diff --git a/src/hubs/boostmovehub.ts b/src/hubs/movehub.ts similarity index 63% rename from src/hubs/boostmovehub.ts rename to src/hubs/movehub.ts index 644b2cf2..a38dbcdb 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/movehub.ts @@ -8,38 +8,38 @@ import { LPF2Hub } from "./lpf2hub"; import * as Consts from "../consts"; import Debug = require("debug"); -const debug = Debug("boostmovehub"); +const debug = Debug("movehub"); /** - * The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. - * @class BoostMoveHub + * The MoveHub is emitted if the discovered device is a Move Hub. + * @class MoveHub * @extends LPF2Hub - * @extends Hub + * @extends BaseHub */ -export class BoostMoveHub extends LPF2Hub { +export class MoveHub extends LPF2Hub { - public static IsBoostMoveHub (peripheral: Peripheral) { + public static IsMoveHub (peripheral: Peripheral) { return ( peripheral.advertisement && peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData && peripheral.advertisement.manufacturerData.length > 3 && - peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.MOVE_HUB_ID ); } constructor (device: IBLEAbstraction) { - super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); - debug("Discovered Boost Move Hub"); + super(device, MoveHub.PortMap, Consts.HubType.MOVE_HUB); + debug("Discovered Move Hub"); } public connect () { return new Promise(async (resolve, reject) => { - debug("Connecting to Boost Move Hub"); + debug("Connecting to Move Hub"); await super.connect(); debug("Connect completed"); return resolve(); @@ -49,21 +49,20 @@ export class BoostMoveHub extends LPF2Hub { protected _checkFirmware (version: string) { if (compareVersion("2.0.00.0017", version) === 1) { - throw new Error(`Your Boost Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`); + throw new Error(`Your Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`); } } } -export namespace BoostMoveHub { +export namespace MoveHub { export const PortMap: {[portName: string]: number} = { "A": 0, "B": 1, "C": 2, - "D": 3, - "TILT": 58 + "D": 3 } } \ No newline at end of file diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts deleted file mode 100644 index c3768561..00000000 --- a/src/hubs/puphub.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { Peripheral } from "@abandonware/noble"; -import compareVersion from "compare-versions"; - -import { IBLEAbstraction } from "../interfaces"; - -import { LPF2Hub } from "./lpf2hub"; - -import * as Consts from "../consts"; - -import Debug = require("debug"); -const debug = Debug("puphub"); - - -/** - * The PUPHub is emitted if the discovered device is a Powered UP Hub. - * @class PUPHub - * @extends LPF2Hub - * @extends Hub - */ -export class PUPHub extends LPF2Hub { - - - public static IsPUPHub (peripheral: Peripheral) { - return ( - peripheral.advertisement && - peripheral.advertisement.serviceUuids && - peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && - peripheral.advertisement.manufacturerData && - peripheral.advertisement.manufacturerData.length > 3 && - peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID - ); - } - - protected _currentPort = 0x3b; - - constructor (device: IBLEAbstraction) { - super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB); - debug("Discovered Powered UP Hub"); - } - - - public connect () { - return new Promise(async (resolve, reject) => { - debug("Connecting to Powered UP Hub"); - await super.connect(); - debug("Connect completed"); - return resolve(); - }); - } - - - protected _checkFirmware (version: string) { - if (compareVersion("1.1.00.0004", version) === 1) { - throw new Error(`Your Powered Up Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`); - } - } - - -} - -export namespace PUPHub { - - export const PortMap: {[portName: string]: number} = { - "A": 0, - "B": 1 - } - -} \ No newline at end of file diff --git a/src/hubs/pupremote.ts b/src/hubs/remotecontrol.ts similarity index 72% rename from src/hubs/pupremote.ts rename to src/hubs/remotecontrol.ts index 7197f16c..3d3c731d 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/remotecontrol.ts @@ -7,35 +7,32 @@ import { LPF2Hub } from "./lpf2hub"; import * as Consts from "../consts"; import Debug = require("debug"); -const debug = Debug("pupremote"); +const debug = Debug("remotecontrol"); /** - * The PUPRemote is emitted if the discovered device is a Powered UP Remote. - * @class PUPRemote + * The RemoteControl is emitted if the discovered device is a Remote Control. + * @class RemoteControl * @extends LPF2Hub - * @extends Hub + * @extends BaseHub */ -export class PUPRemote extends LPF2Hub { +export class RemoteControl extends LPF2Hub { - public static IsPUPRemote (peripheral: Peripheral) { + public static IsRemoteControl (peripheral: Peripheral) { return ( peripheral.advertisement && peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData && peripheral.advertisement.manufacturerData.length > 3 && - peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.REMOTE_CONTROL_ID ); } - protected _ledPort = 0x34; - - constructor (device: IBLEAbstraction) { - super(device, PUPRemote.PortMap, Consts.HubType.POWERED_UP_REMOTE); + super(device, RemoteControl.PortMap, Consts.HubType.REMOTE_CONTROL); debug("Discovered Powered UP Remote"); } @@ -52,7 +49,7 @@ export class PUPRemote extends LPF2Hub { } -export namespace PUPRemote { +export namespace RemoteControl { export const PortMap: {[portName: string]: number} = { "LEFT": 0, diff --git a/src/hubs/controlplushub.ts b/src/hubs/technicmediumhub.ts similarity index 90% rename from src/hubs/controlplushub.ts rename to src/hubs/technicmediumhub.ts index 43929ee4..dc6f2b59 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/technicmediumhub.ts @@ -7,31 +7,31 @@ import { LPF2Hub } from "./lpf2hub"; import * as Consts from "../consts"; import Debug = require("debug"); -const debug = Debug("ControlPlusHub"); +const debug = Debug("technicmediumhub"); /** - * The ControlPlusHub is emitted if the discovered device is a Control+ Hub. - * @class ControlPlusHub + * The TechnicMediumHub is emitted if the discovered device is a Technic Medium Hub. + * @class TechnicMediumHub * @extends LPF2Hub - * @extends Hub + * @extends BaseHub */ -export class ControlPlusHub extends LPF2Hub { +export class TechnicMediumHub extends LPF2Hub { - public static IsControlPlusHub (peripheral: Peripheral) { + public static IsTechnicMediumHub (peripheral: Peripheral) { return ( peripheral.advertisement && peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData && peripheral.advertisement.manufacturerData.length > 3 && - peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.CONTROL_PLUS_LARGE_HUB + peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB ); } constructor (device: IBLEAbstraction) { - super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB); + super(device, TechnicMediumHub.PortMap, Consts.HubType.TECHNIC_MEDIUM_HUB); debug("Discovered Control+ Hub"); } @@ -108,7 +108,7 @@ export class ControlPlusHub extends LPF2Hub { } -export namespace ControlPlusHub { +export namespace TechnicMediumHub { export const PortMap: {[portName: string]: number} = { "A": 0, diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index 7cd607a2..c6791da6 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -2,7 +2,7 @@ import { Peripheral } from "@abandonware/noble"; import { IBLEAbstraction } from "../interfaces"; -import { Hub } from "./hub"; +import { BaseHub } from "./basehub"; import * as Consts from "../consts"; @@ -15,9 +15,9 @@ const debug = Debug("wedo2smarthub"); /** * The WeDo2SmartHub is emitted if the discovered device is a WeDo 2.0 Smart Hub. * @class WeDo2SmartHub - * @extends Hub + * @extends BaseHub */ -export class WeDo2SmartHub extends Hub { +export class WeDo2SmartHub extends BaseHub { public static IsWeDo2SmartHub (peripheral: Peripheral) { diff --git a/src/index-browser.ts b/src/index-browser.ts index 707e687c..d38f8a37 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -2,12 +2,12 @@ import * as Consts from "./consts"; import { PoweredUP } from "./poweredup-browser"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; @@ -19,7 +19,7 @@ import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; -import { PUPRemoteButton } from "./devices/pupremotebutton"; +import { RemoteControlButton } from "./devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor"; @@ -35,12 +35,11 @@ import { isWebBluetooth } from "./utils"; // @ts-ignore window.PoweredUP = { PoweredUP, - Hub, + BaseHub, WeDo2SmartHub, - BoostMoveHub, - ControlPlusHub, - PUPHub, - PUPRemote, + TechnicMediumHub, + Hub, + RemoteControl, DuploTrainBase, Consts, ColorDistanceSensor, @@ -49,9 +48,10 @@ window.PoweredUP = { Light, MediumLinearMotor, MotionSensor, + MoveHub, MoveHubMediumLinearMotor, MoveHubTiltSensor, - PUPRemoteButton, + RemoteControlButton, SimpleMediumLinearMotor, TechnicMediumHubAccelerometerSensor, TechnicMediumHubGyroSensor, diff --git a/src/index-node.ts b/src/index-node.ts index a01c76ee..ce294e96 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -2,12 +2,12 @@ import * as Consts from "./consts"; import { PoweredUP } from "./poweredup-node"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; @@ -19,7 +19,7 @@ import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; -import { PUPRemoteButton } from "./devices/pupremotebutton"; +import { RemoteControlButton } from "./devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor"; @@ -35,12 +35,11 @@ import { isWebBluetooth } from "./utils"; export default PoweredUP; export { PoweredUP, - Hub, + BaseHub, WeDo2SmartHub, - BoostMoveHub, - ControlPlusHub, - PUPHub, - PUPRemote, + TechnicMediumHub, + Hub, + RemoteControl, DuploTrainBase, Consts, ColorDistanceSensor, @@ -49,9 +48,10 @@ export { Light, MediumLinearMotor, MotionSensor, + MoveHub, MoveHubMediumLinearMotor, MoveHubTiltSensor, - PUPRemoteButton, + RemoteControlButton, SimpleMediumLinearMotor, TechnicMediumHubAccelerometerSensor, TechnicMediumHubGyroSensor, diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 1156fdec..339a1b5e 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -1,11 +1,11 @@ import { WebBLEDevice } from "./webbleabstraction"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import * as Consts from "./consts"; @@ -24,7 +24,7 @@ const debug = Debug("poweredup"); export class PoweredUP extends EventEmitter { - private _connectedHubs: {[uuid: string]: Hub} = {}; + private _connectedHubs: {[uuid: string]: BaseHub} = {}; constructor () { @@ -76,7 +76,7 @@ export class PoweredUP extends EventEmitter { /** * Retrieve a list of Powered UP Hubs. * @method PoweredUP#getHubs - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubs () { return Object.values(this._connectedHubs); @@ -87,7 +87,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by UUID. * @method PoweredUP#getHubByUUID * @param {string} uuid - * @returns {Hub | null} + * @returns {BaseHub | null} */ public getHubByUUID (uuid: string) { return this._connectedHubs[uuid]; @@ -98,7 +98,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by primary MAC address. * @method PoweredUP#getHubByPrimaryMACAddress * @param {string} address - * @returns {Hub} + * @returns {BaseHub} */ public getHubByPrimaryMACAddress (address: string) { return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0]; @@ -109,7 +109,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a list of Powered UP Hub by name. * @method PoweredUP#getHubsByName * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByName (name: string) { return Object.values(this._connectedHubs).filter((hub) => hub.name === name); @@ -120,7 +120,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a list of Powered UP Hub by type. * @method PoweredUP#getHubsByType * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByType (hubType: number) { return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType); @@ -139,20 +139,20 @@ export class PoweredUP extends EventEmitter { if (message[2] === 0x01 && message[3] === 0x0b) { process.nextTick(() => { switch (message[5]) { - case Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID: - resolve(Consts.HubType.POWERED_UP_REMOTE); + case Consts.BLEManufacturerData.REMOTE_CONTROL_ID: + resolve(Consts.HubType.REMOTE_CONTROL); break; - case Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID: - resolve(Consts.HubType.BOOST_MOVE_HUB); + case Consts.BLEManufacturerData.MOVE_HUB_ID: + resolve(Consts.HubType.MOVE_HUB); break; - case Consts.BLEManufacturerData.POWERED_UP_HUB_ID: - resolve(Consts.HubType.POWERED_UP_HUB); + case Consts.BLEManufacturerData.HUB_ID: + resolve(Consts.HubType.HUB); break; - case Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID: - resolve(Consts.HubType.DUPLO_TRAIN_HUB); + case Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID: + resolve(Consts.HubType.DUPLO_TRAIN_BASE); break; - case Consts.BLEManufacturerData.CONTROL_PLUS_LARGE_HUB: - resolve(Consts.HubType.CONTROL_PLUS_HUB); + case Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB: + resolve(Consts.HubType.TECHNIC_MEDIUM_HUB); break; } }); @@ -170,7 +170,7 @@ export class PoweredUP extends EventEmitter { const device = new WebBLEDevice(server); - let hub: Hub; + let hub: BaseHub; let hubType = Consts.HubType.UNKNOWN; let isLPF2Hub = false; @@ -195,20 +195,20 @@ export class PoweredUP extends EventEmitter { case Consts.HubType.WEDO2_SMART_HUB: hub = new WeDo2SmartHub(device); break; - case Consts.HubType.BOOST_MOVE_HUB: - hub = new BoostMoveHub(device); + case Consts.HubType.MOVE_HUB: + hub = new MoveHub(device); break; - case Consts.HubType.POWERED_UP_HUB: - hub = new PUPHub(device); + case Consts.HubType.HUB: + hub = new Hub(device); break; - case Consts.HubType.POWERED_UP_REMOTE: - hub = new PUPRemote(device); + case Consts.HubType.REMOTE_CONTROL: + hub = new RemoteControl(device); break; - case Consts.HubType.DUPLO_TRAIN_HUB: + case Consts.HubType.DUPLO_TRAIN_BASE: hub = new DuploTrainBase(device); break; - case Consts.HubType.CONTROL_PLUS_HUB: - hub = new ControlPlusHub(device); + case Consts.HubType.TECHNIC_MEDIUM_HUB: + hub = new TechnicMediumHub(device); break; default: return; @@ -231,7 +231,7 @@ export class PoweredUP extends EventEmitter { /** * Emits when a Powered UP Hub device is found. * @event PoweredUP#discover - * @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub + * @param {WeDo2SmartHub | MoveHub | TechnicMediumHub | Hub | RemoteControl | DuploTrainBase} hub */ this.emit("discover", hub); diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index d5fd57c9..798a77d0 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -2,12 +2,12 @@ import { Peripheral } from "@abandonware/noble"; import { NobleDevice } from "./nobleabstraction"; -import { BoostMoveHub } from "./hubs/boostmovehub"; -import { ControlPlusHub } from "./hubs/controlplushub"; +import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; +import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; -import { PUPHub } from "./hubs/puphub"; -import { PUPRemote } from "./hubs/pupremote"; +import { RemoteControl } from "./hubs/remotecontrol"; +import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import * as Consts from "./consts"; @@ -45,7 +45,7 @@ noble.on("stateChange", (state: string) => { export class PoweredUP extends EventEmitter { - private _connectedHubs: {[uuid: string]: Hub} = {}; + private _connectedHubs: {[uuid: string]: BaseHub} = {}; constructor () { @@ -94,7 +94,7 @@ export class PoweredUP extends EventEmitter { /** * Retrieve a list of Powered UP Hubs. * @method PoweredUP#getHubs - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubs () { return Object.values(this._connectedHubs); @@ -105,7 +105,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by UUID. * @method PoweredUP#getHubByUUID * @param {string} uuid - * @returns {Hub | null} + * @returns {BaseHub | null} */ public getHubByUUID (uuid: string) { return this._connectedHubs[uuid]; @@ -116,7 +116,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a Powered UP Hub by primary MAC address. * @method PoweredUP#getHubByPrimaryMACAddress * @param {string} address - * @returns {Hub} + * @returns {BaseHub} */ public getHubByPrimaryMACAddress (address: string) { return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0]; @@ -127,7 +127,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a list of Powered UP Hub by name. * @method PoweredUP#getHubsByName * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByName (name: string) { return Object.values(this._connectedHubs).filter((hub) => hub.name === name); @@ -138,7 +138,7 @@ export class PoweredUP extends EventEmitter { * Retrieve a list of Powered UP Hub by type. * @method PoweredUP#getHubsByType * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ public getHubsByType (hubType: number) { return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType); @@ -150,20 +150,20 @@ export class PoweredUP extends EventEmitter { peripheral.removeAllListeners(); const device = new NobleDevice(peripheral); - let hub: Hub; + let hub: BaseHub; if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { hub = new WeDo2SmartHub(device); - } else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) { - hub = new BoostMoveHub(device); - } else if (await PUPHub.IsPUPHub(peripheral)) { - hub = new PUPHub(device); - } else if (await PUPRemote.IsPUPRemote(peripheral)) { - hub = new PUPRemote(device); + } else if (await MoveHub.IsMoveHub(peripheral)) { + hub = new MoveHub(device); + } else if (await Hub.IsHub(peripheral)) { + hub = new Hub(device); + } else if (await RemoteControl.IsRemoteControl(peripheral)) { + hub = new RemoteControl(device); } else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) { hub = new DuploTrainBase(device); - } else if (await ControlPlusHub.IsControlPlusHub(peripheral)) { - hub = new ControlPlusHub(device); + } else if (await TechnicMediumHub.IsTechnicMediumHub(peripheral)) { + hub = new TechnicMediumHub(device); } else { return; } @@ -189,7 +189,7 @@ export class PoweredUP extends EventEmitter { /** * Emits when a Powered UP Hub device is found. * @event PoweredUP#discover - * @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub + * @param {WeDo2SmartHub | MoveHub | TechnicMediumHub | Hub | RemoteControl | DuploTrainBase} hub */ this.emit("discover", hub); From 705fbd341a17c2dd22e834e5acff0f2caa24732e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 19 Dec 2019 10:46:38 -0800 Subject: [PATCH 33/62] writeDirect function on device --- src/devices/basicmotor.ts | 8 +------- src/devices/device.ts | 8 ++++++++ src/devices/hubled.ts | 25 ++----------------------- src/devices/light.ts | 8 +------- src/index-browser.ts | 2 +- src/index-node.ts | 2 +- src/poweredup-browser.ts | 2 +- src/poweredup-node.ts | 2 +- 8 files changed, 16 insertions(+), 41 deletions(-) diff --git a/src/devices/basicmotor.ts b/src/devices/basicmotor.ts index ad203d78..eb3be90c 100644 --- a/src/devices/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -22,13 +22,7 @@ export class BasicMotor extends Device { */ public setPower (power: number) { return new Promise((resolve) => { - if (this.isWeDo2SmartHub) { - const data = Buffer.from([this.portId, 0x01, 0x02, mapSpeed(power)]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - } else { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]); - this.send(data); - } + this.writeDirect(0x00, Buffer.from([mapSpeed(power)])); return resolve(); }); } diff --git a/src/devices/device.ts b/src/devices/device.ts index dc988150..cf2d617d 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -77,6 +77,14 @@ export class Device extends EventEmitter { return this._isWeDo2SmartHub; } + public writeDirect (mode: number, data: Buffer, callback?: () => void) { + if (this.isWeDo2SmartHub) { + this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + } else { + this.send(Buffer.concat([Buffer.from([0x81, this.portId, 0x11, 0x51, mode]), data]), Consts.BLECharacteristic.LPF2_ALL, callback); + } + } + public send (data: Buffer, characteristic: string = Consts.BLECharacteristic.LPF2_ALL, callback?: () => void) { this._ensureConnected(); this.hub.send(data, characteristic, callback); diff --git a/src/devices/hubled.ts b/src/devices/hubled.ts index b7c26054..2275cec2 100644 --- a/src/devices/hubled.ts +++ b/src/devices/hubled.ts @@ -26,7 +26,7 @@ export class HubLED extends Device { if (typeof color === "boolean") { color = 0; } - this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, color])); + this.writeDirect(0x00, Buffer.from([color])); return resolve(); }); } @@ -45,28 +45,7 @@ export class HubLED extends Device { if (this.mode !== HubLED.Mode.RGB) { this.subscribe(HubLED.Mode.RGB); } - this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x01, red, green, blue])); - return resolve(); - }); - } - - - - /** - * Set the light brightness. - * @method Light#brightness - * @param {number} brightness Brightness value between 0-100 (0 is off) - * @returns {Promise} Resolved upon successful completion of command. - */ - public setBrightness (brightness: number) { - return new Promise((resolve) => { - if (this.isWeDo2SmartHub) { - const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - } else { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); - this.send(data); - } + this.writeDirect(0x00, Buffer.from([red, green, blue])); return resolve(); }); } diff --git a/src/devices/light.ts b/src/devices/light.ts index 5021615d..8d2ae6b9 100644 --- a/src/devices/light.ts +++ b/src/devices/light.ts @@ -20,13 +20,7 @@ export class Light extends Device { */ public setBrightness (brightness: number) { return new Promise((resolve) => { - if (this.isWeDo2SmartHub) { - const data = Buffer.from([this.portId, 0x01, 0x02, brightness]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - } else { - const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]); - this.send(data); - } + this.writeDirect(0x00, Buffer.from([brightness])); return resolve(); }); } diff --git a/src/index-browser.ts b/src/index-browser.ts index d38f8a37..ae5f3770 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -4,8 +4,8 @@ import { PoweredUP } from "./poweredup-browser"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; diff --git a/src/index-node.ts b/src/index-node.ts index ce294e96..ca403d11 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -4,8 +4,8 @@ import { PoweredUP } from "./poweredup-node"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; diff --git a/src/poweredup-browser.ts b/src/poweredup-browser.ts index 339a1b5e..f9860349 100644 --- a/src/poweredup-browser.ts +++ b/src/poweredup-browser.ts @@ -2,8 +2,8 @@ import { WebBLEDevice } from "./webbleabstraction"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; diff --git a/src/poweredup-node.ts b/src/poweredup-node.ts index 798a77d0..2e95d113 100644 --- a/src/poweredup-node.ts +++ b/src/poweredup-node.ts @@ -4,8 +4,8 @@ import { NobleDevice } from "./nobleabstraction"; import { BaseHub } from "./hubs/basehub"; import { DuploTrainBase } from "./hubs/duplotrainbase"; -import { MoveHub } from "./hubs/movehub"; import { Hub } from "./hubs/hub"; +import { MoveHub } from "./hubs/movehub"; import { RemoteControl } from "./hubs/remotecontrol"; import { TechnicMediumHub } from "./hubs/technicmediumhub"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; From bc644c85b2b974fb9c50e968c50b84de7f5c465e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 19 Dec 2019 12:07:05 -0800 Subject: [PATCH 34/62] Duplo motors and sensors --- src/consts.ts | 10 +++--- src/devices/duplotrainbasecolorsensor.ts | 45 ++++++++++++++++++++++++ src/devices/duplotrainbasemotor.ts | 13 +++++++ src/devices/duplotrainbasespeaker.ts | 38 ++++++++++++++++++++ src/devices/duplotrainbasespeedometer.ts | 43 ++++++++++++++++++++++ src/devices/hubled.ts | 8 ++--- src/devices/remotecontrolbutton.ts | 2 +- src/hubs/basehub.ts | 20 ++++++++++- src/hubs/duplotrainbase.ts | 16 --------- src/index-browser.ts | 8 +++++ src/index-node.ts | 8 +++++ 11 files changed, 182 insertions(+), 29 deletions(-) create mode 100644 src/devices/duplotrainbasecolorsensor.ts create mode 100644 src/devices/duplotrainbasemotor.ts create mode 100644 src/devices/duplotrainbasespeaker.ts create mode 100644 src/devices/duplotrainbasespeedometer.ts diff --git a/src/consts.ts b/src/consts.ts index a08450bb..f9cabba5 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -67,17 +67,17 @@ export enum DeviceType { MOVE_HUB_TILT_SENSOR = 40, DUPLO_TRAIN_BASE_MOTOR = 41, DUPLO_TRAIN_BASE_SPEAKER = 42, - DUPLO_TRAIN_BASE_COLOR = 43, + DUPLO_TRAIN_BASE_COLOR_SENSOR = 43, DUPLO_TRAIN_BASE_SPEEDOMETER = 44, TECHNIC_LARGE_LINEAR_MOTOR = 46, TECHNIC_XLARGE_LINEAR_MOTOR = 47, - CONTROL_PLUS_GEST = 54, - PUP_REMOTE_BUTTON = 55, - RSSI = 56, + TECHNIC_MEDIUM_HUB_GEST_SENSOR = 54, + REMOTE_CONTROL_BUTTON = 55, + REMOTE_CONTROL_RSSI = 56, TECHNIC_MEDIUM_HUB_ACCELEROMETER = 57, TECHNIC_MEDIUM_HUB_GYRO_SENSOR = 58, TECHNIC_MEDIUM_HUB_TILT_SENSOR = 59, - TEMPERATURE = 60, + TECHNIC_MEDIUM_HUB_TEMPERATURE_SENSOR = 60, } diff --git a/src/devices/duplotrainbasecolorsensor.ts b/src/devices/duplotrainbasecolorsensor.ts new file mode 100644 index 00000000..6175a395 --- /dev/null +++ b/src/devices/duplotrainbasecolorsensor.ts @@ -0,0 +1,45 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class DuploTrainBaseColorSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, DuploTrainBaseColorSensor.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case DuploTrainBaseColorSensor.Mode.COLOR: + if (message[4] <= 10) { + const color = message[4]; + + /** + * Emits when a color sensor is activated. + * @event DuploTrainBaseColorSensor#color + * @param {Color} color + */ + this.emit("color", color); + } + break; + + } + } + +} + +export namespace DuploTrainBaseColorSensor { + + export enum Mode { + COLOR = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "color": DuploTrainBaseColorSensor.Mode.COLOR + } + +} \ No newline at end of file diff --git a/src/devices/duplotrainbasemotor.ts b/src/devices/duplotrainbasemotor.ts new file mode 100644 index 00000000..2466832e --- /dev/null +++ b/src/devices/duplotrainbasemotor.ts @@ -0,0 +1,13 @@ +import { BasicMotor } from "./basicmotor"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class DuploTrainBaseMotor extends BasicMotor { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR); + } + +} diff --git a/src/devices/duplotrainbasespeaker.ts b/src/devices/duplotrainbasespeaker.ts new file mode 100644 index 00000000..1bd6be28 --- /dev/null +++ b/src/devices/duplotrainbasespeaker.ts @@ -0,0 +1,38 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class DuploTrainBaseSpeaker extends Device { + + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER); + } + + + /** + * Play a built-in train sound. + * @method DuploTrainBaseSpeaker#playSound + * @param {DuploTrainBaseSound} sound + * @returns {Promise} Resolved upon successful issuance of command. + */ + public playSound (sound: Consts.DuploTrainBaseSound) { + return new Promise((resolve, reject) => { + this.subscribe(DuploTrainBaseSpeaker.Mode.SOUND); + this.writeDirect(0x01, Buffer.from([sound])); + return resolve(); + }); + } + + +} + +export namespace DuploTrainBaseSpeaker { + + export enum Mode { + SOUND = 0x01 + } + +} \ No newline at end of file diff --git a/src/devices/duplotrainbasespeedometer.ts b/src/devices/duplotrainbasespeedometer.ts new file mode 100644 index 00000000..5241f419 --- /dev/null +++ b/src/devices/duplotrainbasespeedometer.ts @@ -0,0 +1,43 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class DuploTrainBaseSpeedometer extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, DuploTrainBaseSpeedometer.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case DuploTrainBaseSpeedometer.Mode.SPEED: + const speed = message.readInt16LE(4); + + /** + * Emits on a speed change. + * @event DuploTrainBaseSpeedometer#speed + * @param {number} speed + */ + this.emit("speed", speed); + break; + + } + } + +} + +export namespace DuploTrainBaseSpeedometer { + + export enum Mode { + SPEED = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "speed": DuploTrainBaseSpeedometer.Mode.SPEED + } + +} \ No newline at end of file diff --git a/src/devices/hubled.ts b/src/devices/hubled.ts index 2275cec2..ed33903c 100644 --- a/src/devices/hubled.ts +++ b/src/devices/hubled.ts @@ -20,12 +20,10 @@ export class HubLED extends Device { */ public setColor (color: number | boolean) { return new Promise((resolve, reject) => { - if (this.mode !== HubLED.Mode.COLOR) { - this.subscribe(HubLED.Mode.COLOR); - } if (typeof color === "boolean") { color = 0; } + this.subscribe(HubLED.Mode.COLOR); this.writeDirect(0x00, Buffer.from([color])); return resolve(); }); @@ -42,9 +40,7 @@ export class HubLED extends Device { */ public setRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { - if (this.mode !== HubLED.Mode.RGB) { - this.subscribe(HubLED.Mode.RGB); - } + this.subscribe(HubLED.Mode.RGB); this.writeDirect(0x00, Buffer.from([red, green, blue])); return resolve(); }); diff --git a/src/devices/remotecontrolbutton.ts b/src/devices/remotecontrolbutton.ts index c2377c89..7c8dce51 100644 --- a/src/devices/remotecontrolbutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -7,7 +7,7 @@ import * as Consts from "../consts"; export class RemoteControlButton extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, RemoteControlButton.ModeMap, Consts.DeviceType.PUP_REMOTE_BUTTON); + super(hub, portId, RemoteControlButton.ModeMap, Consts.DeviceType.REMOTE_CONTROL_BUTTON); } public receive (message: Buffer) { diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index 7f029e34..8c8d08dc 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -5,6 +5,12 @@ import { IBLEAbstraction } from "../interfaces"; import { ColorDistanceSensor } from "../devices/colordistancesensor"; import { CurrentSensor } from "../devices/currentsensor"; import { Device } from "../devices/device"; + +import { DuploTrainBaseColorSensor } from "../devices/duplotrainbasecolorsensor"; +import { DuploTrainBaseMotor } from "../devices/duplotrainbasemotor"; +import { DuploTrainBaseSpeaker } from "../devices/duplotrainbasespeaker"; +import { DuploTrainBaseSpeedometer } from "../devices/duplotrainbasespeedometer"; + import { HubLED } from "../devices/hubled"; import { Light } from "../devices/light"; import { MediumLinearMotor } from "../devices/mediumlinearmotor"; @@ -352,12 +358,24 @@ export class BaseHub extends EventEmitter { case Consts.DeviceType.CURRENT_SENSOR: device = new CurrentSensor(this, portId); break; - case Consts.DeviceType.PUP_REMOTE_BUTTON: + case Consts.DeviceType.REMOTE_CONTROL_BUTTON: device = new RemoteControlButton(this, portId); break; case Consts.DeviceType.HUB_LED: device = new HubLED(this, portId); break; + case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR: + device = new DuploTrainBaseColorSensor(this, portId); + break; + case Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR: + device = new DuploTrainBaseMotor(this, portId); + break; + case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER: + device = new DuploTrainBaseSpeaker(this, portId); + break; + case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER: + device = new DuploTrainBaseSpeedometer(this, portId); + break; default: device = new Device(this, portId, undefined, deviceType); break; diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index 727dd421..d9701a52 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -40,28 +40,12 @@ export class DuploTrainBase extends LPF2Hub { return new Promise(async (resolve, reject) => { debug("Connecting to Duplo Train Base"); await super.connect(); - // this.subscribe(0x01, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER, 0x01); debug("Connect completed"); return resolve(); }); } - /** - * Play a built-in train sound. - * @method DuploTrainBase#playSound - * @param {DuploTrainBaseSound} sound - * @returns {Promise} Resolved upon successful issuance of command. - */ - public playSound (sound: number) { - return new Promise((resolve, reject) => { - const data = Buffer.from([0x81, 0x01, 0x11, 0x51, 0x01, sound]); - this.send(data, Consts.BLECharacteristic.LPF2_ALL); - return resolve(); - }); - } - - } export namespace DuploTrainBase { diff --git a/src/index-browser.ts b/src/index-browser.ts index ae5f3770..622f4e0a 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -13,6 +13,10 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; +import { DuploTrainBaseColorSensor } from "./devices/duplotrainbasecolorsensor"; +import { DuploTrainBaseMotor } from "./devices/duplotrainbasemotor"; +import { DuploTrainBaseSpeaker } from "./devices/duplotrainbasespeaker"; +import { DuploTrainBaseSpeedometer } from "./devices/duplotrainbasespeedometer"; import { HubLED } from "./devices/hubled"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; @@ -44,6 +48,10 @@ window.PoweredUP = { Consts, ColorDistanceSensor, Device, + DuploTrainBaseColorSensor, + DuploTrainBaseMotor, + DuploTrainBaseSpeaker, + DuploTrainBaseSpeedometer, HubLED, Light, MediumLinearMotor, diff --git a/src/index-node.ts b/src/index-node.ts index ca403d11..54d5a1b6 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -13,6 +13,10 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; +import { DuploTrainBaseColorSensor } from "./devices/duplotrainbasecolorsensor"; +import { DuploTrainBaseMotor } from "./devices/duplotrainbasemotor"; +import { DuploTrainBaseSpeaker } from "./devices/duplotrainbasespeaker"; +import { DuploTrainBaseSpeedometer } from "./devices/duplotrainbasespeedometer"; import { HubLED } from "./devices/hubled"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; @@ -44,6 +48,10 @@ export { Consts, ColorDistanceSensor, Device, + DuploTrainBaseColorSensor, + DuploTrainBaseMotor, + DuploTrainBaseSpeaker, + DuploTrainBaseSpeedometer, HubLED, Light, MediumLinearMotor, From 035017617e8afe2150022108490cfd1edcd7baa4 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 19 Dec 2019 15:44:35 -0800 Subject: [PATCH 35/62] Bug fixes, hubs now wait for hub properties before being connected --- examples/multiple_motors.js | 37 +++++++++++++++++ src/devices/hubled.ts | 2 +- src/hubs/basehub.ts | 28 +++++++++---- src/hubs/lpf2hub.ts | 82 +++++++++++++++---------------------- 4 files changed, 93 insertions(+), 56 deletions(-) create mode 100644 examples/multiple_motors.js diff --git a/examples/multiple_motors.js b/examples/multiple_motors.js new file mode 100644 index 00000000..a466273f --- /dev/null +++ b/examples/multiple_motors.js @@ -0,0 +1,37 @@ +/* + * + * This demonstrates connecting multiple hubs to your laptop. Once connected, all the hubs LED lights will cycle through the same colors simultaneously. + * + */ + +const PoweredUP = require(".."); + +const poweredUP = new PoweredUP.PoweredUP(); +poweredUP.scan(); // Start scanning for hubs + +console.log("Looking for Hubs..."); + +poweredUP.on("discover", async (hub) => { // Wait to discover hubs + + await hub.connect(); // Connect to hub + console.log(`Connected to ${hub.name}!`); + + const motorA = await hub.waitForDeviceAtPort("A"); + console.log("Got motor A"); + const motorB = await hub.waitForDeviceAtPort("B"); + console.log("Got motor B"); + const motorC = await hub.waitForDeviceAtPort("C"); + console.log("Got motor C"); + const motorD = await hub.waitForDeviceAtPort("D"); + console.log("Got motor D"); + + motorA.setPower(30); + motorB.setPower(30); + motorC.setPower(30); + motorD.setPower(30); + + hub.on("disconnect", () => { + console.log("Hub disconnected"); + }); + +}); \ No newline at end of file diff --git a/src/devices/hubled.ts b/src/devices/hubled.ts index ed33903c..6aee4e35 100644 --- a/src/devices/hubled.ts +++ b/src/devices/hubled.ts @@ -41,7 +41,7 @@ export class HubLED extends Device { public setRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { this.subscribe(HubLED.Mode.RGB); - this.writeDirect(0x00, Buffer.from([red, green, blue])); + this.writeDirect(0x01, Buffer.from([red, green, blue])); return resolve(); }); } diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index 8c8d08dc..dfa78959 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -54,10 +54,11 @@ export class BaseHub extends EventEmitter { private _type: Consts.HubType; private _portMap: {[portName: string]: number} = {}; - private _attachCallbacks: ((device: Device) => void)[] = []; + private _attachCallbacks: ((device: Device) => boolean)[] = []; constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { super(); + this.setMaxListeners(20); // Technic Medium Hub has 9 built in devices + 4 external ports. Node.js throws a warning after 11 attached event listeners. this._type = type; this._bleDevice = device; this._portMap = portMap; @@ -183,7 +184,7 @@ export class BaseHub extends EventEmitter { public getDeviceAtPort (portName: string) { const portId = this._portMap[portName]; - if (portId) { + if (portId !== undefined) { return this._attachedDevices[portId]; } else { throw new Error(`Port ${portName} does not exist on this hub type`); @@ -199,7 +200,10 @@ export class BaseHub extends EventEmitter { } this._attachCallbacks.push((device) => { if (device.portName === portName) { - return resolve(device); + resolve(device); + return true; + } else { + return false; } }); }); @@ -224,7 +228,10 @@ export class BaseHub extends EventEmitter { } this._attachCallbacks.push((device) => { if (device.type === deviceType) { - return resolve(device); + resolve(device); + return true; + } else { + return false; } }) }); @@ -289,9 +296,15 @@ export class BaseHub extends EventEmitter { * @param {Device} device */ this.emit("attach", device); - this._attachCallbacks.forEach((callback) => { - callback(device); - }); + debug(`Attached device type ${device.type} (${Consts.DeviceTypeNames[device.type]}) on port ${device.portName} (${device.portId})`); + + let i = this._attachCallbacks.length; + while (i--) { + const callback = this._attachCallbacks[i]; + if (callback(device)) { + this._attachCallbacks.splice(i, 1); + } + } } @@ -303,6 +316,7 @@ export class BaseHub extends EventEmitter { * @param {Device} device */ this.emit("detach", device); + debug(`Detached device type ${device.type} (${Consts.DeviceTypeNames[device.type]}) on port ${device.portName} (${device.portId})`); } diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 0ece6943..703f22e0 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -17,20 +17,24 @@ export class LPF2Hub extends BaseHub { private _messageBuffer: Buffer = Buffer.alloc(0); + private _propertyRequestCallbacks: {[property: number]: ((data: Buffer) => void)} = {}; + public connect () { return new Promise(async (resolve, reject) => { + debug("LPF2Hub connecting"); await super.connect(); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); - await this.sleep(100); - this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports - this.send(Buffer.from([0x01, 0x03, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request firmware version - this.send(Buffer.from([0x01, 0x04, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request hardware version - this.send(Buffer.from([0x01, 0x05, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate RSSI updates - this.send(Buffer.from([0x01, 0x06, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate battery level reports - this.send(Buffer.from([0x01, 0x0d, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request primary MAC address + await this.sleep(500); + this._requestHubPropertyReports(0x02); // Activate button reports + await this._requestHubPropertyValue(0x03); // Request firmware version + await this._requestHubPropertyValue(0x04); // Request hardware version + this._requestHubPropertyReports(0x05); // Activate RSSI updates + this._requestHubPropertyReports(0x06); // Activate battery level reports + await this._requestHubPropertyValue(0x0d); // Request primary MAC address this.emit("connect"); + debug("LPF2Hub connected"); resolve(); }); } @@ -131,7 +135,12 @@ export class LPF2Hub extends BaseHub { switch (message[2]) { case 0x01: { - this._parseDeviceInfo(message); + const property = message[3]; + const callback = this._propertyRequestCallbacks[property]; + if (callback) { + callback(message); + } + delete this._propertyRequestCallbacks[property]; break; } case 0x04: { @@ -164,7 +173,23 @@ export class LPF2Hub extends BaseHub { } - private _parseDeviceInfo (message: Buffer) { + private _requestHubPropertyValue (property: number) { + return new Promise((resolve) => { + this._propertyRequestCallbacks[property] = (message) => { + this._parseHubPropertyResponse(message); + return resolve(); + }; + this.send(Buffer.from([0x01, property, 0x05]), Consts.BLECharacteristic.LPF2_ALL); + }); + } + + + private _requestHubPropertyReports (property: number) { + this.send(Buffer.from([0x01, property, 0x02]), Consts.BLECharacteristic.LPF2_ALL); + } + + + private _parseHubPropertyResponse (message: Buffer) { // Button press reports if (message[3] === 0x02) { @@ -356,45 +381,6 @@ export class LPF2Hub extends BaseHub { device.receive(message); } - // if ((data[3] === 0x3d && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Control+ CPU Temperature - // /** - // * Emits when a change is detected on a temperature sensor. Measured in degrees centigrade. - // * @event LPF2Hub#temp - // * @param {string} port For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature. - // * @param {number} temp - // */ - // this.emit("temp", "CPU", ((data.readInt16LE(4) / 900) * 90).toFixed(2)); - // return; - // } - - // const port = this._getPortForPortNumber(data[3]); - - // if (!port) { - // return; - // } - - // if (port && port.connected) { - // switch (port.type) { - // case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: { - // if (data[4] <= 10) { - // this.emit("color", port.id, data[4]); - // } - // break; - // } - // case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER: { - // /** - // * Emits on a speed change. - // * @event LPF2Hub#speed - // * @param {string} port - // * @param {number} speed - // */ - // const speed = data.readInt16LE(4); - // this.emit("speed", port.id, speed); - // break; - // } - // } - // } - } From 2dabeddbabedfb1fa34f048f1a2dde8056309a01 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Fri, 20 Dec 2019 12:35:09 -0800 Subject: [PATCH 36/62] More WeDo 2.0 devices --- src/consts.ts | 2 +- src/devices/currentsensor.ts | 35 +++++---- src/devices/hubled.ts | 18 +++-- src/devices/piezobuzzer.ts | 33 +++++++++ src/devices/voltagesensor.ts | 33 +++++---- src/hubs/basehub.ts | 4 ++ src/hubs/lpf2hub.ts | 6 +- src/hubs/wedo2smarthub.ts | 134 ++--------------------------------- src/index-browser.ts | 2 + src/index-node.ts | 2 + 10 files changed, 106 insertions(+), 163 deletions(-) create mode 100644 src/devices/piezobuzzer.ts diff --git a/src/consts.ts b/src/consts.ts index f9cabba5..0c66aba9 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -57,7 +57,7 @@ export enum DeviceType { LIGHT = 8, VOLTAGE_SENSOR = 20, CURRENT_SENSOR = 21, - PIEZO_TONE = 22, + PIEZO_BUZZER = 22, HUB_LED = 23, TILT_SENSOR = 34, MOTION_SENSOR = 35, diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index efa11c87..e68dbea1 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -11,25 +11,30 @@ export class CurrentSensor extends Device { } public receive (message: Buffer) { - const mode = this._mode; + const mode = this.mode; switch (mode) { case CurrentSensor.Mode.CURRENT: - let maxCurrentValue = CurrentSensor.MaxCurrentValue[this.hub.type]; - if (maxCurrentValue === undefined) { - maxCurrentValue = CurrentSensor.MaxCurrentValue[Consts.HubType.UNKNOWN]; + if (this.isWeDo2SmartHub) { + const current = message.readInt16LE(2) / 1000; + this.emit("current", current); + } else { + let maxCurrentValue = CurrentSensor.MaxCurrentValue[this.hub.type]; + if (maxCurrentValue === undefined) { + maxCurrentValue = CurrentSensor.MaxCurrentValue[Consts.HubType.UNKNOWN]; + } + let maxCurrentRaw = CurrentSensor.MaxCurrentRaw[this.hub.type]; + if (maxCurrentRaw === undefined) { + maxCurrentRaw = CurrentSensor.MaxCurrentRaw[Consts.HubType.UNKNOWN]; + } + const current = message.readUInt16LE(4) * maxCurrentValue / maxCurrentRaw; + /** + * Emits when a current change is detected. + * @event CurrentSensor#current + * @param {number} current + */ + this.emit("current", current); } - let maxCurrentRaw = CurrentSensor.MaxCurrentRaw[this.hub.type]; - if (maxCurrentRaw === undefined) { - maxCurrentRaw = CurrentSensor.MaxCurrentRaw[Consts.HubType.UNKNOWN]; - } - const current = message.readUInt16LE(4) * maxCurrentValue / maxCurrentRaw; - /** - * Emits when a current change is detected. - * @event CurrentSensor#current - * @param {number} current - */ - this.emit("current", current); break; } } diff --git a/src/devices/hubled.ts b/src/devices/hubled.ts index 6aee4e35..807ba1fc 100644 --- a/src/devices/hubled.ts +++ b/src/devices/hubled.ts @@ -23,8 +23,13 @@ export class HubLED extends Device { if (typeof color === "boolean") { color = 0; } - this.subscribe(HubLED.Mode.COLOR); - this.writeDirect(0x00, Buffer.from([color])); + if (this.isWeDo2SmartHub) { + this.send(Buffer.from([0x06, 0x17, 0x01, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); + this.send(Buffer.from([0x06, 0x04, 0x01, color]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + } else { + this.subscribe(HubLED.Mode.COLOR); + this.writeDirect(0x00, Buffer.from([color])); + } return resolve(); }); } @@ -40,8 +45,13 @@ export class HubLED extends Device { */ public setRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { - this.subscribe(HubLED.Mode.RGB); - this.writeDirect(0x01, Buffer.from([red, green, blue])); + if (this.isWeDo2SmartHub) { + this.send(Buffer.from([0x06, 0x17, 0x01, 0x02]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); + this.send(Buffer.from([0x06, 0x04, 0x03, red, green, blue]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + } else { + this.subscribe(HubLED.Mode.RGB); + this.writeDirect(0x01, Buffer.from([red, green, blue])); + } return resolve(); }); } diff --git a/src/devices/piezobuzzer.ts b/src/devices/piezobuzzer.ts new file mode 100644 index 00000000..4c7eb02e --- /dev/null +++ b/src/devices/piezobuzzer.ts @@ -0,0 +1,33 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class PiezoBuzzer extends Device { + + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, {}, Consts.DeviceType.PIEZO_BUZZER); + } + + + /** + * Play a tone on the Hub's in-built buzzer + * @method PiezoBuzzer#playTone + * @param {number} frequency + * @param {number} time How long the tone should play for (in milliseconds). + * @returns {Promise} Resolved upon successful completion of command (ie. once the tone has finished playing). + */ + public playTone (frequency: number, time: number) { + return new Promise((resolve) => { + const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]); + data.writeUInt16LE(frequency, 3); + data.writeUInt16LE(time, 5); + this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); + global.setTimeout(resolve, time); + }); + } + + +} diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index aadf7c5e..45ecc916 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -15,21 +15,26 @@ export class VoltageSensor extends Device { switch (mode) { case VoltageSensor.Mode.VOLTAGE: - let maxVoltageValue = VoltageSensor.MaxVoltageValue[this.hub.type]; - if (maxVoltageValue === undefined) { - maxVoltageValue = VoltageSensor.MaxVoltageValue[Consts.HubType.UNKNOWN]; + if (this.isWeDo2SmartHub) { + const voltage = message.readInt16LE(2) / 40; + this.emit("voltage", voltage); + } else { + let maxVoltageValue = VoltageSensor.MaxVoltageValue[this.hub.type]; + if (maxVoltageValue === undefined) { + maxVoltageValue = VoltageSensor.MaxVoltageValue[Consts.HubType.UNKNOWN]; + } + let maxVoltageRaw = VoltageSensor.MaxVoltageRaw[this.hub.type]; + if (maxVoltageRaw === undefined) { + maxVoltageRaw = VoltageSensor.MaxVoltageRaw[Consts.HubType.UNKNOWN]; + } + const voltage = message.readUInt16LE(4) * maxVoltageValue / maxVoltageRaw; + /** + * Emits when a voltage change is detected. + * @event VoltageSensor#voltage + * @param {number} voltage + */ + this.emit("voltage", voltage); } - let maxVoltageRaw = VoltageSensor.MaxVoltageRaw[this.hub.type]; - if (maxVoltageRaw === undefined) { - maxVoltageRaw = VoltageSensor.MaxVoltageRaw[Consts.HubType.UNKNOWN]; - } - const voltage = message.readUInt16LE(4) * maxVoltageValue / maxVoltageRaw; - /** - * Emits when a voltage change is detected. - * @event VoltageSensor#voltage - * @param {number} voltage - */ - this.emit("voltage", voltage); break; } } diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index dfa78959..fe2f4d30 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -19,6 +19,7 @@ import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor"; import { MoveHubTiltSensor } from "../devices/movehubtiltsensor"; import { RemoteControlButton } from "../devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; +import { PiezoBuzzer } from "../devices/piezobuzzer"; import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; import { TechnicMediumHubAccelerometerSensor } from "../devices/technicmediumhubaccelerometersensor"; import { TechnicMediumHubGyroSensor } from "../devices/technicmediumhubgyrosensor"; @@ -336,6 +337,9 @@ export class BaseHub extends EventEmitter { case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: device = new MoveHubMediumLinearMotor(this, portId); break; + case Consts.DeviceType.PIEZO_BUZZER: + device = new PiezoBuzzer(this, portId); + break; case Consts.DeviceType.MOTION_SENSOR: device = new MotionSensor(this, portId); break; diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 703f22e0..9d27ae74 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -230,7 +230,11 @@ export class LPF2Hub extends BaseHub { // Battery level reports } else if (message[3] === 0x06) { - this._batteryLevel = message[5]; + const batteryLevel = message[5]; + if (batteryLevel !== this._batteryLevel) { + this._batteryLevel = batteryLevel; + this.emit("batteryLevel", batteryLevel); + } } } diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index c6791da6..74f5868f 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -9,6 +9,7 @@ import * as Consts from "../consts"; import { isWebBluetooth } from "../utils"; import Debug = require("debug"); +import { HubLED } from "../devices/hubled"; const debug = Debug("wedo2smarthub"); @@ -53,8 +54,6 @@ export class WeDo2SmartHub extends BaseHub { await this._bleDevice.discoverCharacteristicsForService("battery_service"); await this._bleDevice.discoverCharacteristicsForService("device_information"); } - this.subscribe(0x03, 0x15, 0x00); // Activate voltage reports - this.subscribe(0x04, 0x14, 0x00); // Activate current reports debug("Connect completed"); this.emit("connect"); resolve(); @@ -129,85 +128,6 @@ export class WeDo2SmartHub extends BaseHub { } - /** - * Set the color of the LED on the Hub via a color value. - * @method WeDo2SmartHub#setLEDColor - * @param {Color} color - * @returns {Promise} Resolved upon successful issuance of command. - */ - public setLEDColor (color: number | boolean) { - return new Promise((resolve, reject) => { - let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - this.send(data, Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); - if (typeof color === "boolean") { - color = 0; - } - data = Buffer.from([0x06, 0x04, 0x01, color]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - return resolve(); - }); - } - - - /** - * Set the color of the LED on the Hub via RGB values. - * @method WeDo2SmartHub#setLEDRGB - * @param {number} red - * @param {number} green - * @param {number} blue - * @returns {Promise} Resolved upon successful issuance of command. - */ - public setLEDRGB (red: number, green: number, blue: number) { - return new Promise((resolve, reject) => { - let data = Buffer.from([0x06, 0x17, 0x01, 0x02]); - this.send(data, Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); - data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - return resolve(); - }); - } - - - // /** - // * Ramp the motor speed on a given port. - // * @method WeDo2SmartHub#rampMotorSpeed - // * @param {string} port - // * @param {number} fromSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - // * @param {number} toSpeed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - // * @param {number} time How long the ramp should last (in milliseconds). - // * @returns {Promise} Resolved upon successful completion of command. - // */ - // public rampMotorSpeed (port: string, fromSpeed: number, toSpeed: number, time: number) { - // const portObj = this._portLookup(port); - // portObj.cancelEventTimer(); - // return new Promise((resolve, reject) => { - // this._calculateRamp(fromSpeed, toSpeed, time, portObj) - // .on("changeSpeed", (speed) => { - // this.setMotorSpeed(port, speed, true); - // }) - // .on("finished", resolve); - // }); - // } - - - /** - * Play a tone on the Hub's in-built buzzer - * @method WeDo2SmartHub#playTone - * @param {number} frequency - * @param {number} time How long the tone should play for (in milliseconds). - * @returns {Promise} Resolved upon successful completion of command (ie. once the tone has finished playing). - */ - public playTone (frequency: number, time: number) { - return new Promise((resolve, reject) => { - const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]); - data.writeUInt16LE(frequency, 3); - data.writeUInt16LE(time, 5); - this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); - global.setTimeout(resolve, time); - }); - } - - public send (message: Buffer, uuid: string, callback?: () => void) { if (debug.enabled) { debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message); @@ -245,7 +165,11 @@ export class WeDo2SmartHub extends BaseHub { private _parseBatteryMessage (data: Buffer) { debug("Received Message (WEDO2_BATTERY)", data); - this._batteryLevel = data[0]; + const batteryLevel = data[0]; + if (batteryLevel !== this._batteryLevel) { + this._batteryLevel = batteryLevel; + this.emit("batteryLevel", batteryLevel); + } } @@ -299,52 +223,6 @@ export class WeDo2SmartHub extends BaseHub { device.receive(message); } - // if (data[0] === 0x01) { - // /** - // * Emits when a button is pressed. - // * @event WeDo2SmartHub#button - // * @param {string} button - // * @param {ButtonState} state - // */ - // this.emit("button", "GREEN", Consts.ButtonState.PRESSED); - // return; - // } else if (data[0] === 0x00) { - // this.emit("button", "GREEN", Consts.ButtonState.RELEASED); - // return; - // } - - // // Voltage - // if (data[1] === 0x03) { - // const voltage = data.readInt16LE(2); - // this._voltage = voltage / 40; - // // Current - // } else if (data[1] === 0x04) { - // const current = data.readInt16LE(2); - // this._current = current / 1000; - // } - - // const port = this._getPortForPortNumber(data[1]); - - // if (!port) { - // return; - // } - - // if (port && port.connected) { - // switch (port.type) { - // case Consts.DeviceType.COLOR_DISTANCE_SENSOR: { - // const distance = data[2]; - // /** - // * Emits when a color sensor is activated. - // * @event WeDo2SmartHub#color - // * @param {string} port - // * @param {Color} color - // */ - // this.emit("color", port.id, distance); - // break; - // } - // } - // } - } diff --git a/src/index-browser.ts b/src/index-browser.ts index 622f4e0a..284b682d 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -23,6 +23,7 @@ import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; +import { PiezoBuzzer } from "./devices/piezobuzzer"; import { RemoteControlButton } from "./devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; @@ -59,6 +60,7 @@ window.PoweredUP = { MoveHub, MoveHubMediumLinearMotor, MoveHubTiltSensor, + PiezoBuzzer, RemoteControlButton, SimpleMediumLinearMotor, TechnicMediumHubAccelerometerSensor, diff --git a/src/index-node.ts b/src/index-node.ts index 54d5a1b6..edf4f854 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -23,6 +23,7 @@ import { MediumLinearMotor } from "./devices/mediumlinearmotor"; import { MotionSensor } from "./devices/motionsensor"; import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor"; import { MoveHubTiltSensor } from "./devices/movehubtiltsensor"; +import { PiezoBuzzer } from "./devices/piezobuzzer"; import { RemoteControlButton } from "./devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; @@ -59,6 +60,7 @@ export { MoveHub, MoveHubMediumLinearMotor, MoveHubTiltSensor, + PiezoBuzzer, RemoteControlButton, SimpleMediumLinearMotor, TechnicMediumHubAccelerometerSensor, From 579f82334ce6cacee7823ee223a319a87cfbbc5f Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sun, 22 Dec 2019 18:39:19 -0800 Subject: [PATCH 37/62] Device events now emit on hub too --- package-lock.json | 149 +++++------------- src/devices/colordistancesensor.ts | 6 +- src/devices/currentsensor.ts | 4 +- src/devices/device.ts | 40 +++-- src/devices/duplotrainbasecolorsensor.ts | 2 +- src/devices/duplotrainbasespeedometer.ts | 2 +- src/devices/motionsensor.ts | 2 +- src/devices/movehubtiltsensor.ts | 2 +- src/devices/remotecontrolbutton.ts | 2 +- src/devices/tachomotor.ts | 2 +- .../technicmediumhubaccelerometersensor.ts | 26 +-- src/devices/technicmediumhubgyrosensor.ts | 2 +- src/devices/technicmediumhubtiltsensor.ts | 2 +- src/devices/tiltsensor.ts | 2 +- src/devices/voltagesensor.ts | 4 +- src/hubs/basehub.ts | 110 ++++--------- 16 files changed, 133 insertions(+), 224 deletions(-) diff --git a/package-lock.json b/package-lock.json index a7daa88b..f25098ad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -283,7 +283,6 @@ "version": "6.10.2", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", "integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==", - "dev": true, "requires": { "fast-deep-equal": "^2.0.1", "fast-json-stable-stringify": "^2.0.0", @@ -294,14 +293,12 @@ "ajv-errors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/ajv-errors/-/ajv-errors-1.0.1.tgz", - "integrity": "sha512-DCRfO/4nQ+89p/RK43i8Ezd41EqdGIU4ld7nGF8OQ14oc/we5rEntLCUa7+jrn3nn83BosfwZA0wb4pon2o8iQ==", - "dev": true + "integrity": "sha512-DCRfO/4nQ+89p/RK43i8Ezd41EqdGIU4ld7nGF8OQ14oc/we5rEntLCUa7+jrn3nn83BosfwZA0wb4pon2o8iQ==" }, "ajv-keywords": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.4.1.tgz", - "integrity": "sha512-RO1ibKvd27e6FEShVFfPALuHI3WjSVNeK5FIsmme/LYRNxjKuNj+Dt7bucLa6NdSv3JcVTyMlm9kGR84z1XpaQ==", - "dev": true + "integrity": "sha512-RO1ibKvd27e6FEShVFfPALuHI3WjSVNeK5FIsmme/LYRNxjKuNj+Dt7bucLa6NdSv3JcVTyMlm9kGR84z1XpaQ==" }, "ansi-escape-sequences": { "version": "4.1.0", @@ -805,8 +802,7 @@ "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", - "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", - "dev": true + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" }, "buffer-xor": { "version": "1.0.3", @@ -830,7 +826,6 @@ "version": "12.0.3", "resolved": "https://registry.npmjs.org/cacache/-/cacache-12.0.3.tgz", "integrity": "sha512-kqdmfXEGFepesTuROHMs3MpFLWrPkSSpRqOw80RCflZXy/khxaArvFrQ7uJxSUduzAufc6G0g1VUCOZXxWavPw==", - "dev": true, "requires": { "bluebird": "^3.5.5", "chownr": "^1.1.1", @@ -850,16 +845,14 @@ }, "dependencies": { "bluebird": { - "version": "3.7.1", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.1.tgz", - "integrity": "sha512-DdmyoGCleJnkbp3nkbxTLJ18rjDsE4yCggEwKNXkeV123sPNfOCYeDoeuOY+F2FrSjO1YXcTU+dsy96KMy+gcg==", - "dev": true + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" }, "glob": { "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", - "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -1234,8 +1227,7 @@ "commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", - "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", - "dev": true + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" }, "common-sequence": { "version": "1.0.2", @@ -1246,8 +1238,7 @@ "commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", - "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=", - "dev": true + "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=" }, "compare-versions": { "version": "3.5.1", @@ -1269,7 +1260,6 @@ "version": "1.6.2", "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", - "dev": true, "requires": { "buffer-from": "^1.0.0", "inherits": "^2.0.3", @@ -1316,7 +1306,6 @@ "version": "1.0.5", "resolved": "https://registry.npmjs.org/copy-concurrently/-/copy-concurrently-1.0.5.tgz", "integrity": "sha512-f2domd9fsVDFtaFcbaRZuYXwtdmnzqbADSwhSWYxYB/Q8zsdUUFMXVRwXGDMWmbEzAn1kdRrtI1T/KTFOL4X2A==", - "dev": true, "requires": { "aproba": "^1.1.1", "fs-write-stream-atomic": "^1.0.8", @@ -1409,8 +1398,7 @@ "cyclist": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/cyclist/-/cyclist-1.0.1.tgz", - "integrity": "sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=", - "dev": true + "integrity": "sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=" }, "debug": { "version": "4.1.1", @@ -1605,7 +1593,6 @@ "version": "3.7.1", "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz", "integrity": "sha512-07z8uv2wMyS51kKhD1KsdXJg5WQ6t93RneqRxUHnskXVtlYYkLqM0gqStQZ3pj073g687jPCHrqNfCzawLYh5g==", - "dev": true, "requires": { "end-of-stream": "^1.0.0", "inherits": "^2.0.1", @@ -1669,7 +1656,6 @@ "version": "0.1.7", "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz", "integrity": "sha512-MfrRBDWzIWifgq6tJj60gkAwtLNb6sQPlcFrSOflcP1aFmmruKQ2wRnze/8V6kgyz7H3FF8Npzv78mZ7XLLflg==", - "dev": true, "requires": { "prr": "~1.0.1" } @@ -1902,20 +1888,17 @@ "fast-deep-equal": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", - "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=", - "dev": true + "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" }, "fast-json-stable-stringify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", - "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=", - "dev": true + "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" }, "figgy-pudding": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/figgy-pudding/-/figgy-pudding-3.5.1.tgz", - "integrity": "sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==", - "dev": true + "integrity": "sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==" }, "file-set": { "version": "2.0.1", @@ -1957,7 +1940,6 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-2.1.0.tgz", "integrity": "sha512-Tq6PixE0w/VMFfCgbONnkiQIVol/JJL7nRMi20fqzA4NRs9AfeqMGeRdPi3wIhYkxjeBaWh2rxwapn5Tu3IqOQ==", - "dev": true, "requires": { "commondir": "^1.0.1", "make-dir": "^2.0.0", @@ -1985,7 +1967,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", - "dev": true, "requires": { "locate-path": "^3.0.0" } @@ -2111,7 +2092,6 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/flush-write-stream/-/flush-write-stream-1.1.1.tgz", "integrity": "sha512-3Z4XhFZ3992uIq0XOqb9AreonueSYphE6oYbpt5+3u06JWklbsPkNv3ZKkP9Bz/r+1MWCaMoSQ28P85+1Yc77w==", - "dev": true, "requires": { "inherits": "^2.0.3", "readable-stream": "^2.3.6" @@ -2136,7 +2116,6 @@ "version": "2.3.0", "resolved": "https://registry.npmjs.org/from2/-/from2-2.3.0.tgz", "integrity": "sha1-i/tVAr3kpNNs/e6gB/zKIdfjgq8=", - "dev": true, "requires": { "inherits": "^2.0.1", "readable-stream": "^2.0.0" @@ -2167,7 +2146,6 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.10.tgz", "integrity": "sha1-tH31NJPvkR33VzHnCp3tAYnbQMk=", - "dev": true, "requires": { "graceful-fs": "^4.1.2", "iferr": "^0.1.5", @@ -2843,8 +2821,7 @@ "graceful-fs": { "version": "4.1.15", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", - "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==", - "dev": true + "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" }, "handlebars": { "version": "4.5.3", @@ -3013,8 +2990,7 @@ "iferr": { "version": "0.1.5", "resolved": "https://registry.npmjs.org/iferr/-/iferr-0.1.5.tgz", - "integrity": "sha1-xg7taebY/bazEEofy8ocGS3FtQE=", - "dev": true + "integrity": "sha1-xg7taebY/bazEEofy8ocGS3FtQE=" }, "ignore-walk": { "version": "3.0.3", @@ -3038,14 +3014,12 @@ "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", - "dev": true + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=" }, "infer-owner": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/infer-owner/-/infer-owner-1.0.4.tgz", - "integrity": "sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==", - "dev": true + "integrity": "sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==" }, "inflight": { "version": "1.0.6", @@ -3222,8 +3196,7 @@ "is-wsl": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-1.1.0.tgz", - "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=", - "dev": true + "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=" }, "isarray": { "version": "1.0.0", @@ -3358,8 +3331,7 @@ "json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", - "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", - "dev": true + "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" }, "json5": { "version": "1.0.1", @@ -3432,7 +3404,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", - "dev": true, "requires": { "p-locate": "^3.0.0", "path-exists": "^3.0.0" @@ -3502,7 +3473,6 @@ "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", - "dev": true, "requires": { "yallist": "^3.0.2" } @@ -3511,7 +3481,6 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz", "integrity": "sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==", - "dev": true, "requires": { "pify": "^4.0.1", "semver": "^5.6.0" @@ -3690,7 +3659,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/mississippi/-/mississippi-3.0.0.tgz", "integrity": "sha512-x471SsVjUtBRtcvd4BzKE9kFC+/2TeWgKCgw0bZcw1b9l2X3QX5vCWgF+KaZaYm87Ss//rHnWryupDrgLvmSkA==", - "dev": true, "requires": { "concat-stream": "^1.5.0", "duplexify": "^3.4.2", @@ -3749,7 +3717,6 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", "integrity": "sha1-viwAX9oy4LKa8fBdfEszIUxwH5I=", - "dev": true, "requires": { "aproba": "^1.1.1", "copy-concurrently": "^1.0.0", @@ -4114,7 +4081,6 @@ "version": "2.2.1", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", - "dev": true, "requires": { "p-try": "^2.0.0" } @@ -4123,7 +4089,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", - "dev": true, "requires": { "p-limit": "^2.0.0" } @@ -4131,8 +4096,7 @@ "p-try": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", - "dev": true + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" }, "pako": { "version": "1.0.10", @@ -4144,7 +4108,6 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/parallel-transform/-/parallel-transform-1.2.0.tgz", "integrity": "sha512-P2vSmIu38uIlvdcU7fDkyrxj33gTUy/ABO5ZUbGowxNCopBq/OoD42bP4UmMrJoPyk4Uqf0mu3mtWBhHCZD8yg==", - "dev": true, "requires": { "cyclist": "^1.0.1", "inherits": "^2.0.3", @@ -4192,8 +4155,7 @@ "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", - "dev": true + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" }, "path-is-absolute": { "version": "1.0.1", @@ -4234,14 +4196,12 @@ "pify": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", - "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==", - "dev": true + "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==" }, "pkg-dir": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-3.0.0.tgz", "integrity": "sha512-/E57AYkoeQ25qkxMj5PBOVgF8Kiu/h7cYS30Z5+R7WaiCCBfLq58ZI/dSeaEKb9WVJV5n/03QwrN3IeWIFllvw==", - "dev": true, "requires": { "find-up": "^3.0.0" } @@ -4319,14 +4279,12 @@ "promise-inflight": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz", - "integrity": "sha1-mEcocL8igTL8vdhoEputEsPAKeM=", - "dev": true + "integrity": "sha1-mEcocL8igTL8vdhoEputEsPAKeM=" }, "prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", - "integrity": "sha1-0/wRS6BplaRexok/SEzrHXj19HY=", - "dev": true + "integrity": "sha1-0/wRS6BplaRexok/SEzrHXj19HY=" }, "public-encrypt": { "version": "4.0.3", @@ -4355,7 +4313,6 @@ "version": "1.5.1", "resolved": "https://registry.npmjs.org/pumpify/-/pumpify-1.5.1.tgz", "integrity": "sha512-oClZI37HvuUJJxSKKrC17bZ9Cu0ZYhEAGPsPUy9KlMUmv9dKX2o77RUmq7f3XjIxbwyGwYzbzQ1L2Ks8sIradQ==", - "dev": true, "requires": { "duplexify": "^3.6.0", "inherits": "^2.0.3", @@ -4366,7 +4323,6 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/pump/-/pump-2.0.1.tgz", "integrity": "sha512-ruPMNRkN3MHP1cWJc9OWr+T/xDP0jhXYCLfJcBuX54hhfIBnaQmAUMfDcG4DM5UMWByBbJY69QSphm3jtDKIkA==", - "dev": true, "requires": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -4377,8 +4333,7 @@ "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", - "dev": true + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" }, "querystring": { "version": "0.2.0", @@ -4763,7 +4718,6 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/run-queue/-/run-queue-1.0.3.tgz", "integrity": "sha1-6Eg5bwV9Ij8kOGkkYY4laUFh7Ec=", - "dev": true, "requires": { "aproba": "^1.1.1" } @@ -4816,7 +4770,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-1.0.0.tgz", "integrity": "sha512-i27Mic4KovM/lnGsy8whRCHhc7VicJajAjTrYg11K9zfZXnYIt4k5F+kZkwjnrhKzLic/HLU4j11mjsz2G/75g==", - "dev": true, "requires": { "ajv": "^6.1.0", "ajv-errors": "^1.0.0", @@ -4829,10 +4782,9 @@ "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==" }, "serialize-javascript": { - "version": "1.9.1", - "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-1.9.1.tgz", - "integrity": "sha512-0Vb/54WJ6k5v8sSWN09S0ora+Hnr+cX40r9F170nT+mSkaxltoE/7R3OrIdBSUv1OoiobH1QoWQbCnAO+e8J1A==", - "dev": true + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-2.1.2.tgz", + "integrity": "sha512-rs9OggEUF0V4jUSecXazOYsLfu7OGK2qIn3c7IPBiffz32XniEp/TX9Xmc9LQfK2nQ2QKHvZ2oygKUGU0lG4jQ==" }, "set-blocking": { "version": "2.0.0", @@ -5068,14 +5020,12 @@ "source-list-map": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.1.tgz", - "integrity": "sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==", - "dev": true + "integrity": "sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==" }, "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" }, "source-map-resolve": { "version": "0.5.2", @@ -5094,7 +5044,6 @@ "version": "0.5.16", "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.16.tgz", "integrity": "sha512-efyLRJDr68D9hBBNIPWFjhpFzURh+KJykQwvMyW5UiZzYwoF6l4YMMDIJJEyFWxWCqfyxLzz6tSfUFR+kXXsVQ==", - "dev": true, "requires": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" @@ -5135,7 +5084,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/ssri/-/ssri-6.0.1.tgz", "integrity": "sha512-3Wge10hNcT1Kur4PDFwEieXSCMCJs/7WvSACcrMYrNp+b8kDL1/0wJch5Ni2WrtwEa2IO8OsVfeKIciKCDx/QA==", - "dev": true, "requires": { "figgy-pudding": "^3.5.1" } @@ -5195,7 +5143,6 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/stream-each/-/stream-each-1.2.3.tgz", "integrity": "sha512-vlMC2f8I2u/bZGqkdfLQW/13Zihpej/7PmSiMQsbYddxuTsJp8vRe2x2FvVExZg7FaOds43ROAuFJwPR4MTZLw==", - "dev": true, "requires": { "end-of-stream": "^1.1.0", "stream-shift": "^1.0.0" @@ -5215,10 +5162,9 @@ } }, "stream-shift": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/stream-shift/-/stream-shift-1.0.0.tgz", - "integrity": "sha1-1cdSgl5TZ+eG944Y5EXqIjoVWVI=", - "dev": true + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/stream-shift/-/stream-shift-1.0.1.tgz", + "integrity": "sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==" }, "stream-via": { "version": "1.0.4", @@ -5371,10 +5317,9 @@ "dev": true }, "terser": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.4.0.tgz", - "integrity": "sha512-oDG16n2WKm27JO8h4y/w3iqBGAOSCtq7k8dRmrn4Wf9NouL0b2WpMHGChFGZq4nFAQy1FsNJrVQHfurXOSTmOA==", - "dev": true, + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.4.3.tgz", + "integrity": "sha512-0ikKraVtRDKGzHrzkCv5rUNDzqlhmhowOBqC0XqUHFpW+vJ45+20/IFBcebwKfiS2Z9fJin6Eo+F1zLZsxi8RA==", "requires": { "commander": "^2.20.0", "source-map": "~0.6.1", @@ -5382,16 +5327,15 @@ } }, "terser-webpack-plugin": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-1.4.1.tgz", - "integrity": "sha512-ZXmmfiwtCLfz8WKZyYUuuHf3dMYEjg8NrjHMb0JqHVHVOSkzp3cW2/XG1fP3tRhqEqSzMwzzRQGtAPbs4Cncxg==", - "dev": true, + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-1.4.3.tgz", + "integrity": "sha512-QMxecFz/gHQwteWwSo5nTc6UaICqN1bMedC5sMtUc7y3Ha3Q8y6ZO0iCR8pq4RJC8Hjf0FEPEHZqcMB/+DFCrA==", "requires": { "cacache": "^12.0.2", "find-cache-dir": "^2.1.0", "is-wsl": "^1.1.0", "schema-utils": "^1.0.0", - "serialize-javascript": "^1.7.0", + "serialize-javascript": "^2.1.2", "source-map": "^0.6.1", "terser": "^4.1.2", "webpack-sources": "^1.4.0", @@ -5423,7 +5367,6 @@ "version": "2.0.5", "resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz", "integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==", - "dev": true, "requires": { "readable-stream": "~2.3.6", "xtend": "~4.0.1" @@ -5560,8 +5503,7 @@ "typedarray": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", - "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", - "dev": true + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" }, "typescript": { "version": "3.7.2", @@ -5614,7 +5556,6 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/unique-filename/-/unique-filename-1.1.1.tgz", "integrity": "sha512-Vmp0jIp2ln35UTXuryvjzkjGdRyf9b2lTXuSYUiPmzRcl3FDtYqAwOnTJkAngD9SWhnoJzDbTKwaOrZ+STtxNQ==", - "dev": true, "requires": { "unique-slug": "^2.0.0" } @@ -5623,7 +5564,6 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/unique-slug/-/unique-slug-2.0.2.tgz", "integrity": "sha512-zoWr9ObaxALD3DOPfjPSqxt4fnZiWblxHIgeWqW8x7UqDzEtHEQLzji2cuJYQFCU6KmoJikOYAZlrTHHebjx2w==", - "dev": true, "requires": { "imurmurhash": "^0.1.4" } @@ -5678,7 +5618,6 @@ "version": "4.2.2", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", "integrity": "sha512-KY9Frmirql91X2Qgjry0Wd4Y+YTdrdZheS8TFwvkbLWf/G5KNJDCh6pKL5OZctEW4+0Baa5idK2ZQuELRwPznQ==", - "dev": true, "requires": { "punycode": "^2.1.0" } @@ -5968,7 +5907,6 @@ "version": "1.4.3", "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-1.4.3.tgz", "integrity": "sha512-lgTS3Xhv1lCOKo7SA5TjKXMjpSM4sBjNV5+q2bqesbSPs5FjGmU6jjtBSkX9b4qW87vDIsCIlUPOEhbZrMdjeQ==", - "dev": true, "requires": { "source-list-map": "^2.0.0", "source-map": "~0.6.1" @@ -6024,7 +5962,6 @@ "version": "1.7.0", "resolved": "https://registry.npmjs.org/worker-farm/-/worker-farm-1.7.0.tgz", "integrity": "sha512-rvw3QTZc8lAxyVrqcSGVm5yP/IJ2UcB3U0graE3LCFoZ0Yn2x4EoVSqJKdB/T5M+FLcRPjz4TDacRf3OCfNUzw==", - "dev": true, "requires": { "errno": "~0.1.7" } @@ -6088,14 +6025,12 @@ "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=", - "dev": true + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" }, "y18n": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", - "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==", - "dev": true + "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" }, "yallist": { "version": "3.0.3", diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index f952e637..26ac2b36 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -23,7 +23,7 @@ export class ColorDistanceSensor extends Device { * @event ColorDistanceSensor#color * @param {Color} color */ - this.emit("color", color); + this.emitGlobal("color", color); } break; @@ -39,7 +39,7 @@ export class ColorDistanceSensor extends Device { * @event ColorDistanceSensor#distance * @param {number} distance Distance, in millimeters. */ - this.emit("distance", distance); + this.emitGlobal("distance", distance); } break; @@ -65,7 +65,7 @@ export class ColorDistanceSensor extends Device { */ if (message[4] <= 10) { const color = message[4]; - this.emit("colorAndDistance", color, distance); + this.emitGlobal("colorAndDistance", color, distance); } break; diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index e68dbea1..69ef5499 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -17,7 +17,7 @@ export class CurrentSensor extends Device { case CurrentSensor.Mode.CURRENT: if (this.isWeDo2SmartHub) { const current = message.readInt16LE(2) / 1000; - this.emit("current", current); + this.emitGlobal("current", current); } else { let maxCurrentValue = CurrentSensor.MaxCurrentValue[this.hub.type]; if (maxCurrentValue === undefined) { @@ -33,7 +33,7 @@ export class CurrentSensor extends Device { * @event CurrentSensor#current * @param {number} current */ - this.emit("current", current); + this.emitGlobal("current", current); } break; } diff --git a/src/devices/device.ts b/src/devices/device.ts index cf2d617d..adb9a30a 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -28,16 +28,7 @@ export class Device extends EventEmitter { this._modeMap = modeMap; this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); - const detachListener = (device: Device) => { - if (device.portId === this.portId) { - this._connected = false; - this.hub.removeListener("detach", detachListener); - this.emit("detach"); - } - }; - this.hub.on("detach", detachListener); - - this.on("newListener", (event) => { + const attachListener = (event: string) => { if (event === "detach") { return; } @@ -46,7 +37,25 @@ export class Device extends EventEmitter { this.subscribe(this._modeMap[event]); } } - }); + }; + + const detachListener = (device: Device) => { + if (device.portId === this.portId) { + this._connected = false; + this.hub.removeListener("detach", detachListener); + this.emit("detach"); + } + }; + + for (let event in this._modeMap) { + if (this.hub.listenerCount(event) > 0) { + attachListener(event); + } + } + + this.hub.on("newListener", attachListener); + this.on("newListener", attachListener); + this.hub.on("detach", detachListener); } public get connected () { @@ -99,7 +108,14 @@ export class Device extends EventEmitter { } public receive (message: Buffer) { - this.emit("receive", message); + this.emitGlobal("receive", message); + } + + public emitGlobal (event: string, ...args: any) { + this.emit(event, ...args); + if (this.hub.listenerCount(event) > 0) { + this.hub.emit(event, this, ...args); + } } public finish () { diff --git a/src/devices/duplotrainbasecolorsensor.ts b/src/devices/duplotrainbasecolorsensor.ts index 6175a395..4c3a8aba 100644 --- a/src/devices/duplotrainbasecolorsensor.ts +++ b/src/devices/duplotrainbasecolorsensor.ts @@ -23,7 +23,7 @@ export class DuploTrainBaseColorSensor extends Device { * @event DuploTrainBaseColorSensor#color * @param {Color} color */ - this.emit("color", color); + this.emitGlobal("color", color); } break; diff --git a/src/devices/duplotrainbasespeedometer.ts b/src/devices/duplotrainbasespeedometer.ts index 5241f419..780c49a9 100644 --- a/src/devices/duplotrainbasespeedometer.ts +++ b/src/devices/duplotrainbasespeedometer.ts @@ -22,7 +22,7 @@ export class DuploTrainBaseSpeedometer extends Device { * @event DuploTrainBaseSpeedometer#speed * @param {number} speed */ - this.emit("speed", speed); + this.emitGlobal("speed", speed); break; } diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index 6d547baf..a5d234b1 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -24,7 +24,7 @@ export class MotionSensor extends Device { * @event MotionSensor#distance * @param {number} distance Distance, in millimeters. */ - this.emit("distance", distance * 10); + this.emitGlobal("distance", distance * 10); break; } } diff --git a/src/devices/movehubtiltsensor.ts b/src/devices/movehubtiltsensor.ts index 70192e59..08cb1b4c 100644 --- a/src/devices/movehubtiltsensor.ts +++ b/src/devices/movehubtiltsensor.ts @@ -23,7 +23,7 @@ export class MoveHubTiltSensor extends Device { */ const tiltX = message.readInt8(4); const tiltY = message.readInt8(5); - this.emit("tilt", -tiltX, tiltY); + this.emitGlobal("tilt", -tiltX, tiltY); break; } } diff --git a/src/devices/remotecontrolbutton.ts b/src/devices/remotecontrolbutton.ts index 7c8dce51..8d7966a4 100644 --- a/src/devices/remotecontrolbutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -21,7 +21,7 @@ export class RemoteControlButton extends Device { * @param {number} event */ const event = message[4]; - this.emit("button", event); + this.emitGlobal("button", event); break; } } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 3ae54f6b..583f11c4 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -22,7 +22,7 @@ export class TachoMotor extends BasicMotor { * @event TachoMotor#rotate * @param {number} rotation */ - this.emit("rotate", rotation); + this.emitGlobal("rotate", rotation); break; } } diff --git a/src/devices/technicmediumhubaccelerometersensor.ts b/src/devices/technicmediumhubaccelerometersensor.ts index 51b0a03b..6beb42c8 100644 --- a/src/devices/technicmediumhubaccelerometersensor.ts +++ b/src/devices/technicmediumhubaccelerometersensor.ts @@ -15,19 +15,19 @@ export class TechnicMediumHubAccelerometerSensor extends Device { switch (mode) { case TechnicMediumHubAccelerometerSensor.Mode.ACCEL: - /** - * Emits when accelerometer detects movement. Measured in mG. - * @event LPF2Hub#accel - * @param {string} port - * @param {number} x - * @param {number} y - * @param {number} z - */ - const accelX = Math.round(message.readInt16LE(4) / 4.096); - const accelY = Math.round(message.readInt16LE(6) / 4.096); - const accelZ = Math.round(message.readInt16LE(8) / 4.096); - this.emit("accel", accelX, accelY, accelZ); - break; + /** + * Emits when accelerometer detects movement. Measured in mG. + * @event LPF2Hub#accel + * @param {string} port + * @param {number} x + * @param {number} y + * @param {number} z + */ + const accelX = Math.round(message.readInt16LE(4) / 4.096); + const accelY = Math.round(message.readInt16LE(6) / 4.096); + const accelZ = Math.round(message.readInt16LE(8) / 4.096); + this.emitGlobal("accel", accelX, accelY, accelZ); + break; } } diff --git a/src/devices/technicmediumhubgyrosensor.ts b/src/devices/technicmediumhubgyrosensor.ts index 3151e429..9ecea0b0 100644 --- a/src/devices/technicmediumhubgyrosensor.ts +++ b/src/devices/technicmediumhubgyrosensor.ts @@ -25,7 +25,7 @@ export class TechnicMediumHubGyroSensor extends Device { const gyroX = Math.round(message.readInt16LE(4) * 7 / 400); const gyroY = Math.round(message.readInt16LE(6) * 7 / 400); const gyroZ = Math.round(message.readInt16LE(8) * 7 / 400); - this.emit("gyro", gyroX, gyroY, -gyroZ); + this.emitGlobal("gyro", gyroX, gyroY, -gyroZ); break; } } diff --git a/src/devices/technicmediumhubtiltsensor.ts b/src/devices/technicmediumhubtiltsensor.ts index f5cc6579..47bd4c0e 100644 --- a/src/devices/technicmediumhubtiltsensor.ts +++ b/src/devices/technicmediumhubtiltsensor.ts @@ -25,7 +25,7 @@ export class TechnicMediumHubTiltSensor extends Device { const tiltZ = message.readInt16LE(4); const tiltY = message.readInt16LE(6); const tiltX = message.readInt16LE(8); - this.emit("tilt", tiltX, tiltY, -tiltZ); + this.emitGlobal("tilt", tiltX, tiltY, -tiltZ); break; } } diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index 7e9e45d7..91c2a3d2 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -23,7 +23,7 @@ export class TiltSensor extends Device { * @param {number} x * @param {number} y */ - this.emit("tilt", tiltX, tiltY); + this.emitGlobal("tilt", tiltX, tiltY); break; } } diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index 45ecc916..8610e475 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -17,7 +17,7 @@ export class VoltageSensor extends Device { case VoltageSensor.Mode.VOLTAGE: if (this.isWeDo2SmartHub) { const voltage = message.readInt16LE(2) / 40; - this.emit("voltage", voltage); + this.emitGlobal("voltage", voltage); } else { let maxVoltageValue = VoltageSensor.MaxVoltageValue[this.hub.type]; if (maxVoltageValue === undefined) { @@ -33,7 +33,7 @@ export class VoltageSensor extends Device { * @event VoltageSensor#voltage * @param {number} voltage */ - this.emit("voltage", voltage); + this.emitGlobal("voltage", voltage); } break; } diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index fe2f4d30..e7114557 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -322,84 +322,42 @@ export class BaseHub extends EventEmitter { protected _createDevice (deviceType: number, portId: number) { - let device; - - switch (deviceType) { - case Consts.DeviceType.LIGHT: - device = new Light(this, portId); - break; - case Consts.DeviceType.TRAIN_MOTOR: - device = new TrainMotor(this, portId); - break; - case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR: - device = new SimpleMediumLinearMotor(this, portId); - break; - case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: - device = new MoveHubMediumLinearMotor(this, portId); - break; - case Consts.DeviceType.PIEZO_BUZZER: - device = new PiezoBuzzer(this, portId); - break; - case Consts.DeviceType.MOTION_SENSOR: - device = new MotionSensor(this, portId); - break; - case Consts.DeviceType.TILT_SENSOR: - device = new TiltSensor(this, portId); - break; - case Consts.DeviceType.MOVE_HUB_TILT_SENSOR: - device = new MoveHubTiltSensor(this, portId); - break; - case Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR: - device = new TechnicMediumHubTiltSensor(this, portId); - break; - case Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR: - device = new TechnicMediumHubGyroSensor(this, portId); - break; - case Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER: - device = new TechnicMediumHubAccelerometerSensor(this, portId); - break; - case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: - device = new MediumLinearMotor(this, portId); - break; - case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR: - device = new TechnicLargeLinearMotor(this, portId); - break; - case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR: - device = new TechnicXLargeLinearMotor(this, portId); - break; - case Consts.DeviceType.COLOR_DISTANCE_SENSOR: - device = new ColorDistanceSensor(this, portId); - break; - case Consts.DeviceType.VOLTAGE_SENSOR: - device = new VoltageSensor(this, portId); - break; - case Consts.DeviceType.CURRENT_SENSOR: - device = new CurrentSensor(this, portId); - break; - case Consts.DeviceType.REMOTE_CONTROL_BUTTON: - device = new RemoteControlButton(this, portId); - break; - case Consts.DeviceType.HUB_LED: - device = new HubLED(this, portId); - break; - case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR: - device = new DuploTrainBaseColorSensor(this, portId); - break; - case Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR: - device = new DuploTrainBaseMotor(this, portId); - break; - case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER: - device = new DuploTrainBaseSpeaker(this, portId); - break; - case Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER: - device = new DuploTrainBaseSpeedometer(this, portId); - break; - default: - device = new Device(this, portId, undefined, deviceType); - break; + let constructor; + + // NK TODO: This needs to go in a better place + const deviceConstructors: {[type: number]: typeof Device} = { + [Consts.DeviceType.LIGHT]: Light, + [Consts.DeviceType.TRAIN_MOTOR]: TrainMotor, + [Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR]: SimpleMediumLinearMotor, + [Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR]: MoveHubMediumLinearMotor, + [Consts.DeviceType.MOTION_SENSOR]: MotionSensor, + [Consts.DeviceType.TILT_SENSOR]: TiltSensor, + [Consts.DeviceType.MOVE_HUB_TILT_SENSOR]: MoveHubTiltSensor, + [Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR]: TechnicMediumHubTiltSensor, + [Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR]: TechnicMediumHubGyroSensor, + [Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER]: TechnicMediumHubAccelerometerSensor, + [Consts.DeviceType.MEDIUM_LINEAR_MOTOR]: MediumLinearMotor, + [Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR]: TechnicLargeLinearMotor, + [Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR]: TechnicXLargeLinearMotor, + [Consts.DeviceType.COLOR_DISTANCE_SENSOR]: ColorDistanceSensor, + [Consts.DeviceType.VOLTAGE_SENSOR]: VoltageSensor, + [Consts.DeviceType.CURRENT_SENSOR]: CurrentSensor, + [Consts.DeviceType.REMOTE_CONTROL_BUTTON]: RemoteControlButton, + [Consts.DeviceType.HUB_LED]: HubLED, + [Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR]: DuploTrainBaseColorSensor, + [Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR]: DuploTrainBaseMotor, + [Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER]: DuploTrainBaseSpeaker, + [Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER]: DuploTrainBaseSpeedometer + }; + + constructor = deviceConstructors[deviceType as Consts.DeviceType]; + + if (constructor) { + return new constructor(this, portId); + } else { + return new Device(this, portId, undefined, deviceType); } - return device; } From 9113d22766d86d6ea212174f043d2327ab79ee9c Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Sun, 22 Dec 2019 19:02:35 -0800 Subject: [PATCH 38/62] Named internal ports on hubs --- src/devices/device.ts | 14 +++++++------- src/hubs/hub.ts | 5 ++++- src/hubs/movehub.ts | 6 +++++- src/hubs/remotecontrol.ts | 5 ++++- src/hubs/technicmediumhub.ts | 9 ++++++--- src/hubs/wedo2smarthub.ts | 6 +++++- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/devices/device.ts b/src/devices/device.ts index adb9a30a..e80850df 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -28,7 +28,7 @@ export class Device extends EventEmitter { this._modeMap = modeMap; this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); - const attachListener = (event: string) => { + const eventAttachListener = (event: string) => { if (event === "detach") { return; } @@ -39,23 +39,23 @@ export class Device extends EventEmitter { } }; - const detachListener = (device: Device) => { + const deviceDetachListener = (device: Device) => { if (device.portId === this.portId) { this._connected = false; - this.hub.removeListener("detach", detachListener); + this.hub.removeListener("detach", deviceDetachListener); this.emit("detach"); } }; for (let event in this._modeMap) { if (this.hub.listenerCount(event) > 0) { - attachListener(event); + eventAttachListener(event); } } - this.hub.on("newListener", attachListener); - this.on("newListener", attachListener); - this.hub.on("detach", detachListener); + this.hub.on("newListener", eventAttachListener); + this.on("newListener", eventAttachListener); + this.hub.on("detach", deviceDetachListener); } public get connected () { diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index 0e567d4c..eb6cf295 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -62,7 +62,10 @@ export namespace Hub { export const PortMap: {[portName: string]: number} = { "A": 0, - "B": 1 + "B": 1, + "HUB_LED": 50, + "CURRENT_SENSOR": 59, + "VOLTAGE_SENSOR": 60 } } \ No newline at end of file diff --git a/src/hubs/movehub.ts b/src/hubs/movehub.ts index a38dbcdb..657a083c 100644 --- a/src/hubs/movehub.ts +++ b/src/hubs/movehub.ts @@ -62,7 +62,11 @@ export namespace MoveHub { "A": 0, "B": 1, "C": 2, - "D": 3 + "D": 3, + "HUB_LED": 50, + "TILT_SENSOR": 58, + "CURRENT_SENSOR": 59, + "VOLTAGE_SENSOR": 60 } } \ No newline at end of file diff --git a/src/hubs/remotecontrol.ts b/src/hubs/remotecontrol.ts index 3d3c731d..50e08b4b 100644 --- a/src/hubs/remotecontrol.ts +++ b/src/hubs/remotecontrol.ts @@ -53,7 +53,10 @@ export namespace RemoteControl { export const PortMap: {[portName: string]: number} = { "LEFT": 0, - "RIGHT": 1 + "RIGHT": 1, + "HUB_LED": 52, + "VOLTAGE_SENSOR": 59, + "REMOTE_CONTROL_RSSI": 60 } } \ No newline at end of file diff --git a/src/hubs/technicmediumhub.ts b/src/hubs/technicmediumhub.ts index dc6f2b59..558b113b 100644 --- a/src/hubs/technicmediumhub.ts +++ b/src/hubs/technicmediumhub.ts @@ -115,9 +115,12 @@ export namespace TechnicMediumHub { "B": 1, "C": 2, "D": 3, - "ACCEL": 97, - "GYRO": 98, - "TILT": 99 + "HUB_LED": 50, + "CURRENT_SENSOR": 59, + "VOLTAGE_SENSOR": 60, + "ACCELEROMETER": 97, + "GYRO_SENSOR": 98, + "TILT_SENSOR": 99 } } \ No newline at end of file diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index 74f5868f..c392c214 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -232,7 +232,11 @@ export namespace WeDo2SmartHub { export const PortMap: {[portName: string]: number} = { "A": 1, - "B": 2 + "B": 2, + "CURRENT_SENSOR": 3, + "VOLTAGE_SENSOR": 4, + "PIEZO_BUZZER": 5, + "HUB_LED": 6 } } \ No newline at end of file From c5146bd315c1e70ffa768dee5c8f60f3bfb67dbe Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 23 Dec 2019 20:32:12 -0800 Subject: [PATCH 39/62] Renamed button to remoteButton on RC --- src/devices/remotecontrolbutton.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/devices/remotecontrolbutton.ts b/src/devices/remotecontrolbutton.ts index 8d7966a4..13d03878 100644 --- a/src/devices/remotecontrolbutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -21,7 +21,7 @@ export class RemoteControlButton extends Device { * @param {number} event */ const event = message[4]; - this.emitGlobal("button", event); + this.emitGlobal("remoteButton", event); break; } } @@ -35,7 +35,7 @@ export namespace RemoteControlButton { } export const ModeMap: {[event: string]: number} = { - "button": RemoteControlButton.Mode.BUTTON_EVENTS + "remoteButton": RemoteControlButton.Mode.BUTTON_EVENTS } export const ButtonState: {[state: string]: number} = { From 290f469b478e0863625322ff6e68645463c3bd5d Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 26 Dec 2019 18:18:55 -0800 Subject: [PATCH 40/62] Device events emit value objects --- src/devices/colordistancesensor.ts | 6 +++--- src/devices/currentsensor.ts | 4 ++-- src/devices/device.ts | 2 +- src/devices/duplotrainbasecolorsensor.ts | 2 +- src/devices/duplotrainbasespeedometer.ts | 2 +- src/devices/motionsensor.ts | 3 ++- src/devices/movehubtiltsensor.ts | 6 +++--- src/devices/remotecontrolbutton.ts | 2 +- src/devices/tachomotor.ts | 2 +- src/devices/technicmediumhubaccelerometersensor.ts | 8 ++++---- src/devices/technicmediumhubgyrosensor.ts | 8 ++++---- src/devices/technicmediumhubtiltsensor.ts | 8 ++++---- src/devices/tiltsensor.ts | 6 +++--- src/devices/voltagesensor.ts | 4 ++-- src/hubs/lpf2hub.ts | 8 ++++---- src/hubs/wedo2smarthub.ts | 6 +++--- 16 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index 26ac2b36..bf1a9e61 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -23,7 +23,7 @@ export class ColorDistanceSensor extends Device { * @event ColorDistanceSensor#color * @param {Color} color */ - this.emitGlobal("color", color); + this.emitGlobal("color", { color }); } break; @@ -39,7 +39,7 @@ export class ColorDistanceSensor extends Device { * @event ColorDistanceSensor#distance * @param {number} distance Distance, in millimeters. */ - this.emitGlobal("distance", distance); + this.emitGlobal("distance", { distance }); } break; @@ -65,7 +65,7 @@ export class ColorDistanceSensor extends Device { */ if (message[4] <= 10) { const color = message[4]; - this.emitGlobal("colorAndDistance", color, distance); + this.emitGlobal("colorAndDistance", { color, distance }); } break; diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index 69ef5499..dc688d43 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -17,7 +17,7 @@ export class CurrentSensor extends Device { case CurrentSensor.Mode.CURRENT: if (this.isWeDo2SmartHub) { const current = message.readInt16LE(2) / 1000; - this.emitGlobal("current", current); + this.emitGlobal("current", { current }); } else { let maxCurrentValue = CurrentSensor.MaxCurrentValue[this.hub.type]; if (maxCurrentValue === undefined) { @@ -33,7 +33,7 @@ export class CurrentSensor extends Device { * @event CurrentSensor#current * @param {number} current */ - this.emitGlobal("current", current); + this.emitGlobal("current", { current }); } break; } diff --git a/src/devices/device.ts b/src/devices/device.ts index e80850df..def64884 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -108,7 +108,7 @@ export class Device extends EventEmitter { } public receive (message: Buffer) { - this.emitGlobal("receive", message); + this.emitGlobal("receive", { message }); } public emitGlobal (event: string, ...args: any) { diff --git a/src/devices/duplotrainbasecolorsensor.ts b/src/devices/duplotrainbasecolorsensor.ts index 4c3a8aba..4f8d22d8 100644 --- a/src/devices/duplotrainbasecolorsensor.ts +++ b/src/devices/duplotrainbasecolorsensor.ts @@ -23,7 +23,7 @@ export class DuploTrainBaseColorSensor extends Device { * @event DuploTrainBaseColorSensor#color * @param {Color} color */ - this.emitGlobal("color", color); + this.emitGlobal("color", { color }); } break; diff --git a/src/devices/duplotrainbasespeedometer.ts b/src/devices/duplotrainbasespeedometer.ts index 780c49a9..17186dc7 100644 --- a/src/devices/duplotrainbasespeedometer.ts +++ b/src/devices/duplotrainbasespeedometer.ts @@ -22,7 +22,7 @@ export class DuploTrainBaseSpeedometer extends Device { * @event DuploTrainBaseSpeedometer#speed * @param {number} speed */ - this.emitGlobal("speed", speed); + this.emitGlobal("speed", { speed }); break; } diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index a5d234b1..1a75961d 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -19,12 +19,13 @@ export class MotionSensor extends Device { if (message[this.isWeDo2SmartHub ? 3 : 5] === 1) { distance = distance + 255; } + distance *= 10; /** * Emits when a distance sensor is activated. * @event MotionSensor#distance * @param {number} distance Distance, in millimeters. */ - this.emitGlobal("distance", distance * 10); + this.emitGlobal("distance", { distance }); break; } } diff --git a/src/devices/movehubtiltsensor.ts b/src/devices/movehubtiltsensor.ts index 08cb1b4c..b9bc0d7d 100644 --- a/src/devices/movehubtiltsensor.ts +++ b/src/devices/movehubtiltsensor.ts @@ -21,9 +21,9 @@ export class MoveHubTiltSensor extends Device { * @param {number} x * @param {number} y */ - const tiltX = message.readInt8(4); - const tiltY = message.readInt8(5); - this.emitGlobal("tilt", -tiltX, tiltY); + const x = -message.readInt8(4); + const y = message.readInt8(5); + this.emitGlobal("tilt", { x, y }); break; } } diff --git a/src/devices/remotecontrolbutton.ts b/src/devices/remotecontrolbutton.ts index 13d03878..c43af4fd 100644 --- a/src/devices/remotecontrolbutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -21,7 +21,7 @@ export class RemoteControlButton extends Device { * @param {number} event */ const event = message[4]; - this.emitGlobal("remoteButton", event); + this.emitGlobal("remoteButton", { event }); break; } } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 583f11c4..69a3a282 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -22,7 +22,7 @@ export class TachoMotor extends BasicMotor { * @event TachoMotor#rotate * @param {number} rotation */ - this.emitGlobal("rotate", rotation); + this.emitGlobal("rotate", { rotation }); break; } } diff --git a/src/devices/technicmediumhubaccelerometersensor.ts b/src/devices/technicmediumhubaccelerometersensor.ts index 6beb42c8..80e6901c 100644 --- a/src/devices/technicmediumhubaccelerometersensor.ts +++ b/src/devices/technicmediumhubaccelerometersensor.ts @@ -23,10 +23,10 @@ export class TechnicMediumHubAccelerometerSensor extends Device { * @param {number} y * @param {number} z */ - const accelX = Math.round(message.readInt16LE(4) / 4.096); - const accelY = Math.round(message.readInt16LE(6) / 4.096); - const accelZ = Math.round(message.readInt16LE(8) / 4.096); - this.emitGlobal("accel", accelX, accelY, accelZ); + const x = Math.round(message.readInt16LE(4) / 4.096); + const y = Math.round(message.readInt16LE(6) / 4.096); + const z = Math.round(message.readInt16LE(8) / 4.096); + this.emitGlobal("accel", { x, y, z }); break; } } diff --git a/src/devices/technicmediumhubgyrosensor.ts b/src/devices/technicmediumhubgyrosensor.ts index 9ecea0b0..793749dd 100644 --- a/src/devices/technicmediumhubgyrosensor.ts +++ b/src/devices/technicmediumhubgyrosensor.ts @@ -22,10 +22,10 @@ export class TechnicMediumHubGyroSensor extends Device { * @param {number} y * @param {number} z */ - const gyroX = Math.round(message.readInt16LE(4) * 7 / 400); - const gyroY = Math.round(message.readInt16LE(6) * 7 / 400); - const gyroZ = Math.round(message.readInt16LE(8) * 7 / 400); - this.emitGlobal("gyro", gyroX, gyroY, -gyroZ); + const x = Math.round(message.readInt16LE(4) * 7 / 400); + const y = Math.round(message.readInt16LE(6) * 7 / 400); + const z = Math.round(message.readInt16LE(8) * 7 / 400); + this.emitGlobal("gyro", { x, y, z }); break; } } diff --git a/src/devices/technicmediumhubtiltsensor.ts b/src/devices/technicmediumhubtiltsensor.ts index 47bd4c0e..53f05957 100644 --- a/src/devices/technicmediumhubtiltsensor.ts +++ b/src/devices/technicmediumhubtiltsensor.ts @@ -22,10 +22,10 @@ export class TechnicMediumHubTiltSensor extends Device { * @param {number} y * @param {number} z */ - const tiltZ = message.readInt16LE(4); - const tiltY = message.readInt16LE(6); - const tiltX = message.readInt16LE(8); - this.emitGlobal("tilt", tiltX, tiltY, -tiltZ); + const z = -message.readInt16LE(4); + const y = message.readInt16LE(6); + const x = message.readInt16LE(8); + this.emitGlobal("tilt", { x, y, z }); break; } } diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index 91c2a3d2..301bfd09 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -15,15 +15,15 @@ export class TiltSensor extends Device { switch (mode) { case TiltSensor.Mode.TILT: - const tiltX = message.readInt8(this.isWeDo2SmartHub ? 2 : 4); - const tiltY = message.readInt8(this.isWeDo2SmartHub ? 3 : 5); + const x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4); + const y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5); /** * Emits when a tilt sensor is activated. * @event LPF2Hub#tilt * @param {number} x * @param {number} y */ - this.emitGlobal("tilt", tiltX, tiltY); + this.emitGlobal("tilt", { x, y }); break; } } diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index 8610e475..c37b9edf 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -17,7 +17,7 @@ export class VoltageSensor extends Device { case VoltageSensor.Mode.VOLTAGE: if (this.isWeDo2SmartHub) { const voltage = message.readInt16LE(2) / 40; - this.emitGlobal("voltage", voltage); + this.emitGlobal("voltage", { voltage }); } else { let maxVoltageValue = VoltageSensor.MaxVoltageValue[this.hub.type]; if (maxVoltageValue === undefined) { @@ -33,7 +33,7 @@ export class VoltageSensor extends Device { * @event VoltageSensor#voltage * @param {number} voltage */ - this.emitGlobal("voltage", voltage); + this.emitGlobal("voltage", { voltage }); } break; } diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 9d27ae74..e0dec343 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -200,10 +200,10 @@ export class LPF2Hub extends BaseHub { * @param {string} button * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonState.PRESSED); + this.emit("button", { event: Consts.ButtonState.PRESSED }); return; } else if (message[5] === 0) { - this.emit("button", "GREEN", Consts.ButtonState.RELEASED); + this.emit("button", { event: Consts.ButtonState.RELEASED }); return; } @@ -221,7 +221,7 @@ export class LPF2Hub extends BaseHub { const rssi = message.readInt8(5); if (rssi !== 0) { this._rssi = rssi; - this.emit("rssiChange", this._rssi); + this.emit("rssi", { rssi: this._rssi }); } // primary MAC Address @@ -233,7 +233,7 @@ export class LPF2Hub extends BaseHub { const batteryLevel = message[5]; if (batteryLevel !== this._batteryLevel) { this._batteryLevel = batteryLevel; - this.emit("batteryLevel", batteryLevel); + this.emit("batteryLevel", { batteryLevel }); } } diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index c392c214..b2afaad5 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -168,7 +168,7 @@ export class WeDo2SmartHub extends BaseHub { const batteryLevel = data[0]; if (batteryLevel !== this._batteryLevel) { this._batteryLevel = batteryLevel; - this.emit("batteryLevel", batteryLevel); + this.emit("batteryLevel", { batteryLevel }); } } @@ -209,10 +209,10 @@ export class WeDo2SmartHub extends BaseHub { * @param {string} button * @param {ButtonState} state */ - this.emit("button", "GREEN", Consts.ButtonState.PRESSED); + this.emit("button", { event: Consts.ButtonState.PRESSED }); return; } else if (message[0] === 0x00) { - this.emit("button", "GREEN", Consts.ButtonState.RELEASED); + this.emit("button", { event: Consts.ButtonState.RELEASED }); return; } From 077a7376c0828270975ee7719dec5e0613c95a5e Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 6 Jan 2020 09:49:38 -0800 Subject: [PATCH 41/62] WeDo 2.0 unsubscribing --- src/devices/device.ts | 6 +++++- src/hubs/basehub.ts | 5 +++++ src/hubs/wedo2smarthub.ts | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/devices/device.ts b/src/devices/device.ts index e80850df..ce6f841a 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -107,11 +107,15 @@ export class Device extends EventEmitter { } } + public unsubscribe (mode: number) { + this._ensureConnected(); + } + public receive (message: Buffer) { this.emitGlobal("receive", message); } - public emitGlobal (event: string, ...args: any) { + public emitGlobal (event: string, ...args: any[]) { this.emit(event, ...args); if (this.hub.listenerCount(event) > 0) { this.hub.emit(event, this, ...args); diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index e7114557..d9ace2d3 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -289,6 +289,11 @@ export class BaseHub extends EventEmitter { } + public unsubscribe (portId: number, deviceType: number, mode: number) { + // NK Do nothing here + } + + protected _attachDevice (device: Device) { this._attachedDevices[device.portId] = device; /** diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index c392c214..a1648743 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -141,8 +141,8 @@ export class WeDo2SmartHub extends BaseHub { } - protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { - this.send(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE, callback); + public unsubscribe (portId: number, deviceType: number, mode: number) { + this.send(Buffer.from([0x01, 0x02, portId, deviceType, mode, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); } From 673f5ffcb6b9c3c0479538201b83dc1b87e574de Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 8 Jan 2020 14:38:51 -0800 Subject: [PATCH 42/62] Start of port combining --- package-lock.json | 123 ++++++++++++++++++++++++++++++++---------- src/devices/device.ts | 1 + src/hubs/basehub.ts | 19 ++++--- src/hubs/lpf2hub.ts | 36 ++++++++----- tslint.json | 2 +- 5 files changed, 130 insertions(+), 51 deletions(-) diff --git a/package-lock.json b/package-lock.json index f25098ad..412d11fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -283,6 +283,7 @@ "version": "6.10.2", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", "integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==", + "dev": true, "requires": { "fast-deep-equal": "^2.0.1", "fast-json-stable-stringify": "^2.0.0", @@ -293,12 +294,14 @@ "ajv-errors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/ajv-errors/-/ajv-errors-1.0.1.tgz", - "integrity": "sha512-DCRfO/4nQ+89p/RK43i8Ezd41EqdGIU4ld7nGF8OQ14oc/we5rEntLCUa7+jrn3nn83BosfwZA0wb4pon2o8iQ==" + "integrity": "sha512-DCRfO/4nQ+89p/RK43i8Ezd41EqdGIU4ld7nGF8OQ14oc/we5rEntLCUa7+jrn3nn83BosfwZA0wb4pon2o8iQ==", + "dev": true }, "ajv-keywords": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.4.1.tgz", - "integrity": "sha512-RO1ibKvd27e6FEShVFfPALuHI3WjSVNeK5FIsmme/LYRNxjKuNj+Dt7bucLa6NdSv3JcVTyMlm9kGR84z1XpaQ==" + "integrity": "sha512-RO1ibKvd27e6FEShVFfPALuHI3WjSVNeK5FIsmme/LYRNxjKuNj+Dt7bucLa6NdSv3JcVTyMlm9kGR84z1XpaQ==", + "dev": true }, "ansi-escape-sequences": { "version": "4.1.0", @@ -802,7 +805,8 @@ "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", - "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", + "dev": true }, "buffer-xor": { "version": "1.0.3", @@ -826,6 +830,7 @@ "version": "12.0.3", "resolved": "https://registry.npmjs.org/cacache/-/cacache-12.0.3.tgz", "integrity": "sha512-kqdmfXEGFepesTuROHMs3MpFLWrPkSSpRqOw80RCflZXy/khxaArvFrQ7uJxSUduzAufc6G0g1VUCOZXxWavPw==", + "dev": true, "requires": { "bluebird": "^3.5.5", "chownr": "^1.1.1", @@ -847,12 +852,14 @@ "bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", - "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==", + "dev": true }, "glob": { "version": "7.1.6", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -1227,7 +1234,8 @@ "commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", - "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true }, "common-sequence": { "version": "1.0.2", @@ -1238,7 +1246,8 @@ "commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", - "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=" + "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=", + "dev": true }, "compare-versions": { "version": "3.5.1", @@ -1260,6 +1269,7 @@ "version": "1.6.2", "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "dev": true, "requires": { "buffer-from": "^1.0.0", "inherits": "^2.0.3", @@ -1306,6 +1316,7 @@ "version": "1.0.5", "resolved": "https://registry.npmjs.org/copy-concurrently/-/copy-concurrently-1.0.5.tgz", "integrity": "sha512-f2domd9fsVDFtaFcbaRZuYXwtdmnzqbADSwhSWYxYB/Q8zsdUUFMXVRwXGDMWmbEzAn1kdRrtI1T/KTFOL4X2A==", + "dev": true, "requires": { "aproba": "^1.1.1", "fs-write-stream-atomic": "^1.0.8", @@ -1398,7 +1409,8 @@ "cyclist": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/cyclist/-/cyclist-1.0.1.tgz", - "integrity": "sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=" + "integrity": "sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=", + "dev": true }, "debug": { "version": "4.1.1", @@ -1593,6 +1605,7 @@ "version": "3.7.1", "resolved": "https://registry.npmjs.org/duplexify/-/duplexify-3.7.1.tgz", "integrity": "sha512-07z8uv2wMyS51kKhD1KsdXJg5WQ6t93RneqRxUHnskXVtlYYkLqM0gqStQZ3pj073g687jPCHrqNfCzawLYh5g==", + "dev": true, "requires": { "end-of-stream": "^1.0.0", "inherits": "^2.0.1", @@ -1656,6 +1669,7 @@ "version": "0.1.7", "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz", "integrity": "sha512-MfrRBDWzIWifgq6tJj60gkAwtLNb6sQPlcFrSOflcP1aFmmruKQ2wRnze/8V6kgyz7H3FF8Npzv78mZ7XLLflg==", + "dev": true, "requires": { "prr": "~1.0.1" } @@ -1888,17 +1902,20 @@ "fast-deep-equal": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz", - "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=" + "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=", + "dev": true }, "fast-json-stable-stringify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", - "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=" + "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=", + "dev": true }, "figgy-pudding": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/figgy-pudding/-/figgy-pudding-3.5.1.tgz", - "integrity": "sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==" + "integrity": "sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==", + "dev": true }, "file-set": { "version": "2.0.1", @@ -1940,6 +1957,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-2.1.0.tgz", "integrity": "sha512-Tq6PixE0w/VMFfCgbONnkiQIVol/JJL7nRMi20fqzA4NRs9AfeqMGeRdPi3wIhYkxjeBaWh2rxwapn5Tu3IqOQ==", + "dev": true, "requires": { "commondir": "^1.0.1", "make-dir": "^2.0.0", @@ -1967,6 +1985,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", + "dev": true, "requires": { "locate-path": "^3.0.0" } @@ -2092,6 +2111,7 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/flush-write-stream/-/flush-write-stream-1.1.1.tgz", "integrity": "sha512-3Z4XhFZ3992uIq0XOqb9AreonueSYphE6oYbpt5+3u06JWklbsPkNv3ZKkP9Bz/r+1MWCaMoSQ28P85+1Yc77w==", + "dev": true, "requires": { "inherits": "^2.0.3", "readable-stream": "^2.3.6" @@ -2116,6 +2136,7 @@ "version": "2.3.0", "resolved": "https://registry.npmjs.org/from2/-/from2-2.3.0.tgz", "integrity": "sha1-i/tVAr3kpNNs/e6gB/zKIdfjgq8=", + "dev": true, "requires": { "inherits": "^2.0.1", "readable-stream": "^2.0.0" @@ -2146,6 +2167,7 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.10.tgz", "integrity": "sha1-tH31NJPvkR33VzHnCp3tAYnbQMk=", + "dev": true, "requires": { "graceful-fs": "^4.1.2", "iferr": "^0.1.5", @@ -2821,7 +2843,8 @@ "graceful-fs": { "version": "4.1.15", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", - "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" + "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==", + "dev": true }, "handlebars": { "version": "4.5.3", @@ -2990,7 +3013,8 @@ "iferr": { "version": "0.1.5", "resolved": "https://registry.npmjs.org/iferr/-/iferr-0.1.5.tgz", - "integrity": "sha1-xg7taebY/bazEEofy8ocGS3FtQE=" + "integrity": "sha1-xg7taebY/bazEEofy8ocGS3FtQE=", + "dev": true }, "ignore-walk": { "version": "3.0.3", @@ -3014,12 +3038,14 @@ "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=" + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=", + "dev": true }, "infer-owner": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/infer-owner/-/infer-owner-1.0.4.tgz", - "integrity": "sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==" + "integrity": "sha512-IClj+Xz94+d7irH5qRyfJonOdfTzuDaifE6ZPWfx0N0+/ATZCbuTPq2prFl526urkQd90WyUKIh1DfBQ2hMz9A==", + "dev": true }, "inflight": { "version": "1.0.6", @@ -3196,7 +3222,8 @@ "is-wsl": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-wsl/-/is-wsl-1.1.0.tgz", - "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=" + "integrity": "sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=", + "dev": true }, "isarray": { "version": "1.0.0", @@ -3331,7 +3358,8 @@ "json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", - "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" + "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", + "dev": true }, "json5": { "version": "1.0.1", @@ -3404,6 +3432,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", + "dev": true, "requires": { "p-locate": "^3.0.0", "path-exists": "^3.0.0" @@ -3473,6 +3502,7 @@ "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", "integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==", + "dev": true, "requires": { "yallist": "^3.0.2" } @@ -3481,6 +3511,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz", "integrity": "sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==", + "dev": true, "requires": { "pify": "^4.0.1", "semver": "^5.6.0" @@ -3659,6 +3690,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/mississippi/-/mississippi-3.0.0.tgz", "integrity": "sha512-x471SsVjUtBRtcvd4BzKE9kFC+/2TeWgKCgw0bZcw1b9l2X3QX5vCWgF+KaZaYm87Ss//rHnWryupDrgLvmSkA==", + "dev": true, "requires": { "concat-stream": "^1.5.0", "duplexify": "^3.4.2", @@ -3717,6 +3749,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", "integrity": "sha1-viwAX9oy4LKa8fBdfEszIUxwH5I=", + "dev": true, "requires": { "aproba": "^1.1.1", "copy-concurrently": "^1.0.0", @@ -4081,6 +4114,7 @@ "version": "2.2.1", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", + "dev": true, "requires": { "p-try": "^2.0.0" } @@ -4089,6 +4123,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", + "dev": true, "requires": { "p-limit": "^2.0.0" } @@ -4096,7 +4131,8 @@ "p-try": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", + "dev": true }, "pako": { "version": "1.0.10", @@ -4108,6 +4144,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/parallel-transform/-/parallel-transform-1.2.0.tgz", "integrity": "sha512-P2vSmIu38uIlvdcU7fDkyrxj33gTUy/ABO5ZUbGowxNCopBq/OoD42bP4UmMrJoPyk4Uqf0mu3mtWBhHCZD8yg==", + "dev": true, "requires": { "cyclist": "^1.0.1", "inherits": "^2.0.3", @@ -4155,7 +4192,8 @@ "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true }, "path-is-absolute": { "version": "1.0.1", @@ -4196,12 +4234,14 @@ "pify": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", - "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==" + "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==", + "dev": true }, "pkg-dir": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-3.0.0.tgz", "integrity": "sha512-/E57AYkoeQ25qkxMj5PBOVgF8Kiu/h7cYS30Z5+R7WaiCCBfLq58ZI/dSeaEKb9WVJV5n/03QwrN3IeWIFllvw==", + "dev": true, "requires": { "find-up": "^3.0.0" } @@ -4279,12 +4319,14 @@ "promise-inflight": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz", - "integrity": "sha1-mEcocL8igTL8vdhoEputEsPAKeM=" + "integrity": "sha1-mEcocL8igTL8vdhoEputEsPAKeM=", + "dev": true }, "prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", - "integrity": "sha1-0/wRS6BplaRexok/SEzrHXj19HY=" + "integrity": "sha1-0/wRS6BplaRexok/SEzrHXj19HY=", + "dev": true }, "public-encrypt": { "version": "4.0.3", @@ -4313,6 +4355,7 @@ "version": "1.5.1", "resolved": "https://registry.npmjs.org/pumpify/-/pumpify-1.5.1.tgz", "integrity": "sha512-oClZI37HvuUJJxSKKrC17bZ9Cu0ZYhEAGPsPUy9KlMUmv9dKX2o77RUmq7f3XjIxbwyGwYzbzQ1L2Ks8sIradQ==", + "dev": true, "requires": { "duplexify": "^3.6.0", "inherits": "^2.0.3", @@ -4323,6 +4366,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/pump/-/pump-2.0.1.tgz", "integrity": "sha512-ruPMNRkN3MHP1cWJc9OWr+T/xDP0jhXYCLfJcBuX54hhfIBnaQmAUMfDcG4DM5UMWByBbJY69QSphm3jtDKIkA==", + "dev": true, "requires": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -4333,7 +4377,8 @@ "punycode": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", + "dev": true }, "querystring": { "version": "0.2.0", @@ -4718,6 +4763,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/run-queue/-/run-queue-1.0.3.tgz", "integrity": "sha1-6Eg5bwV9Ij8kOGkkYY4laUFh7Ec=", + "dev": true, "requires": { "aproba": "^1.1.1" } @@ -4770,6 +4816,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-1.0.0.tgz", "integrity": "sha512-i27Mic4KovM/lnGsy8whRCHhc7VicJajAjTrYg11K9zfZXnYIt4k5F+kZkwjnrhKzLic/HLU4j11mjsz2G/75g==", + "dev": true, "requires": { "ajv": "^6.1.0", "ajv-errors": "^1.0.0", @@ -4784,7 +4831,8 @@ "serialize-javascript": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-2.1.2.tgz", - "integrity": "sha512-rs9OggEUF0V4jUSecXazOYsLfu7OGK2qIn3c7IPBiffz32XniEp/TX9Xmc9LQfK2nQ2QKHvZ2oygKUGU0lG4jQ==" + "integrity": "sha512-rs9OggEUF0V4jUSecXazOYsLfu7OGK2qIn3c7IPBiffz32XniEp/TX9Xmc9LQfK2nQ2QKHvZ2oygKUGU0lG4jQ==", + "dev": true }, "set-blocking": { "version": "2.0.0", @@ -5020,12 +5068,14 @@ "source-list-map": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.1.tgz", - "integrity": "sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==" + "integrity": "sha512-qnQ7gVMxGNxsiL4lEuJwe/To8UnK7fAnmbGEEH8RpLouuKbeEm0lhbQVFIrNSuB+G7tVrAlVsZgETT5nljf+Iw==", + "dev": true }, "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true }, "source-map-resolve": { "version": "0.5.2", @@ -5044,6 +5094,7 @@ "version": "0.5.16", "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.16.tgz", "integrity": "sha512-efyLRJDr68D9hBBNIPWFjhpFzURh+KJykQwvMyW5UiZzYwoF6l4YMMDIJJEyFWxWCqfyxLzz6tSfUFR+kXXsVQ==", + "dev": true, "requires": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" @@ -5084,6 +5135,7 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/ssri/-/ssri-6.0.1.tgz", "integrity": "sha512-3Wge10hNcT1Kur4PDFwEieXSCMCJs/7WvSACcrMYrNp+b8kDL1/0wJch5Ni2WrtwEa2IO8OsVfeKIciKCDx/QA==", + "dev": true, "requires": { "figgy-pudding": "^3.5.1" } @@ -5143,6 +5195,7 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/stream-each/-/stream-each-1.2.3.tgz", "integrity": "sha512-vlMC2f8I2u/bZGqkdfLQW/13Zihpej/7PmSiMQsbYddxuTsJp8vRe2x2FvVExZg7FaOds43ROAuFJwPR4MTZLw==", + "dev": true, "requires": { "end-of-stream": "^1.1.0", "stream-shift": "^1.0.0" @@ -5164,7 +5217,8 @@ "stream-shift": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/stream-shift/-/stream-shift-1.0.1.tgz", - "integrity": "sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==" + "integrity": "sha512-AiisoFqQ0vbGcZgQPY1cdP2I76glaVA/RauYR4G4thNFgkTqr90yXTo4LYX60Jl+sIlPNHHdGSwo01AvbKUSVQ==", + "dev": true }, "stream-via": { "version": "1.0.4", @@ -5320,6 +5374,7 @@ "version": "4.4.3", "resolved": "https://registry.npmjs.org/terser/-/terser-4.4.3.tgz", "integrity": "sha512-0ikKraVtRDKGzHrzkCv5rUNDzqlhmhowOBqC0XqUHFpW+vJ45+20/IFBcebwKfiS2Z9fJin6Eo+F1zLZsxi8RA==", + "dev": true, "requires": { "commander": "^2.20.0", "source-map": "~0.6.1", @@ -5330,6 +5385,7 @@ "version": "1.4.3", "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-1.4.3.tgz", "integrity": "sha512-QMxecFz/gHQwteWwSo5nTc6UaICqN1bMedC5sMtUc7y3Ha3Q8y6ZO0iCR8pq4RJC8Hjf0FEPEHZqcMB/+DFCrA==", + "dev": true, "requires": { "cacache": "^12.0.2", "find-cache-dir": "^2.1.0", @@ -5367,6 +5423,7 @@ "version": "2.0.5", "resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz", "integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==", + "dev": true, "requires": { "readable-stream": "~2.3.6", "xtend": "~4.0.1" @@ -5503,7 +5560,8 @@ "typedarray": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", - "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", + "dev": true }, "typescript": { "version": "3.7.2", @@ -5556,6 +5614,7 @@ "version": "1.1.1", "resolved": "https://registry.npmjs.org/unique-filename/-/unique-filename-1.1.1.tgz", "integrity": "sha512-Vmp0jIp2ln35UTXuryvjzkjGdRyf9b2lTXuSYUiPmzRcl3FDtYqAwOnTJkAngD9SWhnoJzDbTKwaOrZ+STtxNQ==", + "dev": true, "requires": { "unique-slug": "^2.0.0" } @@ -5564,6 +5623,7 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/unique-slug/-/unique-slug-2.0.2.tgz", "integrity": "sha512-zoWr9ObaxALD3DOPfjPSqxt4fnZiWblxHIgeWqW8x7UqDzEtHEQLzji2cuJYQFCU6KmoJikOYAZlrTHHebjx2w==", + "dev": true, "requires": { "imurmurhash": "^0.1.4" } @@ -5618,6 +5678,7 @@ "version": "4.2.2", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", "integrity": "sha512-KY9Frmirql91X2Qgjry0Wd4Y+YTdrdZheS8TFwvkbLWf/G5KNJDCh6pKL5OZctEW4+0Baa5idK2ZQuELRwPznQ==", + "dev": true, "requires": { "punycode": "^2.1.0" } @@ -5907,6 +5968,7 @@ "version": "1.4.3", "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-1.4.3.tgz", "integrity": "sha512-lgTS3Xhv1lCOKo7SA5TjKXMjpSM4sBjNV5+q2bqesbSPs5FjGmU6jjtBSkX9b4qW87vDIsCIlUPOEhbZrMdjeQ==", + "dev": true, "requires": { "source-list-map": "^2.0.0", "source-map": "~0.6.1" @@ -5962,6 +6024,7 @@ "version": "1.7.0", "resolved": "https://registry.npmjs.org/worker-farm/-/worker-farm-1.7.0.tgz", "integrity": "sha512-rvw3QTZc8lAxyVrqcSGVm5yP/IJ2UcB3U0graE3LCFoZ0Yn2x4EoVSqJKdB/T5M+FLcRPjz4TDacRf3OCfNUzw==", + "dev": true, "requires": { "errno": "~0.1.7" } @@ -6025,12 +6088,14 @@ "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=", + "dev": true }, "y18n": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", - "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==" + "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==", + "dev": true }, "yallist": { "version": "3.0.3", diff --git a/src/devices/device.ts b/src/devices/device.ts index ce6f841a..ddb0222d 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -19,6 +19,7 @@ export class Device extends EventEmitter { private _modeMap: {[event: string]: number} = {}; private _isWeDo2SmartHub: boolean; + private _isVirtualPort: boolean = false; constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(); diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index d9ace2d3..928b84a1 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -42,7 +42,6 @@ const debug = Debug("basehub"); export class BaseHub extends EventEmitter { protected _attachedDevices: {[portId: number]: Device} = {}; - // protected _virtualPorts: {[portName: string]: Port} = {}; protected _name: string = ""; protected _firmwareVersion: string = "0.0.00.0000"; @@ -50,20 +49,21 @@ export class BaseHub extends EventEmitter { protected _primaryMACAddress: string = "00:00:00:00:00:00"; protected _batteryLevel: number = 100; protected _rssi: number = -60; + protected _portMap: {[portName: string]: number} = {}; protected _bleDevice: IBLEAbstraction; private _type: Consts.HubType; - private _portMap: {[portName: string]: number} = {}; - private _attachCallbacks: ((device: Device) => boolean)[] = []; + private _virtualPorts: number[] = []; + private _attachCallbacks: Array<((device: Device) => boolean)> = []; - constructor (device: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { + constructor (bleDevice: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { super(); - this.setMaxListeners(20); // Technic Medium Hub has 9 built in devices + 4 external ports. Node.js throws a warning after 11 attached event listeners. + this.setMaxListeners(23); // Technic Medium Hub has 9 built in devices + 4 external ports. Node.js throws a warning after 10 attached event listeners. this._type = type; - this._bleDevice = device; + this._bleDevice = bleDevice; this._portMap = portMap; - device.on("disconnect", () => { + bleDevice.on("disconnect", () => { /** * Emits when the hub is disconnected. * @event Hub#disconnect @@ -249,6 +249,11 @@ export class BaseHub extends EventEmitter { } + public isPortVirtual (portId: number) { + return (this._virtualPorts.indexOf(portId) > -1); + } + + /** * Sleep a given amount of time. * diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 9d27ae74..4f4bbc4f 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -94,20 +94,28 @@ export class LPF2Hub extends BaseHub { } - // protected _combinePorts (port: string, type: number) { - // if (!this._ports[port]) { - // return; - // } - // const portObj = this._portLookup(port); - // if (portObj) { - // Object.keys(this._ports).forEach((id) => { - // if (this._ports[id].type === type && this._ports[id].value !== portObj.value && !this._virtualPorts[`${portObj.value < this._ports[id].value ? portObj.id : this._ports[id].id}${portObj.value > this._ports[id].value ? portObj.id : this._ports[id].id}`]) { - // debug("Combining ports", portObj.value < this._ports[id].value ? portObj.id : id, portObj.value > this._ports[id].value ? portObj.id : id); - // this.send(Buffer.from([0x61, 0x01, portObj.value < this._ports[id].value ? portObj.value : this._ports[id].value, portObj.value > this._ports[id].value ? portObj.value : this._ports[id].value]), Consts.BLECharacteristic.LPF2_ALL); - // } - // }); - // } - // } + public createVirtualPort (firstPortName: string, secondPortName: string) { + const firstPortId = this._portMap[firstPortName]; + if (!firstPortId) { + throw new Error(`Port ${firstPortName} does not exist on this hub`); + } + const secondPortId = this._portMap[secondPortName]; + if (!secondPortId) { + throw new Error(`Port ${secondPortName} does not exist on this hub`); + } + const firstDevice = this._getDeviceByPortId(firstPortId); + if (!firstDevice) { + throw new Error(`Port ${firstPortName} does not have an attached device`); + } + const secondDevice = this._getDeviceByPortId(secondPortId); + if (!secondDevice) { + throw new Error(`Port ${secondPortName} does not have an attached device`); + } + if (firstDevice.type !== secondDevice.type) { + throw new Error(`Both devices must be of the same type to create a virtual port`); + } + this.send(Buffer.from([0x61, 0x01, firstPortId, secondPortId]), Consts.BLECharacteristic.LPF2_ALL); + } protected _checkFirmware (version: string) { diff --git a/tslint.json b/tslint.json index c727815d..21cbfd60 100644 --- a/tslint.json +++ b/tslint.json @@ -13,7 +13,7 @@ "prefer-for-of": false, "typedef": true, "no-console": false, - "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"], + "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore", "require-const-for-all-caps", "allow-pascal-case"], "object-literal-key-quotes": false, "object-literal-sort-keys": false, "no-string-literal": false, From 875293a742b8dc2bf18a5798b94905a2edde6927 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 8 Jan 2020 15:10:54 -0800 Subject: [PATCH 43/62] Fixed linting errors and removed namespaces --- package-lock.json | 355 ++++++++++-------- package.json | 12 +- src/devices/colordistancesensor.ts | 32 +- src/devices/currentsensor.ts | 42 +-- src/devices/device.ts | 4 +- src/devices/duplotrainbasecolorsensor.ts | 20 +- src/devices/duplotrainbasespeaker.ts | 12 +- src/devices/duplotrainbasespeedometer.ts | 20 +- src/devices/hubled.ts | 16 +- src/devices/motionsensor.ts | 20 +- src/devices/movehubtiltsensor.ts | 20 +- src/devices/remotecontrolbutton.ts | 32 +- src/devices/tachomotor.ts | 20 +- .../technicmediumhubaccelerometersensor.ts | 20 +- src/devices/technicmediumhubgyrosensor.ts | 20 +- src/devices/technicmediumhubtiltsensor.ts | 20 +- src/devices/tiltsensor.ts | 20 +- src/devices/voltagesensor.ts | 54 ++- src/hubs/basehub.ts | 9 +- src/hubs/duplotrainbase.ts | 17 +- src/hubs/hub.ts | 20 +- src/hubs/movehub.ts | 26 +- src/hubs/remotecontrol.ts | 20 +- src/hubs/technicmediumhub.ts | 30 +- src/hubs/wedo2smarthub.ts | 22 +- 25 files changed, 417 insertions(+), 466 deletions(-) diff --git a/package-lock.json b/package-lock.json index 412d11fa..5d84ef05 100644 --- a/package-lock.json +++ b/package-lock.json @@ -68,15 +68,15 @@ "dev": true }, "@types/node": { - "version": "12.12.7", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.7.tgz", - "integrity": "sha512-E6Zn0rffhgd130zbCbAr/JdXfXkoOUFAKNs/rF8qnafSJ8KYaA/j3oz7dcwal+lYjLA7xvdd5J4wdYpCTlP8+w==", + "version": "13.1.5", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.1.5.tgz", + "integrity": "sha512-wupvfmtbqRJzjCm1H2diy7wo31Gn1OzvqoxCfQuKM9eSecogzP0WTlrjdq7cf7jgSO2ZX6hxwgRPR8Wt7FA22g==", "dev": true }, "@types/web-bluetooth": { - "version": "0.0.4", - "resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.4.tgz", - "integrity": "sha512-C+BgVBBGY9c6ixcc5PsKAmGaCy3bswZ5zx/AWIAik9dgFuBkFsXBA3ze69jJi05xdZQ99QkfBSVIX6zl+6Tmww==", + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@types/web-bluetooth/-/web-bluetooth-0.0.5.tgz", + "integrity": "sha512-gaSAxNePCVJUR1a+4jKMaukjEzE4XuJd9fzSx/mAy6BrVoJXnACmfaBkaXU/K0frxJVfHvMDJx96hbNt4NXd9Q==", "dev": true }, "@webassemblyjs/ast": { @@ -274,9 +274,9 @@ "optional": true }, "acorn": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.3.0.tgz", - "integrity": "sha512-/czfa8BwS88b9gWQVhc8eknunSA2DoJpJyTQkhheIf5E48u1N0R4q/YxxsAeqRrmK9TQ/uYfgLDfZo91UlANIA==", + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.4.0.tgz", + "integrity": "sha512-gac8OEcQ2Li1dxIEWGZzsp2BitJxwkwcOm0zHAJLcPJaVvm58FRnk6RkuLRpU1EujipU2ZFODv2P9DLMfnV8mw==", "dev": true }, "ajv": { @@ -305,7 +305,7 @@ }, "ansi-escape-sequences": { "version": "4.1.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/ansi-escape-sequences/_attachments/ansi-escape-sequences-4.1.0.tgz", + "resolved": "https://registry.npmjs.org/ansi-escape-sequences/-/ansi-escape-sequences-4.1.0.tgz", "integrity": "sha512-dzW9kHxH011uBsidTXd14JXgzye/YLb2LzeKZ4bsgl/Knwx8AtbSFkkGxagdNOoh0DlqHCmfiEjWKBaqjOanVw==", "dev": true, "requires": { @@ -502,9 +502,9 @@ "dev": true }, "array-back": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/array-back/-/array-back-4.0.0.tgz", - "integrity": "sha512-ylVYjv5BzoWXWO7e6fWrzjqzgxmUPWdQrHxgzo/v1EaYXfw6+6ipRdIr7KryAGnVHG08O1Yfpchuv0+YhjPL+Q==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-4.0.1.tgz", + "integrity": "sha512-Z/JnaVEXv+A9xabHzN43FiiiWEE7gPCRXMrVmRm00tWbjZRul1iHm7ECzlyNq1p4a4ATXz+G9FJ3GqGOkOV3fg==", "dev": true }, "array-uniq": { @@ -889,25 +889,14 @@ } }, "cache-point": { - "version": "0.4.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/c/cache-point/_attachments/cache-point-0.4.1.tgz", - "integrity": "sha512-4TgWfe9SF+bUy5cCql8gWHqKNrviufNwSYxLjf2utB0pY4+bdcuFwMmY1hDB+67Gz/L1vmhFNhePAjJTFBtV+Q==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/cache-point/-/cache-point-1.0.0.tgz", + "integrity": "sha512-ZqrZp9Hi5Uq7vfSGmNP2bUT/9DzZC2Y/GXjHB8rUJN1a+KLmbV05+vxHipNsg8+CSVgjcVVzLV8VZms6w8ZeRw==", "dev": true, "requires": { - "array-back": "^2.0.0", + "array-back": "^4.0.0", "fs-then-native": "^2.0.0", - "mkdirp2": "^1.0.3" - }, - "dependencies": { - "array-back": { - "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-2.0.0.tgz", - "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", - "dev": true, - "requires": { - "typical": "^2.6.1" - } - } + "mkdirp2": "^1.0.4" } }, "camelcase": { @@ -1125,7 +1114,7 @@ }, "collect-all": { "version": "1.0.3", - "resolved": "https://npm-registry.rancher.sie.sony.com/c/collect-all/_attachments/collect-all-1.0.3.tgz", + "resolved": "https://registry.npmjs.org/collect-all/-/collect-all-1.0.3.tgz", "integrity": "sha512-0y0rBgoX8IzIjBAUnO73SEtSb4Mhk3IoceWJq5zZSxb9mWORhWH8xLYo4EDSOE1jRBk1LhmfjqWFFt10h/+MEA==", "dev": true, "requires": { @@ -1160,7 +1149,7 @@ }, "command-line-args": { "version": "5.1.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/c/command-line-args/_attachments/command-line-args-5.1.1.tgz", + "resolved": "https://registry.npmjs.org/command-line-args/-/command-line-args-5.1.1.tgz", "integrity": "sha512-hL/eG8lrll1Qy1ezvkant+trihbGnaKaeEjj6Scyr3DN+RC7iQ5Rz84IeLERfAWDGo0HBSNAakczwgCilDXnWg==", "dev": true, "requires": { @@ -1178,7 +1167,7 @@ }, "typical": { "version": "4.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/t/typical/_attachments/typical-4.0.0.tgz", + "resolved": "https://registry.npmjs.org/typical/-/typical-4.0.0.tgz", "integrity": "sha512-VAH4IvQ7BDFYglMd7BPRDfLgxZZX4O4TFcRDA6EN5X7erNJJq+McIEp8np9aVtxrCJ6qx4GTYVfOWNjcqwZgRw==", "dev": true } @@ -1186,7 +1175,7 @@ }, "command-line-tool": { "version": "0.8.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/c/command-line-tool/_attachments/command-line-tool-0.8.0.tgz", + "resolved": "https://registry.npmjs.org/command-line-tool/-/command-line-tool-0.8.0.tgz", "integrity": "sha512-Xw18HVx/QzQV3Sc5k1vy3kgtOeGmsKIqwtFFoyjI4bbcpSgnw2CWVULvtakyw4s6fhyAdI6soQQhXc2OzJy62g==", "dev": true, "requires": { @@ -1199,7 +1188,7 @@ "dependencies": { "array-back": { "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz", "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", "dev": true, "requires": { @@ -1210,7 +1199,7 @@ }, "command-line-usage": { "version": "4.1.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/c/command-line-usage/_attachments/command-line-usage-4.1.0.tgz", + "resolved": "https://registry.npmjs.org/command-line-usage/-/command-line-usage-4.1.0.tgz", "integrity": "sha512-MxS8Ad995KpdAC0Jopo/ovGIroV/m0KHwzKfXxKag6FHOkGsH8/lv5yjgablcRxCJJC0oJeUMuO/gmaq+Wq46g==", "dev": true, "requires": { @@ -1222,7 +1211,7 @@ "dependencies": { "array-back": { "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz", "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", "dev": true, "requires": { @@ -1238,9 +1227,9 @@ "dev": true }, "common-sequence": { - "version": "1.0.2", - "resolved": "https://npm-registry.rancher.sie.sony.com/c/common-sequence/_attachments/common-sequence-1.0.2.tgz", - "integrity": "sha1-MOB/P49vf5s97oVPILLTnu4Ibeg=", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/common-sequence/-/common-sequence-2.0.0.tgz", + "integrity": "sha512-f0QqPLpRTgMQn/pQIynf+SdE73Lw5Q1jn4hjirHLgH/NJ71TiHjXusV16BmOyuK5rRQ1W2f++II+TFZbQOh4hA==", "dev": true }, "commondir": { @@ -1279,7 +1268,7 @@ }, "config-master": { "version": "3.1.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/c/config-master/_attachments/config-master-3.1.0.tgz", + "resolved": "https://registry.npmjs.org/config-master/-/config-master-3.1.0.tgz", "integrity": "sha1-ZnZjWQUFooO/JqSE1oSJ10xUhdo=", "dev": true, "requires": { @@ -1288,7 +1277,7 @@ "dependencies": { "walk-back": { "version": "2.0.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/w/walk-back/_attachments/walk-back-2.0.1.tgz", + "resolved": "https://registry.npmjs.org/walk-back/-/walk-back-2.0.1.tgz", "integrity": "sha1-VU4qnYdPrEeoywBr9EwvDEmYoKQ=", "dev": true } @@ -1494,9 +1483,9 @@ "optional": true }, "des.js": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/des.js/-/des.js-1.0.0.tgz", - "integrity": "sha1-wHTS4qpqipoH29YfmhXCzYPsjsw=", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/des.js/-/des.js-1.0.1.tgz", + "integrity": "sha512-Q0I4pfFrv2VPd34/vfLrFOoRmlYj3OV50i7fskps1jZWK1kApMWWT9G6RRUeYedLcBDIhnSDaUvJMb3AhUlaEA==", "dev": true, "requires": { "inherits": "^2.0.1", @@ -1533,29 +1522,29 @@ } }, "dmd": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/dmd/-/dmd-4.0.4.tgz", - "integrity": "sha512-ZbHUPKUp5Tl8nVVMZw8rc/MQmFVKusvfR10X/lPAXjBUc/LRW7AaXnYrK2LnVIPfTGEw7T6OmsxkvNRX7GnjIQ==", + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/dmd/-/dmd-4.0.6.tgz", + "integrity": "sha512-7ZYAnFQ6jGm4SICArwqNPylJ83PaOdPTAkds3Z/s1ueFqSc5ilJ2F0b7uP+35W1PUbemH++gn5/VlC3KwEgiHQ==", "dev": true, "requires": { - "array-back": "^4.0.0", - "cache-point": "^0.4.1", - "common-sequence": "^1.0.2", - "file-set": "^2.0.1", - "handlebars": "^4.2.0", + "array-back": "^4.0.1", + "cache-point": "^1.0.0", + "common-sequence": "^2.0.0", + "file-set": "^3.0.0", + "handlebars": "^4.5.3", "marked": "^0.7.0", "object-get": "^2.1.0", - "reduce-flatten": "^2.0.0", + "reduce-flatten": "^3.0.0", "reduce-unique": "^2.0.1", "reduce-without": "^1.0.1", "test-value": "^3.0.0", - "walk-back": "^3.0.1" + "walk-back": "^4.0.0" }, "dependencies": { "reduce-flatten": { - "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/r/reduce-flatten/_attachments/reduce-flatten-2.0.0.tgz", - "integrity": "sha512-EJ4UNY/U1t2P/2k6oqotuX2Cc3T6nxJwsM0N0asT7dhrtH1ltUxDn4NalSYmPE2rCkVpcf/X6R0wDwcFpzhd4w==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-3.0.0.tgz", + "integrity": "sha512-eczl8wAYBxJ6Egl6I1ECIF+8z6sHu+KE7BzaEDZTpPXKXfy9SUDQlVYwkRcNTjJLC3Iakxbhss50KuT/R6SYfg==", "dev": true } } @@ -1614,9 +1603,9 @@ } }, "elliptic": { - "version": "6.5.1", - "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.1.tgz", - "integrity": "sha512-xvJINNLbTeWQjrl6X+7eQCrIy/YPv5XCpKW6kB5mKvtnGILoLDcySuwomfdzt0BMdLNVnuRNTuzKNHj0bva1Cg==", + "version": "6.5.2", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.2.tgz", + "integrity": "sha512-f4x70okzZbIQl/NSRLkI/+tteV/9WqL98zx+SQ69KbXxmVrmjwsNUPn/gYJJ0sHvEak24cZgHIPegRePAtA/xw==", "dev": true, "requires": { "bn.js": "^4.4.0", @@ -1718,9 +1707,9 @@ "dev": true }, "events": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/events/-/events-3.0.0.tgz", - "integrity": "sha512-Dc381HFWJzEOhQ+d8pkNon++bk9h6cdAoAj4iE6Q4y6xgTzySWXlKn05/TVNpjnfRqi/X0EpJEJohPjNI3zpVA==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/events/-/events-3.1.0.tgz", + "integrity": "sha512-Rv+u8MLHNOdMjTAFeT3nCjHn2aGlx435FP/sDHNaRhDEMwyI/aB22Kj2qIN8R0cw3z28psEQLYwxVKLsKrMgWg==", "dev": true }, "evp_bytestokey": { @@ -1906,9 +1895,9 @@ "dev": true }, "fast-json-stable-stringify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", - "integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I=", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", + "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==", "dev": true }, "figgy-pudding": { @@ -1918,22 +1907,27 @@ "dev": true }, "file-set": { - "version": "2.0.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/f/file-set/_attachments/file-set-2.0.1.tgz", - "integrity": "sha512-XgOUUpgR6FbbfYcniLw0qm1Am7PnNYIAkd+eXxRt42LiYhjaso0WiuQ+VmrNdtwotyM+cLCfZ56AZrySP3QnKA==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/file-set/-/file-set-3.0.0.tgz", + "integrity": "sha512-B/SdeSIeRv7VlOgIjtH3dkxMI+tEy5m+OeCXfAUsirBoVoY+bGtsmvmmTFPm/G23TBY4RiTtjpcgePCfwXRjqA==", "dev": true, "requires": { - "array-back": "^2.0.0", - "glob": "^7.1.3" + "array-back": "^4.0.0", + "glob": "^7.1.5" }, "dependencies": { - "array-back": { - "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-2.0.0.tgz", - "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", "dev": true, "requires": { - "typical": "^2.6.1" + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" } } } @@ -1966,7 +1960,7 @@ }, "find-replace": { "version": "3.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/f/find-replace/_attachments/find-replace-3.0.0.tgz", + "resolved": "https://registry.npmjs.org/find-replace/-/find-replace-3.0.0.tgz", "integrity": "sha512-6Tb2myMioCAgv5kfvP5/PkZZ/ntTpVK39fHY7WkWBgvbeE+VHd/tZuZ4mrC+bxh4cfOZeYKVPaJIZtZXV7GNCQ==", "dev": true, "requires": { @@ -2159,7 +2153,7 @@ }, "fs-then-native": { "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/f/fs-then-native/_attachments/fs-then-native-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/fs-then-native/-/fs-then-native-2.0.0.tgz", "integrity": "sha1-GaEk2U2QwiyOBF8ujdbr6jbUjGc=", "dev": true }, @@ -2181,14 +2175,15 @@ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "fsevents": { - "version": "1.2.9", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.9.tgz", - "integrity": "sha512-oeyj2H3EjjonWcFjD5NvZNE9Rqe4UW+nQBU2HNeKw0koVLEFIhtyETyAakeAM3de7Z/SW5kcA+fZUait9EApnw==", + "version": "1.2.11", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.11.tgz", + "integrity": "sha512-+ux3lx6peh0BpvY0JebGyZoiR4D+oYzdPZMKJwkZ+sFkNJzpL7tXc/wehS49gUAxg3tmMHPHZkA8JU2rhhgDHw==", "dev": true, "optional": true, "requires": { + "bindings": "^1.5.0", "nan": "^2.12.1", - "node-pre-gyp": "^0.12.0" + "node-pre-gyp": "*" }, "dependencies": { "abbrev": { @@ -2236,7 +2231,7 @@ } }, "chownr": { - "version": "1.1.1", + "version": "1.1.3", "bundled": true, "dev": true, "optional": true @@ -2266,7 +2261,7 @@ "optional": true }, "debug": { - "version": "4.1.1", + "version": "3.2.6", "bundled": true, "dev": true, "optional": true, @@ -2293,12 +2288,12 @@ "optional": true }, "fs-minipass": { - "version": "1.2.5", + "version": "1.2.7", "bundled": true, "dev": true, "optional": true, "requires": { - "minipass": "^2.2.1" + "minipass": "^2.6.0" } }, "fs.realpath": { @@ -2324,7 +2319,7 @@ } }, "glob": { - "version": "7.1.3", + "version": "7.1.6", "bundled": true, "dev": true, "optional": true, @@ -2353,7 +2348,7 @@ } }, "ignore-walk": { - "version": "3.0.1", + "version": "3.0.3", "bundled": true, "dev": true, "optional": true, @@ -2372,7 +2367,7 @@ } }, "inherits": { - "version": "2.0.3", + "version": "2.0.4", "bundled": true, "dev": true, "optional": true @@ -2414,7 +2409,7 @@ "optional": true }, "minipass": { - "version": "2.3.5", + "version": "2.9.0", "bundled": true, "dev": true, "optional": true, @@ -2424,12 +2419,12 @@ } }, "minizlib": { - "version": "1.2.1", + "version": "1.3.3", "bundled": true, "dev": true, "optional": true, "requires": { - "minipass": "^2.2.1" + "minipass": "^2.9.0" } }, "mkdirp": { @@ -2442,24 +2437,24 @@ } }, "ms": { - "version": "2.1.1", + "version": "2.1.2", "bundled": true, "dev": true, "optional": true }, "needle": { - "version": "2.3.0", + "version": "2.4.0", "bundled": true, "dev": true, "optional": true, "requires": { - "debug": "^4.1.0", + "debug": "^3.2.6", "iconv-lite": "^0.4.4", "sax": "^1.2.4" } }, "node-pre-gyp": { - "version": "0.12.0", + "version": "0.14.0", "bundled": true, "dev": true, "optional": true, @@ -2473,7 +2468,7 @@ "rc": "^1.2.7", "rimraf": "^2.6.1", "semver": "^5.3.0", - "tar": "^4" + "tar": "^4.4.2" } }, "nopt": { @@ -2487,13 +2482,22 @@ } }, "npm-bundled": { - "version": "1.0.6", + "version": "1.1.1", + "bundled": true, + "dev": true, + "optional": true, + "requires": { + "npm-normalize-package-bin": "^1.0.1" + } + }, + "npm-normalize-package-bin": { + "version": "1.0.1", "bundled": true, "dev": true, "optional": true }, "npm-packlist": { - "version": "1.4.1", + "version": "1.4.7", "bundled": true, "dev": true, "optional": true, @@ -2564,7 +2568,7 @@ "optional": true }, "process-nextick-args": { - "version": "2.0.0", + "version": "2.0.1", "bundled": true, "dev": true, "optional": true @@ -2605,7 +2609,7 @@ } }, "rimraf": { - "version": "2.6.3", + "version": "2.7.1", "bundled": true, "dev": true, "optional": true, @@ -2632,7 +2636,7 @@ "optional": true }, "semver": { - "version": "5.7.0", + "version": "5.7.1", "bundled": true, "dev": true, "optional": true @@ -2685,18 +2689,18 @@ "optional": true }, "tar": { - "version": "4.4.8", + "version": "4.4.13", "bundled": true, "dev": true, "optional": true, "requires": { "chownr": "^1.1.1", "fs-minipass": "^1.2.5", - "minipass": "^2.3.4", - "minizlib": "^1.1.1", + "minipass": "^2.8.6", + "minizlib": "^1.2.1", "mkdirp": "^0.5.0", "safe-buffer": "^5.1.2", - "yallist": "^3.0.2" + "yallist": "^3.0.3" } }, "util-deprecate": { @@ -2721,7 +2725,7 @@ "optional": true }, "yallist": { - "version": "3.0.3", + "version": "3.1.1", "bundled": true, "dev": true, "optional": true @@ -3304,13 +3308,13 @@ } }, "jsdoc-api": { - "version": "5.0.3", - "resolved": "https://registry.npmjs.org/jsdoc-api/-/jsdoc-api-5.0.3.tgz", - "integrity": "sha512-7F/FR1DCRmRFlyuccpeRwW/4H5GtUD9detREDO/gxLjyEaVfRdD1JDzwZ4tMg32f0jP97PCDTy9CdSr8mW0txQ==", + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/jsdoc-api/-/jsdoc-api-5.0.4.tgz", + "integrity": "sha512-1KMwLnfo0FyhF06TQKzqIm8BiY1yoMIGICxRdJHUjzskaHMzHMmpLlmNFgzoa4pAC8t1CDPK5jWuQTvv1pBsEQ==", "dev": true, "requires": { "array-back": "^4.0.0", - "cache-point": "^0.4.1", + "cache-point": "^1.0.0", "collect-all": "^1.0.3", "file-set": "^2.0.1", "fs-then-native": "^2.0.0", @@ -3318,6 +3322,35 @@ "object-to-spawn-args": "^1.1.1", "temp-path": "^1.0.0", "walk-back": "^3.0.1" + }, + "dependencies": { + "file-set": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/file-set/-/file-set-2.0.1.tgz", + "integrity": "sha512-XgOUUpgR6FbbfYcniLw0qm1Am7PnNYIAkd+eXxRt42LiYhjaso0WiuQ+VmrNdtwotyM+cLCfZ56AZrySP3QnKA==", + "dev": true, + "requires": { + "array-back": "^2.0.0", + "glob": "^7.1.3" + }, + "dependencies": { + "array-back": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz", + "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", + "dev": true, + "requires": { + "typical": "^2.6.1" + } + } + } + }, + "walk-back": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/walk-back/-/walk-back-3.0.1.tgz", + "integrity": "sha512-umiNB2qLO731Sxbp6cfZ9pwURJzTnftxE4Gc7hq8n/ehkuXC//s9F65IEIJA2ZytQZ1ZOsm/Fju4IWx0bivkUQ==", + "dev": true + } } }, "jsdoc-parse": { @@ -3335,18 +3368,18 @@ } }, "jsdoc-to-markdown": { - "version": "5.0.2", - "resolved": "https://registry.npmjs.org/jsdoc-to-markdown/-/jsdoc-to-markdown-5.0.2.tgz", - "integrity": "sha512-Rcs9/3+NO1odClVhLDk0lDNFe11RiYUHh/PnROT5QU2Fpad2zBESmJD+DcmeK7lg1jalFfGG1MKOGuJHs27jsA==", + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/jsdoc-to-markdown/-/jsdoc-to-markdown-5.0.3.tgz", + "integrity": "sha512-tQv5tBV0fTYidRQtE60lJKxE98mmuLcYuITFDKQiDPE9hGccpeEGUNFcVkInq1vigyuPnZmt79bQ8wv2GKjY0Q==", "dev": true, "requires": { - "array-back": "^4.0.0", + "array-back": "^4.0.1", "command-line-tool": "^0.8.0", "config-master": "^3.1.0", - "dmd": "^4.0.4", - "jsdoc-api": "^5.0.3", + "dmd": "^4.0.5", + "jsdoc-api": "^5.0.4", "jsdoc-parse": "^4.0.1", - "walk-back": "^3.0.1" + "walk-back": "^4.0.0" } }, "json-parse-better-errors": { @@ -3446,7 +3479,7 @@ }, "lodash.camelcase": { "version": "4.3.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/l/lodash.camelcase/_attachments/lodash.camelcase-4.3.0.tgz", + "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", "integrity": "sha1-soqmKIorn8ZRA1x3EfZathkDMaY=", "dev": true }, @@ -3482,19 +3515,19 @@ }, "lodash.omit": { "version": "4.5.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/l/lodash.omit/_attachments/lodash.omit-4.5.0.tgz", + "resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz", "integrity": "sha1-brGa5aHuHdnfC5aeZs4Lf6MLXmA=", "dev": true }, "lodash.padend": { "version": "4.6.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/l/lodash.padend/_attachments/lodash.padend-4.6.1.tgz", + "resolved": "https://registry.npmjs.org/lodash.padend/-/lodash.padend-4.6.1.tgz", "integrity": "sha1-U8y6BH0G4VjTEfRdpiX05J5vFm4=", "dev": true }, "lodash.pick": { "version": "4.4.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/l/lodash.pick/_attachments/lodash.pick-4.4.0.tgz", + "resolved": "https://registry.npmjs.org/lodash.pick/-/lodash.pick-4.4.0.tgz", "integrity": "sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=", "dev": true }, @@ -3735,7 +3768,7 @@ }, "mkdirp2": { "version": "1.0.4", - "resolved": "https://npm-registry.rancher.sie.sony.com/m/mkdirp2/_attachments/mkdirp2-1.0.4.tgz", + "resolved": "https://registry.npmjs.org/mkdirp2/-/mkdirp2-1.0.4.tgz", "integrity": "sha512-Q2PKB4ZR4UPtjLl76JfzlgSCUZhSV1AXQgAZa1qt5RiaALFjP/CDrGvFBrOz7Ck6McPcwMAxTsJvWOUjOU8XMw==", "dev": true }, @@ -4007,13 +4040,13 @@ }, "object-get": { "version": "2.1.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/o/object-get/_attachments/object-get-2.1.0.tgz", + "resolved": "https://registry.npmjs.org/object-get/-/object-get-2.1.0.tgz", "integrity": "sha1-ciu9tgA576R8rTxtws5RqFwCxa4=", "dev": true }, "object-to-spawn-args": { "version": "1.1.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/o/object-to-spawn-args/_attachments/object-to-spawn-args-1.1.1.tgz", + "resolved": "https://registry.npmjs.org/object-to-spawn-args/-/object-to-spawn-args-1.1.1.tgz", "integrity": "sha1-d9qIJ/Bz0BHJ4bFz+JV4FHAkZ4U=", "dev": true }, @@ -4563,7 +4596,7 @@ }, "reduce-extract": { "version": "1.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/r/reduce-extract/_attachments/reduce-extract-1.0.0.tgz", + "resolved": "https://registry.npmjs.org/reduce-extract/-/reduce-extract-1.0.0.tgz", "integrity": "sha1-Z/I4W+2mUGG19fQxJmLosIDKFSU=", "dev": true, "requires": { @@ -4572,7 +4605,7 @@ "dependencies": { "array-back": { "version": "1.0.4", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-1.0.4.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-1.0.4.tgz", "integrity": "sha1-ZEun8JX3/898Q7Xw3DnTwfA8Bjs=", "dev": true, "requires": { @@ -4581,7 +4614,7 @@ }, "test-value": { "version": "1.1.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/t/test-value/_attachments/test-value-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/test-value/-/test-value-1.1.0.tgz", "integrity": "sha1-oJE29y7AQ9J8iTcHwrFZv6196T8=", "dev": true, "requires": { @@ -4593,19 +4626,19 @@ }, "reduce-flatten": { "version": "1.0.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/r/reduce-flatten/_attachments/reduce-flatten-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-1.0.1.tgz", "integrity": "sha1-JYx479FT3fk8tWEjf2EYTzaW4yc=", "dev": true }, "reduce-unique": { "version": "2.0.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/r/reduce-unique/_attachments/reduce-unique-2.0.1.tgz", + "resolved": "https://registry.npmjs.org/reduce-unique/-/reduce-unique-2.0.1.tgz", "integrity": "sha512-x4jH/8L1eyZGR785WY+ePtyMNhycl1N2XOLxhCbzZFaqF4AXjLzqSxa2UHgJ2ZVR/HHyPOvl1L7xRnW8ye5MdA==", "dev": true }, "reduce-without": { "version": "1.0.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/r/reduce-without/_attachments/reduce-without-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/reduce-without/-/reduce-without-1.0.1.tgz", "integrity": "sha1-aK0OrRGFXJo31OglbBW7+Hly/Iw=", "dev": true, "requires": { @@ -4614,7 +4647,7 @@ "dependencies": { "array-back": { "version": "1.0.4", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-1.0.4.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-1.0.4.tgz", "integrity": "sha1-ZEun8JX3/898Q7Xw3DnTwfA8Bjs=", "dev": true, "requires": { @@ -4623,7 +4656,7 @@ }, "test-value": { "version": "2.1.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/t/test-value/_attachments/test-value-2.1.0.tgz", + "resolved": "https://registry.npmjs.org/test-value/-/test-value-2.1.0.tgz", "integrity": "sha1-Edpv9nDzRxpztiXKTz/c97t0gpE=", "dev": true, "requires": { @@ -5045,7 +5078,7 @@ }, "sort-array": { "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/s/sort-array/_attachments/sort-array-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/sort-array/-/sort-array-2.0.0.tgz", "integrity": "sha1-OKnG2if9fRR7QuYFVPKBGHtN9HI=", "dev": true, "requires": { @@ -5056,7 +5089,7 @@ "dependencies": { "array-back": { "version": "1.0.4", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-1.0.4.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-1.0.4.tgz", "integrity": "sha1-ZEun8JX3/898Q7Xw3DnTwfA8Bjs=", "dev": true, "requires": { @@ -5173,7 +5206,7 @@ }, "stream-connect": { "version": "1.0.2", - "resolved": "https://npm-registry.rancher.sie.sony.com/s/stream-connect/_attachments/stream-connect-1.0.2.tgz", + "resolved": "https://registry.npmjs.org/stream-connect/-/stream-connect-1.0.2.tgz", "integrity": "sha1-GLyB8u2zW4tdmoAJIAqYUxRCipc=", "dev": true, "requires": { @@ -5182,7 +5215,7 @@ "dependencies": { "array-back": { "version": "1.0.4", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-1.0.4.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-1.0.4.tgz", "integrity": "sha1-ZEun8JX3/898Q7Xw3DnTwfA8Bjs=", "dev": true, "requires": { @@ -5222,7 +5255,7 @@ }, "stream-via": { "version": "1.0.4", - "resolved": "https://npm-registry.rancher.sie.sony.com/s/stream-via/_attachments/stream-via-1.0.4.tgz", + "resolved": "https://registry.npmjs.org/stream-via/-/stream-via-1.0.4.tgz", "integrity": "sha512-DBp0lSvX5G9KGRDTkR/R+a29H+Wk2xItOF+MpZLLNDWbEV9tGPnqLPxHEYjmiz8xGtJHRIqmI+hCjmNzqoA4nQ==", "dev": true }, @@ -5277,7 +5310,7 @@ }, "table-layout": { "version": "0.4.5", - "resolved": "https://npm-registry.rancher.sie.sony.com/t/table-layout/_attachments/table-layout-0.4.5.tgz", + "resolved": "https://registry.npmjs.org/table-layout/-/table-layout-0.4.5.tgz", "integrity": "sha512-zTvf0mcggrGeTe/2jJ6ECkJHAQPIYEwDoqsiqBjI24mvRmQbInK5jq33fyypaCBxX08hMkfmdOqj6haT33EqWw==", "dev": true, "requires": { @@ -5290,7 +5323,7 @@ "dependencies": { "array-back": { "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz", "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", "dev": true, "requires": { @@ -5366,14 +5399,14 @@ }, "temp-path": { "version": "1.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/t/temp-path/_attachments/temp-path-1.0.0.tgz", + "resolved": "https://registry.npmjs.org/temp-path/-/temp-path-1.0.0.tgz", "integrity": "sha1-JLFUOXOrRCiW2a02fdnL2/r+kYs=", "dev": true }, "terser": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.4.3.tgz", - "integrity": "sha512-0ikKraVtRDKGzHrzkCv5rUNDzqlhmhowOBqC0XqUHFpW+vJ45+20/IFBcebwKfiS2Z9fJin6Eo+F1zLZsxi8RA==", + "version": "4.6.2", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.6.2.tgz", + "integrity": "sha512-6FUjJdY2i3WZAtYBtnV06OOcOfzl+4hSKYE9wgac8rkLRBToPDDrBB2AcHwQD/OKDxbnvhVy2YgOPWO2SsKWqg==", "dev": true, "requires": { "commander": "^2.20.0", @@ -5400,7 +5433,7 @@ }, "test-value": { "version": "3.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/t/test-value/_attachments/test-value-3.0.0.tgz", + "resolved": "https://registry.npmjs.org/test-value/-/test-value-3.0.0.tgz", "integrity": "sha512-sVACdAWcZkSU9x7AOmJo5TqE+GyNJknHaHsMrR6ZnhjVlVN9Yx6FjHrsKZ3BjIpPCT68zYesPWkakrNupwfOTQ==", "dev": true, "requires": { @@ -5410,7 +5443,7 @@ "dependencies": { "array-back": { "version": "2.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/a/array-back/_attachments/array-back-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz", "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", "dev": true, "requires": { @@ -5564,14 +5597,14 @@ "dev": true }, "typescript": { - "version": "3.7.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.2.tgz", - "integrity": "sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==", + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.4.tgz", + "integrity": "sha512-A25xv5XCtarLwXpcDNZzCGvW2D1S3/bACratYBx2sax8PefsFhlYmkQicKHvpYflFS8if4zne5zT5kpJ7pzuvw==", "dev": true }, "typical": { "version": "2.6.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/t/typical/_attachments/typical-2.6.1.tgz", + "resolved": "https://registry.npmjs.org/typical/-/typical-2.6.1.tgz", "integrity": "sha1-XAgOXWYcu+OCWdLnCjxyU+hziB0=", "dev": true }, @@ -5582,9 +5615,9 @@ "dev": true }, "uglify-js": { - "version": "3.7.1", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.7.1.tgz", - "integrity": "sha512-pnOF7jY82wdIhATVn87uUY/FHU+MDUdPLkmGFvGoclQmeu229eTkbG5gjGGBi3R7UuYYSEeYXY/TTY5j2aym2g==", + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.7.4.tgz", + "integrity": "sha512-tinYWE8X1QfCHxS1lBS8yiDekyhSXOO6R66yNOCdUJeojxxw+PX2BHAz/BWyW7PQ7pkiWVxJfIEbiDxyLWvUGg==", "dev": true, "optional": true, "requires": { @@ -5751,9 +5784,9 @@ "dev": true }, "walk-back": { - "version": "3.0.1", - "resolved": "https://npm-registry.rancher.sie.sony.com/w/walk-back/_attachments/walk-back-3.0.1.tgz", - "integrity": "sha512-umiNB2qLO731Sxbp6cfZ9pwURJzTnftxE4Gc7hq8n/ehkuXC//s9F65IEIJA2ZytQZ1ZOsm/Fju4IWx0bivkUQ==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/walk-back/-/walk-back-4.0.0.tgz", + "integrity": "sha512-kudCA8PXVQfrqv2mFTG72vDBRi8BKWxGgFLwPpzHcpZnSwZk93WMwUDVcLHWNsnm+Y0AC4Vb6MUNRgaHfyV2DQ==", "dev": true }, "watchpack": { @@ -5768,9 +5801,9 @@ } }, "webpack": { - "version": "4.41.2", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-4.41.2.tgz", - "integrity": "sha512-Zhw69edTGfbz9/8JJoyRQ/pq8FYUoY0diOXqW0T6yhgdhCv6wr0hra5DwwWexNRns2Z2+gsnrNcbe9hbGBgk/A==", + "version": "4.41.5", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-4.41.5.tgz", + "integrity": "sha512-wp0Co4vpyumnp3KlkmpM5LWuzvZYayDwM2n17EHFr4qxBBbRokC7DJawPJC7TfSFZ9HZ6GsdH40EBj4UV0nmpw==", "dev": true, "requires": { "@webassemblyjs/ast": "1.8.5", @@ -5793,7 +5826,7 @@ "node-libs-browser": "^2.2.1", "schema-utils": "^1.0.0", "tapable": "^1.1.3", - "terser-webpack-plugin": "^1.4.1", + "terser-webpack-plugin": "^1.4.3", "watchpack": "^1.6.0", "webpack-sources": "^1.4.1" }, @@ -6012,7 +6045,7 @@ }, "wordwrapjs": { "version": "3.0.0", - "resolved": "https://npm-registry.rancher.sie.sony.com/w/wordwrapjs/_attachments/wordwrapjs-3.0.0.tgz", + "resolved": "https://registry.npmjs.org/wordwrapjs/-/wordwrapjs-3.0.0.tgz", "integrity": "sha512-mO8XtqyPvykVCsrwj5MlOVWvSnCdT+C+QVbm6blradR7JExAhbkZ7hZ9A+9NUtwzSqrlUo9a67ws0EiILrvRpw==", "dev": true, "requires": { diff --git a/package.json b/package.json index 473850b1..54b8fa1c 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "scripts": { "build:node": "tslint -c tslint.json \"./src/**/*.ts\" && tsc", "build:browser": "tslint -c tslint.json \"./src/**/*.ts\" && webpack --mode=production", - "build:all": "tslint -c tslint.json \"./src/*.ts\" && tsc && webpack --mode=production", + "build:all": "tslint -c tslint.json \"./src/**/*.ts\" && tsc && webpack --mode=production", "docs": "jsdoc -d docs -c jsdoc.conf.json -t ./node_modules/ink-docstrap/template -R README.md dist/node/consts.js dist/node/poweredup-node.js dist/node/lpf2hub.js dist/node/wedo2smarthub.js dist/node/boostmovehub.js dist/node/puphub.js dist/node/pupremote.js dist/node/duplotrainbase.js dist/node/controlplushub.js dist/node/hub.js dist/node/consts.js", "all": "npm run build:all && npm run docs", "prepublishOnly": "npm run build:node" @@ -22,15 +22,15 @@ }, "devDependencies": { "@types/debug": "4.1.5", - "@types/node": "^12.12.7", - "@types/web-bluetooth": "0.0.4", + "@types/node": "^13.1.5", + "@types/web-bluetooth": "0.0.5", "ink-docstrap": "^1.3.2", "jsdoc": "^3.6.3", - "jsdoc-to-markdown": "^5.0.2", + "jsdoc-to-markdown": "^5.0.3", "ts-loader": "^6.2.1", "tslint": "^5.20.1", - "typescript": "^3.7.2", - "webpack": "^4.41.2", + "typescript": "^3.7.4", + "webpack": "^4.41.5", "webpack-cli": "^3.3.10" } } diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index bf1a9e61..14a63a3e 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class ColorDistanceSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, ColorDistanceSensor.ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case ColorDistanceSensor.Mode.COLOR: + case Mode.COLOR: if (message[this.isWeDo2SmartHub ? 2 : 4] <= 10) { const color = message[this.isWeDo2SmartHub ? 2 : 4]; @@ -27,7 +27,7 @@ export class ColorDistanceSensor extends Device { } break; - case ColorDistanceSensor.Mode.DISTANCE: + case Mode.DISTANCE: if (this.isWeDo2SmartHub) { break; } @@ -43,7 +43,7 @@ export class ColorDistanceSensor extends Device { } break; - case ColorDistanceSensor.Mode.COLOR_AND_DISTANCE: + case Mode.COLOR_AND_DISTANCE: if (this.isWeDo2SmartHub) { break; } @@ -74,18 +74,14 @@ export class ColorDistanceSensor extends Device { } -export namespace ColorDistanceSensor { - - export enum Mode { - COLOR = 0x00, - DISTANCE = 0x01, - COLOR_AND_DISTANCE = 0x08 - } - - export const ModeMap: {[event: string]: number} = { - "color": ColorDistanceSensor.Mode.COLOR, - "distance": ColorDistanceSensor.Mode.DISTANCE, - "colorAndDistance": ColorDistanceSensor.Mode.COLOR_AND_DISTANCE - } +export enum Mode { + COLOR = 0x00, + DISTANCE = 0x01, + COLOR_AND_DISTANCE = 0x08 +} -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "color": Mode.COLOR, + "distance": Mode.DISTANCE, + "colorAndDistance": Mode.COLOR_AND_DISTANCE +}; diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts index dc688d43..82c1a28e 100644 --- a/src/devices/currentsensor.ts +++ b/src/devices/currentsensor.ts @@ -7,25 +7,25 @@ import * as Consts from "../consts"; export class CurrentSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, CurrentSensor.ModeMap, Consts.DeviceType.CURRENT_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.CURRENT_SENSOR); } public receive (message: Buffer) { const mode = this.mode; switch (mode) { - case CurrentSensor.Mode.CURRENT: + case Mode.CURRENT: if (this.isWeDo2SmartHub) { const current = message.readInt16LE(2) / 1000; this.emitGlobal("current", { current }); } else { - let maxCurrentValue = CurrentSensor.MaxCurrentValue[this.hub.type]; + let maxCurrentValue = MaxCurrentValue[this.hub.type]; if (maxCurrentValue === undefined) { - maxCurrentValue = CurrentSensor.MaxCurrentValue[Consts.HubType.UNKNOWN]; + maxCurrentValue = MaxCurrentValue[Consts.HubType.UNKNOWN]; } - let maxCurrentRaw = CurrentSensor.MaxCurrentRaw[this.hub.type]; + let maxCurrentRaw = MaxCurrentRaw[this.hub.type]; if (maxCurrentRaw === undefined) { - maxCurrentRaw = CurrentSensor.MaxCurrentRaw[Consts.HubType.UNKNOWN]; + maxCurrentRaw = MaxCurrentRaw[Consts.HubType.UNKNOWN]; } const current = message.readUInt16LE(4) * maxCurrentValue / maxCurrentRaw; /** @@ -41,23 +41,19 @@ export class CurrentSensor extends Device { } -export namespace CurrentSensor { - - export enum Mode { - CURRENT = 0x00 - } +export enum Mode { + CURRENT = 0x00 +} - export const ModeMap: {[event: string]: number} = { - "current": CurrentSensor.Mode.CURRENT - } +export const ModeMap: {[event: string]: number} = { + "current": Mode.CURRENT +}; - export const MaxCurrentValue: {[hubType: number]: number} = { - [Consts.HubType.UNKNOWN]: 2444, - [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4175, - } +const MaxCurrentValue: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 2444, + [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4175, +}; - export const MaxCurrentRaw: {[hubType: number]: number} = { - [Consts.HubType.UNKNOWN]: 4095, - } - -} \ No newline at end of file +const MaxCurrentRaw: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 4095, +}; diff --git a/src/devices/device.ts b/src/devices/device.ts index 1a93f78e..42ddb1b4 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -47,8 +47,8 @@ export class Device extends EventEmitter { this.emit("detach"); } }; - - for (let event in this._modeMap) { + + for (const event in this._modeMap) { if (this.hub.listenerCount(event) > 0) { eventAttachListener(event); } diff --git a/src/devices/duplotrainbasecolorsensor.ts b/src/devices/duplotrainbasecolorsensor.ts index 4f8d22d8..e4d94f28 100644 --- a/src/devices/duplotrainbasecolorsensor.ts +++ b/src/devices/duplotrainbasecolorsensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class DuploTrainBaseColorSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, DuploTrainBaseColorSensor.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case DuploTrainBaseColorSensor.Mode.COLOR: + case Mode.COLOR: if (message[4] <= 10) { const color = message[4]; @@ -32,14 +32,10 @@ export class DuploTrainBaseColorSensor extends Device { } -export namespace DuploTrainBaseColorSensor { - - export enum Mode { - COLOR = 0x00 - } - - export const ModeMap: {[event: string]: number} = { - "color": DuploTrainBaseColorSensor.Mode.COLOR - } +export enum Mode { + COLOR = 0x00 +} -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "color": Mode.COLOR +}; diff --git a/src/devices/duplotrainbasespeaker.ts b/src/devices/duplotrainbasespeaker.ts index 1bd6be28..8752e131 100644 --- a/src/devices/duplotrainbasespeaker.ts +++ b/src/devices/duplotrainbasespeaker.ts @@ -20,7 +20,7 @@ export class DuploTrainBaseSpeaker extends Device { */ public playSound (sound: Consts.DuploTrainBaseSound) { return new Promise((resolve, reject) => { - this.subscribe(DuploTrainBaseSpeaker.Mode.SOUND); + this.subscribe(Mode.SOUND); this.writeDirect(0x01, Buffer.from([sound])); return resolve(); }); @@ -29,10 +29,6 @@ export class DuploTrainBaseSpeaker extends Device { } -export namespace DuploTrainBaseSpeaker { - - export enum Mode { - SOUND = 0x01 - } - -} \ No newline at end of file +export enum Mode { + SOUND = 0x01 +} diff --git a/src/devices/duplotrainbasespeedometer.ts b/src/devices/duplotrainbasespeedometer.ts index 17186dc7..e9a7442e 100644 --- a/src/devices/duplotrainbasespeedometer.ts +++ b/src/devices/duplotrainbasespeedometer.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class DuploTrainBaseSpeedometer extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, DuploTrainBaseSpeedometer.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER); + super(hub, portId, ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case DuploTrainBaseSpeedometer.Mode.SPEED: + case Mode.SPEED: const speed = message.readInt16LE(4); /** @@ -30,14 +30,10 @@ export class DuploTrainBaseSpeedometer extends Device { } -export namespace DuploTrainBaseSpeedometer { - - export enum Mode { - SPEED = 0x00 - } - - export const ModeMap: {[event: string]: number} = { - "speed": DuploTrainBaseSpeedometer.Mode.SPEED - } +export enum Mode { + SPEED = 0x00 +} -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "speed": Mode.SPEED +}; diff --git a/src/devices/hubled.ts b/src/devices/hubled.ts index 807ba1fc..ed4df442 100644 --- a/src/devices/hubled.ts +++ b/src/devices/hubled.ts @@ -27,7 +27,7 @@ export class HubLED extends Device { this.send(Buffer.from([0x06, 0x17, 0x01, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); this.send(Buffer.from([0x06, 0x04, 0x01, color]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); } else { - this.subscribe(HubLED.Mode.COLOR); + this.subscribe(Mode.COLOR); this.writeDirect(0x00, Buffer.from([color])); } return resolve(); @@ -49,7 +49,7 @@ export class HubLED extends Device { this.send(Buffer.from([0x06, 0x17, 0x01, 0x02]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE); this.send(Buffer.from([0x06, 0x04, 0x03, red, green, blue]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); } else { - this.subscribe(HubLED.Mode.RGB); + this.subscribe(Mode.RGB); this.writeDirect(0x01, Buffer.from([red, green, blue])); } return resolve(); @@ -59,11 +59,7 @@ export class HubLED extends Device { } -export namespace HubLED { - - export enum Mode { - COLOR = 0x00, - RGB = 0x01 - } - -} \ No newline at end of file +export enum Mode { + COLOR = 0x00, + RGB = 0x01 +} diff --git a/src/devices/motionsensor.ts b/src/devices/motionsensor.ts index 1a75961d..559fecdc 100644 --- a/src/devices/motionsensor.ts +++ b/src/devices/motionsensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class MotionSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, MotionSensor.ModeMap, Consts.DeviceType.MOTION_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.MOTION_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case MotionSensor.Mode.DISTANCE: + case Mode.DISTANCE: let distance = message[this.isWeDo2SmartHub ? 2 : 4]; if (message[this.isWeDo2SmartHub ? 3 : 5] === 1) { distance = distance + 255; @@ -32,14 +32,10 @@ export class MotionSensor extends Device { } -export namespace MotionSensor { - - export enum Mode { - DISTANCE = 0x00 - } - - export const ModeMap: {[event: string]: number} = { - "distance": MotionSensor.Mode.DISTANCE - } +export enum Mode { + DISTANCE = 0x00 +} -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "distance": Mode.DISTANCE +}; diff --git a/src/devices/movehubtiltsensor.ts b/src/devices/movehubtiltsensor.ts index b9bc0d7d..ed6a6499 100644 --- a/src/devices/movehubtiltsensor.ts +++ b/src/devices/movehubtiltsensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class MoveHubTiltSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, MoveHubTiltSensor.ModeMap, Consts.DeviceType.MOVE_HUB_TILT_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.MOVE_HUB_TILT_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case MoveHubTiltSensor.Mode.TILT: + case Mode.TILT: /** * Emits when a tilt sensor is activated. * @event MoveHubTiltSensor#tilt @@ -30,14 +30,10 @@ export class MoveHubTiltSensor extends Device { } -export namespace MoveHubTiltSensor { - - export enum Mode { - TILT = 0x00 - } +export enum Mode { + TILT = 0x00 +} - export const ModeMap: {[event: string]: number} = { - "tilt": MoveHubTiltSensor.Mode.TILT - } - -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "tilt": Mode.TILT +}; diff --git a/src/devices/remotecontrolbutton.ts b/src/devices/remotecontrolbutton.ts index c43af4fd..031744e0 100644 --- a/src/devices/remotecontrolbutton.ts +++ b/src/devices/remotecontrolbutton.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class RemoteControlButton extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, RemoteControlButton.ModeMap, Consts.DeviceType.REMOTE_CONTROL_BUTTON); + super(hub, portId, ModeMap, Consts.DeviceType.REMOTE_CONTROL_BUTTON); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case RemoteControlButton.Mode.BUTTON_EVENTS: + case Mode.BUTTON_EVENTS: /** * Emits when a button on the remote is pressed or released. * @event RemoteControlButton#button @@ -28,21 +28,17 @@ export class RemoteControlButton extends Device { } -export namespace RemoteControlButton { +export enum Mode { + BUTTON_EVENTS = 0x00 +} - export enum Mode { - BUTTON_EVENTS = 0x00 - } +export const ModeMap: {[event: string]: number} = { + "remoteButton": Mode.BUTTON_EVENTS +}; - export const ModeMap: {[event: string]: number} = { - "remoteButton": RemoteControlButton.Mode.BUTTON_EVENTS - } - - export const ButtonState: {[state: string]: number} = { - "UP": 0x01, - "DOWN": 0xff, - "STOP": 0x7f, - "RELEASED": 0x00, - } - -} \ No newline at end of file +export const ButtonState: {[state: string]: number} = { + "UP": 0x01, + "DOWN": 0xff, + "STOP": 0x7f, + "RELEASED": 0x00, +}; diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 69a3a282..3b22e50d 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -8,14 +8,14 @@ import { mapSpeed } from "../utils"; export class TachoMotor extends BasicMotor { constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { - super(hub, portId, Object.assign({}, modeMap, TachoMotor.ModeMap), type); + super(hub, portId, Object.assign({}, modeMap, ModeMap), type); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case TachoMotor.Mode.ROTATION: + case Mode.ROTATION: const rotation = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4); /** * Emits when a rotation sensor is activated. @@ -51,14 +51,10 @@ export class TachoMotor extends BasicMotor { } -export namespace TachoMotor { - - export enum Mode { - ROTATION = 0x02 - } - - export const ModeMap: {[event: string]: number} = { - "rotate": TachoMotor.Mode.ROTATION - } +export enum Mode { + ROTATION = 0x02 +} -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "rotate": Mode.ROTATION +}; diff --git a/src/devices/technicmediumhubaccelerometersensor.ts b/src/devices/technicmediumhubaccelerometersensor.ts index 80e6901c..28990d08 100644 --- a/src/devices/technicmediumhubaccelerometersensor.ts +++ b/src/devices/technicmediumhubaccelerometersensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class TechnicMediumHubAccelerometerSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, TechnicMediumHubAccelerometerSensor.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER); + super(hub, portId, ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case TechnicMediumHubAccelerometerSensor.Mode.ACCEL: + case Mode.ACCEL: /** * Emits when accelerometer detects movement. Measured in mG. * @event LPF2Hub#accel @@ -33,14 +33,10 @@ export class TechnicMediumHubAccelerometerSensor extends Device { } -export namespace TechnicMediumHubAccelerometerSensor { - - export enum Mode { - ACCEL = 0x00 - } +export enum Mode { + ACCEL = 0x00 +} - export const ModeMap: {[event: string]: number} = { - "accel": TechnicMediumHubAccelerometerSensor.Mode.ACCEL - } - -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "accel": Mode.ACCEL +}; diff --git a/src/devices/technicmediumhubgyrosensor.ts b/src/devices/technicmediumhubgyrosensor.ts index 793749dd..929592a2 100644 --- a/src/devices/technicmediumhubgyrosensor.ts +++ b/src/devices/technicmediumhubgyrosensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class TechnicMediumHubGyroSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, TechnicMediumHubGyroSensor.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case TechnicMediumHubGyroSensor.Mode.GYRO: + case Mode.GYRO: /** * Emits when gyroscope detects movement. Measured in DPS - degrees per second. * @event TechnicMediumHubGyroSensor#gyro @@ -32,14 +32,10 @@ export class TechnicMediumHubGyroSensor extends Device { } -export namespace TechnicMediumHubGyroSensor { - - export enum Mode { - GYRO = 0x00 - } +export enum Mode { + GYRO = 0x00 +} - export const ModeMap: {[event: string]: number} = { - "gyro": TechnicMediumHubGyroSensor.Mode.GYRO - } - -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "gyro": Mode.GYRO +}; diff --git a/src/devices/technicmediumhubtiltsensor.ts b/src/devices/technicmediumhubtiltsensor.ts index 53f05957..8f8b3840 100644 --- a/src/devices/technicmediumhubtiltsensor.ts +++ b/src/devices/technicmediumhubtiltsensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class TechnicMediumHubTiltSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, TechnicMediumHubTiltSensor.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case TechnicMediumHubTiltSensor.Mode.TILT: + case Mode.TILT: /** * Emits when a tilt sensor is activated. * @event TechnicMediumHubTiltSensor#tilt @@ -32,14 +32,10 @@ export class TechnicMediumHubTiltSensor extends Device { } -export namespace TechnicMediumHubTiltSensor { - - export enum Mode { - TILT = 0x00 - } +export enum Mode { + TILT = 0x00 +} - export const ModeMap: {[event: string]: number} = { - "tilt": TechnicMediumHubTiltSensor.Mode.TILT - } - -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "tilt": Mode.TILT +}; diff --git a/src/devices/tiltsensor.ts b/src/devices/tiltsensor.ts index 301bfd09..cd99d6fc 100644 --- a/src/devices/tiltsensor.ts +++ b/src/devices/tiltsensor.ts @@ -7,14 +7,14 @@ import * as Consts from "../consts"; export class TiltSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, TiltSensor.ModeMap, Consts.DeviceType.TILT_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.TILT_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case TiltSensor.Mode.TILT: + case Mode.TILT: const x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4); const y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5); /** @@ -30,14 +30,10 @@ export class TiltSensor extends Device { } -export namespace TiltSensor { - - export enum Mode { - TILT = 0x00 - } +export enum Mode { + TILT = 0x00 +} - export const ModeMap: {[event: string]: number} = { - "tilt": TiltSensor.Mode.TILT - } - -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "tilt": Mode.TILT +}; diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts index c37b9edf..906cd3ff 100644 --- a/src/devices/voltagesensor.ts +++ b/src/devices/voltagesensor.ts @@ -7,25 +7,25 @@ import * as Consts from "../consts"; export class VoltageSensor extends Device { constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, VoltageSensor.ModeMap, Consts.DeviceType.VOLTAGE_SENSOR); + super(hub, portId, ModeMap, Consts.DeviceType.VOLTAGE_SENSOR); } public receive (message: Buffer) { const mode = this._mode; switch (mode) { - case VoltageSensor.Mode.VOLTAGE: + case Mode.VOLTAGE: if (this.isWeDo2SmartHub) { const voltage = message.readInt16LE(2) / 40; this.emitGlobal("voltage", { voltage }); } else { - let maxVoltageValue = VoltageSensor.MaxVoltageValue[this.hub.type]; + let maxVoltageValue = MaxVoltageValue[this.hub.type]; if (maxVoltageValue === undefined) { - maxVoltageValue = VoltageSensor.MaxVoltageValue[Consts.HubType.UNKNOWN]; + maxVoltageValue = MaxVoltageValue[Consts.HubType.UNKNOWN]; } - let maxVoltageRaw = VoltageSensor.MaxVoltageRaw[this.hub.type]; + let maxVoltageRaw = MaxVoltageRaw[this.hub.type]; if (maxVoltageRaw === undefined) { - maxVoltageRaw = VoltageSensor.MaxVoltageRaw[Consts.HubType.UNKNOWN]; + maxVoltageRaw = MaxVoltageRaw[Consts.HubType.UNKNOWN]; } const voltage = message.readUInt16LE(4) * maxVoltageValue / maxVoltageRaw; /** @@ -41,27 +41,23 @@ export class VoltageSensor extends Device { } -export namespace VoltageSensor { - - export enum Mode { - VOLTAGE = 0x00 - } - - export const ModeMap: {[event: string]: number} = { - "voltage": VoltageSensor.Mode.VOLTAGE - } - - export const MaxVoltageValue: {[hubType: number]: number} = { - [Consts.HubType.UNKNOWN]: 9.615, - [Consts.HubType.DUPLO_TRAIN_BASE]: 6.4, - [Consts.HubType.REMOTE_CONTROL]: 6.4, - } +export enum Mode { + VOLTAGE = 0x00 +} - export const MaxVoltageRaw: {[hubType: number]: number} = { - [Consts.HubType.UNKNOWN]: 3893, - [Consts.HubType.DUPLO_TRAIN_BASE]: 3047, - [Consts.HubType.REMOTE_CONTROL]: 3200, - [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4095, - } - -} \ No newline at end of file +export const ModeMap: {[event: string]: number} = { + "voltage": Mode.VOLTAGE +}; + +const MaxVoltageValue: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 9.615, + [Consts.HubType.DUPLO_TRAIN_BASE]: 6.4, + [Consts.HubType.REMOTE_CONTROL]: 6.4, +}; + +const MaxVoltageRaw: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 3893, + [Consts.HubType.DUPLO_TRAIN_BASE]: 3047, + [Consts.HubType.REMOTE_CONTROL]: 3200, + [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4095, +}; diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index 928b84a1..4f43ec3b 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -17,9 +17,9 @@ import { MediumLinearMotor } from "../devices/mediumlinearmotor"; import { MotionSensor } from "../devices/motionsensor"; import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor"; import { MoveHubTiltSensor } from "../devices/movehubtiltsensor"; +import { PiezoBuzzer } from "../devices/piezobuzzer"; import { RemoteControlButton } from "../devices/remotecontrolbutton"; import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor"; -import { PiezoBuzzer } from "../devices/piezobuzzer"; import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; import { TechnicMediumHubAccelerometerSensor } from "../devices/technicmediumhubaccelerometersensor"; import { TechnicMediumHubGyroSensor } from "../devices/technicmediumhubgyrosensor"; @@ -234,7 +234,7 @@ export class BaseHub extends EventEmitter { } else { return false; } - }) + }); }); } @@ -313,7 +313,7 @@ export class BaseHub extends EventEmitter { while (i--) { const callback = this._attachCallbacks[i]; if (callback(device)) { - this._attachCallbacks.splice(i, 1); + this._attachCallbacks.splice(i, 1); } } } @@ -343,6 +343,7 @@ export class BaseHub extends EventEmitter { [Consts.DeviceType.MOTION_SENSOR]: MotionSensor, [Consts.DeviceType.TILT_SENSOR]: TiltSensor, [Consts.DeviceType.MOVE_HUB_TILT_SENSOR]: MoveHubTiltSensor, + [Consts.DeviceType.PIEZO_BUZZER]: PiezoBuzzer, [Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR]: TechnicMediumHubTiltSensor, [Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR]: TechnicMediumHubGyroSensor, [Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER]: TechnicMediumHubAccelerometerSensor, @@ -361,7 +362,7 @@ export class BaseHub extends EventEmitter { }; constructor = deviceConstructors[deviceType as Consts.DeviceType]; - + if (constructor) { return new constructor(this, portId); } else { diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index d9701a52..19adfe25 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -28,10 +28,10 @@ export class DuploTrainBase extends LPF2Hub { peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID ); } - + constructor (device: IBLEAbstraction) { - super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_BASE); + super(device, PortMap, Consts.HubType.DUPLO_TRAIN_BASE); debug("Discovered Duplo Train Base"); } @@ -48,12 +48,9 @@ export class DuploTrainBase extends LPF2Hub { } -export namespace DuploTrainBase { - - export const PortMap: {[portName: string]: number} = { - "MOTOR": 0, - "COLOR": 18, - "SPEEDOMETER": 19 - } +export const PortMap: {[portName: string]: number} = { + "MOTOR": 0, + "COLOR": 18, + "SPEEDOMETER": 19 +}; -} \ No newline at end of file diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index eb6cf295..e2c7a7ed 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -34,7 +34,7 @@ export class Hub extends LPF2Hub { protected _currentPort = 0x3b; constructor (device: IBLEAbstraction) { - super(device, Hub.PortMap, Consts.HubType.HUB); + super(device, PortMap, Consts.HubType.HUB); debug("Discovered Powered UP Hub"); } @@ -58,14 +58,10 @@ export class Hub extends LPF2Hub { } -export namespace Hub { - - export const PortMap: {[portName: string]: number} = { - "A": 0, - "B": 1, - "HUB_LED": 50, - "CURRENT_SENSOR": 59, - "VOLTAGE_SENSOR": 60 - } - -} \ No newline at end of file +export const PortMap: {[portName: string]: number} = { + "A": 0, + "B": 1, + "HUB_LED": 50, + "CURRENT_SENSOR": 59, + "VOLTAGE_SENSOR": 60 +}; diff --git a/src/hubs/movehub.ts b/src/hubs/movehub.ts index 657a083c..f769ac4e 100644 --- a/src/hubs/movehub.ts +++ b/src/hubs/movehub.ts @@ -32,7 +32,7 @@ export class MoveHub extends LPF2Hub { } constructor (device: IBLEAbstraction) { - super(device, MoveHub.PortMap, Consts.HubType.MOVE_HUB); + super(device, PortMap, Consts.HubType.MOVE_HUB); debug("Discovered Move Hub"); } @@ -56,17 +56,13 @@ export class MoveHub extends LPF2Hub { } -export namespace MoveHub { - - export const PortMap: {[portName: string]: number} = { - "A": 0, - "B": 1, - "C": 2, - "D": 3, - "HUB_LED": 50, - "TILT_SENSOR": 58, - "CURRENT_SENSOR": 59, - "VOLTAGE_SENSOR": 60 - } - -} \ No newline at end of file +export const PortMap: {[portName: string]: number} = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "HUB_LED": 50, + "TILT_SENSOR": 58, + "CURRENT_SENSOR": 59, + "VOLTAGE_SENSOR": 60 +}; diff --git a/src/hubs/remotecontrol.ts b/src/hubs/remotecontrol.ts index 50e08b4b..a40be286 100644 --- a/src/hubs/remotecontrol.ts +++ b/src/hubs/remotecontrol.ts @@ -32,7 +32,7 @@ export class RemoteControl extends LPF2Hub { constructor (device: IBLEAbstraction) { - super(device, RemoteControl.PortMap, Consts.HubType.REMOTE_CONTROL); + super(device, PortMap, Consts.HubType.REMOTE_CONTROL); debug("Discovered Powered UP Remote"); } @@ -49,14 +49,10 @@ export class RemoteControl extends LPF2Hub { } -export namespace RemoteControl { - - export const PortMap: {[portName: string]: number} = { - "LEFT": 0, - "RIGHT": 1, - "HUB_LED": 52, - "VOLTAGE_SENSOR": 59, - "REMOTE_CONTROL_RSSI": 60 - } - -} \ No newline at end of file +export const PortMap: {[portName: string]: number} = { + "LEFT": 0, + "RIGHT": 1, + "HUB_LED": 52, + "VOLTAGE_SENSOR": 59, + "REMOTE_CONTROL_RSSI": 60 +}; diff --git a/src/hubs/technicmediumhub.ts b/src/hubs/technicmediumhub.ts index 558b113b..db0dfe8c 100644 --- a/src/hubs/technicmediumhub.ts +++ b/src/hubs/technicmediumhub.ts @@ -31,7 +31,7 @@ export class TechnicMediumHub extends LPF2Hub { } constructor (device: IBLEAbstraction) { - super(device, TechnicMediumHub.PortMap, Consts.HubType.TECHNIC_MEDIUM_HUB); + super(device, PortMap, Consts.HubType.TECHNIC_MEDIUM_HUB); debug("Discovered Control+ Hub"); } @@ -108,19 +108,15 @@ export class TechnicMediumHub extends LPF2Hub { } -export namespace TechnicMediumHub { - - export const PortMap: {[portName: string]: number} = { - "A": 0, - "B": 1, - "C": 2, - "D": 3, - "HUB_LED": 50, - "CURRENT_SENSOR": 59, - "VOLTAGE_SENSOR": 60, - "ACCELEROMETER": 97, - "GYRO_SENSOR": 98, - "TILT_SENSOR": 99 - } - -} \ No newline at end of file +export const PortMap: {[portName: string]: number} = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "HUB_LED": 50, + "CURRENT_SENSOR": 59, + "VOLTAGE_SENSOR": 60, + "ACCELEROMETER": 97, + "GYRO_SENSOR": 98, + "TILT_SENSOR": 99 +}; diff --git a/src/hubs/wedo2smarthub.ts b/src/hubs/wedo2smarthub.ts index 1da11d16..1b5b14d7 100644 --- a/src/hubs/wedo2smarthub.ts +++ b/src/hubs/wedo2smarthub.ts @@ -35,7 +35,7 @@ export class WeDo2SmartHub extends BaseHub { constructor (device: IBLEAbstraction) { - super(device, WeDo2SmartHub.PortMap, Consts.HubType.WEDO2_SMART_HUB); + super(device, PortMap, Consts.HubType.WEDO2_SMART_HUB); debug("Discovered WeDo 2.0 Smart Hub"); } @@ -228,15 +228,11 @@ export class WeDo2SmartHub extends BaseHub { } -export namespace WeDo2SmartHub { - - export const PortMap: {[portName: string]: number} = { - "A": 1, - "B": 2, - "CURRENT_SENSOR": 3, - "VOLTAGE_SENSOR": 4, - "PIEZO_BUZZER": 5, - "HUB_LED": 6 - } - -} \ No newline at end of file +export const PortMap: {[portName: string]: number} = { + "A": 1, + "B": 2, + "CURRENT_SENSOR": 3, + "VOLTAGE_SENSOR": 4, + "PIEZO_BUZZER": 5, + "HUB_LED": 6 +}; From bf721eddf626e91c0ab31cb30c56d6127efae5b5 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 8 Jan 2020 15:37:07 -0800 Subject: [PATCH 44/62] Virtual port attachment and detachment --- src/devices/device.ts | 5 +++++ src/hubs/basehub.ts | 4 ++-- src/hubs/lpf2hub.ts | 42 +++++++++++++++++++----------------------- src/interfaces.ts | 1 + 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/devices/device.ts b/src/devices/device.ts index 42ddb1b4..fc34d28b 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -28,6 +28,7 @@ export class Device extends EventEmitter { this._type = type; this._modeMap = modeMap; this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB); + this._isVirtualPort = this.hub.isPortVirtual(portId); const eventAttachListener = (event: string) => { if (event === "detach") { @@ -87,6 +88,10 @@ export class Device extends EventEmitter { return this._isWeDo2SmartHub; } + protected get isVirtualPort () { + return this._isVirtualPort; + } + public writeDirect (mode: number, data: Buffer, callback?: () => void) { if (this.isWeDo2SmartHub) { this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE); diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index 4f43ec3b..fb6f0d19 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -50,11 +50,11 @@ export class BaseHub extends EventEmitter { protected _batteryLevel: number = 100; protected _rssi: number = -60; protected _portMap: {[portName: string]: number} = {}; + protected _virtualPorts: number[] = []; protected _bleDevice: IBLEAbstraction; private _type: Consts.HubType; - private _virtualPorts: number[] = []; private _attachCallbacks: Array<((device: Device) => boolean)> = []; constructor (bleDevice: IBLEAbstraction, portMap: {[portName: string]: number} = {}, type: Consts.HubType = Consts.HubType.UNKNOWN) { @@ -62,7 +62,7 @@ export class BaseHub extends EventEmitter { this.setMaxListeners(23); // Technic Medium Hub has 9 built in devices + 4 external ports. Node.js throws a warning after 10 attached event listeners. this._type = type; this._bleDevice = bleDevice; - this._portMap = portMap; + this._portMap = Object.assign({}, portMap); bleDevice.on("disconnect", () => { /** * Emits when the hub is disconnected. diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index adeab884..0efdb31b 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -273,31 +273,27 @@ export class LPF2Hub extends BaseHub { const device = this._getDeviceByPortId(portId); if (device) { this._detachDevice(device); + if (this.isPortVirtual(portId)) { + const portName = this.getPortNameForPortId(portId); + if (portName) { + delete this._portMap[portName]; + } + this._virtualPorts = this._virtualPorts.filter((virtualPortId) => virtualPortId !== portId); + } } - } - // let port = this._getPortForPortNumber(data[3]); - - // if (!port) { - // if (data[4] === 0x02) { - // const portA = this._getPortForPortNumber(data[7]); - // const portB = this._getPortForPortNumber(data[8]); - // if (portA && portB) { - // this._virtualPorts[`${portA.id}${portB.id}`] = new Port(`${portA.id}${portB.id}`, data[3]); - // port = this._getPortForPortNumber(data[3]); - // if (port) { - // port.connected = true; - // this._registerDeviceAttachment(port, deviceType); - // } else { - // return; - // } - // } else { - // return; - // } - // } else { - // return; - // } - // } + // Handle virtual port creation + } else if (event === 0x02) { + const firstPortName = this.getPortNameForPortId(message[7]); + const secondPortName = this.getPortNameForPortId(message[8]); + // @ts-ignore NK These should never be undefined + const virtualPortName = firstPortName + secondPortName; + const virtualPortId = message[3]; + this._portMap[virtualPortName] = virtualPortId; + this._virtualPorts.push(virtualPortId); + const device = this._createDevice(deviceType, virtualPortId); + this._attachDevice(device); + } } diff --git a/src/interfaces.ts b/src/interfaces.ts index ea7111c7..3b399f19 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -21,4 +21,5 @@ export interface IDeviceInterface extends EventEmitter { getPortNameForPortId: (portId: number) => string | undefined; send: (message: Buffer, uuid: string, callback?: () => void) => void; subscribe: (portId: number, deviceType: number, mode: number) => void; + isPortVirtual: (portId: number) => boolean; } From b43b59e526a7f3f45bc57d0f09c115967031837a Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 9 Jan 2020 10:56:59 -0800 Subject: [PATCH 45/62] Virtual ports tested --- src/hubs/basehub.ts | 2 +- src/hubs/lpf2hub.ts | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index fb6f0d19..d917eadd 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -188,7 +188,7 @@ export class BaseHub extends EventEmitter { if (portId !== undefined) { return this._attachedDevices[portId]; } else { - throw new Error(`Port ${portName} does not exist on this hub type`); + return undefined; } } diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 0efdb31b..bd56b4ed 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -95,19 +95,11 @@ export class LPF2Hub extends BaseHub { public createVirtualPort (firstPortName: string, secondPortName: string) { - const firstPortId = this._portMap[firstPortName]; - if (!firstPortId) { - throw new Error(`Port ${firstPortName} does not exist on this hub`); - } - const secondPortId = this._portMap[secondPortName]; - if (!secondPortId) { - throw new Error(`Port ${secondPortName} does not exist on this hub`); - } - const firstDevice = this._getDeviceByPortId(firstPortId); + const firstDevice = this.getDeviceAtPort(firstPortName); if (!firstDevice) { throw new Error(`Port ${firstPortName} does not have an attached device`); } - const secondDevice = this._getDeviceByPortId(secondPortId); + const secondDevice = this.getDeviceAtPort(secondPortName); if (!secondDevice) { throw new Error(`Port ${secondPortName} does not have an attached device`); } From f7e86d32cfc590d1882b25ca613e3e42d7938e6d Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 9 Jan 2020 12:05:05 -0800 Subject: [PATCH 46/62] Added setSpeed, virtual port multiparam options --- src/devices/basicmotor.ts | 4 +-- src/devices/tachomotor.ts | 55 +++++++++++++++++++++++++++++++++++++-- src/hubs/lpf2hub.ts | 2 +- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/devices/basicmotor.ts b/src/devices/basicmotor.ts index eb3be90c..922d00fc 100644 --- a/src/devices/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -15,8 +15,8 @@ export class BasicMotor extends Device { /** - * Set the motor speed. - * @method BasicMotor#power + * Set the motor power. + * @method BasicMotor#setPower * @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. * @returns {Promise} Resolved upon successful completion of command. */ diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index 3b22e50d..a675a5be 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -27,6 +27,46 @@ export class TachoMotor extends BasicMotor { } } + /** + * Set the motor speed. + * @method TachoMotor#setSpeed + * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + * @returns {Promise} Resolved upon successful completion of command. + */ + public setSpeed (speed: [number, number] | number, time: number | undefined) { + if (!this.isVirtualPort && speed instanceof Array) { + throw new Error("Only virtual ports can accept multiple speeds"); + } + if (this.isWeDo2SmartHub) { + throw new Error("Motor speed is not available on the WeDo 2.0 Smart Hub"); + } + return new Promise((resolve) => { + this._busy = true; + if (speed === undefined || speed === null) { + speed = 100; + } + let message; + if (time !== undefined) { + if (speed instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x0a, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + } + message.writeUInt16LE(time, 4); + } else { + if (speed instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x08, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x07, mapSpeed(speed), 0x64, 0x03, 0x64, 0x7f, 0x03]); + } + } + this.send(message); + this._finished = () => { + return resolve(); + }; + }); + } + /** * Rotate a motor by a given angle. * @method TachoMotor#rotateByAngle @@ -34,13 +74,24 @@ export class TachoMotor extends BasicMotor { * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public rotateByAngle (angle: number, speed: number = 100) { + public rotateByAngle (angle: number, speed: [number, number] | number) { + if (!this.isVirtualPort && speed instanceof Array) { + throw new Error("Only virtual ports can accept multiple speeds"); + } if (this.isWeDo2SmartHub) { throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub"); } return new Promise((resolve) => { this._busy = true; - const message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + if (speed === undefined || speed === null) { + speed = 100; + } + let message; + if (speed instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + } message.writeUInt32LE(angle, 4); this.send(message); this._finished = () => { diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index bd56b4ed..2f503df0 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -106,7 +106,7 @@ export class LPF2Hub extends BaseHub { if (firstDevice.type !== secondDevice.type) { throw new Error(`Both devices must be of the same type to create a virtual port`); } - this.send(Buffer.from([0x61, 0x01, firstPortId, secondPortId]), Consts.BLECharacteristic.LPF2_ALL); + this.send(Buffer.from([0x61, 0x01, firstDevice.portId, secondDevice.portId]), Consts.BLECharacteristic.LPF2_ALL); } From 74be8a752bcd1359c05f6e9e1c995837343e4835 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 9 Jan 2020 16:01:57 -0800 Subject: [PATCH 47/62] Initial absolute motor support --- src/devices/absolutemotor.ts | 109 ++++++++++++++++++++++++ src/devices/techniclargelinearmotor.ts | 4 +- src/devices/technicxlargelinearmotor.ts | 4 +- 3 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 src/devices/absolutemotor.ts diff --git a/src/devices/absolutemotor.ts b/src/devices/absolutemotor.ts new file mode 100644 index 00000000..e0700c00 --- /dev/null +++ b/src/devices/absolutemotor.ts @@ -0,0 +1,109 @@ +import { TachoMotor } from "./tachomotor"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; +import { mapSpeed } from "../utils"; + +export class AbsoluteMotor extends TachoMotor { + + constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { + super(hub, portId, Object.assign({}, modeMap, ModeMap), type); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case Mode.ABSOLUTE: + // const absolute = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4); + // /** + // * Emits when a the motors absolute position is changed. + // * @event AbsoluteMotor#absolute + // * @param {number} absolute + // */ + // this.emitGlobal("absolute", { absolute }); + break; + default: + super.receive(message); + break; + } + } + + /** + * Rotate a motor by a given angle. + * @method AbsoluteMotor#gotoAbsolutePosition + * @param {number} position Absolute position the motor should go to (degrees from 0). + * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. + * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + */ + public gotoAbsolutePosition (position: [number, number] | number, speed: number = 100) { + if (!this.isVirtualPort && position instanceof Array) { + throw new Error("Only virtual ports can accept multiple positions"); + } + if (this.isWeDo2SmartHub) { + throw new Error("Absolute positioning is not available on the WeDo 2.0 Smart Hub"); + } + return new Promise((resolve) => { + this._busy = true; + if (speed === undefined || speed === null) { + speed = 100; + } + let message; + if (position instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + message.writeUInt32LE(position[0], 4); + message.writeUInt32LE(position[1], 8); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + message.writeUInt32LE(position, 4); + } + this.send(message); + this._finished = () => { + return resolve(); + }; + }); + } + + /** + * (Re)set the knowledge of the absolute position to the current position. + * @method AbsoluteMotor#resetAbsolutePosition + * @param {number} position Position to set (degrees from 0). + * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). + */ + public resetAbsolutePosition (position: [number, number] | number) { + if (!this.isVirtualPort && position instanceof Array) { + throw new Error("Only virtual ports can accept multiple positions"); + } + if (this.isWeDo2SmartHub) { + throw new Error("Absolute positioning is not available on the WeDo 2.0 Smart Hub"); + } + return new Promise((resolve) => { + this._busy = true; + let message; + if (position instanceof Array) { + message = Buffer.from([0x81, this.portId, 0x11, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + message.writeUInt32LE(position[0], 4); + message.writeUInt32LE(position[0], 8); + } else { + message = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); + message.writeUInt32LE(position, 5); + } + this.send(message); + this._finished = () => { + return resolve(); + }; + }); + } + +} + +export enum Mode { + ROTATION = 0x02, + ABSOLUTE = 0x03 +} + +export const ModeMap: {[event: string]: number} = { + "rotate": Mode.ROTATION, + "absolute": Mode.ABSOLUTE +}; diff --git a/src/devices/techniclargelinearmotor.ts b/src/devices/techniclargelinearmotor.ts index 103b3a00..8da81967 100644 --- a/src/devices/techniclargelinearmotor.ts +++ b/src/devices/techniclargelinearmotor.ts @@ -1,10 +1,10 @@ -import { TachoMotor } from "./tachomotor"; +import { AbsoluteMotor } from "./absolutemotor"; import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; -export class TechnicLargeLinearMotor extends TachoMotor { +export class TechnicLargeLinearMotor extends AbsoluteMotor { constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, {}, Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR); diff --git a/src/devices/technicxlargelinearmotor.ts b/src/devices/technicxlargelinearmotor.ts index 53b4c0ac..c61ec108 100644 --- a/src/devices/technicxlargelinearmotor.ts +++ b/src/devices/technicxlargelinearmotor.ts @@ -1,10 +1,10 @@ -import { TachoMotor } from "./tachomotor"; +import { AbsoluteMotor } from "./absolutemotor"; import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; -export class TechnicXLargeLinearMotor extends TachoMotor { +export class TechnicXLargeLinearMotor extends AbsoluteMotor { constructor (hub: IDeviceInterface, portId: number) { super(hub, portId, {}, Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR); From 56eaa7ca49f70f0b50db3f2ac7650c7a81985bf8 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Thu, 9 Jan 2020 22:50:44 -0800 Subject: [PATCH 48/62] Fixed parts of absolute angle --- src/devices/absolutemotor.ts | 45 ++++++++++++++++++------------------ src/devices/device.ts | 1 + src/devices/tachomotor.ts | 16 ++++++------- src/utils.ts | 9 ++++++++ 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/devices/absolutemotor.ts b/src/devices/absolutemotor.ts index e0700c00..7d89a0d1 100644 --- a/src/devices/absolutemotor.ts +++ b/src/devices/absolutemotor.ts @@ -3,7 +3,7 @@ import { TachoMotor } from "./tachomotor"; import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; -import { mapSpeed } from "../utils"; +import { mapSpeed, normalizeAngle } from "../utils"; export class AbsoluteMotor extends TachoMotor { @@ -16,13 +16,13 @@ export class AbsoluteMotor extends TachoMotor { switch (mode) { case Mode.ABSOLUTE: - // const absolute = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4); - // /** - // * Emits when a the motors absolute position is changed. - // * @event AbsoluteMotor#absolute - // * @param {number} absolute - // */ - // this.emitGlobal("absolute", { absolute }); + const angle = normalizeAngle(message.readInt16LE(this.isWeDo2SmartHub ? 2 : 4)); + /** + * Emits when a the motors absolute position is changed. + * @event AbsoluteMotor#absolute + * @param {number} absolute + */ + this.emitGlobal("absolute", { angle }); break; default: super.receive(message); @@ -33,12 +33,12 @@ export class AbsoluteMotor extends TachoMotor { /** * Rotate a motor by a given angle. * @method AbsoluteMotor#gotoAbsolutePosition - * @param {number} position Absolute position the motor should go to (degrees from 0). + * @param {number} angle Absolute position the motor should go to (degrees from 0). * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public gotoAbsolutePosition (position: [number, number] | number, speed: number = 100) { - if (!this.isVirtualPort && position instanceof Array) { + public gotoAbsoluteAngle (angle: [number, number] | number, speed: number = 100) { + if (!this.isVirtualPort && angle instanceof Array) { throw new Error("Only virtual ports can accept multiple positions"); } if (this.isWeDo2SmartHub) { @@ -50,16 +50,17 @@ export class AbsoluteMotor extends TachoMotor { speed = 100; } let message; - if (position instanceof Array) { + if (angle instanceof Array) { message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); - message.writeUInt32LE(position[0], 4); - message.writeUInt32LE(position[1], 8); + message.writeInt32LE(normalizeAngle(angle[0]), 4); + message.writeInt32LE(normalizeAngle(angle[1]), 8); } else { message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); - message.writeUInt32LE(position, 4); + message.writeInt32LE(normalizeAngle(angle), 4); } this.send(message); this._finished = () => { + console.log("RESOLVE"); return resolve(); }; }); @@ -68,11 +69,11 @@ export class AbsoluteMotor extends TachoMotor { /** * (Re)set the knowledge of the absolute position to the current position. * @method AbsoluteMotor#resetAbsolutePosition - * @param {number} position Position to set (degrees from 0). + * @param {number} angle Position to set (degrees from 0). * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public resetAbsolutePosition (position: [number, number] | number) { - if (!this.isVirtualPort && position instanceof Array) { + public resetAbsoluteAngle (angle: [number, number] | number) { + if (!this.isVirtualPort && angle instanceof Array) { throw new Error("Only virtual ports can accept multiple positions"); } if (this.isWeDo2SmartHub) { @@ -81,13 +82,13 @@ export class AbsoluteMotor extends TachoMotor { return new Promise((resolve) => { this._busy = true; let message; - if (position instanceof Array) { + if (angle instanceof Array) { message = Buffer.from([0x81, this.portId, 0x11, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); - message.writeUInt32LE(position[0], 4); - message.writeUInt32LE(position[0], 8); + message.writeInt32LE(normalizeAngle(angle[0]), 4); + message.writeInt32LE(normalizeAngle(angle[0]), 8); } else { message = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); - message.writeUInt32LE(position, 5); + message.writeInt32LE(normalizeAngle(angle), 5); } this.send(message); this._finished = () => { diff --git a/src/devices/device.ts b/src/devices/device.ts index fc34d28b..2897abd0 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -36,6 +36,7 @@ export class Device extends EventEmitter { } if (this.autoSubscribe) { if (this._modeMap[event] !== undefined) { + console.log(this._modeMap[event]); this.subscribe(this._modeMap[event]); } } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index a675a5be..ffceeab2 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -16,13 +16,13 @@ export class TachoMotor extends BasicMotor { switch (mode) { case Mode.ROTATION: - const rotation = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4); + const degrees = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4); /** * Emits when a rotation sensor is activated. * @event TachoMotor#rotate * @param {number} rotation */ - this.emitGlobal("rotate", { rotation }); + this.emitGlobal("rotate", { degrees }); break; } } @@ -68,18 +68,18 @@ export class TachoMotor extends BasicMotor { } /** - * Rotate a motor by a given angle. - * @method TachoMotor#rotateByAngle - * @param {number} angle How much the motor should be rotated (in degrees). + * Rotate a motor by a given amount of degrees. + * @method TachoMotor#rotateByDegrees + * @param {number} degrees How much the motor should be rotated (in degrees). * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public rotateByAngle (angle: number, speed: [number, number] | number) { + public rotateByDegrees (degrees: number, speed: [number, number] | number) { if (!this.isVirtualPort && speed instanceof Array) { throw new Error("Only virtual ports can accept multiple speeds"); } if (this.isWeDo2SmartHub) { - throw new Error("Angle rotation is not available on the WeDo 2.0 Smart Hub"); + throw new Error("Rotation is not available on the WeDo 2.0 Smart Hub"); } return new Promise((resolve) => { this._busy = true; @@ -92,7 +92,7 @@ export class TachoMotor extends BasicMotor { } else { message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); } - message.writeUInt32LE(angle, 4); + message.writeUInt32LE(degrees, 4); this.send(message); this._finished = () => { return resolve(); diff --git a/src/utils.ts b/src/utils.ts index 4bff8d57..4e330a47 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -29,3 +29,12 @@ export const decodeVersion = (version: number) => { export const decodeMACAddress = (address: Uint8Array) => { return Array.from(address).map((part) => toHex(part, 2)).join(":"); }; + +export const normalizeAngle = (angle: number) => { + if (angle >= 180) { + return angle - (360 * ((angle + 180) / 360)); + } else if (angle < -180) { + return angle + (360 * ((180 - angle) / 360)); + } + return angle; +}; From 63f1c10fcc8ae3921c02e5337a81ef8306cd6bc9 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Fri, 10 Jan 2020 12:27:23 -0800 Subject: [PATCH 49/62] Servo calibration and reset --- src/devices/absolutemotor.ts | 95 ++++++++++++++++++++++++++++++++---- src/devices/basicmotor.ts | 12 ++++- src/devices/device.ts | 1 - src/devices/tachomotor.ts | 12 ++--- src/interfaces.ts | 1 + src/utils.ts | 17 +++++++ 6 files changed, 120 insertions(+), 18 deletions(-) diff --git a/src/devices/absolutemotor.ts b/src/devices/absolutemotor.ts index 7d89a0d1..54e2f534 100644 --- a/src/devices/absolutemotor.ts +++ b/src/devices/absolutemotor.ts @@ -3,7 +3,7 @@ import { TachoMotor } from "./tachomotor"; import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; -import { mapSpeed, normalizeAngle } from "../utils"; +import { mapSpeed, normalizeAngle, roundAngleToNearest90 } from "../utils"; export class AbsoluteMotor extends TachoMotor { @@ -32,12 +32,12 @@ export class AbsoluteMotor extends TachoMotor { /** * Rotate a motor by a given angle. - * @method AbsoluteMotor#gotoAbsolutePosition + * @method AbsoluteMotor#gotoAngle * @param {number} angle Absolute position the motor should go to (degrees from 0). * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public gotoAbsoluteAngle (angle: [number, number] | number, speed: number = 100) { + public gotoAngle (angle: [number, number] | number, speed: number = 100) { if (!this.isVirtualPort && angle instanceof Array) { throw new Error("Only virtual ports can accept multiple positions"); } @@ -51,28 +51,27 @@ export class AbsoluteMotor extends TachoMotor { } let message; if (angle instanceof Array) { - message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7e, 0x00]); message.writeInt32LE(normalizeAngle(angle[0]), 4); message.writeInt32LE(normalizeAngle(angle[1]), 8); } else { - message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7e, 0x00]); message.writeInt32LE(normalizeAngle(angle), 4); } this.send(message); this._finished = () => { - console.log("RESOLVE"); return resolve(); }; }); } /** - * (Re)set the knowledge of the absolute position to the current position. - * @method AbsoluteMotor#resetAbsolutePosition + * (Re)set the knowledge of the absolute position to the current angle. + * @method AbsoluteMotor#resetAngle * @param {number} angle Position to set (degrees from 0). * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). */ - public resetAbsoluteAngle (angle: [number, number] | number) { + public resetAngle (angle: [number, number] | number) { if (!this.isVirtualPort && angle instanceof Array) { throw new Error("Only virtual ports can accept multiple positions"); } @@ -85,7 +84,7 @@ export class AbsoluteMotor extends TachoMotor { if (angle instanceof Array) { message = Buffer.from([0x81, this.portId, 0x11, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); message.writeInt32LE(normalizeAngle(angle[0]), 4); - message.writeInt32LE(normalizeAngle(angle[0]), 8); + message.writeInt32LE(normalizeAngle(angle[1]), 8); } else { message = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]); message.writeInt32LE(normalizeAngle(angle), 5); @@ -97,6 +96,82 @@ export class AbsoluteMotor extends TachoMotor { }); } + public async calibrateServo () { + const oldMode = this.mode; + let currentAngle = 0; + const listener = ({ angle }: { angle: number }) => { + currentAngle = angle; + }; + this.on("absolute", listener); + await this.resetAngle(0); + await this.stop(); + this.gotoAngle(0, 50); + await this.hub.sleep(600); + await this.stop(); + await this.hub.sleep(500); + const absPositionAt0 = currentAngle; + this.gotoAngle(-160, 60); + await this.hub.sleep(600); + await this.stop(); + await this.hub.sleep(500); + const absPositionAtMin160 = currentAngle; + this.gotoAngle(160, 60); + await this.hub.sleep(600); + await this.stop(); + await this.hub.sleep(500); + const absPositionAt160 = currentAngle; + const midPoint1 = normalizeAngle((absPositionAtMin160 + absPositionAt160) / 2); + const midPoint2 = normalizeAngle(midPoint1 + 180); + const baseAngle = (Math.abs(normalizeAngle(midPoint1 - absPositionAt0)) < Math.abs(normalizeAngle(midPoint2 - absPositionAt0))) ? + roundAngleToNearest90(midPoint1) : + roundAngleToNearest90(midPoint2); + const resetToAngle = normalizeAngle(currentAngle - baseAngle); + await this.resetAngle(0); + await this.stop(); + this.gotoAngle(0, 40); + await this.hub.sleep(50); + await this.stop(); + await this.resetAngle(resetToAngle); + this.gotoAngle(0, 40); + await this.hub.sleep(600); + await this.stop(); + this.removeListener("absolute", listener); + if (oldMode !== undefined) { + this.subscribe(oldMode); + } + } + + public async resetServo (angle: number) { + const oldMode = this.mode; + let currentAngle = 0; + const listener = ({ angle }: { angle: number }) => { + currentAngle = angle; + }; + this.on("absolute", listener); + angle = Math.max(-180, Math.min(179, angle)); + const resetToAngle = normalizeAngle(currentAngle - angle); + await this.resetAngle(0); + await this.stop(); + this.gotoAngle(0, 40); + await this.hub.sleep(50); + await this.stop(); + await this.resetAngle(resetToAngle); + this.gotoAngle(0, 40); + await this.hub.sleep(500); + const diff = Math.abs(normalizeAngle(currentAngle - angle)); + if (diff > 5) { + await this.resetAngle(0); + await this.stop(); + this.gotoAngle(0, 40); + await this.hub.sleep(50); + await this.stop(); + } + this.removeListener("absolute", listener); + if (oldMode !== undefined) { + this.subscribe(oldMode); + } + } + } export enum Mode { diff --git a/src/devices/basicmotor.ts b/src/devices/basicmotor.ts index 922d00fc..c72151d5 100644 --- a/src/devices/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -29,7 +29,17 @@ export class BasicMotor extends Device { /** - * Fully (hard) stop the motor. + * Stop the motor. + * @method BasicMotor#stop + * @returns {Promise} Resolved upon successful completion of command. + */ + public stop () { + return this.setPower(0); + } + + + /** + * Brake the motor. * @method BasicMotor#brake * @returns {Promise} Resolved upon successful completion of command. */ diff --git a/src/devices/device.ts b/src/devices/device.ts index 2897abd0..fc34d28b 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -36,7 +36,6 @@ export class Device extends EventEmitter { } if (this.autoSubscribe) { if (this._modeMap[event] !== undefined) { - console.log(this._modeMap[event]); this.subscribe(this._modeMap[event]); } } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index ffceeab2..f7b192d4 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -48,16 +48,16 @@ export class TachoMotor extends BasicMotor { let message; if (time !== undefined) { if (speed instanceof Array) { - message = Buffer.from([0x81, this.portId, 0x11, 0x0a, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x0a, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7e, 0x00]); } else { - message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7e, 0x00]); } message.writeUInt16LE(time, 4); } else { if (speed instanceof Array) { - message = Buffer.from([0x81, this.portId, 0x11, 0x08, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x08, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7e, 0x00]); } else { - message = Buffer.from([0x81, this.portId, 0x11, 0x07, mapSpeed(speed), 0x64, 0x03, 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x07, mapSpeed(speed), 0x64, 0x03, 0x64, 0x7e, 0x00]); } } this.send(message); @@ -88,9 +88,9 @@ export class TachoMotor extends BasicMotor { } let message; if (speed instanceof Array) { - message = Buffer.from([0x81, this.portId, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed[0]), mapSpeed(speed[1]), 0x64, 0x7e, 0x00]); } else { - message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7f, 0x03]); + message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, mapSpeed(speed), 0x64, 0x7e, 0x00]); } message.writeUInt32LE(degrees, 4); this.send(message); diff --git a/src/interfaces.ts b/src/interfaces.ts index 3b399f19..5390575f 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -22,4 +22,5 @@ export interface IDeviceInterface extends EventEmitter { send: (message: Buffer, uuid: string, callback?: () => void) => void; subscribe: (portId: number, deviceType: number, mode: number) => void; isPortVirtual: (portId: number) => boolean; + sleep: (delay: number) => Promise; } diff --git a/src/utils.ts b/src/utils.ts index 4e330a47..590d9989 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -38,3 +38,20 @@ export const normalizeAngle = (angle: number) => { } return angle; }; + +export const roundAngleToNearest90 = (angle: number) => { + angle = normalizeAngle(angle); + if (angle < -135) { + return -180; + } + if (angle < -45) { + return -90; + } + if (angle < 45) { + return 0; + } + if (angle < 135) { + return 90; + } + return -180; +}; From 5fde49c0c20f31fbaae16f01be68cd0adc31c8d6 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 13 Jan 2020 10:58:57 -0800 Subject: [PATCH 50/62] Adding power and brightness ramping back in --- src/devices/absolutemotor.ts | 4 ++++ src/devices/basicmotor.ts | 29 +++++++++++++++++++++++++++-- src/devices/device.ts | 12 ++++++++++++ src/devices/light.ts | 28 ++++++++++++++++++++++++++-- src/devices/tachomotor.ts | 2 ++ src/utils.ts | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 4 deletions(-) diff --git a/src/devices/absolutemotor.ts b/src/devices/absolutemotor.ts index 54e2f534..817e7f5f 100644 --- a/src/devices/absolutemotor.ts +++ b/src/devices/absolutemotor.ts @@ -44,6 +44,7 @@ export class AbsoluteMotor extends TachoMotor { if (this.isWeDo2SmartHub) { throw new Error("Absolute positioning is not available on the WeDo 2.0 Smart Hub"); } + this.cancelEventTimer(); return new Promise((resolve) => { this._busy = true; if (speed === undefined || speed === null) { @@ -78,6 +79,7 @@ export class AbsoluteMotor extends TachoMotor { if (this.isWeDo2SmartHub) { throw new Error("Absolute positioning is not available on the WeDo 2.0 Smart Hub"); } + this.cancelEventTimer(); return new Promise((resolve) => { this._busy = true; let message; @@ -97,6 +99,7 @@ export class AbsoluteMotor extends TachoMotor { } public async calibrateServo () { + this.cancelEventTimer(); const oldMode = this.mode; let currentAngle = 0; const listener = ({ angle }: { angle: number }) => { @@ -142,6 +145,7 @@ export class AbsoluteMotor extends TachoMotor { } public async resetServo (angle: number) { + this.cancelEventTimer(); const oldMode = this.mode; let currentAngle = 0; const listener = ({ angle }: { angle: number }) => { diff --git a/src/devices/basicmotor.ts b/src/devices/basicmotor.ts index c72151d5..234d472b 100644 --- a/src/devices/basicmotor.ts +++ b/src/devices/basicmotor.ts @@ -4,7 +4,7 @@ import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; -import { mapSpeed } from "../utils"; +import { calculateRamp, mapSpeed } from "../utils"; export class BasicMotor extends Device { @@ -20,7 +20,10 @@ export class BasicMotor extends Device { * @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. * @returns {Promise} Resolved upon successful completion of command. */ - public setPower (power: number) { + public setPower (power: number, interrupt: boolean = true) { + if (interrupt) { + this.cancelEventTimer(); + } return new Promise((resolve) => { this.writeDirect(0x00, Buffer.from([mapSpeed(power)])); return resolve(); @@ -28,12 +31,33 @@ export class BasicMotor extends Device { } + /** + * Ramp the motor power. + * @method BasicMotor#rampPower + * @param {number} fromPower For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + * @param {number} toPower For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. + * @param {number} time How long the ramp should last (in milliseconds). + * @returns {Promise} Resolved upon successful completion of command. + */ + public rampPower (fromPower: number, toPower: number, time: number) { + this.cancelEventTimer(); + return new Promise((resolve) => { + calculateRamp(this, fromPower, toPower, time) + .on("changePower", (power) => { + this.setPower(power, false); + }) + .on("finished", resolve); + }); + } + + /** * Stop the motor. * @method BasicMotor#stop * @returns {Promise} Resolved upon successful completion of command. */ public stop () { + this.cancelEventTimer(); return this.setPower(0); } @@ -44,6 +68,7 @@ export class BasicMotor extends Device { * @returns {Promise} Resolved upon successful completion of command. */ public brake () { + this.cancelEventTimer(); return this.setPower(127); } diff --git a/src/devices/device.ts b/src/devices/device.ts index fc34d28b..36d659c9 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -20,6 +20,7 @@ export class Device extends EventEmitter { private _isWeDo2SmartHub: boolean; private _isVirtualPort: boolean = false; + private _eventTimer: NodeJS.Timer | null = null; constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) { super(); @@ -136,6 +137,17 @@ export class Device extends EventEmitter { } } + public setEventTimer (timer: NodeJS.Timer) { + this._eventTimer = timer; + } + + public cancelEventTimer () { + if (this._eventTimer) { + clearTimeout(this._eventTimer); + this._eventTimer = null; + } + } + private _ensureConnected () { if (!this.connected) { throw new Error("Device is not connected"); diff --git a/src/devices/light.ts b/src/devices/light.ts index 8d2ae6b9..4f2079b2 100644 --- a/src/devices/light.ts +++ b/src/devices/light.ts @@ -3,6 +3,7 @@ import { Device } from "./device"; import { IDeviceInterface } from "../interfaces"; import * as Consts from "../consts"; +import { calculateRamp } from "../utils"; export class Light extends Device { @@ -14,11 +15,14 @@ export class Light extends Device { /** * Set the light brightness. - * @method Light#brightness + * @method Light#setBrightness * @param {number} brightness Brightness value between 0-100 (0 is off) * @returns {Promise} Resolved upon successful completion of command. */ - public setBrightness (brightness: number) { + public setBrightness (brightness: number, interrupt: boolean = true) { + if (interrupt) { + this.cancelEventTimer(); + } return new Promise((resolve) => { this.writeDirect(0x00, Buffer.from([brightness])); return resolve(); @@ -26,4 +30,24 @@ export class Light extends Device { } + /** + * Ramp the light brightness. + * @method Light#rampBrightness + * @param {number} fromBrightness Brightness value between 0-100 (0 is off) + * @param {number} toBrightness Brightness value between 0-100 (0 is off) + * @param {number} time How long the ramp should last (in milliseconds). + * @returns {Promise} Resolved upon successful completion of command. + */ + public rampBrightness (fromBrightness: number, toBrightness: number, time: number) { + this.cancelEventTimer(); + return new Promise((resolve) => { + calculateRamp(this, fromBrightness, toBrightness, time) + .on("changePower", (power) => { + this.setBrightness(power, false); + }) + .on("finished", resolve); + }); + } + + } diff --git a/src/devices/tachomotor.ts b/src/devices/tachomotor.ts index f7b192d4..9e2f9f46 100644 --- a/src/devices/tachomotor.ts +++ b/src/devices/tachomotor.ts @@ -40,6 +40,7 @@ export class TachoMotor extends BasicMotor { if (this.isWeDo2SmartHub) { throw new Error("Motor speed is not available on the WeDo 2.0 Smart Hub"); } + this.cancelEventTimer(); return new Promise((resolve) => { this._busy = true; if (speed === undefined || speed === null) { @@ -81,6 +82,7 @@ export class TachoMotor extends BasicMotor { if (this.isWeDo2SmartHub) { throw new Error("Rotation is not available on the WeDo 2.0 Smart Hub"); } + this.cancelEventTimer(); return new Promise((resolve) => { this._busy = true; if (speed === undefined || speed === null) { diff --git a/src/utils.ts b/src/utils.ts index 590d9989..36710dea 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,6 @@ +import { EventEmitter } from "events"; +import { Device } from "./devices/device"; + // @ts-ignore export const isWebBluetooth = (typeof navigator !== "undefined" && navigator && navigator.bluetooth); @@ -55,3 +58,33 @@ export const roundAngleToNearest90 = (angle: number) => { } return -180; }; + +export const calculateRamp = (device: Device, fromPower: number, toPower: number, time: number) => { + const emitter = new EventEmitter(); + const steps = Math.abs(toPower - fromPower); + let delay = time / steps; + let increment = 1; + if (delay < 50 && steps > 0) { + increment = 50 / delay; + delay = 50; + } + if (fromPower > toPower) { + increment = -increment; + } + let i = 0; + const interval = setInterval(() => { + let power = Math.round(fromPower + (++i * increment)); + if (toPower > fromPower && power > toPower) { + power = toPower; + } else if (fromPower > toPower && power < toPower) { + power = toPower; + } + emitter.emit("changePower", power); + if (power === toPower) { + clearInterval(interval); + emitter.emit("finished"); + } + }, delay); + device.setEventTimer(interval); + return emitter; +}; From ea20b1bee04a150aa44b62e76fa7bb4d21927898 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 13 Jan 2020 12:17:51 -0800 Subject: [PATCH 51/62] Can not retrieve human readable device type from devices. Updated web bluetooth example --- src/devices/device.ts | 4 ++++ src/hubs/lpf2hub.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/devices/device.ts b/src/devices/device.ts index 36d659c9..245c5d66 100644 --- a/src/devices/device.ts +++ b/src/devices/device.ts @@ -81,6 +81,10 @@ export class Device extends EventEmitter { return this._type; } + public get typeName () { + return Consts.DeviceTypeNames[this.type]; + } + public get mode () { return this._mode; } diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index 2f503df0..8d980f5d 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -139,6 +139,8 @@ export class LPF2Hub extends BaseHub { const callback = this._propertyRequestCallbacks[property]; if (callback) { callback(message); + } else { + this._parseHubPropertyResponse(message); } delete this._propertyRequestCallbacks[property]; break; @@ -193,6 +195,7 @@ export class LPF2Hub extends BaseHub { // Button press reports if (message[3] === 0x02) { + console.log("BUTTON PRESS", message); if (message[5] === 1) { /** * Emits when a button is pressed. From dfd22b1d3d94c92535c718c11e91d179bfe0ddc6 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 13 Jan 2020 12:18:00 -0800 Subject: [PATCH 52/62] Can not retrieve human readable device type from devices. Updated web bluetooth example --- examples/web_bluetooth.html | 55 ++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/examples/web_bluetooth.html b/examples/web_bluetooth.html index 5e63c35d..c6425370 100644 --- a/examples/web_bluetooth.html +++ b/examples/web_bluetooth.html @@ -3,8 +3,8 @@ Web Bluetooth node-poweredup Example - - + + - - - - - -

Vernie / PlayStation DualShock 4 Remote Control

- -
-Current Color:
 
-
- - - \ No newline at end of file diff --git a/examples/winter_village_train.js b/examples/winter_village_train.js deleted file mode 100644 index 7e27f403..00000000 --- a/examples/winter_village_train.js +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * This runs the train under our Christmas tree. It uses the 10254 Winter Holiday Train retrofitted with a Powered UP hub and train motor. - * It also uses a WeDo 2.0 hub with Powered UP distance sensor to detect the train approaching the station and slow it down. - * - * Note that if you want to use this yourself you don't need to use a WeDo 2.0 hub, you can use any hub that can accept a distance or color/distance sensor. - * - * The maximum speed of the train is set to a constant 50. A further improvement can be made by scaling the speed up according to the current battery voltage, - * so the speed doesn't slow as the battery starts dying. - * - */ - -const PoweredUP = require(".."); - -const poweredUP = new PoweredUP.PoweredUP(); -poweredUP.scan(); // Start scanning for hubs - -console.log("Looking for Hubs..."); - -let train = null; -let sensor = null; - -let ramping = false; - -poweredUP.on("discover", async (hub) => { // Wait to discover hubs - - if (hub.name === "NK_Winter_Train") { - await hub.connect(); // Connect to hub - console.log(`Connected to train!`); - train = hub; - } else if (hub.name === "NK_Winter_Sensor") { - await hub.connect(); // Connect to hub - console.log(`Connected to sensor!`); - sensor = hub; - - sensor.on("distance", (_, distance) => { - if (distance < 5 && !ramping) { - await stopTrain(); - } - }); - - } - - if (train && sensor) { - console.log("Train and sensor connected, starting!"); - await startTrain(); - } - -}); - -const startTrain = async () => { - ramping = true; - await train.rampMotorSpeed("A", 0, 50, 2000); - ramping = false; -} - -const stopTrain = async () => { - ramping = true; - await train.rampMotorSpeed("A", 50, 0, 2000); -} \ No newline at end of file From c0009f8f08d8a81c94520e66a65a71c0f044dad3 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Fri, 7 Feb 2020 14:28:32 -0800 Subject: [PATCH 60/62] Updated vernie sample, added braking style --- README.md | 15 +- docs/BaseHub.html | 1419 +++ docs/DuploTrainBase.html | 4646 +------- docs/Hub.html | 855 +- docs/LPF2Hub.html | 3611 +----- docs/MoveHub.html | 2412 ++++ docs/PoweredUP.html | 206 +- docs/RemoteControl.html | 2412 ++++ docs/TechnicMediumHub.html | 2412 ++++ docs/WeDo2SmartHub.html | 4009 +------ docs/classes.list.html | 9932 ++--------------- docs/consts.js.html | 105 +- docs/devices_absolutemotor.js.html | 296 + docs/devices_basicmotor.js.html | 285 + docs/devices_colordistancesensor.js.html | 295 + docs/devices_currentsensor.js.html | 275 + .../devices_duplotrainbasecolorsensor.js.html | 256 + docs/devices_duplotrainbasespeaker.js.html | 250 + .../devices_duplotrainbasespeedometer.js.html | 254 + docs/devices_hubled.js.html | 281 + docs/devices_light.js.html | 267 + docs/devices_motionsensor.js.html | 258 + docs/devices_movehubtiltsensor.js.html | 256 + docs/devices_piezobuzzer.js.html | 249 + docs/devices_remotecontrolbutton.js.html | 260 + docs/devices_tachomotor.js.html | 336 + docs/devices_techniccolorsensor.js.html | 278 + docs/devices_technicdistancesensor.js.html | 277 + docs/devices_technicforcesensor.js.html | 276 + ...echnicmediumhubaccelerometersensor.js.html | 259 + ...devices_technicmediumhubgyrosensor.js.html | 258 + ...devices_technicmediumhubtiltsensor.js.html | 258 + docs/devices_tiltsensor.js.html | 256 + docs/devices_voltagesensor.js.html | 279 + docs/global.html | 324 +- docs/hubs_basehub.js.html | 543 + docs/hubs_duplotrainbase.js.html | 263 + docs/hubs_hub.js.html | 275 + docs/hubs_lpf2hub.js.html | 552 + docs/hubs_movehub.js.html | 277 + docs/hubs_remotecontrol.js.html | 265 + docs/hubs_technicmediumhub.js.html | 271 + docs/hubs_wedo2smarthub.js.html | 417 + docs/index.html | 28 +- docs/poweredup-node.js.html | 84 +- docs/quicksearch.html | 2 +- examples/vernie_remote.js | 52 +- package.json | 8 +- src/consts.ts | 11 + src/devices/absolutemotor.ts | 114 +- src/devices/basicmotor.ts | 2 +- src/devices/tachomotor.ts | 20 +- src/hubs/technicmediumhub.ts | 59 - 53 files changed, 19883 insertions(+), 21677 deletions(-) create mode 100644 docs/BaseHub.html create mode 100644 docs/MoveHub.html create mode 100644 docs/RemoteControl.html create mode 100644 docs/TechnicMediumHub.html create mode 100644 docs/devices_absolutemotor.js.html create mode 100644 docs/devices_basicmotor.js.html create mode 100644 docs/devices_colordistancesensor.js.html create mode 100644 docs/devices_currentsensor.js.html create mode 100644 docs/devices_duplotrainbasecolorsensor.js.html create mode 100644 docs/devices_duplotrainbasespeaker.js.html create mode 100644 docs/devices_duplotrainbasespeedometer.js.html create mode 100644 docs/devices_hubled.js.html create mode 100644 docs/devices_light.js.html create mode 100644 docs/devices_motionsensor.js.html create mode 100644 docs/devices_movehubtiltsensor.js.html create mode 100644 docs/devices_piezobuzzer.js.html create mode 100644 docs/devices_remotecontrolbutton.js.html create mode 100644 docs/devices_tachomotor.js.html create mode 100644 docs/devices_techniccolorsensor.js.html create mode 100644 docs/devices_technicdistancesensor.js.html create mode 100644 docs/devices_technicforcesensor.js.html create mode 100644 docs/devices_technicmediumhubaccelerometersensor.js.html create mode 100644 docs/devices_technicmediumhubgyrosensor.js.html create mode 100644 docs/devices_technicmediumhubtiltsensor.js.html create mode 100644 docs/devices_tiltsensor.js.html create mode 100644 docs/devices_voltagesensor.js.html create mode 100644 docs/hubs_basehub.js.html create mode 100644 docs/hubs_duplotrainbase.js.html create mode 100644 docs/hubs_hub.js.html create mode 100644 docs/hubs_lpf2hub.js.html create mode 100644 docs/hubs_movehub.js.html create mode 100644 docs/hubs_remotecontrol.js.html create mode 100644 docs/hubs_technicmediumhub.js.html create mode 100644 docs/hubs_wedo2smarthub.js.html diff --git a/README.md b/README.md index dbb17718..2c86c9aa 100644 --- a/README.md +++ b/README.md @@ -69,17 +69,22 @@ const poweredUP = new PoweredUP.PoweredUP(); poweredUP.on("discover", async (hub) => { // Wait to discover a Hub console.log(`Discovered ${hub.name}!`); await hub.connect(); // Connect to the Hub + const motorA = hub.waitForDeviceAtPort("A"); // Make sure a motor is plugged into port A + const motorB = hub.waitForDeviceAtPort("B"); // Make sure a motor is plugged into port B console.log("Connected"); - await hub.sleep(3000); // Sleep for 3 seconds before starting while (true) { // Repeat indefinitely console.log("Running motor B at speed 75"); - hub.setMotorSpeed("B", 75); // Start a motor attached to port B to run a 3/4 speed (75) indefinitely + motorB.setPower("B", 75); // Start a motor attached to port B to run a 3/4 speed (75) indefinitely console.log("Running motor A at speed 100 for 2 seconds"); - await hub.setMotorSpeed("A", 100, 2000); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop + motorA.setPower("A", 100); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop + await hub.sleep(2000); + motorA.setPower("A", 0); await hub.sleep(1000); // Do nothing for 1 second - console.log("Running motor A at speed -50 for 1 seconds"); - await hub.setMotorSpeed("A", -50, 1000); // Run a motor attached to port A for 1 second at 1/2 speed in reverse (-50) then stop + console.log("Running motor A at speed -50 for 1 second"); + motorA.setPower("A", -50); // Run a motor attached to port A for 1 second at 1/2 speed in reverse (-50) then stop + hub.sleep(1000); + motorA.setPower("A", 0); await hub.sleep(1000); // Do nothing for 1 second } }); diff --git a/docs/BaseHub.html b/docs/BaseHub.html new file mode 100644 index 00000000..b582d8f9 --- /dev/null +++ b/docs/BaseHub.html @@ -0,0 +1,1419 @@ + + + + + + + node-poweredup Class: BaseHub + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: BaseHub

+
+ +
+ +

+ BaseHub +

+ + +
+ + +
+
+ + +
+
+

new BaseHub()

+ + +
+
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + +
    +
  • EventEmitter
  • +
+ + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> batteryLevel

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
batteryLevel + + +number + + + + +

Battery level of the hub (Percentage between 0-100)

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> firmwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Firmware version of the hub

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hardwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Hardware version of the hub

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> name

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

Name of the hub

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> ports

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
ports + + +Array.<string> + + + + +

Array of port names

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> primaryMACAddress

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
primaryMACAddress + + +string + + + + +

Primary MAC address of the hub

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> rssi

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
rssi + + +number + + + + +

Signal strength of the hub

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +string + + + + +

Hub type

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> uuid

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
uuid + + +string + + + + +

UUID of the hub

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + + + + + +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/DuploTrainBase.html b/docs/DuploTrainBase.html index b5f2e065..48883edb 100644 --- a/docs/DuploTrainBase.html +++ b/docs/DuploTrainBase.html @@ -33,21 +33,14 @@ - - @@ -152,8 +145,8 @@

new Dup
@@ -194,7 +187,7 @@

Extends

@@ -290,17 +283,12 @@
Properties:
- -
Overrides:
-
@@ -326,8 +314,8 @@
Properties:
@@ -347,8 +335,8 @@
Properties:

-
-

<readonly> current

+
+

<readonly> firmwareVersion

@@ -387,13 +375,13 @@
Properties:
- current + firmwareVersion -number +string @@ -404,7 +392,7 @@
Properties:
-

Current usage of the hub (Milliamps)

+

Firmware version of the hub

@@ -423,17 +411,12 @@
Properties:
- -
Overrides:
-
@@ -459,8 +442,8 @@
Properties:
@@ -480,8 +463,8 @@
Properties:

-
-

<readonly> firmwareVersion

+
+

<readonly> hardwareVersion

@@ -537,7 +520,7 @@
Properties:
-

Firmware version of the hub

+

Hardware version of the hub

@@ -556,17 +539,12 @@
Properties:
- -
Overrides:
-
@@ -592,8 +570,8 @@
Properties:
@@ -613,8 +591,8 @@
Properties:

-
-

<readonly> hardwareVersion

+
+

<readonly> name

@@ -653,7 +631,7 @@
Properties:
- firmwareVersion + name @@ -670,7 +648,7 @@
Properties:
-

Hardware version of the hub

+

Name of the hub

@@ -689,17 +667,12 @@
Properties:
- -
Overrides:
-
@@ -725,8 +698,8 @@
Properties:
@@ -746,8 +719,8 @@
Properties:

-
-

<readonly> name

+
+

<readonly> ports

@@ -786,13 +759,13 @@
Properties:
- name + ports -string +Array.<string> @@ -803,7 +776,7 @@
Properties:
-

Name of the hub

+

Array of port names

@@ -822,17 +795,12 @@
Properties:
- -
Overrides:
-
@@ -858,8 +826,8 @@
Properties:
@@ -955,17 +923,12 @@
Properties:
- -
Overrides:
-
@@ -991,8 +954,8 @@
Properties:
@@ -1088,17 +1051,12 @@
Properties:
- -
Overrides:
-
@@ -1124,8 +1082,8 @@
Properties:
@@ -1145,8 +1103,8 @@
Properties:

-
-

<readonly> uuid

+
+

<readonly> type

@@ -1185,7 +1143,7 @@
Properties:
- uuid + type @@ -1202,7 +1160,7 @@
Properties:
-

UUID of the hub

+

Hub type

@@ -1221,17 +1179,12 @@
Properties:
- -
Overrides:
-
@@ -1257,8 +1210,8 @@
Properties:
@@ -1278,8 +1231,8 @@
Properties:

-
-

<readonly> voltage

+
+

<readonly> uuid

@@ -1318,13 +1271,13 @@
Properties:
- voltage + uuid -number +string @@ -1335,7 +1288,7 @@
Properties:
-

Voltage of the hub (Volts)

+

UUID of the hub

@@ -1354,17 +1307,12 @@
Properties:
- -
Overrides:
-
@@ -1390,8 +1338,8 @@
Properties:
@@ -1418,7 +1366,7 @@

Methods


-

brakeMotor(port)

+

setName(name)

@@ -1426,7 +1374,7 @@

brakeMotor(
-

Fully (hard) stop the motor on a given port.

+

Set the name of the Hub.

@@ -1461,7 +1409,7 @@

Parameters:
- port + name @@ -1478,7 +1426,7 @@
Parameters:
- +

New name of the hub (14 characters or less, ASCII only).

@@ -1496,6 +1444,15 @@
Parameters:
+
Inherited From:
+
+ +
+ @@ -1522,8 +1479,8 @@
Parameters:
@@ -1552,7 +1509,7 @@
Returns:
-

Resolved upon successful completion of command.

+

Resolved upon successful issuance of command.

@@ -1580,7 +1537,7 @@
Returns:

-

connect()

+

shutdown()

@@ -1588,7 +1545,7 @@

connect()

-

Connect to the Hub.

+

Shutdown the Hub.

@@ -1612,17 +1569,12 @@

connect()

- -
Overrides:
-
@@ -1648,8 +1600,8 @@

connect()

@@ -1678,7 +1630,7 @@
Returns:
-

Resolved upon successful connect.

+

Resolved upon successful disconnect.

@@ -1702,11 +1654,19 @@
Returns:
- + + + + + + +

Events

+ +

-

disconnect()

+

accel

@@ -1714,7 +1674,7 @@

disconnect(
-

Disconnect the Hub.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -1724,131 +1684,127 @@

disconnect( +

Parameters:
+ - -
- + + + + + + - + - + - -
Inherited From:
-
- -
- + - -
Overrides:
-
- + + + + - + + + + - + - + - + + + + - -
Source:
-
- -
- + + + + - + - + - + - + + - + + + + - -
Returns:
- + + + - - + + + + + + + + + + - -
-

Get the hub type.

-
- + - + - + + + +
NameTypeDescription
port + + +string - - - + +
x + + +number - - - + +
y -
-

Resolved upon successful disconnect.

-
+ +number -
-
- Type -
-
- -Promise + +
z + + +number - - - - -
-
-

getHubType()

- - -
-
+
+ @@ -1864,17 +1820,12 @@

getHubType(
- -
Overrides:
-
@@ -1900,8 +1851,8 @@

getHubType(
@@ -1926,26 +1877,6 @@

getHubType( -

Returns:
- - - - -
-
- Type -
-
- -HubType - - - -
-
- - - @@ -1954,7 +1885,7 @@
Returns:

-

getPortDeviceType(port)

+

button

@@ -1962,7 +1893,7 @@

getP
-

Get the device type for a given port.

+

Emits when a button is pressed.

@@ -1997,7 +1928,7 @@

Parameters:
- port + button @@ -2018,4183 +1949,16 @@
Parameters:
- - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -DeviceType - - - -
-
- - - - - - - - - -
-
-

playSound(sound)

- - -
-
- - -
-

Play a built-in train sound.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
sound - - -DuploTrainBaseSound - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

rampMotorSpeed(port, fromSpeed, toSpeed, time)

- - -
-
- - -
-

Ramp the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
fromSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

toSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

time - - -number - - - - -

How long the ramp should last (in milliseconds).

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDColor(color)

- - -
-
- - -
-

Set the color of the LED on the Hub via a color value.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
color - - -Color - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDRGB(red, green, blue)

- - -
-
- - -
-

Set the color of the LED on the Hub via RGB values.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
red - - -number - - - - -
green - - -number - - - - -
blue - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setMotorSpeed(port, speed [, time])

- - -
-
- - -
-

Set the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
speed - - -number -| - -Array.<number> - - - - - - - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

time - - -number - - - - - - - <optional>
- - - - - -

How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command. If time is specified, this is once the motor is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setName(name)

- - -
-
- - -
-

Set the name of the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
name - - -string - - - - -

New name of the hub (14 characters or less, ASCII only).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

shutdown()

- - -
-
- - -
-

Shutdown the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

sleep(delay)

- - -
-
- - -
-

Sleep a given amount of time.

-

This is a helper method to make it easier to add delays into a chain of commands.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
delay - - -number - - - - -

How long to sleep (in milliseconds).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the delay is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

subscribe(port [, mode])

- - -
-
- - -
-

Subscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
mode - - -number - - - - - - - <optional>
- - - - - -

The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

unsubscribe(port)

- - -
-
- - -
-

Unsubscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

wait(commands)

- - -
-
- - -
-

Wait until a given list of concurrently running commands are complete.

-

This is a helper method to make it easier to wait for concurrent commands to complete.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
commands - - -Array.<Promise.<any>> - - - - -

Array of executing commands.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the commands are finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- -
- - - - - -

Events

- -
- -
-
-

accel

- - -
-
- - -
-

Emits when accelerometer detects movement. Measured in mG.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

button

- - -
-
- - -
-

Emits when a button is pressed.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
button - - -string - - - - -
state - - -ButtonState - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

color

- - -
-
- - -
-

Emits when a color sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

colorAndDistance

- - -
-
- - -
-

A combined color and distance event, emits when the sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

detach

- - -
-
- - -
-

Emits when an attached motor or sensor is detached from the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

disconnect

- - -
-
- - -
-

Emits when the hub is disconnected.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

distance

- - -
-
- - -
-

Emits when a distance sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

gyro

- - -
-
- - -
-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

rotate

- - -
-
- - -
-

Emits when a rotation sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
rotation - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

speed

- - -
-
- - -
-

Emits on a speed change.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
speed - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

temp

- - -
-
- - -
-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - @@ -6404,30 +2144,6 @@
Parameters:
- - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

tempstate -number +ButtonState @@ -6227,7 +1991,7 @@
Parameters:
@@ -6258,8 +2022,8 @@
Parameters:
@@ -6333,30 +2097,6 @@
Parameters:
port - - -string - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x
z - - -number - - - - -

(Only available when using a Control+ Hub)

@@ -6477,8 +2193,8 @@
Parameters:
@@ -6557,7 +2273,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/Hub.html b/docs/Hub.html index 4326aede..19999146 100644 --- a/docs/Hub.html +++ b/docs/Hub.html @@ -33,21 +33,14 @@ - - @@ -105,6 +98,10 @@

new Hub()

+
+

The Hub is emitted if the discovered device is a Hub.

+
+ @@ -148,8 +145,8 @@

new Hub()

@@ -188,7 +185,9 @@

Extends

@@ -280,6 +279,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -306,8 +314,8 @@
Properties:
@@ -327,8 +335,8 @@
Properties:

-
-

<readonly> current

+
+

<readonly> firmwareVersion

@@ -367,13 +375,13 @@
Properties:
- current + firmwareVersion -number +string @@ -384,7 +392,7 @@
Properties:
-

Current usage of the hub (Milliamps)

+

Firmware version of the hub

@@ -399,6 +407,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -425,8 +442,8 @@
Properties:
@@ -446,8 +463,8 @@
Properties:

-
-

<readonly> firmwareVersion

+
+

<readonly> hardwareVersion

@@ -503,7 +520,7 @@
Properties:
-

Firmware version of the hub

+

Hardware version of the hub

@@ -518,6 +535,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -544,8 +570,8 @@
Properties:
@@ -565,8 +591,8 @@
Properties:

-
-

<readonly> hardwareVersion

+
+

<readonly> name

@@ -605,7 +631,7 @@
Properties:
- firmwareVersion + name @@ -622,7 +648,7 @@
Properties:
-

Hardware version of the hub

+

Name of the hub

@@ -637,6 +663,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -663,8 +698,8 @@
Properties:
@@ -684,8 +719,8 @@
Properties:

-
-

<readonly> name

+
+

<readonly> ports

@@ -724,13 +759,13 @@
Properties:
- name + ports -string +Array.<string> @@ -741,7 +776,7 @@
Properties:
-

Name of the hub

+

Array of port names

@@ -756,6 +791,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -782,8 +826,8 @@
Properties:
@@ -875,6 +919,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -901,8 +954,8 @@
Properties:
@@ -994,6 +1047,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -1020,8 +1082,8 @@
Properties:
@@ -1041,8 +1103,8 @@
Properties:

-
-

<readonly> uuid

+
+

<readonly> type

@@ -1081,7 +1143,7 @@
Properties:
- uuid + type @@ -1098,7 +1160,7 @@
Properties:
-

UUID of the hub

+

Hub type

@@ -1113,6 +1175,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -1139,8 +1210,8 @@
Properties:
@@ -1160,8 +1231,8 @@
Properties:

-
-

<readonly> voltage

+
+

<readonly> uuid

@@ -1200,13 +1271,13 @@
Properties:
- voltage + uuid -number +string @@ -1217,7 +1288,7 @@
Properties:
-

Voltage of the hub (Volts)

+

UUID of the hub

@@ -1232,6 +1303,15 @@
Properties:
+
Inherited From:
+
+ +
+ @@ -1258,8 +1338,8 @@
Properties:
@@ -1340,8 +1420,8 @@

connect()

@@ -1452,8 +1532,8 @@

disconnect(
@@ -1510,7 +1590,7 @@

Returns:

-

getHubType()

+

setName(name)

@@ -1518,7 +1598,7 @@

getHubType(
-

Get the hub type.

+

Set the name of the Hub.

@@ -1528,6 +1608,56 @@

getHubType( +

Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

New name of the hub (14 characters or less, ASCII only).

+ +
@@ -1538,6 +1668,15 @@

getHubType( +
Inherited From:
+
+ +
+ @@ -1564,8 +1703,8 @@

getHubType(
@@ -1593,6 +1732,10 @@

getHubType(

Returns:
+
+

Resolved upon successful issuance of command.

+
+
@@ -1601,7 +1744,7 @@
Returns:
-HubType +Promise @@ -1618,7 +1761,7 @@
Returns:

-

getPortDeviceType(port)

+

shutdown()

@@ -1626,7 +1769,7 @@

getP
-

Get the device type for a given port.

+

Shutdown the Hub.

@@ -1636,56 +1779,6 @@

getP -

Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- -
@@ -1696,6 +1789,15 @@
Parameters:
+
Inherited From:
+
+ +
+ @@ -1722,8 +1824,8 @@
Parameters:
@@ -1751,6 +1853,10 @@
Parameters:
Returns:
+
+

Resolved upon successful disconnect.

+
+
@@ -1759,7 +1865,7 @@
Returns:
-DeviceType +Promise @@ -1881,8 +1987,8 @@
Parameters:
@@ -1939,7 +2045,7 @@
Returns:

-

subscribe(port [, mode])

+

wait(commands)

@@ -1947,7 +2053,8 @@

subscribe(po
-

Subscribe to sensor notifications on a given port.

+

Wait until a given list of concurrently running commands are complete.

+

This is a helper method to make it easier to wait for concurrent commands to complete.

@@ -1970,8 +2077,6 @@

Parameters:
Type - Argument - @@ -1984,13 +2089,13 @@
Parameters:
- port + commands -string +Array.<Promise.<any>> @@ -1998,69 +2103,27 @@
Parameters:
- - - - - - - - - +

Array of executing commands.

+ + - - - mode - + - - - -number + +
+ + + - - - - - - - <optional>
- - - - - - - - - - -

The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

- - - - - - - - - -
- - - - - - - + @@ -2087,8 +2150,8 @@
Parameters:
@@ -2117,7 +2180,7 @@
Returns:
-

Resolved upon successful issuance of command.

+

Resolved after the commands are finished.

@@ -2141,11 +2204,19 @@
Returns:
- +
+ + + + + +

Events

+ +

-

unsubscribe(port)

+

accel

@@ -2153,7 +2224,7 @@

unsubscrib
-

Unsubscribe to sensor notifications on a given port.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -2205,6 +2276,78 @@

Parameters:
+ + + + + + + + x + + + + + +number + + + + + + + + + + + + + + + + + + y + + + + + +number + + + + + + + + + + + + + + + + + + z + + + + + +number + + + + + + + + + + @@ -2223,6 +2366,15 @@
Parameters:
+
Inherited From:
+
+ +
+ @@ -2249,8 +2401,8 @@
Parameters:
@@ -2275,30 +2427,6 @@
Parameters:
-
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - @@ -2307,7 +2435,7 @@
Returns:

-

wait(commands)

+

attach

@@ -2315,8 +2443,7 @@

wait(commands) -

Wait until a given list of concurrently running commands are complete.

-

This is a helper method to make it easier to wait for concurrent commands to complete.

+

Emits when a device is attached to the Hub.

@@ -2351,13 +2478,13 @@

Parameters:
- commands + device -Array.<Promise.<any>> +Device @@ -2368,7 +2495,7 @@
Parameters:
-

Array of executing commands.

+ @@ -2412,8 +2539,8 @@
Parameters:
@@ -2438,43 +2565,11 @@
Parameters:
-
Returns:
- - -
-

Resolved after the commands are finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - -
- - - - - -

Events

- -
+
@@ -2486,7 +2581,7 @@

attach

-

Emits when a motor or sensor is attached to the Hub.

+

Emits when a device is detached from the Hub.

@@ -2521,37 +2616,13 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - type + device -DeviceType +Device @@ -2606,8 +2677,8 @@
Parameters:
@@ -2640,7 +2711,7 @@
Parameters:

-

detach

+

button

@@ -2648,7 +2719,7 @@

detach

-

Emits when an attached motor or sensor is detached from the Hub.

+

Emits when a button is pressed.

@@ -2683,7 +2754,7 @@
Parameters:
- port + button @@ -2700,6 +2771,30 @@
Parameters:
+ + + + + + + + state + + + + + +ButtonState + + + + + + + + + + @@ -2718,6 +2813,15 @@
Parameters:
+
Inherited From:
+
+ +
+ @@ -2744,8 +2848,8 @@
Parameters:
@@ -2832,8 +2936,179 @@

disconnect

+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
+
+

tilt

+ + +
+
+ + +
+

Emits when a tilt sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
x + + +number + + + + +
y + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+
@@ -2912,7 +3187,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/LPF2Hub.html b/docs/LPF2Hub.html index 4a95752b..9a8d0b46 100644 --- a/docs/LPF2Hub.html +++ b/docs/LPF2Hub.html @@ -33,21 +33,14 @@ - - @@ -148,8 +141,8 @@

new LPF2Hub()<
@@ -188,7 +181,7 @@

Extends

@@ -284,7 +277,7 @@
Properties:
@@ -315,8 +308,8 @@
Properties:
@@ -336,8 +329,8 @@
Properties:

-
-

<readonly> current

+
+

<readonly> firmwareVersion

@@ -376,13 +369,13 @@
Properties:
- current + firmwareVersion -number +string @@ -393,7 +386,7 @@
Properties:
-

Current usage of the hub (Milliamps)

+

Firmware version of the hub

@@ -412,7 +405,7 @@
Properties:
@@ -443,8 +436,8 @@
Properties:
@@ -464,8 +457,8 @@
Properties:

-
-

<readonly> firmwareVersion

+
+

<readonly> hardwareVersion

@@ -521,7 +514,7 @@
Properties:
-

Firmware version of the hub

+

Hardware version of the hub

@@ -540,7 +533,7 @@
Properties:
@@ -571,8 +564,8 @@
Properties:
@@ -592,8 +585,8 @@
Properties:

-
-

<readonly> hardwareVersion

+
+

<readonly> name

@@ -632,7 +625,7 @@
Properties:
- firmwareVersion + name @@ -649,7 +642,7 @@
Properties:
-

Hardware version of the hub

+

Name of the hub

@@ -668,7 +661,7 @@
Properties:
@@ -699,8 +692,8 @@
Properties:
@@ -720,8 +713,8 @@
Properties:

-
-

<readonly> name

+
+

<readonly> ports

@@ -760,13 +753,13 @@
Properties:
- name + ports -string +Array.<string> @@ -777,7 +770,7 @@
Properties:
-

Name of the hub

+

Array of port names

@@ -796,7 +789,7 @@
Properties:
@@ -827,8 +820,8 @@
Properties:
@@ -924,7 +917,7 @@
Properties:
@@ -955,8 +948,8 @@
Properties:
@@ -1052,7 +1045,7 @@
Properties:
@@ -1083,8 +1076,8 @@
Properties:
@@ -1104,8 +1097,8 @@
Properties:

-
-

<readonly> uuid

+
+

<readonly> type

@@ -1144,7 +1137,7 @@
Properties:
- uuid + type @@ -1161,7 +1154,7 @@
Properties:
-

UUID of the hub

+

Hub type

@@ -1180,7 +1173,7 @@
Properties:
@@ -1211,8 +1204,8 @@
Properties:
@@ -1232,8 +1225,8 @@
Properties:

-
-

<readonly> voltage

+
+

<readonly> uuid

@@ -1272,13 +1265,13 @@
Properties:
- voltage + uuid -number +string @@ -1289,7 +1282,7 @@
Properties:
-

Voltage of the hub (Volts)

+

UUID of the hub

@@ -1308,7 +1301,7 @@
Properties:
@@ -1339,8 +1332,8 @@
Properties:
@@ -1367,7 +1360,7 @@

Methods


-

connect()

+

setName(name)

@@ -1375,7 +1368,7 @@

connect()

-

Connect to the Hub.

+

Set the name of the Hub.

@@ -1385,132 +1378,56 @@

connect()

+
Parameters:
+ - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- + + + + + + - + - + - + - + + + + - -
Returns:
- + + + -
-

Resolved upon successful connect.

-
- - - -
-
- Type -
-
- -Promise - - -
-
+ - -
-
-

disconnect()

- - -
-
- -
-

Disconnect the Hub.

-
- + - +
+ + +
NameTypeDescription
name + + +string - - - + +

New name of the hub (14 characters or less, ASCII only).

-
@@ -1521,15 +1438,6 @@

disconnect( -
Inherited From:
-
- -
- @@ -1556,8 +1464,8 @@

disconnect(
@@ -1586,7 +1494,7 @@

Returns:
-

Resolved upon successful disconnect.

+

Resolved upon successful issuance of command.

@@ -1614,7 +1522,7 @@
Returns:

-

getHubType()

+

shutdown()

@@ -1622,7 +1530,7 @@

getHubType(
-

Get the hub type.

+

Shutdown the Hub.

@@ -1642,15 +1550,6 @@

getHubType( -
Inherited From:
-
- -
- @@ -1677,8 +1576,8 @@

getHubType(
@@ -1706,6 +1605,10 @@

getHubType(

Returns:
+
+

Resolved upon successful disconnect.

+
+
@@ -1714,7 +1617,7 @@
Returns:
-HubType +Promise @@ -1727,11 +1630,19 @@
Returns:
- +
+ + + + + +

Events

+ +

-

getPortDeviceType(port)

+

accel

@@ -1739,7 +1650,7 @@

getP
-

Get the device type for a given port.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -1795,3059 +1706,49 @@

Parameters:
- - - + + + x + - -
- + + + +number - - - -
Inherited From:
-
- -
- + + - + - + - + + - + + + y + - + + + +number - - - + + + - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -DeviceType - - - -
-
- - - - - -
- - - -
-
-

setLEDColor(color)

- - -
-
- - -
-

Set the color of the LED on the Hub via a color value.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
color - - -Color - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDRGB(red, green, blue)

- - -
-
- - -
-

Set the color of the LED on the Hub via RGB values.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
red - - -number - - - - -
green - - -number - - - - -
blue - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setName(name)

- - -
-
- - -
-

Set the name of the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
name - - -string - - - - -

New name of the hub (14 characters or less, ASCII only).

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

shutdown()

- - -
-
- - -
-

Shutdown the Hub.

-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

sleep(delay)

- - -
-
- - -
-

Sleep a given amount of time.

-

This is a helper method to make it easier to add delays into a chain of commands.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
delay - - -number - - - - -

How long to sleep (in milliseconds).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the delay is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

subscribe(port [, mode])

- - -
-
- - -
-

Subscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
mode - - -number - - - - - - - <optional>
- - - - - -

The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

unsubscribe(port)

- - -
-
- - -
-

Unsubscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

wait(commands)

- - -
-
- - -
-

Wait until a given list of concurrently running commands are complete.

-

This is a helper method to make it easier to wait for concurrent commands to complete.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
commands - - -Array.<Promise.<any>> - - - - -

Array of executing commands.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the commands are finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- -
- - - - - -

Events

- -
- -
-
-

accel

- - -
-
- - -
-

Emits when accelerometer detects movement. Measured in mG.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

button

- - -
-
- - -
-

Emits when a button is pressed.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
button - - -string - - - - -
state - - -ButtonState - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

color

- - -
-
- - -
-

Emits when a color sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

colorAndDistance

- - -
-
- - -
-

A combined color and distance event, emits when the sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

detach

- - -
-
- - -
-

Emits when an attached motor or sensor is detached from the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

disconnect

- - -
-
- - -
-

Emits when the hub is disconnected.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

distance

- - -
-
- - -
-

Emits when a distance sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

gyro

- - -
-
- - -
-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

rotate

- - -
-
- - -
-

Emits when a rotation sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -4856,7 +1757,7 @@
Parameters:
- + - + - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
rotationz @@ -4917,8 +1818,8 @@
Parameters:
@@ -4951,7 +1852,7 @@
Parameters:

-

speed

+

button

@@ -4959,7 +1860,7 @@

speed

-

Emits on a speed change.

+

Emits when a button is pressed.

@@ -4994,7 +1895,7 @@
Parameters:
portbutton @@ -5018,175 +1919,13 @@
Parameters:
speed - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

temp

- - -
-
- - -
-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - @@ -5387,30 +2102,6 @@
Parameters:
- - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

tempstate -number +ButtonState @@ -5241,8 +1980,8 @@
Parameters:
@@ -5316,30 +2055,6 @@
Parameters:
port - - -string - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x
z - - -number - - - - -

(Only available when using a Control+ Hub)

@@ -5451,8 +2142,8 @@
Parameters:
@@ -5531,7 +2222,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/MoveHub.html b/docs/MoveHub.html new file mode 100644 index 00000000..1dc1a1f0 --- /dev/null +++ b/docs/MoveHub.html @@ -0,0 +1,2412 @@ + + + + + + + node-poweredup Class: MoveHub + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: MoveHub

+
+ +
+ +

+ MoveHub +

+ + +
+ + +
+
+ + +
+
+

new MoveHub()

+ + +
+
+ + +
+

The MoveHub is emitted if the discovered device is a Move Hub.

+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> batteryLevel

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
batteryLevel + + +number + + + + +

Battery level of the hub (Percentage between 0-100)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> firmwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Firmware version of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hardwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Hardware version of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> name

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

Name of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> ports

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
ports + + +Array.<string> + + + + +

Array of port names

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> primaryMACAddress

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
primaryMACAddress + + +string + + + + +

Primary MAC address of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> rssi

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
rssi + + +number + + + + +

Signal strength of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +string + + + + +

Hub type

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> uuid

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
uuid + + +string + + + + +

UUID of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+
+

setName(name)

+ + +
+
+ + +
+

Set the name of the Hub.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

New name of the hub (14 characters or less, ASCII only).

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

shutdown()

+ + +
+
+ + +
+

Shutdown the Hub.

+
+ + + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful disconnect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ +
+ + + + + +

Events

+ +
+ +
+
+

accel

+ + +
+
+ + +
+

Emits when accelerometer detects movement. Measured in mG.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
x + + +number + + + + +
y + + +number + + + + +
z + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

button

+ + +
+
+ + +
+

Emits when a button is pressed.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
button + + +string + + + + +
state + + +ButtonState + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

tilt

+ + +
+
+ + +
+

Emits when a tilt sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
x + + +number + + + + +
y + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/PoweredUP.html b/docs/PoweredUP.html index 03923a7a..d9f67de6 100644 --- a/docs/PoweredUP.html +++ b/docs/PoweredUP.html @@ -33,14 +33,14 @@ @@ -211,7 +211,7 @@

Methods


-

getConnectedHubByPrimaryMACAddress(address)

+

getHubByPrimaryMACAddress(address)

@@ -316,7 +316,7 @@
Parameters:
@@ -352,7 +352,7 @@
Returns:
-Hub +BaseHub @@ -369,7 +369,7 @@
Returns:

-

getConnectedHubByUUID(uuid)

+

getHubByUUID(uuid)

@@ -474,7 +474,7 @@
Parameters:
@@ -510,7 +510,7 @@
Returns:
-Hub +BaseHub | null @@ -530,7 +530,7 @@
Returns:

-

getConnectedHubs()

+

getHubs()

@@ -585,7 +585,7 @@

getCo @@ -621,7 +621,7 @@

Returns:
-Array.<Hub> +Array.<BaseHub> @@ -638,7 +638,7 @@
Returns:

-

getConnectedHubsByName(name)

+

getHubsByName(name)

@@ -743,7 +743,7 @@
Parameters:
@@ -779,7 +779,165 @@
Returns:
-Array.<Hub> +Array.<BaseHub> + + + +
+
+ + + + + + + + + +
+
+

getHubsByType(name)

+ + +
+
+ + +
+

Retrieve a list of Powered UP Hub by type.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +Array.<BaseHub> @@ -851,7 +1009,7 @@

scan()

@@ -939,7 +1097,7 @@

stop()

@@ -1029,22 +1187,22 @@
Parameters:
-WeDo2SmartHub +WeDo2SmartHub | -BoostMoveHub +MoveHub | -ControlPlusHub +TechnicMediumHub | -PUPHub +Hub | -PUPRemote +RemoteControl | -DuploTrainBase +DuploTrainBase @@ -1100,7 +1258,7 @@
Parameters:
@@ -1179,7 +1337,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/RemoteControl.html b/docs/RemoteControl.html new file mode 100644 index 00000000..21f65a42 --- /dev/null +++ b/docs/RemoteControl.html @@ -0,0 +1,2412 @@ + + + + + + + node-poweredup Class: RemoteControl + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: RemoteControl

+
+ +
+ +

+ RemoteControl +

+ + +
+ + +
+
+ + +
+
+

new RemoteControl()

+ + +
+
+ + +
+

The RemoteControl is emitted if the discovered device is a Remote Control.

+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> batteryLevel

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
batteryLevel + + +number + + + + +

Battery level of the hub (Percentage between 0-100)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> firmwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Firmware version of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hardwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Hardware version of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> name

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

Name of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> ports

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
ports + + +Array.<string> + + + + +

Array of port names

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> primaryMACAddress

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
primaryMACAddress + + +string + + + + +

Primary MAC address of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> rssi

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
rssi + + +number + + + + +

Signal strength of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +string + + + + +

Hub type

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> uuid

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
uuid + + +string + + + + +

UUID of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+
+

setName(name)

+ + +
+
+ + +
+

Set the name of the Hub.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

New name of the hub (14 characters or less, ASCII only).

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

shutdown()

+ + +
+
+ + +
+

Shutdown the Hub.

+
+ + + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful disconnect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ +
+ + + + + +

Events

+ +
+ +
+
+

accel

+ + +
+
+ + +
+

Emits when accelerometer detects movement. Measured in mG.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
x + + +number + + + + +
y + + +number + + + + +
z + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

button

+ + +
+
+ + +
+

Emits when a button is pressed.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
button + + +string + + + + +
state + + +ButtonState + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

tilt

+ + +
+
+ + +
+

Emits when a tilt sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
x + + +number + + + + +
y + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicMediumHub.html b/docs/TechnicMediumHub.html new file mode 100644 index 00000000..9c221df6 --- /dev/null +++ b/docs/TechnicMediumHub.html @@ -0,0 +1,2412 @@ + + + + + + + node-poweredup Class: TechnicMediumHub + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: TechnicMediumHub

+
+ +
+ +

+ TechnicMediumHub +

+ + +
+ + +
+
+ + +
+
+

new TechnicMediumHub()

+ + +
+
+ + +
+

The TechnicMediumHub is emitted if the discovered device is a Technic Medium Hub.

+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> batteryLevel

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
batteryLevel + + +number + + + + +

Battery level of the hub (Percentage between 0-100)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> firmwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Firmware version of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hardwareVersion

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firmwareVersion + + +string + + + + +

Hardware version of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> name

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

Name of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> ports

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
ports + + +Array.<string> + + + + +

Array of port names

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> primaryMACAddress

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
primaryMACAddress + + +string + + + + +

Primary MAC address of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> rssi

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
rssi + + +number + + + + +

Signal strength of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +string + + + + +

Hub type

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> uuid

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
uuid + + +string + + + + +

UUID of the hub

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + +
Overrides:
+
+ + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+
+

setName(name)

+ + +
+
+ + +
+

Set the name of the Hub.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
name + + +string + + + + +

New name of the hub (14 characters or less, ASCII only).

+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

shutdown()

+ + +
+
+ + +
+

Shutdown the Hub.

+
+ + + + + + + + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful disconnect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ +
+ + + + + +

Events

+ +
+ +
+
+

accel

+ + +
+
+ + +
+

Emits when accelerometer detects movement. Measured in mG.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
port + + +string + + + + +
x + + +number + + + + +
y + + +number + + + + +
z + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

button

+ + +
+
+ + +
+

Emits when a button is pressed.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
button + + +string + + + + +
state + + +ButtonState + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

tilt

+ + +
+
+ + +
+

Emits when a tilt sensor is activated.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
x + + +number + + + + +
y + + +number + + + + +
+ + + + +
+ + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/WeDo2SmartHub.html b/docs/WeDo2SmartHub.html index 2b9db75f..5479d686 100644 --- a/docs/WeDo2SmartHub.html +++ b/docs/WeDo2SmartHub.html @@ -33,21 +33,14 @@ - - @@ -152,8 +145,8 @@

new WeDo
@@ -192,7 +185,7 @@

Extends

@@ -288,7 +281,7 @@
Properties:
@@ -319,8 +312,8 @@
Properties:
@@ -340,8 +333,8 @@
Properties:

-
-

<readonly> current

+
+

<readonly> firmwareVersion

@@ -380,13 +373,13 @@
Properties:
- current + firmwareVersion -number +string @@ -397,7 +390,7 @@
Properties:
-

Current usage of the hub (Milliamps)

+

Firmware version of the hub

@@ -416,7 +409,7 @@
Properties:
@@ -447,8 +440,8 @@
Properties:
@@ -468,8 +461,8 @@
Properties:

-
-

<readonly> firmwareVersion

+
+

<readonly> hardwareVersion

@@ -525,7 +518,7 @@
Properties:
-

Firmware version of the hub

+

Hardware version of the hub

@@ -544,7 +537,7 @@
Properties:
@@ -575,8 +568,8 @@
Properties:
@@ -596,8 +589,8 @@
Properties:

-
-

<readonly> hardwareVersion

+
+

<readonly> name

@@ -636,7 +629,7 @@
Properties:
- firmwareVersion + name @@ -653,7 +646,7 @@
Properties:
-

Hardware version of the hub

+

Name of the hub

@@ -672,7 +665,7 @@
Properties:
@@ -703,8 +696,8 @@
Properties:
@@ -724,8 +717,8 @@
Properties:

-
-

<readonly> name

+
+

<readonly> ports

@@ -764,13 +757,13 @@
Properties:
- name + ports -string +Array.<string> @@ -781,7 +774,7 @@
Properties:
-

Name of the hub

+

Array of port names

@@ -800,7 +793,7 @@
Properties:
@@ -831,8 +824,8 @@
Properties:
@@ -928,7 +921,7 @@
Properties:
@@ -959,8 +952,8 @@
Properties:
@@ -1056,7 +1049,7 @@
Properties:
@@ -1087,8 +1080,8 @@
Properties:
@@ -1108,8 +1101,8 @@
Properties:

-
-

<readonly> uuid

+
+

<readonly> type

@@ -1148,7 +1141,7 @@
Properties:
- uuid + type @@ -1165,7 +1158,7 @@
Properties:
-

UUID of the hub

+

Hub type

@@ -1184,7 +1177,7 @@
Properties:
@@ -1215,8 +1208,8 @@
Properties:
@@ -1236,8 +1229,8 @@
Properties:

-
-

<readonly> voltage

+
+

<readonly> uuid

@@ -1276,13 +1269,13 @@
Properties:
- voltage + uuid -number +string @@ -1293,7 +1286,7 @@
Properties:
-

Voltage of the hub (Volts)

+

UUID of the hub

@@ -1312,7 +1305,7 @@
Properties:
@@ -1343,8 +1336,8 @@
Properties:
@@ -1371,7 +1364,7 @@

Methods


-

brakeMotor(port)

+

setName(name)

@@ -1379,7 +1372,7 @@

brakeMotor(
-

Fully (hard) stop the motor on a given port.

+

Set the name of the Hub.

@@ -1414,7 +1407,7 @@

Parameters:
- port + name @@ -1431,7 +1424,7 @@
Parameters:
- +

New name of the hub (14 characters or less, ASCII only).

@@ -1475,8 +1468,8 @@
Parameters:
@@ -1505,7 +1498,7 @@
Returns:
-

Resolved upon successful completion of command.

+

Resolved upon successful issuance of command.

@@ -1533,7 +1526,7 @@
Returns:

-

connect()

+

shutdown()

@@ -1541,7 +1534,7 @@

connect()

-

Connect to the Hub.

+

Shutdown the Hub.

@@ -1561,21 +1554,7 @@

connect()

-
Inherited From:
-
- -
- - -
Overrides:
-
@@ -1601,8 +1580,8 @@

connect()

@@ -1631,7 +1610,7 @@
Returns:
-

Resolved upon successful connect.

+

Resolved upon successful disconnect.

@@ -1655,132 +1634,19 @@
Returns:
- - -
-
-

disconnect()

- - -
-
- - -
-

Disconnect the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - +
-
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - +

Events

- +

-

getHubType()

+

button

@@ -1788,7 +1654,7 @@

getHubType(
-

Get the hub type.

+

Emits when a button is pressed.

@@ -1798,3713 +1664,62 @@

getHubType( +

Parameters:
+ - -
- - - - - + + + + + + - -
Inherited From:
-
- -
- + - + - + - + + + + - - - - - - - - - - - - - -
Source:
-
- -
- + + + + - + - + - + - + + - - - -
Returns:
- - - - -
-
- Type -
-
- -HubType - - - -
-
- - - - - - - - - -
-
-

getPortDeviceType(port)

- - -
-
- - -
-

Get the device type for a given port.

-
- - - - - - - - -
Parameters:
- - -
NameTypeDescription
button + + +string - - - + +
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -DeviceType - - - -
-
- - - - - - - - - -
-
-

playTone(frequency, time)

- - -
-
- - -
-

Play a tone on the Hub's in-built buzzer

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
frequency - - -number - - - - -
time - - -number - - - - -

How long the tone should play for (in milliseconds).

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command (ie. once the tone has finished playing).

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

rampMotorSpeed(port, fromSpeed, toSpeed, time)

- - -
-
- - -
-

Ramp the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
fromSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

toSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

time - - -number - - - - -

How long the ramp should last (in milliseconds).

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDColor(color)

- - -
-
- - -
-

Set the color of the LED on the Hub via a color value.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
color - - -Color - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDRGB(red, green, blue)

- - -
-
- - -
-

Set the color of the LED on the Hub via RGB values.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
red - - -number - - - - -
green - - -number - - - - -
blue - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLightBrightness(port, brightness [, time])

- - -
-
- - -
-

Set the light brightness on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
brightness - - -number - - - - - - - - - - -

Brightness value between 0-100 (0 is off)

time - - -number - - - - - - - <optional>
- - - - - -

How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command. If time is specified, this is once the light is turned off.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setMotorSpeed(port, speed [, time])

- - -
-
- - -
-

Set the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
speed - - -number - - - - - - - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

time - - -number - - - - - - - <optional>
- - - - - -

How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command. If time is specified, this is once the motor is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setName(name)

- - -
-
- - -
-

Set the name of the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
name - - -string - - - - -

New name of the hub (14 characters or less, ASCII only).

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

shutdown()

- - -
-
- - -
-

Shutdown the Hub.

-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

sleep(delay)

- - -
-
- - -
-

Sleep a given amount of time.

-

This is a helper method to make it easier to add delays into a chain of commands.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
delay - - -number - - - - -

How long to sleep (in milliseconds).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the delay is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

subscribe(port [, mode])

- - -
-
- - -
-

Subscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
mode - - -number - - - - - - - <optional>
- - - - - -

The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

unsubscribe(port)

- - -
-
- - -
-

Unsubscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

wait(commands)

- - -
-
- - -
-

Wait until a given list of concurrently running commands are complete.

-

This is a helper method to make it easier to wait for concurrent commands to complete.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
commands - - -Array.<Promise.<any>> - - - - -

Array of executing commands.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the commands are finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- -
- - - - - -

Events

- -
- -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

button

- - -
-
- - -
-

Emits when a button is pressed.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
button - - -string - - - - -
state - - -ButtonState - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

color

- - -
-
- - -
-

Emits when a color sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

detach

- - -
-
- - -
-

Emits when an attached motor or sensor is detached from the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

disconnect

- - -
-
- - -
-

Emits when the hub is disconnected.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

distance

- - -
-
- - -
-

Emits when a distance sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

rotate

- - -
-
- - -
-

Emits when a rotation sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
rotation - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

tilt

- - -
-
- - -
-

Emits when a tilt sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - + - + - + + @@ -692,15 +563,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -727,8 +589,8 @@
Parameters:
@@ -761,7 +623,7 @@
Parameters:

-

color

+

distance

@@ -769,7 +631,7 @@

color

-

Emits when a color sensor is activated.

+

Emits when a distance sensor is activated.

@@ -804,37 +666,13 @@
Parameters:
- - - - - - - - - - - - - - - - - + + @@ -863,15 +701,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -898,8 +727,8 @@
Parameters:
@@ -932,7 +761,7 @@
Parameters:

-

colorAndDistance

+

current

@@ -940,7 +769,7 @@

colorAndDistance

-

A combined color and distance event, emits when the sensor is activated.

+

Emits when a current change is detected.

@@ -975,55 +804,7 @@
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -1058,15 +839,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -1093,8 +865,8 @@
Parameters:
@@ -1127,7 +899,7 @@
Parameters:

-

detach

+

accel

@@ -1135,7 +907,7 @@

detach

-

Emits when an attached motor or sensor is detached from the Hub.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -1191,112 +963,82 @@
Parameters:
- -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y
state -number +ButtonState @@ -5559,8 +1774,8 @@
Parameters:
@@ -5639,7 +1854,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/classes.list.html b/docs/classes.list.html index 6d0338f7..5d56af0e 100644 --- a/docs/classes.list.html +++ b/docs/classes.list.html @@ -33,21 +33,14 @@ - - @@ -149,10 +142,7 @@

Classes

-
BoostMoveHub
-
- -
ControlPlusHub
+
BaseHub
DuploTrainBase
@@ -164,13 +154,13 @@

Classes

LPF2Hub
-
PoweredUP
+
MoveHub
-
PUPHub
+
RemoteControl
-
PUPRemote
+
TechnicMediumHub
WeDo2SmartHub
@@ -195,7 +185,7 @@

Events


-

accel

+

absolute

@@ -203,7 +193,7 @@

accel

-

Emits when accelerometer detects movement. Measured in mG.

+

Emits when a the motors absolute position is changed.

@@ -238,79 +228,7 @@
Parameters:
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
zabsolute @@ -345,15 +263,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -380,8 +289,8 @@
Parameters:
@@ -414,7 +323,7 @@
Parameters:

-

attach

+

color

@@ -422,7 +331,7 @@

attach

-

Emits when a motor or sensor is attached to the Hub.

+

Emits when a color sensor is activated.

@@ -457,37 +366,13 @@
Parameters:
port - - -string - - - - -
typecolor -DeviceType +Color @@ -516,21 +401,7 @@
Parameters:
-
Inherited From:
-
- -
- - -
Overrides:
-
@@ -556,8 +427,8 @@
Parameters:
@@ -590,7 +461,7 @@
Parameters:

-

button

+

colorAndDistance

@@ -598,7 +469,7 @@

button

-

Emits when a button is pressed.

+

A combined color and distance event, emits when the sensor is activated.

@@ -633,13 +504,13 @@
Parameters:
buttoncolor -string +Color @@ -657,13 +528,13 @@
Parameters:
statedistance -ButtonState +number @@ -674,7 +545,7 @@
Parameters:
-

Distance, in millimeters.

port - - -string - - - - -
colordistance -Color +number @@ -845,7 +683,7 @@
Parameters:
-

Distance, in millimeters.

port - - -string - - - - -
color - - -Color - - - - -
distancecurrent @@ -1040,7 +821,7 @@
Parameters:
-

Distance, in millimeters.

- - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- + + + x + - + + + +number - - - + + - + - + - + + - + + + + y + + + + +number - -
Source:
-
- -
- - - + + - -
+ + - + + - + + + z + - + + + +number - - - -
+ + - -
-
-

disconnect

- - -
-
- -
-

Emits when the hub is disconnected.

-
- + - + + + + -
@@ -1311,17 +1053,12 @@

disconnect

- -
Overrides:
-
@@ -1347,8 +1084,8 @@

disconnect

@@ -1381,7 +1118,7 @@

disconnect


-

distance

+

button

@@ -1389,7 +1126,7 @@

distance

-

Emits when a distance sensor is activated.

+

Emits when a button is pressed.

@@ -1424,7 +1161,7 @@
Parameters:
- port + button @@ -1448,13 +1185,13 @@
Parameters:
- distance + state -number +ButtonState @@ -1465,7 +1202,7 @@
Parameters:
-

Distance, in millimeters.

+ @@ -1487,7 +1224,7 @@
Parameters:
@@ -1518,8 +1255,8 @@
Parameters:
@@ -1552,7 +1289,7 @@
Parameters:

-

gyro

+

tilt

@@ -1560,7 +1297,7 @@

gyro

-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

+

Emits when a tilt sensor is activated.

@@ -1593,30 +1330,6 @@
Parameters:
- - - port - - - - - -string - - - - - - - - - - - - - - - x @@ -1660,30 +1373,6 @@
Parameters:
- - - - - - - - z - - - - - -number - - - - - - - - - - @@ -1706,7 +1395,7 @@
Parameters:
@@ -1737,8 +1426,8 @@
Parameters:
@@ -1771,7 +1460,7 @@
Parameters:

-

rotate

+

color

@@ -1779,7 +1468,7 @@

rotate

-

Emits when a rotation sensor is activated.

+

Emits when a color sensor is activated.

@@ -1814,37 +1503,13 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - rotation + color -number +Color @@ -1873,15 +1538,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -1908,8 +1564,8 @@
Parameters:
@@ -1983,30 +1639,6 @@
Parameters:
- - - port - - - - - -string - - - - - - - - - - - - - - - speed @@ -2044,15 +1676,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -2079,8 +1702,8 @@
Parameters:
@@ -2113,7 +1736,7 @@
Parameters:

-

temp

+

accel

@@ -2121,7 +1744,7 @@

temp

-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -2173,14 +1796,14 @@
Parameters:
-

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

+ - temp + x @@ -2201,7580 +1824,40 @@
Parameters:
- - - + + + y + - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

tilt

- - -
-
- - -
-

Emits when a tilt sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -

(Only available when using a Control+ Hub)

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

accel

- - -
-
- - -
-

Emits when accelerometer detects movement. Measured in mG.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

button

- - -
-
- - -
-

Emits when a button is pressed.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
button - - -string - - - - -
state - - -ButtonState - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

color

- - -
-
- - -
-

Emits when a color sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

colorAndDistance

- - -
-
- - -
-

A combined color and distance event, emits when the sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

detach

- - -
-
- - -
-

Emits when an attached motor or sensor is detached from the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

disconnect

- - -
-
- - -
-

Emits when the hub is disconnected.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

distance

- - -
-
- - -
-

Emits when a distance sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

gyro

- - -
-
- - -
-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

rotate

- - -
-
- - -
-

Emits when a rotation sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
rotation - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

speed

- - -
-
- - -
-

Emits on a speed change.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
speed - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

temp

- - -
-
- - -
-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

temp - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

tilt

- - -
-
- - -
-

Emits when a tilt sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -

(Only available when using a Control+ Hub)

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

accel

- - -
-
- - -
-

Emits when accelerometer detects movement. Measured in mG.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

button

- - -
-
- - -
-

Emits when a button is pressed.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
button - - -string - - - - -
state - - -ButtonState - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

color

- - -
-
- - -
-

Emits when a color sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

colorAndDistance

- - -
-
- - -
-

A combined color and distance event, emits when the sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

detach

- - -
-
- - -
-

Emits when an attached motor or sensor is detached from the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

disconnect

- - -
-
- - -
-

Emits when the hub is disconnected.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

distance

- - -
-
- - -
-

Emits when a distance sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

gyro

- - -
-
- - -
-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

rotate

- - -
-
- - -
-

Emits when a rotation sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
rotation - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

speed

- - -
-
- - -
-

Emits on a speed change.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
speed - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

temp

- - -
-
- - -
-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

temp - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

tilt

- - -
-
- - -
-

Emits when a tilt sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -

(Only available when using a Control+ Hub)

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

detach

- - -
-
- - -
-

Emits when an attached motor or sensor is detached from the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

disconnect

- - -
-
- - -
-

Emits when the hub is disconnected.

-
- - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

accel

- - -
-
- - -
-

Emits when accelerometer detects movement. Measured in mG.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

button

- - -
-
- - -
-

Emits when a button is pressed.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
button - - -string - - - - -
state - - -ButtonState - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

color

- - -
-
- - -
-

Emits when a color sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

colorAndDistance

- - -
-
- - -
-

A combined color and distance event, emits when the sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

detach

- - -
-
- - -
-

Emits when an attached motor or sensor is detached from the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

disconnect

- - -
-
- - -
-

Emits when the hub is disconnected.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

distance

- - -
-
- - -
-

Emits when a distance sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
distance - - -number - - - - -

Distance, in millimeters.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

gyro

- - -
-
- - -
-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

rotate

- - -
-
- - -
-

Emits when a rotation sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
rotation - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

speed

- - -
-
- - -
-

Emits on a speed change.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
speed - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

temp

- - -
-
- - -
-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

temp - - -number - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

tilt

- - -
-
- - -
-

Emits when a tilt sensor is activated.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -

(Only available when using a Control+ Hub)

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

discover

- - -
-
- - -
-

Emits when a Powered UP Hub device is found.

-
- - - - - + + + +number - - -
Parameters:
- - - - - - - - + + - + - + - - - + + - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - + - + + @@ -10730,7 +2596,7 @@
Parameters:
@@ -10761,8 +2627,8 @@
Parameters:
@@ -10795,7 +2661,7 @@
Parameters:

-

detach

+

accel

@@ -10803,7 +2669,7 @@

detach

-

Emits when an attached motor or sensor is detached from the Hub.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -10859,112 +2725,82 @@
Parameters:
- -
NameTypeDescription
hubz -WeDo2SmartHub -| - -BoostMoveHub -| - -ControlPlusHub -| - -PUPHub -| - -PUPRemote -| - -DuploTrainBase +number @@ -9803,6 +1886,15 @@
Parameters:
+
Inherited From:
+
+ +
+ @@ -9829,8 +1921,8 @@
Parameters:
@@ -9863,7 +1955,7 @@
Parameters:

-

accel

+

attach

@@ -9871,7 +1963,7 @@

accel

-

Emits when accelerometer detects movement. Measured in mG.

+

Emits when a device is attached to the Hub.

@@ -9906,85 +1998,13 @@
Parameters:
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
zdevice -number +Device @@ -10013,15 +2033,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -10048,8 +2059,8 @@
Parameters:
@@ -10090,7 +2101,7 @@

attach

-

Emits when a motor or sensor is attached to the Hub.

+

Emits when a device is detached from the Hub.

@@ -10125,37 +2136,13 @@
Parameters:
port - - -string - - - - -
typedevice -DeviceType +Device @@ -10184,21 +2171,7 @@
Parameters:
-
Inherited From:
-
- -
- - -
Overrides:
-
@@ -10224,8 +2197,8 @@
Parameters:
@@ -10331,7 +2304,7 @@
Parameters:
-ButtonState +ButtonState @@ -10395,8 +2368,8 @@
Parameters:
@@ -10429,7 +2402,7 @@
Parameters:

-

color

+

disconnect

@@ -10437,7 +2410,7 @@

color

-

Emits when a color sensor is activated.

+

Emits when the hub is disconnected.

@@ -10447,80 +2420,6 @@

color

-
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- -
@@ -10531,15 +2430,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -10566,8 +2456,8 @@
Parameters:
@@ -10600,7 +2490,7 @@
Parameters:

-

colorAndDistance

+

tilt

@@ -10608,7 +2498,7 @@

colorAndDistance

-

A combined color and distance event, emits when the sensor is activated.

+

Emits when a tilt sensor is activated.

@@ -10643,37 +2533,13 @@
Parameters:
port - - -string - - - - -
colorx -Color +number @@ -10691,7 +2557,7 @@
Parameters:
distancey @@ -10708,7 +2574,7 @@
Parameters:
-

Distance, in millimeters.

- + + + x + - -
- + + + +number - - - -
Inherited From:
-
- -
- + + - -
Overrides:
-
- + - + - + + - - - - - - - - - - + + + y + + + + +number - -
Source:
-
- -
- - - + + - -
+ + - + + - + + + z + - + + + +number - - - -
+ + - -
-
-

disconnect

- - -
-
- -
-

Emits when the hub is disconnected.

-
- + - + + + + -
@@ -10975,21 +2811,7 @@

disconnect

-
Inherited From:
-
- -
- - -
Overrides:
-
@@ -11015,8 +2837,8 @@

disconnect

@@ -11049,7 +2871,7 @@

disconnect


-

distance

+

button

@@ -11057,7 +2879,7 @@

distance

-

Emits when a distance sensor is activated.

+

Emits when a button is pressed.

@@ -11092,7 +2914,7 @@
Parameters:
- port + button @@ -11116,13 +2938,13 @@
Parameters:
- distance + state -number +ButtonState @@ -11133,7 +2955,7 @@
Parameters:
-

Distance, in millimeters.

+ @@ -11151,15 +2973,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -11186,8 +2999,8 @@
Parameters:
@@ -11220,7 +3033,7 @@
Parameters:

-

gyro

+

tilt

@@ -11228,7 +3041,7 @@

gyro

-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

+

Emits when a tilt sensor is activated.

@@ -11263,13 +3076,13 @@
Parameters:
- port + x -string +number @@ -11287,7 +3100,7 @@
Parameters:
- x + y @@ -11300,42 +3113,132 @@
Parameters:
- + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+
+

distance

+ + +
+
- - - + +
+

Emits when a distance sensor is activated.

+
- - - y - + - - - -number + + + +
Parameters:
+ + + + + + + - - + - + - + - - + + + + - + + @@ -11370,15 +3273,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -11405,8 +3299,8 @@
Parameters:
@@ -11439,7 +3333,7 @@
Parameters:

-

rotate

+

accel

@@ -11447,7 +3341,7 @@

rotate

-

Emits when a rotation sensor is activated.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -11506,7 +3400,55 @@
Parameters:
- + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + + - + - - - - - - - - - - - - - - - - @@ -12064,30 +3982,6 @@
Parameters:
- - - - - - - - - - - - - - - -
NameType
Description
zdistance @@ -11352,7 +3255,7 @@
Parameters:
-

Distance, in millimeters.

rotationx + + +number + + + + +
y + + +number + + + + +
z @@ -11545,7 +3487,7 @@
Parameters:
@@ -11576,8 +3518,8 @@
Parameters:
@@ -11610,7 +3552,7 @@
Parameters:

-

speed

+

button

@@ -11618,7 +3560,7 @@

speed

-

Emits on a speed change.

+

Emits when a button is pressed.

@@ -11653,7 +3595,7 @@
Parameters:
portbutton @@ -11677,13 +3619,13 @@
Parameters:
speedstate -number +ButtonState @@ -11716,7 +3658,7 @@
Parameters:
@@ -11747,8 +3689,8 @@
Parameters:
@@ -11781,7 +3723,7 @@
Parameters:

-

temp

+

tilt

@@ -11789,7 +3731,7 @@

temp

-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

+

Emits when a tilt sensor is activated.

@@ -11824,13 +3766,13 @@
Parameters:
portx -string +number @@ -11841,14 +3783,14 @@
Parameters:
-

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

tempy @@ -11887,7 +3829,7 @@
Parameters:
@@ -11918,8 +3860,8 @@
Parameters:
@@ -11993,30 +3935,6 @@
Parameters:
port - - -string - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

x
z - - -number - - - - -

(Only available when using a Control+ Hub)

@@ -12102,15 +3996,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -12137,8 +4022,8 @@
Parameters:
@@ -12356,8 +4241,8 @@
Parameters:
@@ -12390,7 +4275,7 @@
Parameters:

-

attach

+

button

@@ -12398,7 +4283,7 @@

attach

-

Emits when a motor or sensor is attached to the Hub.

+

Emits when a button is pressed.

@@ -12433,7 +4318,7 @@
Parameters:
- port + button @@ -12457,13 +4342,13 @@
Parameters:
- type + state -DeviceType +ButtonState @@ -12496,17 +4381,12 @@
Parameters:
- -
Overrides:
-
@@ -12532,8 +4412,8 @@
Parameters:
@@ -12566,7 +4446,7 @@
Parameters:

-

button

+

tilt

@@ -12574,7 +4454,7 @@

button

-

Emits when a button is pressed.

+

Emits when a tilt sensor is activated.

@@ -12609,13 +4489,13 @@
Parameters:
- button + x -string +number @@ -12633,13 +4513,13 @@
Parameters:
- state + y -ButtonState +number @@ -12672,7 +4552,7 @@
Parameters:
@@ -12703,8 +4583,8 @@
Parameters:
@@ -12737,7 +4617,7 @@
Parameters:

-

color

+

button

@@ -12745,7 +4625,7 @@

color

-

Emits when a color sensor is activated.

+

Emits when a button on the remote is pressed or released.

@@ -12780,37 +4660,13 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - color + event -Color +number @@ -12833,20 +4689,11 @@
Parameters:
- - - - - - -
Inherited From:
-
- -
+ + + + + @@ -12874,8 +4721,8 @@
Parameters:
@@ -12908,7 +4755,7 @@
Parameters:

-

colorAndDistance

+

rotate

@@ -12916,7 +4763,7 @@

colorAndDistance

-

A combined color and distance event, emits when the sensor is activated.

+

Emits when a rotation sensor is activated.

@@ -12951,55 +4798,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - color - - - - - -Color - - - - - - - - - - - - - - - - - - distance + rotation @@ -13016,7 +4815,7 @@
Parameters:
-

Distance, in millimeters.

+ @@ -13034,15 +4833,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -13069,8 +4859,8 @@
Parameters:
@@ -13103,7 +4893,7 @@
Parameters:

-

detach

+

from 0 to 100.

@@ -13111,7 +4901,7 @@

detach

-

Emits when an attached motor or sensor is detached from the Hub.

+

Emits when the ambient light changes.

@@ -13146,13 +4936,13 @@
Parameters:
- port + ambient -string +number @@ -13181,21 +4971,7 @@
Parameters:
-
Inherited From:
-
- -
- - -
Overrides:
-
@@ -13221,8 +4997,8 @@
Parameters:
@@ -13255,7 +5031,7 @@
Parameters:

-

disconnect

+

color

@@ -13263,7 +5039,7 @@

disconnect

-

Emits when the hub is disconnected.

+

Emits when a color sensor is activated.

@@ -13273,6 +5049,56 @@

disconnect

+
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
color + + +Color + + + + +
+ +
@@ -13283,21 +5109,7 @@

disconnect

-
Inherited From:
-
- -
- - -
Overrides:
-
@@ -13323,8 +5135,8 @@

disconnect

@@ -13357,7 +5169,7 @@

disconnect


-

distance

+

from 0 to 100.

@@ -13365,7 +5177,7 @@

distance

-

Emits when a distance sensor is activated.

+

Emits when the light reflectivity changes.

@@ -13400,31 +5212,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - distance + reflect @@ -13441,7 +5229,7 @@
Parameters:
-

Distance, in millimeters.

+ @@ -13459,15 +5247,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -13494,8 +5273,8 @@
Parameters:
@@ -13528,7 +5307,7 @@
Parameters:

-

gyro

+

from 40 to 2500mm

@@ -13536,7 +5315,7 @@

gyro

-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

+

Emits when the detected distance changes (Slow sampling covers 40mm to 2500mm).

@@ -13558,92 +5337,20 @@
Parameters:
Type - - - - - Description - - - - - - - - - port - - - - - -string - - - - - - - - - - - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - + - + - - + Description + + + - z + distance @@ -13678,15 +5385,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -13713,8 +5411,8 @@
Parameters:
@@ -13747,7 +5445,7 @@
Parameters:

-

rotate

+

from 50 to 320mm

@@ -13755,7 +5453,7 @@

rotate

-

Emits when a rotation sensor is activated.

+

Emits when the detected distance changes (Fast sampling covers 50mm to 320mm).

@@ -13790,31 +5488,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - rotation + fastDistance @@ -13849,15 +5523,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -13884,8 +5549,8 @@
Parameters:
@@ -13918,7 +5583,7 @@
Parameters:

-

speed

+

touch Touched on/off (boolean).

@@ -13926,7 +5591,7 @@

speed

-

Emits on a speed change.

+

Emits when the sensor is touched.

@@ -13961,37 +5626,13 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - speed + touch -number +boolean @@ -14020,15 +5661,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -14055,8 +5687,8 @@
Parameters:
@@ -14089,7 +5721,7 @@
Parameters:

-

temp

+

in newtons (0-10).

@@ -14097,7 +5729,7 @@

temp

-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

+

Emits when force is applied.

@@ -14132,31 +5764,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - -

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

- - - - - - - temp + force @@ -14191,15 +5799,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -14226,8 +5825,8 @@
Parameters:
@@ -14260,7 +5859,7 @@
Parameters:

-

tilt

+

from 0-3.

@@ -14268,7 +5867,7 @@

tilt

-

Emits when a tilt sensor is activated.

+

Emits when the sensor is tapped.

@@ -14303,55 +5902,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - -

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

- - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y + tapped @@ -14372,30 +5923,6 @@
Parameters:
- - - - z - - - - - -number - - - - - - - - - - -

(Only available when using a Control+ Hub)

- - - @@ -14410,15 +5937,6 @@
Parameters:
-
Inherited From:
-
- -
- @@ -14445,8 +5963,8 @@
Parameters:
@@ -14479,7 +5997,7 @@
Parameters:

-

attach

+

accel

@@ -14487,7 +6005,7 @@

attach

-

Emits when a motor or sensor is attached to the Hub.

+

Emits when accelerometer detects movement. Measured in mG.

@@ -14513,22 +6031,70 @@
Parameters:
- Description - - + Description + + + + + + + + + port + + + + + +string + + + + + + + + + + + + + + + + + + x + + + + + +number + + + + + + + + + + + + - - port + y -string +number @@ -14546,13 +6112,13 @@
Parameters:
- type + z -DeviceType +number @@ -14585,7 +6151,7 @@
Parameters:
@@ -14616,8 +6182,8 @@
Parameters:
@@ -14723,7 +6289,7 @@
Parameters:
-ButtonState +ButtonState @@ -14752,6 +6318,15 @@
Parameters:
+
Inherited From:
+
+ +
+ @@ -14778,8 +6353,8 @@
Parameters:
@@ -14812,7 +6387,7 @@
Parameters:

-

color

+

tilt

@@ -14820,7 +6395,7 @@

color

-

Emits when a color sensor is activated.

+

Emits when a tilt sensor is activated.

@@ -14855,13 +6430,13 @@
Parameters:
- port + x -string +number @@ -14879,13 +6454,13 @@
Parameters:
- color + y -Color +number @@ -14914,6 +6489,15 @@
Parameters:
+
Inherited From:
+
+ +
+ @@ -14940,8 +6524,8 @@
Parameters:
@@ -14974,7 +6558,7 @@
Parameters:

-

detach

+

gyro

@@ -14982,7 +6566,7 @@

detach

-

Emits when an attached motor or sensor is detached from the Hub.

+

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

@@ -15017,13 +6601,13 @@
Parameters:
- port + x -string +number @@ -15038,107 +6622,58 @@
Parameters:
- - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - + + + y + + + + +number - -
Source:
-
- -
- - - + + - -
+ + - + + - + + + z + - + + + +number - - - - + + - -
-
-

disconnect

- - -
-
- -
-

Emits when the hub is disconnected.

-
- + - + + + + -
@@ -15149,15 +6684,6 @@

disconnect

-
Inherited From:
-
- -
- @@ -15184,8 +6710,8 @@

disconnect

@@ -15218,7 +6744,7 @@

disconnect


-

distance

+

tilt

@@ -15226,7 +6752,7 @@

distance

-

Emits when a distance sensor is activated.

+

Emits when a tilt sensor is activated.

@@ -15261,13 +6787,13 @@
Parameters:
- port + x -string +number @@ -15285,7 +6811,7 @@
Parameters:
- distance + y @@ -15302,7 +6828,31 @@
Parameters:
-

Distance, in millimeters.

+ + + + + + + + z + + + + + +number + + + + + + + + + + + @@ -15346,8 +6896,8 @@
Parameters:
@@ -15380,7 +6930,7 @@
Parameters:

-

rotate

+

voltage

@@ -15388,7 +6938,7 @@

rotate

-

Emits when a rotation sensor is activated.

+

Emits when a voltage change is detected.

@@ -15423,31 +6973,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - rotation + voltage @@ -15508,8 +7034,8 @@
Parameters:
@@ -15542,7 +7068,7 @@
Parameters:

-

tilt

+

button

@@ -15550,7 +7076,7 @@

tilt

-

Emits when a tilt sensor is activated.

+

Emits when a button is pressed.

@@ -15585,7 +7111,7 @@
Parameters:
- port + button @@ -15609,37 +7135,13 @@
Parameters:
- x - - - - - -number - - - - - - - - - - - - - - - - - - y + state -number +ButtonState @@ -15694,8 +7196,8 @@
Parameters:
@@ -15774,7 +7276,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/consts.js.html b/docs/consts.js.html index 15c52b7c..05bd8d0b 100644 --- a/docs/consts.js.html +++ b/docs/consts.js.html @@ -33,14 +33,14 @@ @@ -90,7 +90,7 @@

Source: consts.js

* @typedef HubType * @property {number} UNKNOWN 0 * @property {number} WEDO2_SMART_HUB 1 - * @property {number} BOOST_MOVE_HUB 2 + * @property {number} MOVE_HUB 2 * @property {number} POWERED_UP_HUB 3 * @property {number} POWERED_UP_REMOTE 4 * @property {number} DUPLO_TRAIN_HUB 5 @@ -100,18 +100,18 @@

Source: consts.js

(function (HubType) { HubType[HubType["UNKNOWN"] = 0] = "UNKNOWN"; HubType[HubType["WEDO2_SMART_HUB"] = 1] = "WEDO2_SMART_HUB"; - HubType[HubType["BOOST_MOVE_HUB"] = 2] = "BOOST_MOVE_HUB"; - HubType[HubType["POWERED_UP_HUB"] = 3] = "POWERED_UP_HUB"; - HubType[HubType["POWERED_UP_REMOTE"] = 4] = "POWERED_UP_REMOTE"; - HubType[HubType["DUPLO_TRAIN_HUB"] = 5] = "DUPLO_TRAIN_HUB"; - HubType[HubType["CONTROL_PLUS_HUB"] = 6] = "CONTROL_PLUS_HUB"; + HubType[HubType["MOVE_HUB"] = 2] = "MOVE_HUB"; + HubType[HubType["HUB"] = 3] = "HUB"; + HubType[HubType["REMOTE_CONTROL"] = 4] = "REMOTE_CONTROL"; + HubType[HubType["DUPLO_TRAIN_BASE"] = 5] = "DUPLO_TRAIN_BASE"; + HubType[HubType["TECHNIC_MEDIUM_HUB"] = 6] = "TECHNIC_MEDIUM_HUB"; })(HubType = exports.HubType || (exports.HubType = {})); // tslint:disable-next-line exports.HubTypeNames = HubType; /** * @typedef DeviceType * @property {number} UNKNOWN 0 - * @property {number} BASIC_MOTOR 1 + * @property {number} SIMPLE_MEDIUM_LINEAR_MOTOR 1 * @property {number} TRAIN_MOTOR 2 * @property {number} LED_LIGHTS 8 * @property {number} VOLTAGE 20 @@ -120,9 +120,9 @@

Source: consts.js

* @property {number} RGB_LIGHT 23 * @property {number} WEDO2_TILT 34 * @property {number} WEDO2_DISTANCE 35 - * @property {number} BOOST_DISTANCE 37 - * @property {number} BOOST_TACHO_MOTOR 38 - * @property {number} BOOST_MOVE_HUB_MOTOR 39 + * @property {number} COLOR_DISTANCE_SENSOR 37 + * @property {number} MEDIUM_LINEAR_MOTOR 38 + * @property {number} MOVE_HUB_MEDIUM_LINEAR_MOTOR 39 * @property {number} BOOST_TILT 40 * @property {number} DUPLO_TRAIN_BASE_MOTOR 41 * @property {number} DUPLO_TRAIN_BASE_SPEAKER 42 @@ -138,32 +138,37 @@

Source: consts.js

var DeviceType; (function (DeviceType) { DeviceType[DeviceType["UNKNOWN"] = 0] = "UNKNOWN"; - DeviceType[DeviceType["BASIC_MOTOR"] = 1] = "BASIC_MOTOR"; + DeviceType[DeviceType["SIMPLE_MEDIUM_LINEAR_MOTOR"] = 1] = "SIMPLE_MEDIUM_LINEAR_MOTOR"; DeviceType[DeviceType["TRAIN_MOTOR"] = 2] = "TRAIN_MOTOR"; - DeviceType[DeviceType["LED_LIGHTS"] = 8] = "LED_LIGHTS"; - DeviceType[DeviceType["VOLTAGE"] = 20] = "VOLTAGE"; - DeviceType[DeviceType["CURRENT"] = 21] = "CURRENT"; - DeviceType[DeviceType["PIEZO_TONE"] = 22] = "PIEZO_TONE"; - DeviceType[DeviceType["RGB_LIGHT"] = 23] = "RGB_LIGHT"; - DeviceType[DeviceType["WEDO2_TILT"] = 34] = "WEDO2_TILT"; - DeviceType[DeviceType["WEDO2_DISTANCE"] = 35] = "WEDO2_DISTANCE"; - DeviceType[DeviceType["BOOST_DISTANCE"] = 37] = "BOOST_DISTANCE"; - DeviceType[DeviceType["BOOST_TACHO_MOTOR"] = 38] = "BOOST_TACHO_MOTOR"; - DeviceType[DeviceType["BOOST_MOVE_HUB_MOTOR"] = 39] = "BOOST_MOVE_HUB_MOTOR"; - DeviceType[DeviceType["BOOST_TILT"] = 40] = "BOOST_TILT"; + DeviceType[DeviceType["LIGHT"] = 8] = "LIGHT"; + DeviceType[DeviceType["VOLTAGE_SENSOR"] = 20] = "VOLTAGE_SENSOR"; + DeviceType[DeviceType["CURRENT_SENSOR"] = 21] = "CURRENT_SENSOR"; + DeviceType[DeviceType["PIEZO_BUZZER"] = 22] = "PIEZO_BUZZER"; + DeviceType[DeviceType["HUB_LED"] = 23] = "HUB_LED"; + DeviceType[DeviceType["TILT_SENSOR"] = 34] = "TILT_SENSOR"; + DeviceType[DeviceType["MOTION_SENSOR"] = 35] = "MOTION_SENSOR"; + DeviceType[DeviceType["COLOR_DISTANCE_SENSOR"] = 37] = "COLOR_DISTANCE_SENSOR"; + DeviceType[DeviceType["MEDIUM_LINEAR_MOTOR"] = 38] = "MEDIUM_LINEAR_MOTOR"; + DeviceType[DeviceType["MOVE_HUB_MEDIUM_LINEAR_MOTOR"] = 39] = "MOVE_HUB_MEDIUM_LINEAR_MOTOR"; + DeviceType[DeviceType["MOVE_HUB_TILT_SENSOR"] = 40] = "MOVE_HUB_TILT_SENSOR"; DeviceType[DeviceType["DUPLO_TRAIN_BASE_MOTOR"] = 41] = "DUPLO_TRAIN_BASE_MOTOR"; DeviceType[DeviceType["DUPLO_TRAIN_BASE_SPEAKER"] = 42] = "DUPLO_TRAIN_BASE_SPEAKER"; - DeviceType[DeviceType["DUPLO_TRAIN_BASE_COLOR"] = 43] = "DUPLO_TRAIN_BASE_COLOR"; + DeviceType[DeviceType["DUPLO_TRAIN_BASE_COLOR_SENSOR"] = 43] = "DUPLO_TRAIN_BASE_COLOR_SENSOR"; DeviceType[DeviceType["DUPLO_TRAIN_BASE_SPEEDOMETER"] = 44] = "DUPLO_TRAIN_BASE_SPEEDOMETER"; - DeviceType[DeviceType["CONTROL_PLUS_LARGE_MOTOR"] = 46] = "CONTROL_PLUS_LARGE_MOTOR"; - DeviceType[DeviceType["CONTROL_PLUS_XLARGE_MOTOR"] = 47] = "CONTROL_PLUS_XLARGE_MOTOR"; - DeviceType[DeviceType["CONTROL_PLUS_GEST"] = 54] = "CONTROL_PLUS_GEST"; - DeviceType[DeviceType["POWERED_UP_REMOTE_BUTTON"] = 55] = "POWERED_UP_REMOTE_BUTTON"; - DeviceType[DeviceType["RSSI"] = 56] = "RSSI"; - DeviceType[DeviceType["CONTROL_PLUS_ACCELEROMETER"] = 57] = "CONTROL_PLUS_ACCELEROMETER"; - DeviceType[DeviceType["CONTROL_PLUS_GYRO"] = 58] = "CONTROL_PLUS_GYRO"; - DeviceType[DeviceType["CONTROL_PLUS_TILT"] = 59] = "CONTROL_PLUS_TILT"; - DeviceType[DeviceType["TEMPERATURE"] = 60] = "TEMPERATURE"; + DeviceType[DeviceType["TECHNIC_LARGE_LINEAR_MOTOR"] = 46] = "TECHNIC_LARGE_LINEAR_MOTOR"; + DeviceType[DeviceType["TECHNIC_XLARGE_LINEAR_MOTOR"] = 47] = "TECHNIC_XLARGE_LINEAR_MOTOR"; + DeviceType[DeviceType["TECHNIC_MEDIUM_ANGULAR_MOTOR"] = 48] = "TECHNIC_MEDIUM_ANGULAR_MOTOR"; + DeviceType[DeviceType["TECHNIC_LARGE_ANGULAR_MOTOR"] = 49] = "TECHNIC_LARGE_ANGULAR_MOTOR"; + DeviceType[DeviceType["TECHNIC_MEDIUM_HUB_GEST_SENSOR"] = 54] = "TECHNIC_MEDIUM_HUB_GEST_SENSOR"; + DeviceType[DeviceType["REMOTE_CONTROL_BUTTON"] = 55] = "REMOTE_CONTROL_BUTTON"; + DeviceType[DeviceType["REMOTE_CONTROL_RSSI"] = 56] = "REMOTE_CONTROL_RSSI"; + DeviceType[DeviceType["TECHNIC_MEDIUM_HUB_ACCELEROMETER"] = 57] = "TECHNIC_MEDIUM_HUB_ACCELEROMETER"; + DeviceType[DeviceType["TECHNIC_MEDIUM_HUB_GYRO_SENSOR"] = 58] = "TECHNIC_MEDIUM_HUB_GYRO_SENSOR"; + DeviceType[DeviceType["TECHNIC_MEDIUM_HUB_TILT_SENSOR"] = 59] = "TECHNIC_MEDIUM_HUB_TILT_SENSOR"; + DeviceType[DeviceType["TECHNIC_MEDIUM_HUB_TEMPERATURE_SENSOR"] = 60] = "TECHNIC_MEDIUM_HUB_TEMPERATURE_SENSOR"; + DeviceType[DeviceType["TECHNIC_COLOR_SENSOR"] = 61] = "TECHNIC_COLOR_SENSOR"; + DeviceType[DeviceType["TECHNIC_DISTANCE_SENSOR"] = 62] = "TECHNIC_DISTANCE_SENSOR"; + DeviceType[DeviceType["TECHNIC_FORCE_SENSOR"] = 63] = "TECHNIC_FORCE_SENSOR"; // Spike Prime })(DeviceType = exports.DeviceType || (exports.DeviceType = {})); // tslint:disable-next-line exports.DeviceTypeNames = DeviceType; @@ -209,12 +214,22 @@

Source: consts.js

*/ var ButtonState; (function (ButtonState) { - ButtonState[ButtonState["PRESSED"] = 0] = "PRESSED"; - ButtonState[ButtonState["RELEASED"] = 1] = "RELEASED"; - ButtonState[ButtonState["UP"] = 2] = "UP"; - ButtonState[ButtonState["DOWN"] = 3] = "DOWN"; - ButtonState[ButtonState["STOP"] = 4] = "STOP"; + ButtonState[ButtonState["PRESSED"] = 2] = "PRESSED"; + ButtonState[ButtonState["RELEASED"] = 0] = "RELEASED"; + ButtonState[ButtonState["UP"] = 1] = "UP"; + ButtonState[ButtonState["DOWN"] = 255] = "DOWN"; + ButtonState[ButtonState["STOP"] = 127] = "STOP"; })(ButtonState = exports.ButtonState || (exports.ButtonState = {})); +/** + * @typedef BrakingStyle + * @property {number} HOLD 127 + * @property {number} BRAKE 128 + */ +var BrakingStyle; +(function (BrakingStyle) { + BrakingStyle[BrakingStyle["HOLD"] = 127] = "HOLD"; + BrakingStyle[BrakingStyle["BRAKE"] = 128] = "BRAKE"; +})(BrakingStyle = exports.BrakingStyle || (exports.BrakingStyle = {})); /** * @typedef DuploTrainBaseSound * @property {number} BRAKE 3 @@ -233,11 +248,11 @@

Source: consts.js

})(DuploTrainBaseSound = exports.DuploTrainBaseSound || (exports.DuploTrainBaseSound = {})); var BLEManufacturerData; (function (BLEManufacturerData) { - BLEManufacturerData[BLEManufacturerData["DUPLO_TRAIN_HUB_ID"] = 32] = "DUPLO_TRAIN_HUB_ID"; - BLEManufacturerData[BLEManufacturerData["BOOST_MOVE_HUB_ID"] = 64] = "BOOST_MOVE_HUB_ID"; - BLEManufacturerData[BLEManufacturerData["POWERED_UP_HUB_ID"] = 65] = "POWERED_UP_HUB_ID"; - BLEManufacturerData[BLEManufacturerData["POWERED_UP_REMOTE_ID"] = 66] = "POWERED_UP_REMOTE_ID"; - BLEManufacturerData[BLEManufacturerData["CONTROL_PLUS_LARGE_HUB"] = 128] = "CONTROL_PLUS_LARGE_HUB"; + BLEManufacturerData[BLEManufacturerData["DUPLO_TRAIN_BASE_ID"] = 32] = "DUPLO_TRAIN_BASE_ID"; + BLEManufacturerData[BLEManufacturerData["MOVE_HUB_ID"] = 64] = "MOVE_HUB_ID"; + BLEManufacturerData[BLEManufacturerData["HUB_ID"] = 65] = "HUB_ID"; + BLEManufacturerData[BLEManufacturerData["REMOTE_CONTROL_ID"] = 66] = "REMOTE_CONTROL_ID"; + BLEManufacturerData[BLEManufacturerData["TECHNIC_MEDIUM_HUB"] = 128] = "TECHNIC_MEDIUM_HUB"; })(BLEManufacturerData = exports.BLEManufacturerData || (exports.BLEManufacturerData = {})); var BLEService; (function (BLEService) { @@ -310,7 +325,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/devices_absolutemotor.js.html b/docs/devices_absolutemotor.js.html new file mode 100644 index 00000000..b326086a --- /dev/null +++ b/docs/devices_absolutemotor.js.html @@ -0,0 +1,296 @@ + + + + + + + node-poweredup Source: devices/absolutemotor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/absolutemotor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const tachomotor_1 = require("./tachomotor");
+const Consts = __importStar(require("../consts"));
+const utils_1 = require("../utils");
+class AbsoluteMotor extends tachomotor_1.TachoMotor {
+    constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) {
+        super(hub, portId, Object.assign({}, modeMap, exports.ModeMap), type);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.ABSOLUTE:
+                const angle = utils_1.normalizeAngle(message.readInt16LE(this.isWeDo2SmartHub ? 2 : 4));
+                /**
+                 * Emits when a the motors absolute position is changed.
+                 * @event AbsoluteMotor#absolute
+                 * @param {number} absolute
+                 */
+                this.notify("absolute", { angle });
+                break;
+            default:
+                super.receive(message);
+                break;
+        }
+    }
+    /**
+     * Rotate a motor by a given angle.
+     * @method AbsoluteMotor#gotoAngle
+     * @param {number} angle Absolute position the motor should go to (degrees from 0).
+     * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
+     * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
+     */
+    gotoAngle(angle, speed = 100) {
+        if (!this.isVirtualPort && angle instanceof Array) {
+            throw new Error("Only virtual ports can accept multiple positions");
+        }
+        if (this.isWeDo2SmartHub) {
+            throw new Error("Absolute positioning is not available on the WeDo 2.0 Smart Hub");
+        }
+        this.cancelEventTimer();
+        return new Promise((resolve) => {
+            this._busy = true;
+            if (speed === undefined || speed === null) {
+                speed = 100;
+            }
+            let message;
+            if (angle instanceof Array) {
+                message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, utils_1.mapSpeed(speed), 0x64, this._brakeStyle, 0x00]);
+                message.writeInt32LE(utils_1.normalizeAngle(angle[0]), 4);
+                message.writeInt32LE(utils_1.normalizeAngle(angle[1]), 8);
+            }
+            else {
+                message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, utils_1.mapSpeed(speed), 0x64, this._brakeStyle, 0x00]);
+                message.writeInt32LE(utils_1.normalizeAngle(angle), 4);
+            }
+            this.send(message);
+            this._finished = () => {
+                return resolve();
+            };
+        });
+    }
+}
+exports.AbsoluteMotor = AbsoluteMotor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["ROTATION"] = 2] = "ROTATION";
+    Mode[Mode["ABSOLUTE"] = 3] = "ABSOLUTE";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "rotate": Mode.ROTATION,
+    "absolute": Mode.ABSOLUTE
+};
+//# sourceMappingURL=absolutemotor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_basicmotor.js.html b/docs/devices_basicmotor.js.html new file mode 100644 index 00000000..5a8203b0 --- /dev/null +++ b/docs/devices_basicmotor.js.html @@ -0,0 +1,285 @@ + + + + + + + node-poweredup Source: devices/basicmotor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/basicmotor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+const utils_1 = require("../utils");
+class BasicMotor extends device_1.Device {
+    constructor(hub, portId, modeMap, type = Consts.DeviceType.UNKNOWN) {
+        super(hub, portId, modeMap, type);
+    }
+    /**
+     * Set the motor power.
+     * @method BasicMotor#setPower
+     * @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    setPower(power, interrupt = true) {
+        if (interrupt) {
+            this.cancelEventTimer();
+        }
+        return new Promise((resolve) => {
+            this.writeDirect(0x00, Buffer.from([utils_1.mapSpeed(power)]));
+            return resolve();
+        });
+    }
+    /**
+     * Ramp the motor power.
+     * @method BasicMotor#rampPower
+     * @param {number} fromPower For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
+     * @param {number} toPower For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
+     * @param {number} time How long the ramp should last (in milliseconds).
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    rampPower(fromPower, toPower, time) {
+        this.cancelEventTimer();
+        return new Promise((resolve) => {
+            utils_1.calculateRamp(this, fromPower, toPower, time)
+                .on("changePower", (power) => {
+                this.setPower(power, false);
+            })
+                .on("finished", resolve);
+        });
+    }
+    /**
+     * Stop the motor.
+     * @method BasicMotor#stop
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    stop() {
+        this.cancelEventTimer();
+        return this.setPower(0);
+    }
+    /**
+     * Brake the motor.
+     * @method BasicMotor#brake
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    brake() {
+        this.cancelEventTimer();
+        return this.setPower(Consts.BrakingStyle.BRAKE);
+    }
+}
+exports.BasicMotor = BasicMotor;
+//# sourceMappingURL=basicmotor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_colordistancesensor.js.html b/docs/devices_colordistancesensor.js.html new file mode 100644 index 00000000..ecbb8489 --- /dev/null +++ b/docs/devices_colordistancesensor.js.html @@ -0,0 +1,295 @@ + + + + + + + node-poweredup Source: devices/colordistancesensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/colordistancesensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class ColorDistanceSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.COLOR:
+                if (message[this.isWeDo2SmartHub ? 2 : 4] <= 10) {
+                    const color = message[this.isWeDo2SmartHub ? 2 : 4];
+                    /**
+                     * Emits when a color sensor is activated.
+                     * @event ColorDistanceSensor#color
+                     * @param {Color} color
+                     */
+                    this.notify("color", { color });
+                }
+                break;
+            case Mode.DISTANCE:
+                if (this.isWeDo2SmartHub) {
+                    break;
+                }
+                if (message[4] <= 10) {
+                    const distance = Math.floor(message[4] * 25.4) - 20;
+                    /**
+                     * Emits when a distance sensor is activated.
+                     * @event ColorDistanceSensor#distance
+                     * @param {number} distance Distance, in millimeters.
+                     */
+                    this.notify("distance", { distance });
+                }
+                break;
+            case Mode.COLOR_AND_DISTANCE:
+                if (this.isWeDo2SmartHub) {
+                    break;
+                }
+                let distance = message[5];
+                const partial = message[7];
+                if (partial > 0) {
+                    distance += 1.0 / partial;
+                }
+                distance = Math.floor(distance * 25.4) - 20;
+                /**
+                 * A combined color and distance event, emits when the sensor is activated.
+                 * @event ColorDistanceSensor#colorAndDistance
+                 * @param {Color} color
+                 * @param {number} distance Distance, in millimeters.
+                 */
+                if (message[4] <= 10) {
+                    const color = message[4];
+                    this.notify("colorAndDistance", { color, distance });
+                }
+                break;
+        }
+    }
+}
+exports.ColorDistanceSensor = ColorDistanceSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["COLOR"] = 0] = "COLOR";
+    Mode[Mode["DISTANCE"] = 1] = "DISTANCE";
+    Mode[Mode["COLOR_AND_DISTANCE"] = 8] = "COLOR_AND_DISTANCE";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "color": Mode.COLOR,
+    "distance": Mode.DISTANCE,
+    "colorAndDistance": Mode.COLOR_AND_DISTANCE
+};
+//# sourceMappingURL=colordistancesensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_currentsensor.js.html b/docs/devices_currentsensor.js.html new file mode 100644 index 00000000..1505e38a --- /dev/null +++ b/docs/devices_currentsensor.js.html @@ -0,0 +1,275 @@ + + + + + + + node-poweredup Source: devices/currentsensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/currentsensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class CurrentSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.CURRENT_SENSOR);
+    }
+    receive(message) {
+        const mode = this.mode;
+        switch (mode) {
+            case Mode.CURRENT:
+                if (this.isWeDo2SmartHub) {
+                    const current = message.readInt16LE(2) / 1000;
+                    this.notify("current", { current });
+                }
+                else {
+                    let maxCurrentValue = MaxCurrentValue[this.hub.type];
+                    if (maxCurrentValue === undefined) {
+                        maxCurrentValue = MaxCurrentValue[Consts.HubType.UNKNOWN];
+                    }
+                    let maxCurrentRaw = MaxCurrentRaw[this.hub.type];
+                    if (maxCurrentRaw === undefined) {
+                        maxCurrentRaw = MaxCurrentRaw[Consts.HubType.UNKNOWN];
+                    }
+                    const current = message.readUInt16LE(4) * maxCurrentValue / maxCurrentRaw;
+                    /**
+                     * Emits when a current change is detected.
+                     * @event CurrentSensor#current
+                     * @param {number} current
+                     */
+                    this.notify("current", { current });
+                }
+                break;
+        }
+    }
+}
+exports.CurrentSensor = CurrentSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["CURRENT"] = 0] = "CURRENT";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "current": Mode.CURRENT
+};
+const MaxCurrentValue = {
+    [Consts.HubType.UNKNOWN]: 2444,
+    [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4175,
+};
+const MaxCurrentRaw = {
+    [Consts.HubType.UNKNOWN]: 4095,
+};
+//# sourceMappingURL=currentsensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_duplotrainbasecolorsensor.js.html b/docs/devices_duplotrainbasecolorsensor.js.html new file mode 100644 index 00000000..8a4df3ad --- /dev/null +++ b/docs/devices_duplotrainbasecolorsensor.js.html @@ -0,0 +1,256 @@ + + + + + + + node-poweredup Source: devices/duplotrainbasecolorsensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/duplotrainbasecolorsensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class DuploTrainBaseColorSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.COLOR:
+                if (message[4] <= 10) {
+                    const color = message[4];
+                    /**
+                     * Emits when a color sensor is activated.
+                     * @event DuploTrainBaseColorSensor#color
+                     * @param {Color} color
+                     */
+                    this.notify("color", { color });
+                }
+                break;
+        }
+    }
+}
+exports.DuploTrainBaseColorSensor = DuploTrainBaseColorSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["COLOR"] = 0] = "COLOR";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "color": Mode.COLOR
+};
+//# sourceMappingURL=duplotrainbasecolorsensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_duplotrainbasespeaker.js.html b/docs/devices_duplotrainbasespeaker.js.html new file mode 100644 index 00000000..f7985b85 --- /dev/null +++ b/docs/devices_duplotrainbasespeaker.js.html @@ -0,0 +1,250 @@ + + + + + + + node-poweredup Source: devices/duplotrainbasespeaker.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/duplotrainbasespeaker.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class DuploTrainBaseSpeaker extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER);
+    }
+    /**
+     * Play a built-in train sound.
+     * @method DuploTrainBaseSpeaker#playSound
+     * @param {DuploTrainBaseSound} sound
+     * @returns {Promise} Resolved upon successful issuance of command.
+     */
+    playSound(sound) {
+        return new Promise((resolve, reject) => {
+            this.subscribe(Mode.SOUND);
+            this.writeDirect(0x01, Buffer.from([sound]));
+            return resolve();
+        });
+    }
+}
+exports.DuploTrainBaseSpeaker = DuploTrainBaseSpeaker;
+var Mode;
+(function (Mode) {
+    Mode[Mode["SOUND"] = 1] = "SOUND";
+})(Mode = exports.Mode || (exports.Mode = {}));
+//# sourceMappingURL=duplotrainbasespeaker.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_duplotrainbasespeedometer.js.html b/docs/devices_duplotrainbasespeedometer.js.html new file mode 100644 index 00000000..6be089b3 --- /dev/null +++ b/docs/devices_duplotrainbasespeedometer.js.html @@ -0,0 +1,254 @@ + + + + + + + node-poweredup Source: devices/duplotrainbasespeedometer.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/duplotrainbasespeedometer.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class DuploTrainBaseSpeedometer extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.SPEED:
+                const speed = message.readInt16LE(4);
+                /**
+                 * Emits on a speed change.
+                 * @event DuploTrainBaseSpeedometer#speed
+                 * @param {number} speed
+                 */
+                this.notify("speed", { speed });
+                break;
+        }
+    }
+}
+exports.DuploTrainBaseSpeedometer = DuploTrainBaseSpeedometer;
+var Mode;
+(function (Mode) {
+    Mode[Mode["SPEED"] = 0] = "SPEED";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "speed": Mode.SPEED
+};
+//# sourceMappingURL=duplotrainbasespeedometer.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_hubled.js.html b/docs/devices_hubled.js.html new file mode 100644 index 00000000..33402631 --- /dev/null +++ b/docs/devices_hubled.js.html @@ -0,0 +1,281 @@ + + + + + + + node-poweredup Source: devices/hubled.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/hubled.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class HubLED extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, {}, Consts.DeviceType.HUB_LED);
+    }
+    /**
+     * Set the color of the LED on the Hub via a color value.
+     * @method HubLED#setColor
+     * @param {Color} color
+     * @returns {Promise} Resolved upon successful issuance of command.
+     */
+    setColor(color) {
+        return new Promise((resolve, reject) => {
+            if (typeof color === "boolean") {
+                color = 0;
+            }
+            if (this.isWeDo2SmartHub) {
+                this.send(Buffer.from([0x06, 0x17, 0x01, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE);
+                this.send(Buffer.from([0x06, 0x04, 0x01, color]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
+            }
+            else {
+                this.subscribe(Mode.COLOR);
+                this.writeDirect(0x00, Buffer.from([color]));
+            }
+            return resolve();
+        });
+    }
+    /**
+     * Set the color of the LED on the Hub via RGB values.
+     * @method HubLED#setRGB
+     * @param {number} red
+     * @param {number} green
+     * @param {number} blue
+     * @returns {Promise} Resolved upon successful issuance of command.
+     */
+    setRGB(red, green, blue) {
+        return new Promise((resolve, reject) => {
+            if (this.isWeDo2SmartHub) {
+                this.send(Buffer.from([0x06, 0x17, 0x01, 0x02]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE);
+                this.send(Buffer.from([0x06, 0x04, 0x03, red, green, blue]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
+            }
+            else {
+                this.subscribe(Mode.RGB);
+                this.writeDirect(0x01, Buffer.from([red, green, blue]));
+            }
+            return resolve();
+        });
+    }
+}
+exports.HubLED = HubLED;
+var Mode;
+(function (Mode) {
+    Mode[Mode["COLOR"] = 0] = "COLOR";
+    Mode[Mode["RGB"] = 1] = "RGB";
+})(Mode = exports.Mode || (exports.Mode = {}));
+//# sourceMappingURL=hubled.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_light.js.html b/docs/devices_light.js.html new file mode 100644 index 00000000..87521bc8 --- /dev/null +++ b/docs/devices_light.js.html @@ -0,0 +1,267 @@ + + + + + + + node-poweredup Source: devices/light.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/light.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+const utils_1 = require("../utils");
+class Light extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, {}, Consts.DeviceType.LIGHT);
+    }
+    /**
+     * Set the light brightness.
+     * @method Light#setBrightness
+     * @param {number} brightness Brightness value between 0-100 (0 is off)
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    setBrightness(brightness, interrupt = true) {
+        if (interrupt) {
+            this.cancelEventTimer();
+        }
+        return new Promise((resolve) => {
+            this.writeDirect(0x00, Buffer.from([brightness]));
+            return resolve();
+        });
+    }
+    /**
+     * Ramp the light brightness.
+     * @method Light#rampBrightness
+     * @param {number} fromBrightness Brightness value between 0-100 (0 is off)
+     * @param {number} toBrightness Brightness value between 0-100 (0 is off)
+     * @param {number} time How long the ramp should last (in milliseconds).
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    rampBrightness(fromBrightness, toBrightness, time) {
+        this.cancelEventTimer();
+        return new Promise((resolve) => {
+            utils_1.calculateRamp(this, fromBrightness, toBrightness, time)
+                .on("changePower", (power) => {
+                this.setBrightness(power, false);
+            })
+                .on("finished", resolve);
+        });
+    }
+}
+exports.Light = Light;
+//# sourceMappingURL=light.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_motionsensor.js.html b/docs/devices_motionsensor.js.html new file mode 100644 index 00000000..c4f16b10 --- /dev/null +++ b/docs/devices_motionsensor.js.html @@ -0,0 +1,258 @@ + + + + + + + node-poweredup Source: devices/motionsensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/motionsensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class MotionSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.MOTION_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.DISTANCE:
+                let distance = message[this.isWeDo2SmartHub ? 2 : 4];
+                if (message[this.isWeDo2SmartHub ? 3 : 5] === 1) {
+                    distance = distance + 255;
+                }
+                distance *= 10;
+                /**
+                 * Emits when a distance sensor is activated.
+                 * @event MotionSensor#distance
+                 * @param {number} distance Distance, in millimeters.
+                 */
+                this.notify("distance", { distance });
+                break;
+        }
+    }
+}
+exports.MotionSensor = MotionSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["DISTANCE"] = 0] = "DISTANCE";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "distance": Mode.DISTANCE
+};
+//# sourceMappingURL=motionsensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_movehubtiltsensor.js.html b/docs/devices_movehubtiltsensor.js.html new file mode 100644 index 00000000..c3bdc7c8 --- /dev/null +++ b/docs/devices_movehubtiltsensor.js.html @@ -0,0 +1,256 @@ + + + + + + + node-poweredup Source: devices/movehubtiltsensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/movehubtiltsensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class MoveHubTiltSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.MOVE_HUB_TILT_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.TILT:
+                /**
+                 * Emits when a tilt sensor is activated.
+                 * @event MoveHubTiltSensor#tilt
+                 * @param {number} x
+                 * @param {number} y
+                 */
+                const x = -message.readInt8(4);
+                const y = message.readInt8(5);
+                this.notify("tilt", { x, y });
+                break;
+        }
+    }
+}
+exports.MoveHubTiltSensor = MoveHubTiltSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["TILT"] = 0] = "TILT";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "tilt": Mode.TILT
+};
+//# sourceMappingURL=movehubtiltsensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_piezobuzzer.js.html b/docs/devices_piezobuzzer.js.html new file mode 100644 index 00000000..9edb6bd7 --- /dev/null +++ b/docs/devices_piezobuzzer.js.html @@ -0,0 +1,249 @@ + + + + + + + node-poweredup Source: devices/piezobuzzer.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/piezobuzzer.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class PiezoBuzzer extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, {}, Consts.DeviceType.PIEZO_BUZZER);
+    }
+    /**
+     * Play a tone on the Hub's in-built buzzer
+     * @method PiezoBuzzer#playTone
+     * @param {number} frequency
+     * @param {number} time How long the tone should play for (in milliseconds).
+     * @returns {Promise} Resolved upon successful completion of command (ie. once the tone has finished playing).
+     */
+    playTone(frequency, time) {
+        return new Promise((resolve) => {
+            const data = Buffer.from([0x05, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00]);
+            data.writeUInt16LE(frequency, 3);
+            data.writeUInt16LE(time, 5);
+            this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
+            global.setTimeout(resolve, time);
+        });
+    }
+}
+exports.PiezoBuzzer = PiezoBuzzer;
+//# sourceMappingURL=piezobuzzer.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_remotecontrolbutton.js.html b/docs/devices_remotecontrolbutton.js.html new file mode 100644 index 00000000..db272343 --- /dev/null +++ b/docs/devices_remotecontrolbutton.js.html @@ -0,0 +1,260 @@ + + + + + + + node-poweredup Source: devices/remotecontrolbutton.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/remotecontrolbutton.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class RemoteControlButton extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.REMOTE_CONTROL_BUTTON);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.BUTTON_EVENTS:
+                /**
+                 * Emits when a button on the remote is pressed or released.
+                 * @event RemoteControlButton#button
+                 * @param {number} event
+                 */
+                const event = message[4];
+                this.notify("remoteButton", { event });
+                break;
+        }
+    }
+}
+exports.RemoteControlButton = RemoteControlButton;
+var Mode;
+(function (Mode) {
+    Mode[Mode["BUTTON_EVENTS"] = 0] = "BUTTON_EVENTS";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "remoteButton": Mode.BUTTON_EVENTS
+};
+exports.ButtonState = {
+    "UP": 0x01,
+    "DOWN": 0xff,
+    "STOP": 0x7f,
+    "RELEASED": 0x00,
+};
+//# sourceMappingURL=remotecontrolbutton.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_tachomotor.js.html b/docs/devices_tachomotor.js.html new file mode 100644 index 00000000..1e2ce115 --- /dev/null +++ b/docs/devices_tachomotor.js.html @@ -0,0 +1,336 @@ + + + + + + + node-poweredup Source: devices/tachomotor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/tachomotor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const basicmotor_1 = require("./basicmotor");
+const Consts = __importStar(require("../consts"));
+const utils_1 = require("../utils");
+class TachoMotor extends basicmotor_1.BasicMotor {
+    constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) {
+        super(hub, portId, Object.assign({}, modeMap, exports.ModeMap), type);
+        this._brakeStyle = Consts.BrakingStyle.BRAKE;
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.ROTATION:
+                const degrees = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4);
+                /**
+                 * Emits when a rotation sensor is activated.
+                 * @event TachoMotor#rotate
+                 * @param {number} rotation
+                 */
+                this.notify("rotate", { degrees });
+                break;
+        }
+    }
+    setBrakingStyle(style) {
+        this._brakeStyle = style;
+    }
+    /**
+     * Set the motor speed.
+     * @method TachoMotor#setSpeed
+     * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    setSpeed(speed, time) {
+        if (!this.isVirtualPort && speed instanceof Array) {
+            throw new Error("Only virtual ports can accept multiple speeds");
+        }
+        if (this.isWeDo2SmartHub) {
+            throw new Error("Motor speed is not available on the WeDo 2.0 Smart Hub");
+        }
+        this.cancelEventTimer();
+        return new Promise((resolve) => {
+            this._busy = true;
+            if (speed === undefined || speed === null) {
+                speed = 100;
+            }
+            let message;
+            if (time !== undefined) {
+                if (speed instanceof Array) {
+                    message = Buffer.from([0x81, this.portId, 0x11, 0x0a, 0x00, 0x00, utils_1.mapSpeed(speed[0]), utils_1.mapSpeed(speed[1]), 0x64, this._brakeStyle, 0x00]);
+                }
+                else {
+                    message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, utils_1.mapSpeed(speed), 0x64, this._brakeStyle, 0x00]);
+                }
+                message.writeUInt16LE(time, 4);
+            }
+            else {
+                if (speed instanceof Array) {
+                    message = Buffer.from([0x81, this.portId, 0x11, 0x08, utils_1.mapSpeed(speed[0]), utils_1.mapSpeed(speed[1]), 0x64, this._brakeStyle, 0x00]);
+                }
+                else {
+                    message = Buffer.from([0x81, this.portId, 0x11, 0x07, utils_1.mapSpeed(speed), 0x64, 0x03, 0x64, this._brakeStyle, 0x00]);
+                }
+            }
+            this.send(message);
+            this._finished = () => {
+                return resolve();
+            };
+        });
+    }
+    /**
+     * Rotate a motor by a given amount of degrees.
+     * @method TachoMotor#rotateByDegrees
+     * @param {number} degrees How much the motor should be rotated (in degrees).
+     * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
+     * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
+     */
+    rotateByDegrees(degrees, speed) {
+        if (!this.isVirtualPort && speed instanceof Array) {
+            throw new Error("Only virtual ports can accept multiple speeds");
+        }
+        if (this.isWeDo2SmartHub) {
+            throw new Error("Rotation is not available on the WeDo 2.0 Smart Hub");
+        }
+        this.cancelEventTimer();
+        return new Promise((resolve) => {
+            this._busy = true;
+            if (speed === undefined || speed === null) {
+                speed = 100;
+            }
+            let message;
+            if (speed instanceof Array) {
+                message = Buffer.from([0x81, this.portId, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, utils_1.mapSpeed(speed[0]), utils_1.mapSpeed(speed[1]), 0x64, this._brakeStyle, 0x03]);
+            }
+            else {
+                message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, utils_1.mapSpeed(speed), 0x64, this._brakeStyle, 0x03]);
+            }
+            message.writeUInt32LE(degrees, 4);
+            this.send(message);
+            this._finished = () => {
+                return resolve();
+            };
+        });
+    }
+}
+exports.TachoMotor = TachoMotor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["ROTATION"] = 2] = "ROTATION";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "rotate": Mode.ROTATION
+};
+//# sourceMappingURL=tachomotor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_techniccolorsensor.js.html b/docs/devices_techniccolorsensor.js.html new file mode 100644 index 00000000..104ed63c --- /dev/null +++ b/docs/devices_techniccolorsensor.js.html @@ -0,0 +1,278 @@ + + + + + + + node-poweredup Source: devices/techniccolorsensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/techniccolorsensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class TechnicColorSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_COLOR_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.COLOR:
+                if (message[4] <= 10) {
+                    const color = message[4];
+                    /**
+                     * Emits when a color sensor is activated.
+                     * @event TechnicColorSensor#color
+                     * @param {Color} color
+                     */
+                    this.notify("color", { color });
+                }
+                break;
+            case Mode.REFLECTIVITY:
+                const reflect = message[4];
+                /**
+                 * Emits when the light reflectivity changes.
+                 * @event TechnicColorSensor#reflect Percentage, from 0 to 100.
+                 * @param {number} reflect
+                 */
+                this.notify("reflect", { reflect });
+                break;
+            case Mode.AMBIENT_LIGHT:
+                const ambient = message[4];
+                /**
+                 * Emits when the ambient light changes.
+                 * @event TechnicColorSensor#ambient Percentage, from 0 to 100.
+                 * @param {number} ambient
+                 */
+                this.notify("ambient", { ambient });
+                break;
+        }
+    }
+}
+exports.TechnicColorSensor = TechnicColorSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["COLOR"] = 0] = "COLOR";
+    Mode[Mode["REFLECTIVITY"] = 1] = "REFLECTIVITY";
+    Mode[Mode["AMBIENT_LIGHT"] = 2] = "AMBIENT_LIGHT";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "color": Mode.COLOR,
+    "reflect": Mode.REFLECTIVITY,
+    "ambient": Mode.AMBIENT_LIGHT
+};
+//# sourceMappingURL=techniccolorsensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_technicdistancesensor.js.html b/docs/devices_technicdistancesensor.js.html new file mode 100644 index 00000000..ede059c7 --- /dev/null +++ b/docs/devices_technicdistancesensor.js.html @@ -0,0 +1,277 @@ + + + + + + + node-poweredup Source: devices/technicdistancesensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/technicdistancesensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class TechnicDistanceSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_DISTANCE_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.DISTANCE:
+                const distance = message.readUInt16LE(4);
+                /**
+                 * Emits when the detected distance changes (Slow sampling covers 40mm to 2500mm).
+                 * @event TechnicDistanceSensor#distance Distance, from 40 to 2500mm
+                 * @param {number} distance
+                 */
+                this.notify("distance", { distance });
+                break;
+            case Mode.FAST_DISTANCE:
+                const fastDistance = message.readUInt16LE(4);
+                /**
+                 * Emits when the detected distance changes (Fast sampling covers 50mm to 320mm).
+                 * @event TechnicDistanceSensor#fastDistance Distance, from 50 to 320mm
+                 * @param {number} fastDistance
+                 */
+                this.notify("fastDistance", { fastDistance });
+                break;
+        }
+    }
+    /**
+     * Set the brightness (or turn on/off) the lights around the eyes.
+     * @method TechnicDistanceSensor#setBrightness
+     * @param {number} topLeft Top left quadrant (above left eye). 0-100 brightness.
+     * @param {number} bottomLeft Bottom left quadrant (below left eye). 0-100 brightness.
+     * @param {number} topRight Top right quadrant (above right eye). 0-100 brightness.
+     * @param {number} bottomRight Bottom right quadrant (below right eye). 0-100 brightness.
+     * @returns {Promise} Resolved upon successful completion of command.
+     */
+    setBrightness(topLeft, bottomLeft, topRight, bottomRight) {
+        this.writeDirect(0x05, Buffer.from([topLeft, topRight, bottomLeft, bottomRight]));
+    }
+}
+exports.TechnicDistanceSensor = TechnicDistanceSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["DISTANCE"] = 0] = "DISTANCE";
+    Mode[Mode["FAST_DISTANCE"] = 1] = "FAST_DISTANCE";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "distance": Mode.DISTANCE,
+    "fastDistance": Mode.FAST_DISTANCE
+};
+//# sourceMappingURL=technicdistancesensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_technicforcesensor.js.html b/docs/devices_technicforcesensor.js.html new file mode 100644 index 00000000..3d2c10e7 --- /dev/null +++ b/docs/devices_technicforcesensor.js.html @@ -0,0 +1,276 @@ + + + + + + + node-poweredup Source: devices/technicforcesensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/technicforcesensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class TechnicForceSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_FORCE_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.FORCE:
+                const force = message[4] / 10;
+                /**
+                 * Emits when force is applied.
+                 * @event TechnicForceSensor#force Force, in newtons (0-10).
+                 * @param {number} force
+                 */
+                this.notify("force", { force });
+                break;
+            case Mode.TOUCHED:
+                const touched = message[4] ? true : false;
+                /**
+                 * Emits when the sensor is touched.
+                 * @event TechnicForceSensor#touch Touched on/off (boolean).
+                 * @param {boolean} touch
+                 */
+                this.notify("touched", { touched });
+                break;
+            case Mode.TAPPED:
+                const tapped = message[4];
+                /**
+                 * Emits when the sensor is tapped.
+                 * @event TechnicForceSensor#tapped How hard the sensor was tapped, from 0-3.
+                 * @param {number} tapped
+                 */
+                this.notify("tapped", { tapped });
+                break;
+        }
+    }
+}
+exports.TechnicForceSensor = TechnicForceSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["FORCE"] = 0] = "FORCE";
+    Mode[Mode["TOUCHED"] = 1] = "TOUCHED";
+    Mode[Mode["TAPPED"] = 2] = "TAPPED";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "force": Mode.FORCE,
+    "touched": Mode.TOUCHED,
+    "tapped": Mode.TAPPED
+};
+//# sourceMappingURL=technicforcesensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_technicmediumhubaccelerometersensor.js.html b/docs/devices_technicmediumhubaccelerometersensor.js.html new file mode 100644 index 00000000..a5b737f9 --- /dev/null +++ b/docs/devices_technicmediumhubaccelerometersensor.js.html @@ -0,0 +1,259 @@ + + + + + + + node-poweredup Source: devices/technicmediumhubaccelerometersensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/technicmediumhubaccelerometersensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class TechnicMediumHubAccelerometerSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.ACCEL:
+                /**
+                 * Emits when accelerometer detects movement. Measured in mG.
+                 * @event LPF2Hub#accel
+                 * @param {string} port
+                 * @param {number} x
+                 * @param {number} y
+                 * @param {number} z
+                 */
+                const x = Math.round(message.readInt16LE(4) / 4.096);
+                const y = Math.round(message.readInt16LE(6) / 4.096);
+                const z = Math.round(message.readInt16LE(8) / 4.096);
+                this.notify("accel", { x, y, z });
+                break;
+        }
+    }
+}
+exports.TechnicMediumHubAccelerometerSensor = TechnicMediumHubAccelerometerSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["ACCEL"] = 0] = "ACCEL";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "accel": Mode.ACCEL
+};
+//# sourceMappingURL=technicmediumhubaccelerometersensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_technicmediumhubgyrosensor.js.html b/docs/devices_technicmediumhubgyrosensor.js.html new file mode 100644 index 00000000..b2674324 --- /dev/null +++ b/docs/devices_technicmediumhubgyrosensor.js.html @@ -0,0 +1,258 @@ + + + + + + + node-poweredup Source: devices/technicmediumhubgyrosensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/technicmediumhubgyrosensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class TechnicMediumHubGyroSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.GYRO:
+                /**
+                 * Emits when gyroscope detects movement. Measured in DPS - degrees per second.
+                 * @event TechnicMediumHubGyroSensor#gyro
+                 * @param {number} x
+                 * @param {number} y
+                 * @param {number} z
+                 */
+                const x = Math.round(message.readInt16LE(4) * 7 / 400);
+                const y = Math.round(message.readInt16LE(6) * 7 / 400);
+                const z = Math.round(message.readInt16LE(8) * 7 / 400);
+                this.notify("gyro", { x, y, z });
+                break;
+        }
+    }
+}
+exports.TechnicMediumHubGyroSensor = TechnicMediumHubGyroSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["GYRO"] = 0] = "GYRO";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "gyro": Mode.GYRO
+};
+//# sourceMappingURL=technicmediumhubgyrosensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_technicmediumhubtiltsensor.js.html b/docs/devices_technicmediumhubtiltsensor.js.html new file mode 100644 index 00000000..0453b844 --- /dev/null +++ b/docs/devices_technicmediumhubtiltsensor.js.html @@ -0,0 +1,258 @@ + + + + + + + node-poweredup Source: devices/technicmediumhubtiltsensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/technicmediumhubtiltsensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class TechnicMediumHubTiltSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.TILT:
+                /**
+                 * Emits when a tilt sensor is activated.
+                 * @event TechnicMediumHubTiltSensor#tilt
+                 * @param {number} x
+                 * @param {number} y
+                 * @param {number} z
+                 */
+                const z = -message.readInt16LE(4);
+                const y = message.readInt16LE(6);
+                const x = message.readInt16LE(8);
+                this.notify("tilt", { x, y, z });
+                break;
+        }
+    }
+}
+exports.TechnicMediumHubTiltSensor = TechnicMediumHubTiltSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["TILT"] = 0] = "TILT";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "tilt": Mode.TILT
+};
+//# sourceMappingURL=technicmediumhubtiltsensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_tiltsensor.js.html b/docs/devices_tiltsensor.js.html new file mode 100644 index 00000000..4c098820 --- /dev/null +++ b/docs/devices_tiltsensor.js.html @@ -0,0 +1,256 @@ + + + + + + + node-poweredup Source: devices/tiltsensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/tiltsensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class TiltSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.TILT_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.TILT:
+                const x = message.readInt8(this.isWeDo2SmartHub ? 2 : 4);
+                const y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5);
+                /**
+                 * Emits when a tilt sensor is activated.
+                 * @event LPF2Hub#tilt
+                 * @param {number} x
+                 * @param {number} y
+                 */
+                this.notify("tilt", { x, y });
+                break;
+        }
+    }
+}
+exports.TiltSensor = TiltSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["TILT"] = 0] = "TILT";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "tilt": Mode.TILT
+};
+//# sourceMappingURL=tiltsensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_voltagesensor.js.html b/docs/devices_voltagesensor.js.html new file mode 100644 index 00000000..a341a208 --- /dev/null +++ b/docs/devices_voltagesensor.js.html @@ -0,0 +1,279 @@ + + + + + + + node-poweredup Source: devices/voltagesensor.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: devices/voltagesensor.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const device_1 = require("./device");
+const Consts = __importStar(require("../consts"));
+class VoltageSensor extends device_1.Device {
+    constructor(hub, portId) {
+        super(hub, portId, exports.ModeMap, Consts.DeviceType.VOLTAGE_SENSOR);
+    }
+    receive(message) {
+        const mode = this._mode;
+        switch (mode) {
+            case Mode.VOLTAGE:
+                if (this.isWeDo2SmartHub) {
+                    const voltage = message.readInt16LE(2) / 40;
+                    this.notify("voltage", { voltage });
+                }
+                else {
+                    let maxVoltageValue = MaxVoltageValue[this.hub.type];
+                    if (maxVoltageValue === undefined) {
+                        maxVoltageValue = MaxVoltageValue[Consts.HubType.UNKNOWN];
+                    }
+                    let maxVoltageRaw = MaxVoltageRaw[this.hub.type];
+                    if (maxVoltageRaw === undefined) {
+                        maxVoltageRaw = MaxVoltageRaw[Consts.HubType.UNKNOWN];
+                    }
+                    const voltage = message.readUInt16LE(4) * maxVoltageValue / maxVoltageRaw;
+                    /**
+                     * Emits when a voltage change is detected.
+                     * @event VoltageSensor#voltage
+                     * @param {number} voltage
+                     */
+                    this.notify("voltage", { voltage });
+                }
+                break;
+        }
+    }
+}
+exports.VoltageSensor = VoltageSensor;
+var Mode;
+(function (Mode) {
+    Mode[Mode["VOLTAGE"] = 0] = "VOLTAGE";
+})(Mode = exports.Mode || (exports.Mode = {}));
+exports.ModeMap = {
+    "voltage": Mode.VOLTAGE
+};
+const MaxVoltageValue = {
+    [Consts.HubType.UNKNOWN]: 9.615,
+    [Consts.HubType.DUPLO_TRAIN_BASE]: 6.4,
+    [Consts.HubType.REMOTE_CONTROL]: 6.4,
+};
+const MaxVoltageRaw = {
+    [Consts.HubType.UNKNOWN]: 3893,
+    [Consts.HubType.DUPLO_TRAIN_BASE]: 3047,
+    [Consts.HubType.REMOTE_CONTROL]: 3200,
+    [Consts.HubType.TECHNIC_MEDIUM_HUB]: 4095,
+};
+//# sourceMappingURL=voltagesensor.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/global.html b/docs/global.html index be0995be..2d215b1a 100644 --- a/docs/global.html +++ b/docs/global.html @@ -33,14 +33,14 @@ @@ -160,6 +160,292 @@

Type Definitions

+
+
+

BrakingStyle

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
HOLD + + +number + + + + +

127

BRAKE + + +number + + + + +

128

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

BrakingStyle

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
HOLD + + +number + + + + +

127

BRAKE + + +number + + + + +

128

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + +

ButtonState

@@ -356,7 +642,7 @@
Properties:
@@ -571,7 +857,7 @@
Properties:
@@ -954,7 +1240,7 @@
Properties:
@@ -1337,7 +1623,7 @@
Properties:
@@ -1421,7 +1707,7 @@
Properties:
- BASIC_MOTOR + SIMPLE_MEDIUM_LINEAR_MOTOR @@ -1637,7 +1923,7 @@
Properties:
- BOOST_DISTANCE + COLOR_DISTANCE_SENSOR @@ -1661,7 +1947,7 @@
Properties:
- BOOST_TACHO_MOTOR + MEDIUM_LINEAR_MOTOR @@ -1685,7 +1971,7 @@
Properties:
- BOOST_MOVE_HUB_MOTOR + MOVE_HUB_MEDIUM_LINEAR_MOTOR @@ -2092,7 +2378,7 @@
Properties:
- BASIC_MOTOR + SIMPLE_MEDIUM_LINEAR_MOTOR @@ -2308,7 +2594,7 @@
Properties:
- BOOST_DISTANCE + COLOR_DISTANCE_SENSOR @@ -2332,7 +2618,7 @@
Properties:
- BOOST_TACHO_MOTOR + MEDIUM_LINEAR_MOTOR @@ -2356,7 +2642,7 @@
Properties:
- BOOST_MOVE_HUB_MOTOR + MOVE_HUB_MEDIUM_LINEAR_MOTOR @@ -2894,7 +3180,7 @@
Properties:
@@ -3109,7 +3395,7 @@
Properties:
@@ -3217,7 +3503,7 @@
Properties:
- BOOST_MOVE_HUB + MOVE_HUB @@ -3480,7 +3766,7 @@
Properties:
- BOOST_MOVE_HUB + MOVE_HUB @@ -3704,7 +3990,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/hubs_basehub.js.html b/docs/hubs_basehub.js.html new file mode 100644 index 00000000..af7ab7af --- /dev/null +++ b/docs/hubs_basehub.js.html @@ -0,0 +1,543 @@ + + + + + + + node-poweredup Source: hubs/basehub.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/basehub.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const events_1 = require("events");
+const colordistancesensor_1 = require("../devices/colordistancesensor");
+const currentsensor_1 = require("../devices/currentsensor");
+const device_1 = require("../devices/device");
+const duplotrainbasecolorsensor_1 = require("../devices/duplotrainbasecolorsensor");
+const duplotrainbasemotor_1 = require("../devices/duplotrainbasemotor");
+const duplotrainbasespeaker_1 = require("../devices/duplotrainbasespeaker");
+const duplotrainbasespeedometer_1 = require("../devices/duplotrainbasespeedometer");
+const hubled_1 = require("../devices/hubled");
+const light_1 = require("../devices/light");
+const mediumlinearmotor_1 = require("../devices/mediumlinearmotor");
+const motionsensor_1 = require("../devices/motionsensor");
+const movehubmediumlinearmotor_1 = require("../devices/movehubmediumlinearmotor");
+const movehubtiltsensor_1 = require("../devices/movehubtiltsensor");
+const piezobuzzer_1 = require("../devices/piezobuzzer");
+const remotecontrolbutton_1 = require("../devices/remotecontrolbutton");
+const simplemediumlinearmotor_1 = require("../devices/simplemediumlinearmotor");
+const techniccolorsensor_1 = require("../devices/techniccolorsensor");
+const technicdistancesensor_1 = require("../devices/technicdistancesensor");
+const technicforcesensor_1 = require("../devices/technicforcesensor");
+const techniclargeangularmotor_1 = require("../devices/techniclargeangularmotor");
+const techniclargelinearmotor_1 = require("../devices/techniclargelinearmotor");
+const technicmediumangularmotor_1 = require("../devices/technicmediumangularmotor");
+const technicmediumhubaccelerometersensor_1 = require("../devices/technicmediumhubaccelerometersensor");
+const technicmediumhubgyrosensor_1 = require("../devices/technicmediumhubgyrosensor");
+const technicmediumhubtiltsensor_1 = require("../devices/technicmediumhubtiltsensor");
+const technicxlargelinearmotor_1 = require("../devices/technicxlargelinearmotor");
+const tiltsensor_1 = require("../devices/tiltsensor");
+const trainmotor_1 = require("../devices/trainmotor");
+const voltagesensor_1 = require("../devices/voltagesensor");
+const Consts = __importStar(require("../consts"));
+const Debug = require("debug");
+const debug = Debug("basehub");
+/**
+ * @class BaseHub
+ * @extends EventEmitter
+ */
+class BaseHub extends events_1.EventEmitter {
+    constructor(bleDevice, portMap = {}, type = Consts.HubType.UNKNOWN) {
+        super();
+        this._attachedDevices = {};
+        this._name = "";
+        this._firmwareVersion = "0.0.00.0000";
+        this._hardwareVersion = "0.0.00.0000";
+        this._primaryMACAddress = "00:00:00:00:00:00";
+        this._batteryLevel = 100;
+        this._rssi = -60;
+        this._portMap = {};
+        this._virtualPorts = [];
+        this._attachCallbacks = [];
+        this.setMaxListeners(23); // Technic Medium Hub has 9 built in devices + 4 external ports. Node.js throws a warning after 10 attached event listeners.
+        this._type = type;
+        this._bleDevice = bleDevice;
+        this._portMap = Object.assign({}, portMap);
+        bleDevice.on("disconnect", () => {
+            /**
+             * Emits when the hub is disconnected.
+             * @event Hub#disconnect
+             */
+            this.emit("disconnect");
+        });
+    }
+    /**
+     * @readonly
+     * @property {string} name Name of the hub
+     */
+    get name() {
+        return this._bleDevice.name;
+    }
+    /**
+     * @readonly
+     * @property {string} type Hub type
+     */
+    get type() {
+        return this._type;
+    }
+    /**
+     * @readonly
+     * @property {string[]} ports Array of port names
+     */
+    get ports() {
+        return Object.keys(this._portMap);
+    }
+    /**
+     * @readonly
+     * @property {string} firmwareVersion Firmware version of the hub
+     */
+    get firmwareVersion() {
+        return this._firmwareVersion;
+    }
+    /**
+     * @readonly
+     * @property {string} firmwareVersion Hardware version of the hub
+     */
+    get hardwareVersion() {
+        return this._hardwareVersion;
+    }
+    /**
+     * @readonly
+     * @property {string} primaryMACAddress Primary MAC address of the hub
+     */
+    get primaryMACAddress() {
+        return this._primaryMACAddress;
+    }
+    /**
+     * @readonly
+     * @property {string} uuid UUID of the hub
+     */
+    get uuid() {
+        return this._bleDevice.uuid;
+    }
+    /**
+     * @readonly
+     * @property {number} batteryLevel Battery level of the hub (Percentage between 0-100)
+     */
+    get batteryLevel() {
+        return this._batteryLevel;
+    }
+    /**
+     * @readonly
+     * @property {number} rssi Signal strength of the hub
+     */
+    get rssi() {
+        return this._rssi;
+    }
+    /**
+     * Connect to the Hub.
+     * @method Hub#connect
+     * @returns {Promise} Resolved upon successful connect.
+     */
+    connect() {
+        return new Promise(async (connectResolve, connectReject) => {
+            if (this._bleDevice.connecting) {
+                return connectReject("Already connecting");
+            }
+            else if (this._bleDevice.connected) {
+                return connectReject("Already connected");
+            }
+            await this._bleDevice.connect();
+            return connectResolve();
+        });
+    }
+    /**
+     * Disconnect the Hub.
+     * @method Hub#disconnect
+     * @returns {Promise} Resolved upon successful disconnect.
+     */
+    disconnect() {
+        return this._bleDevice.disconnect();
+    }
+    getDeviceAtPort(portName) {
+        const portId = this._portMap[portName];
+        if (portId !== undefined) {
+            return this._attachedDevices[portId];
+        }
+        else {
+            return undefined;
+        }
+    }
+    waitForDeviceAtPort(portName) {
+        return new Promise((resolve) => {
+            const existingDevice = this.getDeviceAtPort(portName);
+            if (existingDevice) {
+                return resolve(existingDevice);
+            }
+            this._attachCallbacks.push((device) => {
+                if (device.portName === portName) {
+                    resolve(device);
+                    return true;
+                }
+                else {
+                    return false;
+                }
+            });
+        });
+    }
+    getDevices() {
+        return Object.values(this._attachedDevices);
+    }
+    getDevicesByType(deviceType) {
+        return this.getDevices().filter((device) => device.type === deviceType);
+    }
+    waitForDeviceByType(deviceType) {
+        return new Promise((resolve) => {
+            const existingDevices = this.getDevicesByType(deviceType);
+            if (existingDevices.length >= 1) {
+                return resolve(existingDevices[0]);
+            }
+            this._attachCallbacks.push((device) => {
+                if (device.type === deviceType) {
+                    resolve(device);
+                    return true;
+                }
+                else {
+                    return false;
+                }
+            });
+        });
+    }
+    getPortNameForPortId(portId) {
+        for (const port of Object.keys(this._portMap)) {
+            if (this._portMap[port] === portId) {
+                return port;
+            }
+        }
+        return;
+    }
+    isPortVirtual(portId) {
+        return (this._virtualPorts.indexOf(portId) > -1);
+    }
+    /**
+     * Sleep a given amount of time.
+     *
+     * This is a helper method to make it easier to add delays into a chain of commands.
+     * @method Hub#sleep
+     * @param {number} delay How long to sleep (in milliseconds).
+     * @returns {Promise} Resolved after the delay is finished.
+     */
+    sleep(delay) {
+        return new Promise((resolve) => {
+            global.setTimeout(resolve, delay);
+        });
+    }
+    /**
+     * Wait until a given list of concurrently running commands are complete.
+     *
+     * This is a helper method to make it easier to wait for concurrent commands to complete.
+     * @method Hub#wait
+     * @param {Array<Promise<any>>} commands Array of executing commands.
+     * @returns {Promise} Resolved after the commands are finished.
+     */
+    wait(commands) {
+        return Promise.all(commands);
+    }
+    send(message, uuid, callback) {
+        if (callback) {
+            callback();
+        }
+    }
+    subscribe(portId, deviceType, mode) {
+        // NK Do nothing here
+    }
+    unsubscribe(portId, deviceType, mode) {
+        // NK Do nothing here
+    }
+    _attachDevice(device) {
+        this._attachedDevices[device.portId] = device;
+        /**
+         * Emits when a device is attached to the Hub.
+         * @event Hub#attach
+         * @param {Device} device
+         */
+        this.emit("attach", device);
+        debug(`Attached device type ${device.type} (${Consts.DeviceTypeNames[device.type]}) on port ${device.portName} (${device.portId})`);
+        let i = this._attachCallbacks.length;
+        while (i--) {
+            const callback = this._attachCallbacks[i];
+            if (callback(device)) {
+                this._attachCallbacks.splice(i, 1);
+            }
+        }
+    }
+    _detachDevice(device) {
+        delete this._attachedDevices[device.portId];
+        /**
+         * Emits when a device is detached from the Hub.
+         * @event Hub#attach
+         * @param {Device} device
+         */
+        this.emit("detach", device);
+        debug(`Detached device type ${device.type} (${Consts.DeviceTypeNames[device.type]}) on port ${device.portName} (${device.portId})`);
+    }
+    _createDevice(deviceType, portId) {
+        let constructor;
+        // NK TODO: This needs to go in a better place
+        const deviceConstructors = {
+            [Consts.DeviceType.LIGHT]: light_1.Light,
+            [Consts.DeviceType.TRAIN_MOTOR]: trainmotor_1.TrainMotor,
+            [Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR]: simplemediumlinearmotor_1.SimpleMediumLinearMotor,
+            [Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR]: movehubmediumlinearmotor_1.MoveHubMediumLinearMotor,
+            [Consts.DeviceType.MOTION_SENSOR]: motionsensor_1.MotionSensor,
+            [Consts.DeviceType.TILT_SENSOR]: tiltsensor_1.TiltSensor,
+            [Consts.DeviceType.MOVE_HUB_TILT_SENSOR]: movehubtiltsensor_1.MoveHubTiltSensor,
+            [Consts.DeviceType.PIEZO_BUZZER]: piezobuzzer_1.PiezoBuzzer,
+            [Consts.DeviceType.TECHNIC_COLOR_SENSOR]: techniccolorsensor_1.TechnicColorSensor,
+            [Consts.DeviceType.TECHNIC_DISTANCE_SENSOR]: technicdistancesensor_1.TechnicDistanceSensor,
+            [Consts.DeviceType.TECHNIC_FORCE_SENSOR]: technicforcesensor_1.TechnicForceSensor,
+            [Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR]: technicmediumhubtiltsensor_1.TechnicMediumHubTiltSensor,
+            [Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR]: technicmediumhubgyrosensor_1.TechnicMediumHubGyroSensor,
+            [Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER]: technicmediumhubaccelerometersensor_1.TechnicMediumHubAccelerometerSensor,
+            [Consts.DeviceType.MEDIUM_LINEAR_MOTOR]: mediumlinearmotor_1.MediumLinearMotor,
+            [Consts.DeviceType.TECHNIC_MEDIUM_ANGULAR_MOTOR]: technicmediumangularmotor_1.TechnicMediumAngularMotor,
+            [Consts.DeviceType.TECHNIC_LARGE_ANGULAR_MOTOR]: techniclargeangularmotor_1.TechnicLargeAngularMotor,
+            [Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR]: techniclargelinearmotor_1.TechnicLargeLinearMotor,
+            [Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR]: technicxlargelinearmotor_1.TechnicXLargeLinearMotor,
+            [Consts.DeviceType.COLOR_DISTANCE_SENSOR]: colordistancesensor_1.ColorDistanceSensor,
+            [Consts.DeviceType.VOLTAGE_SENSOR]: voltagesensor_1.VoltageSensor,
+            [Consts.DeviceType.CURRENT_SENSOR]: currentsensor_1.CurrentSensor,
+            [Consts.DeviceType.REMOTE_CONTROL_BUTTON]: remotecontrolbutton_1.RemoteControlButton,
+            [Consts.DeviceType.HUB_LED]: hubled_1.HubLED,
+            [Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR]: duplotrainbasecolorsensor_1.DuploTrainBaseColorSensor,
+            [Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR]: duplotrainbasemotor_1.DuploTrainBaseMotor,
+            [Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER]: duplotrainbasespeaker_1.DuploTrainBaseSpeaker,
+            [Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER]: duplotrainbasespeedometer_1.DuploTrainBaseSpeedometer
+        };
+        constructor = deviceConstructors[deviceType];
+        if (constructor) {
+            return new constructor(this, portId);
+        }
+        else {
+            return new device_1.Device(this, portId, undefined, deviceType);
+        }
+    }
+    _getDeviceByPortId(portId) {
+        return this._attachedDevices[portId];
+    }
+}
+exports.BaseHub = BaseHub;
+//# sourceMappingURL=basehub.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_duplotrainbase.js.html b/docs/hubs_duplotrainbase.js.html new file mode 100644 index 00000000..7d4c71bb --- /dev/null +++ b/docs/hubs_duplotrainbase.js.html @@ -0,0 +1,263 @@ + + + + + + + node-poweredup Source: hubs/duplotrainbase.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/duplotrainbase.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const lpf2hub_1 = require("./lpf2hub");
+const Consts = __importStar(require("../consts"));
+const Debug = require("debug");
+const debug = Debug("duplotrainbase");
+/**
+ * The DuploTrainBase is emitted if the discovered device is a Duplo Train Base.
+ * @class DuploTrainBase
+ * @extends LPF2Hub
+ * @extends BaseHub
+ */
+class DuploTrainBase extends lpf2hub_1.LPF2Hub {
+    static IsDuploTrainBase(peripheral) {
+        return (peripheral.advertisement &&
+            peripheral.advertisement.serviceUuids &&
+            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
+            peripheral.advertisement.manufacturerData &&
+            peripheral.advertisement.manufacturerData.length > 3 &&
+            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_BASE_ID);
+    }
+    constructor(device) {
+        super(device, exports.PortMap, Consts.HubType.DUPLO_TRAIN_BASE);
+        debug("Discovered Duplo Train Base");
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("Connecting to Duplo Train Base");
+            await super.connect();
+            debug("Connect completed");
+            return resolve();
+        });
+    }
+}
+exports.DuploTrainBase = DuploTrainBase;
+exports.PortMap = {
+    "MOTOR": 0,
+    "COLOR": 18,
+    "SPEEDOMETER": 19
+};
+//# sourceMappingURL=duplotrainbase.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_hub.js.html b/docs/hubs_hub.js.html new file mode 100644 index 00000000..bf00f19f --- /dev/null +++ b/docs/hubs_hub.js.html @@ -0,0 +1,275 @@ + + + + + + + node-poweredup Source: hubs/hub.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/hub.js

+ +
+
+
"use strict";
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const compare_versions_1 = __importDefault(require("compare-versions"));
+const lpf2hub_1 = require("./lpf2hub");
+const Consts = __importStar(require("../consts"));
+const Debug = require("debug");
+const debug = Debug("hub");
+/**
+ * The Hub is emitted if the discovered device is a Hub.
+ * @class Hub
+ * @extends LPF2Hub
+ * @extends BaseHub
+ */
+class Hub extends lpf2hub_1.LPF2Hub {
+    constructor(device) {
+        super(device, exports.PortMap, Consts.HubType.HUB);
+        this._currentPort = 0x3b;
+        debug("Discovered Powered UP Hub");
+    }
+    static IsHub(peripheral) {
+        return (peripheral.advertisement &&
+            peripheral.advertisement.serviceUuids &&
+            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
+            peripheral.advertisement.manufacturerData &&
+            peripheral.advertisement.manufacturerData.length > 3 &&
+            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.HUB_ID);
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("Connecting to Powered UP Hub");
+            await super.connect();
+            debug("Connect completed");
+            return resolve();
+        });
+    }
+    _checkFirmware(version) {
+        if (compare_versions_1.default("1.1.00.0004", version) === 1) {
+            throw new Error(`Your Powered Up Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
+        }
+    }
+}
+exports.Hub = Hub;
+exports.PortMap = {
+    "A": 0,
+    "B": 1,
+    "HUB_LED": 50,
+    "CURRENT_SENSOR": 59,
+    "VOLTAGE_SENSOR": 60
+};
+//# sourceMappingURL=hub.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_lpf2hub.js.html b/docs/hubs_lpf2hub.js.html new file mode 100644 index 00000000..13d46e81 --- /dev/null +++ b/docs/hubs_lpf2hub.js.html @@ -0,0 +1,552 @@ + + + + + + + node-poweredup Source: hubs/lpf2hub.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/lpf2hub.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const basehub_1 = require("./basehub");
+const Consts = __importStar(require("../consts"));
+const utils_1 = require("../utils");
+const Debug = require("debug");
+const debug = Debug("lpf2hub");
+const modeInfoDebug = Debug("lpf2hubmodeinfo");
+/**
+ * @class LPF2Hub
+ * @extends BaseHub
+ */
+class LPF2Hub extends basehub_1.BaseHub {
+    constructor() {
+        super(...arguments);
+        this._messageBuffer = Buffer.alloc(0);
+        this._propertyRequestCallbacks = {};
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("LPF2Hub connecting");
+            await super.connect();
+            await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB);
+            this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this));
+            await this.sleep(500);
+            this._requestHubPropertyReports(0x02); // Activate button reports
+            await this._requestHubPropertyValue(0x03); // Request firmware version
+            await this._requestHubPropertyValue(0x04); // Request hardware version
+            this._requestHubPropertyReports(0x05); // Activate RSSI updates
+            this._requestHubPropertyReports(0x06); // Activate battery level reports
+            await this._requestHubPropertyValue(0x0d); // Request primary MAC address
+            this.emit("connect");
+            debug("LPF2Hub connected");
+            resolve();
+        });
+    }
+    /**
+     * Shutdown the Hub.
+     * @method LPF2Hub#shutdown
+     * @returns {Promise} Resolved upon successful disconnect.
+     */
+    shutdown() {
+        return new Promise((resolve, reject) => {
+            this.send(Buffer.from([0x02, 0x01]), Consts.BLECharacteristic.LPF2_ALL, () => {
+                return resolve();
+            });
+        });
+    }
+    /**
+     * Set the name of the Hub.
+     * @method LPF2Hub#setName
+     * @param {string} name New name of the hub (14 characters or less, ASCII only).
+     * @returns {Promise} Resolved upon successful issuance of command.
+     */
+    setName(name) {
+        if (name.length > 14) {
+            throw new Error("Name must be 14 characters or less");
+        }
+        return new Promise((resolve, reject) => {
+            let data = Buffer.from([0x01, 0x01, 0x01]);
+            data = Buffer.concat([data, Buffer.from(name, "ascii")]);
+            // Send this twice, as sometimes the first time doesn't take
+            this.send(data, Consts.BLECharacteristic.LPF2_ALL);
+            this.send(data, Consts.BLECharacteristic.LPF2_ALL);
+            this._name = name;
+            return resolve();
+        });
+    }
+    send(message, uuid, callback) {
+        message = Buffer.concat([Buffer.alloc(2), message]);
+        message[0] = message.length;
+        debug("Sent Message (LPF2_ALL)", message);
+        this._bleDevice.writeToCharacteristic(uuid, message, callback);
+    }
+    subscribe(portId, deviceType, mode) {
+        this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL);
+    }
+    unsubscribe(portId, mode) {
+        this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.LPF2_ALL);
+    }
+    createVirtualPort(firstPortName, secondPortName) {
+        const firstDevice = this.getDeviceAtPort(firstPortName);
+        if (!firstDevice) {
+            throw new Error(`Port ${firstPortName} does not have an attached device`);
+        }
+        const secondDevice = this.getDeviceAtPort(secondPortName);
+        if (!secondDevice) {
+            throw new Error(`Port ${secondPortName} does not have an attached device`);
+        }
+        if (firstDevice.type !== secondDevice.type) {
+            throw new Error(`Both devices must be of the same type to create a virtual port`);
+        }
+        this.send(Buffer.from([0x61, 0x01, firstDevice.portId, secondDevice.portId]), Consts.BLECharacteristic.LPF2_ALL);
+    }
+    _checkFirmware(version) {
+        return;
+    }
+    _parseMessage(data) {
+        if (data) {
+            this._messageBuffer = Buffer.concat([this._messageBuffer, data]);
+        }
+        if (this._messageBuffer.length <= 0) {
+            return;
+        }
+        const len = this._messageBuffer[0];
+        if (len >= this._messageBuffer.length) {
+            const message = this._messageBuffer.slice(0, len);
+            this._messageBuffer = this._messageBuffer.slice(len);
+            debug("Received Message (LPF2_ALL)", message);
+            switch (message[2]) {
+                case 0x01: {
+                    const property = message[3];
+                    const callback = this._propertyRequestCallbacks[property];
+                    if (callback) {
+                        callback(message);
+                    }
+                    else {
+                        this._parseHubPropertyResponse(message);
+                    }
+                    delete this._propertyRequestCallbacks[property];
+                    break;
+                }
+                case 0x04: {
+                    this._parsePortMessage(message);
+                    break;
+                }
+                case 0x43: {
+                    this._parsePortInformationResponse(message);
+                    break;
+                }
+                case 0x44: {
+                    this._parseModeInformationResponse(message);
+                    break;
+                }
+                case 0x45: {
+                    this._parseSensorMessage(message);
+                    break;
+                }
+                case 0x82: {
+                    this._parsePortAction(message);
+                    break;
+                }
+            }
+            if (this._messageBuffer.length > 0) {
+                this._parseMessage();
+            }
+        }
+    }
+    _requestHubPropertyValue(property) {
+        return new Promise((resolve) => {
+            this._propertyRequestCallbacks[property] = (message) => {
+                this._parseHubPropertyResponse(message);
+                return resolve();
+            };
+            this.send(Buffer.from([0x01, property, 0x05]), Consts.BLECharacteristic.LPF2_ALL);
+        });
+    }
+    _requestHubPropertyReports(property) {
+        this.send(Buffer.from([0x01, property, 0x02]), Consts.BLECharacteristic.LPF2_ALL);
+    }
+    _parseHubPropertyResponse(message) {
+        // Button press reports
+        if (message[3] === 0x02) {
+            if (message[5] === 1) {
+                /**
+                 * Emits when a button is pressed.
+                 * @event LPF2Hub#button
+                 * @param {string} button
+                 * @param {ButtonState} state
+                 */
+                this.emit("button", { event: Consts.ButtonState.PRESSED });
+                return;
+            }
+            else if (message[5] === 0) {
+                this.emit("button", { event: Consts.ButtonState.RELEASED });
+                return;
+            }
+            // Firmware version
+        }
+        else if (message[3] === 0x03) {
+            this._firmwareVersion = utils_1.decodeVersion(message.readInt32LE(5));
+            this._checkFirmware(this._firmwareVersion);
+            // Hardware version
+        }
+        else if (message[3] === 0x04) {
+            this._hardwareVersion = utils_1.decodeVersion(message.readInt32LE(5));
+            // RSSI update
+        }
+        else if (message[3] === 0x05) {
+            const rssi = message.readInt8(5);
+            if (rssi !== 0) {
+                this._rssi = rssi;
+                this.emit("rssi", { rssi: this._rssi });
+            }
+            // primary MAC Address
+        }
+        else if (message[3] === 0x0d) {
+            this._primaryMACAddress = utils_1.decodeMACAddress(message.slice(5));
+            // Battery level reports
+        }
+        else if (message[3] === 0x06) {
+            const batteryLevel = message[5];
+            if (batteryLevel !== this._batteryLevel) {
+                this._batteryLevel = batteryLevel;
+                this.emit("batteryLevel", { batteryLevel });
+            }
+        }
+    }
+    _parsePortMessage(message) {
+        const portId = message[3];
+        const event = message[4];
+        const deviceType = event ? message.readUInt16LE(5) : 0;
+        // Handle device attachments
+        if (event === 0x01) {
+            if (modeInfoDebug.enabled) {
+                const deviceTypeName = Consts.DeviceTypeNames[message[5]] || "Unknown";
+                modeInfoDebug(`Port ${utils_1.toHex(portId)}, type ${utils_1.toHex(deviceType, 4)} (${deviceTypeName})`);
+                const hwVersion = utils_1.decodeVersion(message.readInt32LE(7));
+                const swVersion = utils_1.decodeVersion(message.readInt32LE(11));
+                modeInfoDebug(`Port ${utils_1.toHex(portId)}, hardware version ${hwVersion}, software version ${swVersion}`);
+                this._sendPortInformationRequest(portId);
+            }
+            const device = this._createDevice(deviceType, portId);
+            this._attachDevice(device);
+            // Handle device detachments
+        }
+        else if (event === 0x00) {
+            const device = this._getDeviceByPortId(portId);
+            if (device) {
+                this._detachDevice(device);
+                if (this.isPortVirtual(portId)) {
+                    const portName = this.getPortNameForPortId(portId);
+                    if (portName) {
+                        delete this._portMap[portName];
+                    }
+                    this._virtualPorts = this._virtualPorts.filter((virtualPortId) => virtualPortId !== portId);
+                }
+            }
+            // Handle virtual port creation
+        }
+        else if (event === 0x02) {
+            const firstPortName = this.getPortNameForPortId(message[7]);
+            const secondPortName = this.getPortNameForPortId(message[8]);
+            // @ts-ignore NK These should never be undefined
+            const virtualPortName = firstPortName + secondPortName;
+            const virtualPortId = message[3];
+            this._portMap[virtualPortName] = virtualPortId;
+            this._virtualPorts.push(virtualPortId);
+            const device = this._createDevice(deviceType, virtualPortId);
+            this._attachDevice(device);
+        }
+    }
+    _sendPortInformationRequest(port) {
+        this.send(Buffer.from([0x21, port, 0x01]), Consts.BLECharacteristic.LPF2_ALL);
+        this.send(Buffer.from([0x21, port, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Mode combinations
+    }
+    _parsePortInformationResponse(message) {
+        const port = message[3];
+        if (message[4] === 2) {
+            const modeCombinationMasks = [];
+            for (let i = 5; i < message.length; i += 2) {
+                modeCombinationMasks.push(message.readUInt16LE(i));
+            }
+            modeInfoDebug(`Port ${utils_1.toHex(port)}, mode combinations [${modeCombinationMasks.map((c) => utils_1.toBin(c, 0)).join(", ")}]`);
+            return;
+        }
+        const count = message[6];
+        const input = utils_1.toBin(message.readUInt16LE(7), count);
+        const output = utils_1.toBin(message.readUInt16LE(9), count);
+        modeInfoDebug(`Port ${utils_1.toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`);
+        for (let i = 0; i < count; i++) {
+            this._sendModeInformationRequest(port, i, 0x00); // Mode Name
+            this._sendModeInformationRequest(port, i, 0x01); // RAW Range
+            this._sendModeInformationRequest(port, i, 0x02); // PCT Range
+            this._sendModeInformationRequest(port, i, 0x03); // SI Range
+            this._sendModeInformationRequest(port, i, 0x04); // SI Symbol
+            this._sendModeInformationRequest(port, i, 0x80); // Value Format
+        }
+    }
+    _sendModeInformationRequest(port, mode, type) {
+        this.send(Buffer.from([0x22, port, mode, type]), Consts.BLECharacteristic.LPF2_ALL);
+    }
+    _parseModeInformationResponse(message) {
+        const port = utils_1.toHex(message[3]);
+        const mode = message[4];
+        const type = message[5];
+        switch (type) {
+            case 0x00: // Mode Name
+                modeInfoDebug(`Port ${port}, mode ${mode}, name ${message.slice(6, message.length).toString()}`);
+                break;
+            case 0x01: // RAW Range
+                modeInfoDebug(`Port ${port}, mode ${mode}, RAW min ${message.readFloatLE(6)}, max ${message.readFloatLE(10)}`);
+                break;
+            case 0x02: // PCT Range
+                modeInfoDebug(`Port ${port}, mode ${mode}, PCT min ${message.readFloatLE(6)}, max ${message.readFloatLE(10)}`);
+                break;
+            case 0x03: // SI Range
+                modeInfoDebug(`Port ${port}, mode ${mode}, SI min ${message.readFloatLE(6)}, max ${message.readFloatLE(10)}`);
+                break;
+            case 0x04: // SI Symbol
+                modeInfoDebug(`Port ${port}, mode ${mode}, SI symbol ${message.slice(6, message.length).toString()}`);
+                break;
+            case 0x80: // Value Format
+                const numValues = message[6];
+                const dataType = ["8bit", "16bit", "32bit", "float"][message[7]];
+                const totalFigures = message[8];
+                const decimals = message[9];
+                modeInfoDebug(`Port ${port}, mode ${mode}, Value ${numValues} x ${dataType}, Decimal format ${totalFigures}.${decimals}`);
+        }
+    }
+    _parsePortAction(message) {
+        const portId = message[3];
+        const device = this._getDeviceByPortId(portId);
+        if (device) {
+            const finished = (message[4] === 0x0a);
+            if (finished) {
+                device.finish();
+            }
+        }
+    }
+    _parseSensorMessage(message) {
+        const portId = message[3];
+        const device = this._getDeviceByPortId(portId);
+        if (device) {
+            device.receive(message);
+        }
+    }
+}
+exports.LPF2Hub = LPF2Hub;
+//# sourceMappingURL=lpf2hub.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_movehub.js.html b/docs/hubs_movehub.js.html new file mode 100644 index 00000000..8e2433e3 --- /dev/null +++ b/docs/hubs_movehub.js.html @@ -0,0 +1,277 @@ + + + + + + + node-poweredup Source: hubs/movehub.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/movehub.js

+ +
+
+
"use strict";
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const compare_versions_1 = __importDefault(require("compare-versions"));
+const lpf2hub_1 = require("./lpf2hub");
+const Consts = __importStar(require("../consts"));
+const Debug = require("debug");
+const debug = Debug("movehub");
+/**
+ * The MoveHub is emitted if the discovered device is a Move Hub.
+ * @class MoveHub
+ * @extends LPF2Hub
+ * @extends BaseHub
+ */
+class MoveHub extends lpf2hub_1.LPF2Hub {
+    static IsMoveHub(peripheral) {
+        return (peripheral.advertisement &&
+            peripheral.advertisement.serviceUuids &&
+            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
+            peripheral.advertisement.manufacturerData &&
+            peripheral.advertisement.manufacturerData.length > 3 &&
+            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.MOVE_HUB_ID);
+    }
+    constructor(device) {
+        super(device, exports.PortMap, Consts.HubType.MOVE_HUB);
+        debug("Discovered Move Hub");
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("Connecting to Move Hub");
+            await super.connect();
+            debug("Connect completed");
+            return resolve();
+        });
+    }
+    _checkFirmware(version) {
+        if (compare_versions_1.default("2.0.00.0017", version) === 1) {
+            throw new Error(`Your Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
+        }
+    }
+}
+exports.MoveHub = MoveHub;
+exports.PortMap = {
+    "A": 0,
+    "B": 1,
+    "C": 2,
+    "D": 3,
+    "HUB_LED": 50,
+    "TILT_SENSOR": 58,
+    "CURRENT_SENSOR": 59,
+    "VOLTAGE_SENSOR": 60
+};
+//# sourceMappingURL=movehub.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_remotecontrol.js.html b/docs/hubs_remotecontrol.js.html new file mode 100644 index 00000000..e46113af --- /dev/null +++ b/docs/hubs_remotecontrol.js.html @@ -0,0 +1,265 @@ + + + + + + + node-poweredup Source: hubs/remotecontrol.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/remotecontrol.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const lpf2hub_1 = require("./lpf2hub");
+const Consts = __importStar(require("../consts"));
+const Debug = require("debug");
+const debug = Debug("remotecontrol");
+/**
+ * The RemoteControl is emitted if the discovered device is a Remote Control.
+ * @class RemoteControl
+ * @extends LPF2Hub
+ * @extends BaseHub
+ */
+class RemoteControl extends lpf2hub_1.LPF2Hub {
+    static IsRemoteControl(peripheral) {
+        return (peripheral.advertisement &&
+            peripheral.advertisement.serviceUuids &&
+            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
+            peripheral.advertisement.manufacturerData &&
+            peripheral.advertisement.manufacturerData.length > 3 &&
+            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.REMOTE_CONTROL_ID);
+    }
+    constructor(device) {
+        super(device, exports.PortMap, Consts.HubType.REMOTE_CONTROL);
+        debug("Discovered Powered UP Remote");
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("Connecting to Powered UP Remote");
+            await super.connect();
+            debug("Connect completed");
+            return resolve();
+        });
+    }
+}
+exports.RemoteControl = RemoteControl;
+exports.PortMap = {
+    "LEFT": 0,
+    "RIGHT": 1,
+    "HUB_LED": 52,
+    "VOLTAGE_SENSOR": 59,
+    "REMOTE_CONTROL_RSSI": 60
+};
+//# sourceMappingURL=remotecontrol.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_technicmediumhub.js.html b/docs/hubs_technicmediumhub.js.html new file mode 100644 index 00000000..b5791f78 --- /dev/null +++ b/docs/hubs_technicmediumhub.js.html @@ -0,0 +1,271 @@ + + + + + + + node-poweredup Source: hubs/technicmediumhub.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/technicmediumhub.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const lpf2hub_1 = require("./lpf2hub");
+const Consts = __importStar(require("../consts"));
+const Debug = require("debug");
+const debug = Debug("technicmediumhub");
+/**
+ * The TechnicMediumHub is emitted if the discovered device is a Technic Medium Hub.
+ * @class TechnicMediumHub
+ * @extends LPF2Hub
+ * @extends BaseHub
+ */
+class TechnicMediumHub extends lpf2hub_1.LPF2Hub {
+    static IsTechnicMediumHub(peripheral) {
+        return (peripheral.advertisement &&
+            peripheral.advertisement.serviceUuids &&
+            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
+            peripheral.advertisement.manufacturerData &&
+            peripheral.advertisement.manufacturerData.length > 3 &&
+            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.TECHNIC_MEDIUM_HUB);
+    }
+    constructor(device) {
+        super(device, exports.PortMap, Consts.HubType.TECHNIC_MEDIUM_HUB);
+        debug("Discovered Control+ Hub");
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("Connecting to Control+ Hub");
+            await super.connect();
+            this.send(Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Temperature
+            debug("Connect completed");
+            return resolve();
+        });
+    }
+}
+exports.TechnicMediumHub = TechnicMediumHub;
+exports.PortMap = {
+    "A": 0,
+    "B": 1,
+    "C": 2,
+    "D": 3,
+    "HUB_LED": 50,
+    "CURRENT_SENSOR": 59,
+    "VOLTAGE_SENSOR": 60,
+    "ACCELEROMETER": 97,
+    "GYRO_SENSOR": 98,
+    "TILT_SENSOR": 99
+};
+//# sourceMappingURL=technicmediumhub.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_wedo2smarthub.js.html b/docs/hubs_wedo2smarthub.js.html new file mode 100644 index 00000000..724ce5ec --- /dev/null +++ b/docs/hubs_wedo2smarthub.js.html @@ -0,0 +1,417 @@ + + + + + + + node-poweredup Source: hubs/wedo2smarthub.js + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Source: hubs/wedo2smarthub.js

+ +
+
+
"use strict";
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
+    result["default"] = mod;
+    return result;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const basehub_1 = require("./basehub");
+const Consts = __importStar(require("../consts"));
+const utils_1 = require("../utils");
+const Debug = require("debug");
+const debug = Debug("wedo2smarthub");
+/**
+ * The WeDo2SmartHub is emitted if the discovered device is a WeDo 2.0 Smart Hub.
+ * @class WeDo2SmartHub
+ * @extends BaseHub
+ */
+class WeDo2SmartHub extends basehub_1.BaseHub {
+    constructor(device) {
+        super(device, exports.PortMap, Consts.HubType.WEDO2_SMART_HUB);
+        this._lastTiltX = 0;
+        this._lastTiltY = 0;
+        debug("Discovered WeDo 2.0 Smart Hub");
+    }
+    static IsWeDo2SmartHub(peripheral) {
+        return (peripheral.advertisement &&
+            peripheral.advertisement.serviceUuids &&
+            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0);
+    }
+    connect() {
+        return new Promise(async (resolve, reject) => {
+            debug("Connecting to WeDo 2.0 Smart Hub");
+            await super.connect();
+            await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB);
+            await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_2);
+            if (!utils_1.isWebBluetooth) {
+                await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_3);
+                await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_4);
+                await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.WEDO2_SMART_HUB_5);
+            }
+            else {
+                await this._bleDevice.discoverCharacteristicsForService("battery_service");
+                await this._bleDevice.discoverCharacteristicsForService("device_information");
+            }
+            debug("Connect completed");
+            this.emit("connect");
+            resolve();
+            this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_PORT_TYPE, this._parsePortMessage.bind(this));
+            this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_SENSOR_VALUE, this._parseSensorMessage.bind(this));
+            this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BUTTON, this._parseSensorMessage.bind(this));
+            if (!utils_1.isWebBluetooth) {
+                this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, this._parseBatteryMessage.bind(this));
+                this._bleDevice.readFromCharacteristic(Consts.BLECharacteristic.WEDO2_BATTERY, (err, data) => {
+                    if (data) {
+                        this._parseBatteryMessage(data);
+                    }
+                });
+            }
+            else {
+                this._bleDevice.readFromCharacteristic("00002a19-0000-1000-8000-00805f9b34fb", (err, data) => {
+                    if (data) {
+                        this._parseBatteryMessage(data);
+                    }
+                });
+                this._bleDevice.subscribeToCharacteristic("00002a19-0000-1000-8000-00805f9b34fb", this._parseHighCurrentAlert.bind(this));
+            }
+            this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.WEDO2_HIGH_CURRENT_ALERT, this._parseHighCurrentAlert.bind(this));
+            if (!utils_1.isWebBluetooth) {
+                this._bleDevice.readFromCharacteristic(Consts.BLECharacteristic.WEDO2_FIRMWARE_REVISION, (err, data) => {
+                    if (data) {
+                        this._parseFirmwareRevisionString(data);
+                    }
+                });
+            }
+            else {
+                this._bleDevice.readFromCharacteristic("00002a26-0000-1000-8000-00805f9b34fb", (err, data) => {
+                    if (data) {
+                        this._parseFirmwareRevisionString(data);
+                    }
+                });
+            }
+        });
+    }
+    /**
+     * Shutdown the Hub.
+     * @method WeDo2SmartHub#shutdown
+     * @returns {Promise} Resolved upon successful disconnect.
+     */
+    shutdown() {
+        return new Promise((resolve, reject) => {
+            this.send(Buffer.from([0x00]), Consts.BLECharacteristic.WEDO2_DISCONNECT, () => {
+                return resolve();
+            });
+        });
+    }
+    /**
+     * Set the name of the Hub.
+     * @method WeDo2SmartHub#setName
+     * @param {string} name New name of the hub (14 characters or less, ASCII only).
+     * @returns {Promise} Resolved upon successful issuance of command.
+     */
+    setName(name) {
+        if (name.length > 14) {
+            throw new Error("Name must be 14 characters or less");
+        }
+        return new Promise((resolve, reject) => {
+            const data = Buffer.from(name, "ascii");
+            // Send this twice, as sometimes the first time doesn't take
+            this.send(data, Consts.BLECharacteristic.WEDO2_NAME_ID);
+            this.send(data, Consts.BLECharacteristic.WEDO2_NAME_ID);
+            this._name = name;
+            return resolve();
+        });
+    }
+    send(message, uuid, callback) {
+        if (debug.enabled) {
+            debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
+        }
+        this._bleDevice.writeToCharacteristic(uuid, message, callback);
+    }
+    subscribe(portId, deviceType, mode) {
+        this.send(Buffer.from([0x01, 0x02, portId, deviceType, mode, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE);
+    }
+    unsubscribe(portId, deviceType, mode) {
+        this.send(Buffer.from([0x01, 0x02, portId, deviceType, mode, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.WEDO2_PORT_TYPE_WRITE);
+    }
+    _getCharacteristicNameFromUUID(uuid) {
+        const keys = Object.keys(Consts.BLECharacteristic);
+        for (let i = 0; i < keys.length; i++) {
+            const key = keys[i];
+            if (Consts.BLECharacteristic[key] === uuid) {
+                return key;
+            }
+        }
+        return "UNKNOWN";
+    }
+    _parseHighCurrentAlert(data) {
+        debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data);
+    }
+    _parseBatteryMessage(data) {
+        debug("Received Message (WEDO2_BATTERY)", data);
+        const batteryLevel = data[0];
+        if (batteryLevel !== this._batteryLevel) {
+            this._batteryLevel = batteryLevel;
+            this.emit("batteryLevel", { batteryLevel });
+        }
+    }
+    _parseFirmwareRevisionString(data) {
+        debug("Received Message (WEDO2_FIRMWARE_REVISION)", data);
+        this._firmwareVersion = data.toString();
+    }
+    _parsePortMessage(data) {
+        debug("Received Message (WEDO2_PORT_TYPE)", data);
+        const portId = data[0];
+        const event = data[1];
+        const deviceType = event ? data[3] : 0;
+        if (event === 0x01) {
+            const device = this._createDevice(deviceType, portId);
+            this._attachDevice(device);
+        }
+        else if (event === 0x00) {
+            const device = this._getDeviceByPortId(portId);
+            if (device) {
+                this._detachDevice(device);
+            }
+        }
+    }
+    _parseSensorMessage(message) {
+        debug("Received Message (WEDO2_SENSOR_VALUE)", message);
+        if (message[0] === 0x01) {
+            /**
+             * Emits when a button is pressed.
+             * @event WeDo2SmartHub#button
+             * @param {string} button
+             * @param {ButtonState} state
+             */
+            this.emit("button", { event: Consts.ButtonState.PRESSED });
+            return;
+        }
+        else if (message[0] === 0x00) {
+            this.emit("button", { event: Consts.ButtonState.RELEASED });
+            return;
+        }
+        const portId = message[1];
+        const device = this._getDeviceByPortId(portId);
+        if (device) {
+            device.receive(message);
+        }
+    }
+}
+exports.WeDo2SmartHub = WeDo2SmartHub;
+exports.PortMap = {
+    "A": 1,
+    "B": 2,
+    "CURRENT_SENSOR": 3,
+    "VOLTAGE_SENSOR": 4,
+    "PIEZO_BUZZER": 5,
+    "HUB_LED": 6
+};
+//# sourceMappingURL=wedo2smarthub.js.map
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Fri Feb 7th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/index.html b/docs/index.html index 8df7b001..e99f5cd8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,21 +33,14 @@ - - @@ -290,17 +283,22 @@

Node.js Sample Usage

poweredUP.on("discover", async (hub) => { // Wait to discover a Hub console.log(`Discovered ${hub.name}!`); await hub.connect(); // Connect to the Hub + const motorA = hub.waitForDeviceAtPort("A"); // Make sure a motor is plugged into port A + const motorB = hub.waitForDeviceAtPort("B"); // Make sure a motor is plugged into port B console.log("Connected"); - await hub.sleep(3000); // Sleep for 3 seconds before starting while (true) { // Repeat indefinitely console.log("Running motor B at speed 75"); - hub.setMotorSpeed("B", 75); // Start a motor attached to port B to run a 3/4 speed (75) indefinitely + motorB.setPower("B", 75); // Start a motor attached to port B to run a 3/4 speed (75) indefinitely console.log("Running motor A at speed 100 for 2 seconds"); - await hub.setMotorSpeed("A", 100, 2000); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop + motorA.setPower("A", 100); // Run a motor attached to port A for 2 seconds at maximum speed (100) then stop + await hub.sleep(2000); + motorA.setPower("A", 0); await hub.sleep(1000); // Do nothing for 1 second - console.log("Running motor A at speed -50 for 1 seconds"); - await hub.setMotorSpeed("A", -50, 1000); // Run a motor attached to port A for 1 second at 1/2 speed in reverse (-50) then stop + console.log("Running motor A at speed -50 for 1 second"); + motorA.setPower("A", -50); // Run a motor attached to port A for 1 second at 1/2 speed in reverse (-50) then stop + hub.sleep(1000); + motorA.setPower("A", 0); await hub.sleep(1000); // Do nothing for 1 second } }); @@ -360,7 +358,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/poweredup-node.js.html b/docs/poweredup-node.js.html index 0bfb7ae4..46a1b7dd 100644 --- a/docs/poweredup-node.js.html +++ b/docs/poweredup-node.js.html @@ -33,14 +33,14 @@ @@ -86,13 +86,13 @@

Source: poweredup-node.js

"use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-const boostmovehub_1 = require("./boostmovehub");
-const controlplushub_1 = require("./controlplushub");
-const duplotrainbase_1 = require("./duplotrainbase");
-const nobledevice_1 = require("./nobledevice");
-const puphub_1 = require("./puphub");
-const pupremote_1 = require("./pupremote");
-const wedo2smarthub_1 = require("./wedo2smarthub");
+const nobleabstraction_1 = require("./nobleabstraction");
+const duplotrainbase_1 = require("./hubs/duplotrainbase");
+const hub_1 = require("./hubs/hub");
+const movehub_1 = require("./hubs/movehub");
+const remotecontrol_1 = require("./hubs/remotecontrol");
+const technicmediumhub_1 = require("./hubs/technicmediumhub");
+const wedo2smarthub_1 = require("./hubs/wedo2smarthub");
 const events_1 = require("events");
 const Debug = require("debug");
 const debug = Debug("poweredup");
@@ -122,7 +122,6 @@ 

Source: poweredup-node.js

class PoweredUP extends events_1.EventEmitter { constructor() { super(); - this.autoSubscribe = true; this._connectedHubs = {}; this._discoveryEventHandler = this._discoveryEventHandler.bind(this); } @@ -156,60 +155,69 @@

Source: poweredup-node.js

} /** * Retrieve a list of Powered UP Hubs. - * @method PoweredUP#getConnectedHubs - * @returns {Hub[]} + * @method PoweredUP#getHubs + * @returns {BaseHub[]} */ - getConnectedHubs() { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]); + getHubs() { + return Object.values(this._connectedHubs); } /** * Retrieve a Powered UP Hub by UUID. - * @method PoweredUP#getConnectedHubByUUID + * @method PoweredUP#getHubByUUID * @param {string} uuid - * @returns {Hub | null} + * @returns {BaseHub | null} */ - getConnectedHubByUUID(uuid) { + getHubByUUID(uuid) { return this._connectedHubs[uuid]; } /** * Retrieve a Powered UP Hub by primary MAC address. - * @method PoweredUP#getConnectedHubByPrimaryMACAddress + * @method PoweredUP#getHubByPrimaryMACAddress * @param {string} address - * @returns {Hub} + * @returns {BaseHub} */ - getConnectedHubByPrimaryMACAddress(address) { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.primaryMACAddress === address)[0]; + getHubByPrimaryMACAddress(address) { + return Object.values(this._connectedHubs).filter((hub) => hub.primaryMACAddress === address)[0]; } /** * Retrieve a list of Powered UP Hub by name. - * @method PoweredUP#getConnectedHubsByName + * @method PoweredUP#getHubsByName * @param {string} name - * @returns {Hub[]} + * @returns {BaseHub[]} */ - getConnectedHubsByName(name) { - return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]).filter((hub) => hub.name === name); + getHubsByName(name) { + return Object.values(this._connectedHubs).filter((hub) => hub.name === name); + } + /** + * Retrieve a list of Powered UP Hub by type. + * @method PoweredUP#getHubsByType + * @param {string} name + * @returns {BaseHub[]} + */ + getHubsByType(hubType) { + return Object.values(this._connectedHubs).filter((hub) => hub.type === hubType); } async _discoveryEventHandler(peripheral) { peripheral.removeAllListeners(); - const device = new nobledevice_1.NobleDevice(peripheral); + const device = new nobleabstraction_1.NobleDevice(peripheral); let hub; if (await wedo2smarthub_1.WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { - hub = new wedo2smarthub_1.WeDo2SmartHub(device, this.autoSubscribe); + hub = new wedo2smarthub_1.WeDo2SmartHub(device); } - else if (await boostmovehub_1.BoostMoveHub.IsBoostMoveHub(peripheral)) { - hub = new boostmovehub_1.BoostMoveHub(device, this.autoSubscribe); + else if (await movehub_1.MoveHub.IsMoveHub(peripheral)) { + hub = new movehub_1.MoveHub(device); } - else if (await puphub_1.PUPHub.IsPUPHub(peripheral)) { - hub = new puphub_1.PUPHub(device, this.autoSubscribe); + else if (await hub_1.Hub.IsHub(peripheral)) { + hub = new hub_1.Hub(device); } - else if (await pupremote_1.PUPRemote.IsPUPRemote(peripheral)) { - hub = new pupremote_1.PUPRemote(device, this.autoSubscribe); + else if (await remotecontrol_1.RemoteControl.IsRemoteControl(peripheral)) { + hub = new remotecontrol_1.RemoteControl(device); } else if (await duplotrainbase_1.DuploTrainBase.IsDuploTrainBase(peripheral)) { - hub = new duplotrainbase_1.DuploTrainBase(device, this.autoSubscribe); + hub = new duplotrainbase_1.DuploTrainBase(device); } - else if (await controlplushub_1.ControlPlusHub.IsControlPlusHub(peripheral)) { - hub = new controlplushub_1.ControlPlusHub(device, this.autoSubscribe); + else if (await technicmediumhub_1.TechnicMediumHub.IsTechnicMediumHub(peripheral)) { + hub = new technicmediumhub_1.TechnicMediumHub(device); } else { return; @@ -230,7 +238,7 @@

Source: poweredup-node.js

/** * Emits when a Powered UP Hub device is found. * @event PoweredUP#discover - * @param {WeDo2SmartHub | BoostMoveHub | ControlPlusHub | PUPHub | PUPRemote | DuploTrainBase} hub + * @param {WeDo2SmartHub | MoveHub | TechnicMediumHub | Hub | RemoteControl | DuploTrainBase} hub */ this.emit("discover", hub); }); @@ -282,7 +290,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Fri Feb 7th 2020 using the DocStrap template. diff --git a/docs/quicksearch.html b/docs/quicksearch.html index 1d145412..942391bb 100644 --- a/docs/quicksearch.html +++ b/docs/quicksearch.html @@ -7,7 +7,7 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/BaseHub.html b/docs/BaseHub.html index b582d8f9..6edd204a 100644 --- a/docs/BaseHub.html +++ b/docs/BaseHub.html @@ -33,14 +33,14 @@ @@ -479,7 +479,7 @@
Properties:
- firmwareVersion + hardwareVersion @@ -1325,7 +1325,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/BasicMotor.html b/docs/BasicMotor.html new file mode 100644 index 00000000..9757e339 --- /dev/null +++ b/docs/BasicMotor.html @@ -0,0 +1,1716 @@ + + + + + + + node-poweredup Class: BasicMotor + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: BasicMotor

+
+ +
+ +

+ BasicMotor +

+ + +
+ + +
+
+ + +
+
+

new BasicMotor()

+ + +
+
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> connected

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
connected + + +boolean + + + + +

Check if the device is still attached.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hub

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
hub + + +Hub + + + + +

The Hub the device is attached to.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> isVirtualPort

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
isVirtualPort + + +boolean + + + + +

Is this device attached to a virtual port (ie. a combined device)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> mode

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
mode + + +number + + + + +

The mode the device is currently in

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> portName

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
portName + + +string + + + + +

The port the device is attached to.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +number + + + + +

The type of the device

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + +

Methods

+ +
+ +
+
+

brake()

+ + +
+
+ + +
+

Brake the motor.

+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of the command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

rampPower(fromPower, toPower, time)

+ + +
+
+ + +
+

Ramp the motor power.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
fromPower + + +number + + + + +

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

toPower + + +number + + + + +

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

time + + +number + + + + +

How long the ramp should last (in milliseconds).

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful completion of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

setPower(power)

+ + +
+
+ + +
+

Set the motor power.

+
+ + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
power + + +number + + + + +

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of the command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ + + +
+
+

stop()

+ + +
+
+ + +
+

Stop the motor.

+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful issuance of the command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/BoostMoveHub.html b/docs/BoostMoveHub.html index 739b94c3..bc48e580 100644 --- a/docs/BoostMoveHub.html +++ b/docs/BoostMoveHub.html @@ -33,21 +33,14 @@ - - @@ -152,8 +145,8 @@

new Boost
@@ -209,77 +202,36 @@

Extends

-

Members

+ + +

Methods


-
-

<readonly> batteryLevel

- +
+

connect()

+
- - - -
+
+

Connect to the Hub.

+
-
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - -
NameTypeDescription
batteryLevel - - -number - - - - -

Battery level of the hub (Percentage between 0-100)

-
+ +
+ @@ -290,7 +242,7 @@
Properties:
@@ -299,7 +251,7 @@
Properties:
Overrides:
@@ -326,8 +278,8 @@
Properties:
@@ -342,30 +294,71 @@
Properties:
+ + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful connect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + +

-
-

<readonly> current

- +
+

createVirtualPort(firstPortName, secondPortName)

+
- - +
+

Combines two ports with into a single virtual port.

+

Note: The devices attached to the ports must be of the same device type.

+
-
+ -
Properties:
+ -
+ + +
Parameters:
+ - +
@@ -387,13 +380,37 @@
Properties:
- + + + + + + + + + + + + + + + + + +
currentfirstPortName -number +string + + + + +

First port name

secondPortName + + +string @@ -404,16 +421,19 @@
Properties:
-

Current usage of the hub (Milliamps)

Second port name

-
+ +
+ + @@ -423,17 +443,12 @@
Properties:
- -
Overrides:
-
@@ -459,8 +474,8 @@
Properties:
@@ -475,77 +490,70 @@
Properties:
-
- - -
-
-

<readonly> firmwareVersion

+ -
-
+ -
+
Returns:
+ +
+

Resolved upon successful issuance of command.

+
-
Properties:
- -
- - - - - - - - - - +
+
+ Type +
+
+Promise -
- - - - - - - - + + - +
+
+

disconnect()

+ + +
+
- + +
+

Disconnect the Hub.

+
+ - + -
- + - -
NameTypeDescription
firmwareVersion - - -string + + + + -

Firmware version of the hub

-
+ +
+ @@ -556,17 +564,12 @@
Properties:
- -
Overrides:
-
@@ -592,8 +595,8 @@
Properties:
@@ -608,4740 +611,40 @@
Properties:
-
- - -
-
-

<readonly> hardwareVersion

+ -
-
+ -
+
Returns:
+ +
+

Resolved upon successful disconnect.

+
-
Properties:
-
- - - - - +
+
+ Type +
+
+Promise -
- - + + - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
firmwareVersion - - -string - - - - -

Hardware version of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> name

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
name - - -string - - - - -

Name of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> primaryMACAddress

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
primaryMACAddress - - -string - - - - -

Primary MAC address of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> rssi

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
rssi - - -number - - - - -

Signal strength of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> uuid

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
uuid - - -string - - - - -

UUID of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> voltage

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
voltage - - -number - - - - -

Voltage of the hub (Volts)

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- -
- - - -

Methods

- -
- -
-
-

brakeMotor(port)

- - -
-
- - -
-

Fully (hard) stop the motor on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

connect()

- - -
-
- - -
-

Connect to the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful connect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

disconnect()

- - -
-
- - -
-

Disconnect the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

getHubType()

- - -
-
- - -
-

Get the hub type.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -HubType - - - -
-
- - - - - -
- - - -
-
-

getPortDeviceType(port)

- - -
-
- - -
-

Get the device type for a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -DeviceType - - - -
-
- - - - - -
- - - -
-
-

rampMotorSpeed(port, fromSpeed, toSpeed, time)

- - -
-
- - -
-

Ramp the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
fromSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

toSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

time - - -number - - - - -

How long the ramp should last (in milliseconds).

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

resetAbsolutePosition(port)

- - -
-
- - -
-

Reset the current motor position as absolute position zero

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command (ie. once the motor is finished).

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setAbsolutePosition(port, pos [, speed])

- - -
-
- - -
-

Tell motor to goto an absolute position

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDefaultDescription
port - - -string - - - - - - - - - - - - -
pos - - -number - - - - - - - - - - - - -

The position of the motor to go to

speed - - -number -| - -Array.<number> - - - - - - - <optional>
- - - - - -
- - 100 - -

A value between 1 - 100 should be set (Direction does not apply when going to absolute position)

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command (ie. once the motor is finished).

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDColor(color)

- - -
-
- - -
-

Set the color of the LED on the Hub via a color value.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
color - - -Color - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDRGB(red, green, blue)

- - -
-
- - -
-

Set the color of the LED on the Hub via RGB values.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
red - - -number - - - - -
green - - -number - - - - -
blue - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLightBrightness(port, brightness [, time])

- - -
-
- - -
-

Set the light brightness on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
brightness - - -number - - - - - - - - - - -

Brightness value between 0-100 (0 is off)

time - - -number - - - - - - - <optional>
- - - - - -

How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command. If time is specified, this is once the light is turned off.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setMotorAngle(port, angle [, speed])

- - -
-
- - -
-

Rotate a motor by a given angle.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDefaultDescription
port - - -string - - - - - - - - - - - - -
angle - - -number - - - - - - - - - - - - -

How much the motor should be rotated (in degrees).

speed - - -number -| - -Array.<number> - - - - - - - <optional>
- - - - - -
- - 100 - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command (ie. once the motor is finished).

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setMotorSpeed(port, speed [, time])

- - -
-
- - -
-

Set the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
speed - - -number -| - -Array.<number> - - - - - - - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

time - - -number - - - - - - - <optional>
- - - - - -

How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command. If time is specified, this is once the motor is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setName(name)

- - -
-
- - -
-

Set the name of the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
name - - -string - - - - -

New name of the hub (14 characters or less, ASCII only).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

shutdown()

- - -
-
- - -
-

Shutdown the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

sleep(delay)

- - -
-
- - -
-

Sleep a given amount of time.

-

This is a helper method to make it easier to add delays into a chain of commands.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
delay - - -number - - - - -

How long to sleep (in milliseconds).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the delay is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

subscribe(port [, mode])

- - -
-
- - -
-

Subscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
mode - - -number - - - - - - - <optional>
- - - - - -

The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

unsubscribe(port)

- - -
-
- - -
-

Unsubscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

wait(commands)

- - -
-
- - -
-

Wait until a given list of concurrently running commands are complete.

-

This is a helper method to make it easier to wait for concurrent commands to complete.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
commands - - -Array.<Promise.<any>> - - - - -

Array of executing commands.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the commands are finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- -
- - - - - -

Events

- -
- -
-
-

accel

- - -
-
- - -
-

Emits when accelerometer detects movement. Measured in mG.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - +
@@ -5350,7 +653,7 @@
Parameters:

-

button

+

getDeviceAtPort(portName)

@@ -5358,7 +661,7 @@

button

-

Emits when a button is pressed.

+

Retrieves the device attached to a given port.

@@ -5393,7 +696,7 @@
Parameters:
- button + portName @@ -5410,31 +713,7 @@
Parameters:
- - - - - - - - state - - - - - -ButtonState - - - - - - - - - - - +

The name of the port to retrieve the device from.

@@ -5456,7 +735,7 @@
Parameters:
@@ -5487,8 +766,8 @@
Parameters:
@@ -5513,6 +792,33 @@
Parameters:
+
Returns:
+ + +
+

The device attached to the port.

+
+ + + +
+
+ Type +
+
+ +Device +| + +undefined + + + +
+
+ + + @@ -5521,7 +827,7 @@
Parameters:

-

color

+

getDevices()

@@ -5529,7 +835,7 @@

color

-

Emits when a color sensor is activated.

+

Retrieves all attached devices.

@@ -5539,80 +845,6 @@

color

-
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- -
@@ -5627,7 +859,7 @@
Parameters:
@@ -5658,8 +890,8 @@
Parameters:
@@ -5684,6 +916,30 @@
Parameters:
+
Returns:
+ + +
+

Array of all attached devices.

+
+ + + +
+
+ Type +
+
+ +Array.<Device> + + + +
+
+ + + @@ -5692,7 +948,7 @@
Parameters:

-

colorAndDistance

+

getDevicesByType(deviceType)

@@ -5700,7 +956,7 @@

colorAndDistance

-

A combined color and distance event, emits when the sensor is activated.

+

Retrieves an array of devices of the specified type.

@@ -5735,55 +991,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - color - - - - - -Color - - - - - - - - - - - - - - - - - - distance + deviceType @@ -5800,7 +1008,7 @@
Parameters:
-

Distance, in millimeters.

+

The device type to lookup.

@@ -5822,7 +1030,7 @@
Parameters:
@@ -5853,8 +1061,8 @@
Parameters:
@@ -5870,15 +1078,39 @@
Parameters:
- + + + + + + + + + +
Returns:
+ + +
+

Array of all devices of the specified type.

+
+ + + +
+
+ Type +
+
+ +Array.<Device> + - - +
+
- - + @@ -5887,7 +1119,7 @@
Parameters:

-

detach

+

setName(name)

@@ -5895,7 +1127,7 @@

detach

-

Emits when an attached motor or sensor is detached from the Hub.

+

Set the name of the Hub.

@@ -5930,7 +1162,7 @@
Parameters:
- port + name @@ -5947,7 +1179,7 @@
Parameters:
- +

New name of the hub (14 characters or less, ASCII only).

@@ -5969,17 +1201,12 @@
Parameters:
- -
Overrides:
-
@@ -6005,8 +1232,8 @@
Parameters:
@@ -6031,6 +1258,30 @@
Parameters:
+
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6039,7 +1290,7 @@
Parameters:

-

disconnect

+

shutdown()

@@ -6047,7 +1298,7 @@

disconnect

-

Emits when the hub is disconnected.

+

Shutdown the Hub.

@@ -6071,17 +1322,12 @@

disconnect

- -
Overrides:
-
@@ -6107,8 +1353,8 @@

disconnect

@@ -6133,6 +1379,30 @@

disconnect

+
Returns:
+ + +
+

Resolved upon successful disconnect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6141,7 +1411,7 @@

disconnect


-

distance

+

sleep(delay)

@@ -6149,7 +1419,8 @@

distance

-

Emits when a distance sensor is activated.

+

Sleep a given amount of time.

+

Note: This is a helper method to make it easier to add delays into a chain of commands.

@@ -6184,31 +1455,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - distance + delay @@ -6225,7 +1472,7 @@
Parameters:
-

Distance, in millimeters.

+

How long to sleep (in milliseconds).

@@ -6247,7 +1494,7 @@
Parameters:
@@ -6278,8 +1525,8 @@
Parameters:
@@ -6304,6 +1551,30 @@
Parameters:
+
Returns:
+ + +
+

Resolved after the delay is finished.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6312,7 +1583,7 @@
Parameters:

-

gyro

+

wait(commands)

@@ -6320,7 +1591,8 @@

gyro

-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

+

Wait until a given list of concurrently running commands are complete.

+

Note: This is a helper method to make it easier to wait for concurrent commands to complete.

@@ -6355,13 +1627,13 @@
Parameters:
- port + commands -string +Array.<Promise.<any>> @@ -6372,68 +1644,168 @@
Parameters:
- +

Array of executing commands.

+ + - - - x - + - - - -number + +
+ + + - - + +
Inherited From:
+
+ +
+ - + - + - - + - + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ - y +
+

Resolved after the commands are finished.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + + + + +
+
+

waitForDeviceAtPort(portName)

+ + +
+
+ + +
+

Retrieves the device attached to a given port, waiting until one is attached if there isn't one.

+

Note: If a device is never attached, the returned promise may never resolve.

+
+ - - - -number + + + + +
Parameters:
+ - - + + + + + + - + - + - - + + + + + + - + + @@ -6466,7 +1838,7 @@
Parameters:
@@ -6497,8 +1869,8 @@
Parameters:
@@ -6523,6 +1895,30 @@
Parameters:
+
Returns:
+ + +
+

Resolved once a device is attached, or resolved immediately if a device is already attached.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6531,7 +1927,7 @@
Parameters:

-

rotate

+

waitForDeviceByType(deviceType)

@@ -6539,7 +1935,8 @@

rotate

-

Emits when a rotation sensor is activated.

+

Retrieves the first device attached of the specified type, waiting until one is attached if there isn't one.

+

Note: If a device is never attached, the returned promise may never resolve.

@@ -6574,31 +1971,7 @@
Parameters:
- - - - - - - - - - - - - - - - - + + @@ -6637,7 +2010,7 @@
Parameters:
@@ -6668,8 +2041,8 @@
Parameters:
@@ -6694,15 +2067,47 @@
Parameters:
+
Returns:
+ + +
+

Resolved once a device is attached, or resolved immediately if a device is already attached.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + - + + + + + + +

Events

+ +

-

speed

+

attach

@@ -6710,7 +2115,7 @@

speed

-

Emits on a speed change.

+

Emits when a device is attached to the Hub.

@@ -6745,37 +2150,13 @@
Parameters:
- - - - - - - - - - - - - - - - - + - + + - + - + + + +
NameType
Description
zportName -number +string @@ -6444,7 +1816,7 @@
Parameters:
-

The name of the port to retrieve the device from.

port - - -string - - - - -
rotationdeviceType @@ -6615,7 +1988,7 @@
Parameters:
-

The device type to lookup.

port - - -string - - - - -
speeddevice -number +Device @@ -6808,7 +2189,7 @@
Parameters:
@@ -6839,8 +2220,8 @@
Parameters:
@@ -6873,7 +2254,7 @@
Parameters:

-

temp

+

button

@@ -6881,7 +2262,7 @@

temp

-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

+

Emits when a button is pressed.

@@ -6916,7 +2297,7 @@
Parameters:
portbutton @@ -6933,20 +2314,20 @@
Parameters:
-

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

tempstate -number +ButtonState @@ -6979,7 +2360,7 @@
Parameters:
@@ -7010,8 +2391,8 @@
Parameters:
@@ -7044,7 +2425,7 @@
Parameters:

-

tilt

+

detach

@@ -7052,7 +2433,7 @@

tilt

-

Emits when a tilt sensor is activated.

+

Emits when a device is detached from the Hub.

@@ -7087,13 +2468,13 @@
Parameters:
portdevice -string +Device @@ -7104,88 +2485,113 @@
Parameters:
-

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

- - - x - + - - - -number + +
+ + + - - + +
Inherited From:
+
+ +
+ - + - + - - + - - - y - + - - - -number + + + - - + - - - - + +
Source:
+
+ +
+ + + + + + + +
+ - - - z - + - - - -number + + + - - + - + +
+ +
+
+

disconnect

+ + +
+
-

(Only available when using a Control+ Hub)

- + +
+

Emits when the hub is disconnected.

+
+ - - + + +
@@ -7198,7 +2604,7 @@
Parameters:
@@ -7229,8 +2635,8 @@
Parameters:
@@ -7309,7 +2715,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/ColorDistanceSensor.html b/docs/ColorDistanceSensor.html new file mode 100644 index 00000000..30697a55 --- /dev/null +++ b/docs/ColorDistanceSensor.html @@ -0,0 +1,1576 @@ + + + + + + + node-poweredup Class: ColorDistanceSensor + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: ColorDistanceSensor

+
+ +
+ +

+ ColorDistanceSensor +

+ + +
+ + +
+
+ + +
+
+

new ColorDistanceSensor()

+ + +
+
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> connected

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
connected + + +boolean + + + + +

Check if the device is still attached.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hub

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
hub + + +Hub + + + + +

The Hub the device is attached to.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> isVirtualPort

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
isVirtualPort + + +boolean + + + + +

Is this device attached to a virtual port (ie. a combined device)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> mode

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
mode + + +number + + + + +

The mode the device is currently in

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> portName

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
portName + + +string + + + + +

The port the device is attached to.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +number + + + + +

The type of the device

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + + + + + +

Events

+ +
+ +
+
+

color

+ + +
+
+ + +
+

Emits when a color sensor is activated.

+
+ + + + + +
Type: +object + + +
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
color + + +Color + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

colorAndDistance

+ + +
+
+ + +
+

A combined color and distance event, emits when the sensor is activated.

+
+ + + + + +
Type: +object + + +
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
color + + +Color + + + + +
distance + + +number + + + + +

Distance, in millimeters.

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+
+

distance

+ + +
+
+ + +
+

Emits when a distance sensor is activated.

+
+ + + + + +
Type: +object + + +
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
distance + + +number + + + + +

Distance, in millimeters.

+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/ControlPlusHub.html b/docs/ControlPlusHub.html index 0c2287ee..44f68ea4 100644 --- a/docs/ControlPlusHub.html +++ b/docs/ControlPlusHub.html @@ -33,21 +33,14 @@ - - @@ -152,8 +145,8 @@

new Con
@@ -209,77 +202,36 @@

Extends

-

Members

+ + +

Methods


-
-

<readonly> batteryLevel

- +
+

connect()

+
- - - -
+
+

Connect to the Hub.

+
-
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - -
NameTypeDescription
batteryLevel - - -number - - - - -

Battery level of the hub (Percentage between 0-100)

-
+ +
+ @@ -290,7 +242,7 @@
Properties:
@@ -299,7 +251,7 @@
Properties:
Overrides:
@@ -326,8 +278,8 @@
Properties:
@@ -342,30 +294,71 @@
Properties:
+ + + + + + + + + + +
Returns:
+ + +
+

Resolved upon successful connect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + +

-
-

<readonly> current

- +
+

createVirtualPort(firstPortName, secondPortName)

+
- - +
+

Combines two ports with into a single virtual port.

+

Note: The devices attached to the ports must be of the same device type.

+
-
+ -
Properties:
+ -
+ + +
Parameters:
+ - +
@@ -387,13 +380,37 @@
Properties:
- + + + + + + + + + + + + + + + + + +
currentfirstPortName -number +string + + + + +

First port name

secondPortName + + +string @@ -404,16 +421,19 @@
Properties:
-

Current usage of the hub (Milliamps)

Second port name

-
+ +
+ + @@ -423,17 +443,12 @@
Properties:
- -
Overrides:
-
@@ -459,8 +474,8 @@
Properties:
@@ -475,77 +490,70 @@
Properties:
-
- - -
-
-

<readonly> firmwareVersion

+ -
-
+ -
+
Returns:
+ +
+

Resolved upon successful issuance of command.

+
-
Properties:
- -
- - - - - - - - - - +
+
+ Type +
+
+Promise -
- - - - - - - - + + - +
+
+

disconnect()

+ + +
+
- + +
+

Disconnect the Hub.

+
+ - + -
- + - -
NameTypeDescription
firmwareVersion - - -string + + + + -

Firmware version of the hub

-
+ +
+ @@ -556,17 +564,12 @@
Properties:
- -
Overrides:
-
@@ -592,8 +595,8 @@
Properties:
@@ -608,4740 +611,40 @@
Properties:
-
- - -
-
-

<readonly> hardwareVersion

+ -
-
+ -
+
Returns:
+ +
+

Resolved upon successful disconnect.

+
-
Properties:
-
- - - - - +
+
+ Type +
+
+Promise -
- - + + - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
firmwareVersion - - -string - - - - -

Hardware version of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> name

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
name - - -string - - - - -

Name of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> primaryMACAddress

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
primaryMACAddress - - -string - - - - -

Primary MAC address of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> rssi

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
rssi - - -number - - - - -

Signal strength of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> uuid

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
uuid - - -string - - - - -

UUID of the hub

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- - - -
-
-

<readonly> voltage

- - -
-
- - - - - -
- - -
Properties:
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
voltage - - -number - - - - -

Voltage of the hub (Volts)

-
- - - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - -
- -
- - - -

Methods

- -
- -
-
-

brakeMotor(port)

- - -
-
- - -
-

Fully (hard) stop the motor on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

connect()

- - -
-
- - -
-

Connect to the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful connect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

disconnect()

- - -
-
- - -
-

Disconnect the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

getHubType()

- - -
-
- - -
-

Get the hub type.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -HubType - - - -
-
- - - - - -
- - - -
-
-

getPortDeviceType(port)

- - -
-
- - -
-

Get the device type for a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - - - -
-
- Type -
-
- -DeviceType - - - -
-
- - - - - -
- - - -
-
-

rampMotorSpeed(port, fromSpeed, toSpeed, time)

- - -
-
- - -
-

Ramp the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
fromSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

toSpeed - - -number - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

time - - -number - - - - -

How long the ramp should last (in milliseconds).

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

resetAbsolutePosition(port)

- - -
-
- - -
-

Reset the current motor position as absolute position zero

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command (ie. once the motor is finished).

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setAbsolutePosition(port, pos [, speed])

- - -
-
- - -
-

Tell motor to goto an absolute position

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDefaultDescription
port - - -string - - - - - - - - - - - - -
pos - - -number - - - - - - - - - - - - -

The position of the motor to go to

speed - - -number -| - -Array.<number> - - - - - - - <optional>
- - - - - -
- - 100 - -

A value between 1 - 100 should be set (Direction does not apply when going to absolute position)

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command (ie. once the motor is finished).

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDColor(color)

- - -
-
- - -
-

Set the color of the LED on the Hub via a color value.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
color - - -Color - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLEDRGB(red, green, blue)

- - -
-
- - -
-

Set the color of the LED on the Hub via RGB values.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
red - - -number - - - - -
green - - -number - - - - -
blue - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setLightBrightness(port, brightness [, time])

- - -
-
- - -
-

Set the light brightness on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
brightness - - -number - - - - - - - - - - -

Brightness value between 0-100 (0 is off)

time - - -number - - - - - - - <optional>
- - - - - -

How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command. If time is specified, this is once the light is turned off.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setMotorAngle(port, angle [, speed])

- - -
-
- - -
-

Rotate a motor by a given angle.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDefaultDescription
port - - -string - - - - - - - - - - - - -
angle - - -number - - - - - - - - - - - - -

How much the motor should be rotated (in degrees).

speed - - -number -| - -Array.<number> - - - - - - - <optional>
- - - - - -
- - 100 - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command (ie. once the motor is finished).

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setMotorSpeed(port, speed [, time])

- - -
-
- - -
-

Set the motor speed on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
speed - - -number -| - -Array.<number> - - - - - - - - - - -

For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

time - - -number - - - - - - - <optional>
- - - - - -

How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.

- - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful completion of command. If time is specified, this is once the motor is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

setName(name)

- - -
-
- - -
-

Set the name of the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
name - - -string - - - - -

New name of the hub (14 characters or less, ASCII only).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

shutdown()

- - -
-
- - -
-

Shutdown the Hub.

-
- - - - - - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful disconnect.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

sleep(delay)

- - -
-
- - -
-

Sleep a given amount of time.

-

This is a helper method to make it easier to add delays into a chain of commands.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
delay - - -number - - - - -

How long to sleep (in milliseconds).

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the delay is finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

subscribe(port [, mode])

- - -
-
- - -
-

Subscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
port - - -string - - - - - - - - - - -
mode - - -number - - - - - - - <optional>
- - - - - -

The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

unsubscribe(port)

- - -
-
- - -
-

Unsubscribe to sensor notifications on a given port.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- - - -
-
-

wait(commands)

- - -
-
- - -
-

Wait until a given list of concurrently running commands are complete.

-

This is a helper method to make it easier to wait for concurrent commands to complete.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
commands - - -Array.<Promise.<any>> - - - - -

Array of executing commands.

- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - -
Returns:
- - -
-

Resolved after the commands are finished.

-
- - - -
-
- Type -
-
- -Promise - - - -
-
- - - - - -
- -
- - - - - -

Events

- -
- -
-
-

accel

- - -
-
- - -
-

Emits when accelerometer detects movement. Measured in mG.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
x - - -number - - - - -
y - - -number - - - - -
z - - -number - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - - - - -
- - - -
-
-

attach

- - -
-
- - -
-

Emits when a motor or sensor is attached to the Hub.

-
- - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
type - - -DeviceType - - - - -
- - - - -
- - - - - - - -
Inherited From:
-
- -
- - - -
Overrides:
-
- - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - +
@@ -5350,7 +653,7 @@
Parameters:

-

button

+

getDeviceAtPort(portName)

@@ -5358,7 +661,7 @@

button

-

Emits when a button is pressed.

+

Retrieves the device attached to a given port.

@@ -5393,7 +696,7 @@
Parameters:
- button + portName @@ -5410,31 +713,7 @@
Parameters:
- - - - - - - - state - - - - - -ButtonState - - - - - - - - - - - +

The name of the port to retrieve the device from.

@@ -5456,7 +735,7 @@
Parameters:
@@ -5487,8 +766,8 @@
Parameters:
@@ -5513,6 +792,33 @@
Parameters:
+
Returns:
+ + +
+

The device attached to the port.

+
+ + + +
+
+ Type +
+
+ +Device +| + +undefined + + + +
+
+ + +
@@ -5521,7 +827,7 @@
Parameters:

-

color

+

getDevices()

@@ -5529,7 +835,7 @@

color

-

Emits when a color sensor is activated.

+

Retrieves all attached devices.

@@ -5539,80 +845,6 @@

color

-
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
port - - -string - - - - -
color - - -Color - - - - -
- -
@@ -5627,7 +859,7 @@
Parameters:
@@ -5658,8 +890,8 @@
Parameters:
@@ -5684,6 +916,30 @@
Parameters:
+
Returns:
+ + +
+

Array of all attached devices.

+
+ + + +
+
+ Type +
+
+ +Array.<Device> + + + +
+
+ + + @@ -5692,7 +948,7 @@
Parameters:

-

colorAndDistance

+

getDevicesByType(deviceType)

@@ -5700,7 +956,7 @@

colorAndDistance

-

A combined color and distance event, emits when the sensor is activated.

+

Retrieves an array of devices of the specified type.

@@ -5735,55 +991,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - color - - - - - -Color - - - - - - - - - - - - - - - - - - distance + deviceType @@ -5800,7 +1008,7 @@
Parameters:
-

Distance, in millimeters.

+

The device type to lookup.

@@ -5822,7 +1030,7 @@
Parameters:
@@ -5853,8 +1061,8 @@
Parameters:
@@ -5870,15 +1078,39 @@
Parameters:
- + + + + + + + + + +
Returns:
+ + +
+

Array of all devices of the specified type.

+
+ + + +
+
+ Type +
+
+ +Array.<Device> + - - +
+
- - + @@ -5887,7 +1119,7 @@
Parameters:

-

detach

+

setName(name)

@@ -5895,7 +1127,7 @@

detach

-

Emits when an attached motor or sensor is detached from the Hub.

+

Set the name of the Hub.

@@ -5930,7 +1162,7 @@
Parameters:
- port + name @@ -5947,7 +1179,7 @@
Parameters:
- +

New name of the hub (14 characters or less, ASCII only).

@@ -5969,17 +1201,12 @@
Parameters:
- -
Overrides:
-
@@ -6005,8 +1232,8 @@
Parameters:
@@ -6031,6 +1258,30 @@
Parameters:
+
Returns:
+ + +
+

Resolved upon successful issuance of command.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6039,7 +1290,7 @@
Parameters:

-

disconnect

+

shutdown()

@@ -6047,7 +1298,7 @@

disconnect

-

Emits when the hub is disconnected.

+

Shutdown the Hub.

@@ -6071,17 +1322,12 @@

disconnect

- -
Overrides:
-
@@ -6107,8 +1353,8 @@

disconnect

@@ -6133,6 +1379,30 @@

disconnect

+
Returns:
+ + +
+

Resolved upon successful disconnect.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6141,7 +1411,7 @@

disconnect


-

distance

+

sleep(delay)

@@ -6149,7 +1419,8 @@

distance

-

Emits when a distance sensor is activated.

+

Sleep a given amount of time.

+

Note: This is a helper method to make it easier to add delays into a chain of commands.

@@ -6184,31 +1455,7 @@
Parameters:
- port - - - - - -string - - - - - - - - - - - - - - - - - - distance + delay @@ -6225,7 +1472,7 @@
Parameters:
-

Distance, in millimeters.

+

How long to sleep (in milliseconds).

@@ -6247,7 +1494,7 @@
Parameters:
@@ -6278,8 +1525,8 @@
Parameters:
@@ -6304,6 +1551,30 @@
Parameters:
+
Returns:
+ + +
+

Resolved after the delay is finished.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6312,7 +1583,7 @@
Parameters:

-

gyro

+

wait(commands)

@@ -6320,7 +1591,8 @@

gyro

-

Emits when gyroscope detects movement. Measured in DPS - degrees per second.

+

Wait until a given list of concurrently running commands are complete.

+

Note: This is a helper method to make it easier to wait for concurrent commands to complete.

@@ -6355,13 +1627,13 @@
Parameters:
- port + commands -string +Array.<Promise.<any>> @@ -6372,68 +1644,168 @@
Parameters:
- +

Array of executing commands.

+ + - - - x - + - - - -number + +
+ + + - - + +
Inherited From:
+
+ +
+ - + - + - - + - + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ - y +
+

Resolved after the commands are finished.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + + + + + + +
+
+

waitForDeviceAtPort(portName)

+ + +
+
+ + +
+

Retrieves the device attached to a given port, waiting until one is attached if there isn't one.

+

Note: If a device is never attached, the returned promise may never resolve.

+
+ - - - -number + + + + +
Parameters:
+ - - + + + + + + - + - + - - + + + + + + - + + @@ -6466,7 +1838,7 @@
Parameters:
@@ -6497,8 +1869,8 @@
Parameters:
@@ -6523,6 +1895,30 @@
Parameters:
+
Returns:
+ + +
+

Resolved once a device is attached, or resolved immediately if a device is already attached.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + @@ -6531,7 +1927,7 @@
Parameters:

-

rotate

+

waitForDeviceByType(deviceType)

@@ -6539,7 +1935,8 @@

rotate

-

Emits when a rotation sensor is activated.

+

Retrieves the first device attached of the specified type, waiting until one is attached if there isn't one.

+

Note: If a device is never attached, the returned promise may never resolve.

@@ -6574,31 +1971,7 @@
Parameters:
- - - - - - - - - - - - - - - - - + + @@ -6637,7 +2010,7 @@
Parameters:
@@ -6668,8 +2041,8 @@
Parameters:
@@ -6694,15 +2067,47 @@
Parameters:
+
Returns:
+ + +
+

Resolved once a device is attached, or resolved immediately if a device is already attached.

+
+ + + +
+
+ Type +
+
+ +Promise + + + +
+
+ + + - + + + + + + +

Events

+ +

-

speed

+

attach

@@ -6710,7 +2115,7 @@

speed

-

Emits on a speed change.

+

Emits when a device is attached to the Hub.

@@ -6745,37 +2150,13 @@
Parameters:
- - - - - - - - - - - - - - - - - + - + + - + - + + + +
NameType
Description
zportName -number +string @@ -6444,7 +1816,7 @@
Parameters:
-

The name of the port to retrieve the device from.

port - - -string - - - - -
rotationdeviceType @@ -6615,7 +1988,7 @@
Parameters:
-

The device type to lookup.

port - - -string - - - - -
speeddevice -number +Device @@ -6808,7 +2189,7 @@
Parameters:
@@ -6839,8 +2220,8 @@
Parameters:
@@ -6873,7 +2254,7 @@
Parameters:

-

temp

+

button

@@ -6881,7 +2262,7 @@

temp

-

Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

+

Emits when a button is pressed.

@@ -6916,7 +2297,7 @@
Parameters:
portbutton @@ -6933,20 +2314,20 @@
Parameters:
-

For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

tempstate -number +ButtonState @@ -6979,7 +2360,7 @@
Parameters:
@@ -7010,8 +2391,8 @@
Parameters:
@@ -7044,7 +2425,7 @@
Parameters:

-

tilt

+

detach

@@ -7052,7 +2433,7 @@

tilt

-

Emits when a tilt sensor is activated.

+

Emits when a device is detached from the Hub.

@@ -7087,13 +2468,13 @@
Parameters:
portdevice -string +Device @@ -7104,88 +2485,113 @@
Parameters:
-

If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

- - - x - + - - - -number + +
+ + + - - + +
Inherited From:
+
+ +
+ - + - + - - + - - - y - + - - - -number + + + - - + - - - - + +
Source:
+
+ +
+ + + + + + + +
+ - - - z - + - - - -number + + + - - + - + +
+ +
+
+

disconnect

+ + +
+
-

(Only available when using a Control+ Hub)

- + +
+

Emits when the hub is disconnected.

+
+ - - + + +
@@ -7198,7 +2604,7 @@
Parameters:
@@ -7229,8 +2635,8 @@
Parameters:
@@ -7309,7 +2715,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/CurrentSensor.html b/docs/CurrentSensor.html new file mode 100644 index 00000000..f34476ab --- /dev/null +++ b/docs/CurrentSensor.html @@ -0,0 +1,1264 @@ + + + + + + + node-poweredup Class: CurrentSensor + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: CurrentSensor

+
+ +
+ +

+ CurrentSensor +

+ + +
+ + +
+
+ + +
+
+

new CurrentSensor()

+ + +
+
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + + + + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> connected

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
connected + + +boolean + + + + +

Check if the device is still attached.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hub

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
hub + + +Hub + + + + +

The Hub the device is attached to.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> isVirtualPort

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
isVirtualPort + + +boolean + + + + +

Is this device attached to a virtual port (ie. a combined device)

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> mode

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
mode + + +number + + + + +

The mode the device is currently in

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> portName

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
portName + + +string + + + + +

The port the device is attached to.

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +number + + + + +

The type of the device

+
+ + + + + + + + +
Inherited From:
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + + + + + +

Events

+ +
+ +
+
+

current

+ + +
+
+ + +
+

Emits when a current change is detected.

+
+ + + + + +
Type: +object + + +
+ + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
current + + +number + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/Device.html b/docs/Device.html new file mode 100644 index 00000000..015810ed --- /dev/null +++ b/docs/Device.html @@ -0,0 +1,1062 @@ + + + + + + + node-poweredup Class: Device + + + + + + + + + + + + + +
+
+ + +
+ +
+ + +

Class: Device

+
+ +
+ +

+ Device +

+ + +
+ + +
+
+ + +
+
+

new Device()

+ + +
+
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + + + +
+ + +
+ + +

Extends

+ + + + +
    +
  • EventEmitter
  • +
+ + + + + + + + + + + + + +

Members

+ +
+ +
+
+

<readonly> connected

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
connected + + +boolean + + + + +

Check if the device is still attached.

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> hub

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
hub + + +Hub + + + + +

The Hub the device is attached to.

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> isVirtualPort

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
isVirtualPort + + +boolean + + + + +

Is this device attached to a virtual port (ie. a combined device)

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> mode

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
mode + + +number + + + + +

The mode the device is currently in

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> portName

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
portName + + +string + + + + +

The port the device is attached to.

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ + + +
+
+

<readonly> type

+ + +
+
+ + + + + +
+ + +
Properties:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
type + + +number + + + + +

The type of the device

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + +
+ +
+ + + + + + + +
+ +
+ + + + +
+
+ +
+ + +
+ +
+ + +
+
+ + + + + +
+ + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/DuploTrainBase.html b/docs/DuploTrainBase.html index 48883edb..c00f7489 100644 --- a/docs/DuploTrainBase.html +++ b/docs/DuploTrainBase.html @@ -33,14 +33,14 @@ @@ -503,7 +503,7 @@
Properties:
- firmwareVersion + hardwareVersion @@ -1366,7 +1366,7 @@

Methods


-

setName(name)

+

createVirtualPort(firstPortName, secondPortName)

@@ -1374,7 +1374,8 @@

setName(name)<
-

Set the name of the Hub.

+

Combines two ports with into a single virtual port.

+

Note: The devices attached to the ports must be of the same device type.

@@ -1409,7 +1410,7 @@

Parameters:
- name + firstPortName @@ -1426,135 +1427,38 @@
Parameters:
-

New name of the hub (14 characters or less, ASCII only).

+

First port name

- - - - - - -
- - - - - - - -
Inherited From:
-
- -
- - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- -
- - - - - - - -
- - - - - - - - - - - - -
Returns:
- + + + secondPortName -
-

Resolved upon successful issuance of command.

-
- - - -
-
- Type -
-
- -Promise - + + + +string -
-
- - - -
+ + - -
-
-

shutdown()

- - -
-
- -
-

Shutdown the Hub.

-
- + - +

Second port name

+ + + -
@@ -1569,7 +1473,7 @@

shutdown()

@@ -1601,7 +1505,7 @@

shutdown()
  • hubs/lpf2hub.js, - line 44 + line 88
  • @@ -1630,7 +1534,7 @@

    Returns:
    -

    Resolved upon successful disconnect.

    +

    Resolved upon successful issuance of command.

    @@ -1654,19 +1558,11 @@
    Returns:
    -
    - - - - - -

    Events

    - -
    +
    -

    accel

    +

    setName(name)

    @@ -1674,7 +1570,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Set the name of the Hub.

    @@ -1709,7 +1605,7 @@
    Parameters:
    - port + name @@ -1726,79 +1622,7 @@
    Parameters:
    - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z - - - - - -number - - - - - - - - - - - +

    New name of the hub (14 characters or less, ASCII only).

    @@ -1820,7 +1644,7 @@
    Parameters:
    @@ -1851,8 +1675,8 @@
    Parameters:
    @@ -1877,6 +1701,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful issuance of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -1885,7 +1733,7 @@
    Parameters:

    -

    button

    +

    shutdown()

    @@ -1893,7 +1741,7 @@

    button

    -

    Emits when a button is pressed.

    +

    Shutdown the Hub.

    @@ -1903,80 +1751,6 @@

    button

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    button - - -string - - - - -
    state - - -ButtonState - - - - -
    - -
    @@ -1991,7 +1765,7 @@
    Parameters:
    @@ -2023,7 +1797,7 @@
    Parameters:
    @@ -2048,15 +1822,47 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful disconnect.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + - +
    + + + + + +

    Events

    + +

    -

    tilt

    +

    button

    @@ -2064,7 +1870,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a button is pressed.

    @@ -2099,13 +1905,13 @@
    Parameters:
    - x + button -number +string @@ -2123,13 +1929,13 @@
    Parameters:
    - y + state -number +ButtonState @@ -2162,7 +1968,7 @@
    Parameters:
    @@ -2193,8 +1999,8 @@
    Parameters:
    @@ -2273,7 +2079,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/DuploTrainBaseColorSensor.html b/docs/DuploTrainBaseColorSensor.html new file mode 100644 index 00000000..9cdafe9b --- /dev/null +++ b/docs/DuploTrainBaseColorSensor.html @@ -0,0 +1,1264 @@ + + + + + + + node-poweredup Class: DuploTrainBaseColorSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: DuploTrainBaseColorSensor

    +
    + +
    + +

    + DuploTrainBaseColorSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new DuploTrainBaseColorSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    color

    + + +
    +
    + + +
    +

    Emits when a color sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    color + + +Color + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/DuploTrainBaseMotor.html b/docs/DuploTrainBaseMotor.html new file mode 100644 index 00000000..a3b6dba9 --- /dev/null +++ b/docs/DuploTrainBaseMotor.html @@ -0,0 +1,1752 @@ + + + + + + + node-poweredup Class: DuploTrainBaseMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: DuploTrainBaseMotor

    +
    + +
    + +

    + DuploTrainBaseMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new DuploTrainBaseMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    brake()

    + + +
    +
    + + +
    +

    Brake the motor.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rampPower(fromPower, toPower, time)

    + + +
    +
    + + +
    +

    Ramp the motor power.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    fromPower + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    toPower + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    time + + +number + + + + +

    How long the ramp should last (in milliseconds).

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setPower(power)

    + + +
    +
    + + +
    +

    Set the motor power.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    power + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    stop()

    + + +
    +
    + + +
    +

    Stop the motor.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/DuploTrainBaseSpeaker.html b/docs/DuploTrainBaseSpeaker.html new file mode 100644 index 00000000..618b18d8 --- /dev/null +++ b/docs/DuploTrainBaseSpeaker.html @@ -0,0 +1,1282 @@ + + + + + + + node-poweredup Class: DuploTrainBaseSpeaker + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: DuploTrainBaseSpeaker

    +
    + +
    + +

    + DuploTrainBaseSpeaker +

    + + +
    + + +
    +
    + + +
    +
    +

    new DuploTrainBaseSpeaker()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    playSound(sound)

    + + +
    +
    + + +
    +

    Play a built-in train sound.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    sound + + +DuploTrainBaseSound + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/DuploTraniBaseSpeedometer.html b/docs/DuploTraniBaseSpeedometer.html new file mode 100644 index 00000000..f2a44180 --- /dev/null +++ b/docs/DuploTraniBaseSpeedometer.html @@ -0,0 +1,1116 @@ + + + + + + + node-poweredup Class: DuploTraniBaseSpeedometer + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: DuploTraniBaseSpeedometer

    +
    + +
    + +

    + DuploTraniBaseSpeedometer +

    + + +
    + + +
    +
    + + +
    +
    +

    new DuploTraniBaseSpeedometer()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/Hub.html b/docs/Hub.html index 19999146..e9f93d3b 100644 --- a/docs/Hub.html +++ b/docs/Hub.html @@ -33,14 +33,14 @@ @@ -503,7 +503,7 @@
    Properties:
    - firmwareVersion + hardwareVersion @@ -1472,6 +1472,202 @@
    Returns:
    + + + + +
    +
    +

    createVirtualPort(firstPortName, secondPortName)

    + + +
    +
    + + +
    +

    Combines two ports with into a single virtual port.

    +

    Note: The devices attached to the ports must be of the same device type.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    firstPortName + + +string + + + + +

    First port name

    secondPortName + + +string + + + + +

    Second port name

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + +
    @@ -1590,7 +1786,7 @@
    Returns:

    -

    setName(name)

    +

    getDeviceAtPort(portName)

    @@ -1598,7 +1794,7 @@

    setName(name)<
    -

    Set the name of the Hub.

    +

    Retrieves the device attached to a given port.

    @@ -1633,7 +1829,7 @@

    Parameters:
    - name + portName @@ -1650,7 +1846,7 @@
    Parameters:
    -

    New name of the hub (14 characters or less, ASCII only).

    +

    The name of the port to retrieve the device from.

    @@ -1668,15 +1864,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -1703,8 +1890,8 @@
    Parameters:
    @@ -1733,7 +1920,7 @@
    Returns:
    -

    Resolved upon successful issuance of command.

    +

    The device attached to the port.

    @@ -1744,7 +1931,10 @@
    Returns:
    -Promise +Device +| + +undefined @@ -1761,7 +1951,7 @@
    Returns:

    -

    shutdown()

    +

    getDevices()

    @@ -1769,7 +1959,7 @@

    shutdown() -

    Shutdown the Hub.

    +

    Retrieves all attached devices.

    @@ -1789,15 +1979,6 @@

    shutdown()Inherited From: -
    - -
    - @@ -1824,8 +2005,8 @@

    shutdown() @@ -1854,7 +2035,7 @@

    Returns:
    -

    Resolved upon successful disconnect.

    +

    Array of all attached devices.

    @@ -1865,7 +2046,7 @@
    Returns:
    -Promise +Array.<Device> @@ -1882,7 +2063,7 @@
    Returns:

    -

    sleep(delay)

    +

    getDevicesByType(deviceType)

    @@ -1890,8 +2071,7 @@

    sleep(delay)

    -

    Sleep a given amount of time.

    -

    This is a helper method to make it easier to add delays into a chain of commands.

    +

    Retrieves an array of devices of the specified type.

    @@ -1926,7 +2106,7 @@
    Parameters:
    - delay + deviceType @@ -1943,7 +2123,7 @@
    Parameters:
    -

    How long to sleep (in milliseconds).

    +

    The device type to lookup.

    @@ -1988,7 +2168,7 @@
    Parameters:
    @@ -2017,7 +2197,7 @@
    Returns:
    -

    Resolved after the delay is finished.

    +

    Array of all devices of the specified type.

    @@ -2028,7 +2208,7 @@
    Returns:
    -Promise +Array.<Device> @@ -2045,7 +2225,7 @@
    Returns:

    -

    wait(commands)

    +

    setName(name)

    @@ -2053,8 +2233,7 @@

    wait(commands) -

    Wait until a given list of concurrently running commands are complete.

    -

    This is a helper method to make it easier to wait for concurrent commands to complete.

    +

    Set the name of the Hub.

    @@ -2089,13 +2268,13 @@

    Parameters:
    - commands + name -Array.<Promise.<any>> +string @@ -2106,7 +2285,7 @@
    Parameters:
    -

    Array of executing commands.

    +

    New name of the hub (14 characters or less, ASCII only).

    @@ -2124,6 +2303,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -2150,8 +2338,8 @@
    Parameters:
    @@ -2180,7 +2368,7 @@
    Returns:
    -

    Resolved after the commands are finished.

    +

    Resolved upon successful issuance of command.

    @@ -2204,19 +2392,132 @@
    Returns:
    -
    - - + + +
    +
    +

    shutdown()

    + +
    +
    -

    Events

    +
    +

    Shutdown the Hub.

    +
    + -
    + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful disconnect.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    -

    accel

    +

    sleep(delay)

    @@ -2224,7 +2525,8 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Sleep a given amount of time.

    +

    Note: This is a helper method to make it easier to add delays into a chain of commands.

    @@ -2259,13 +2561,13 @@
    Parameters:
    - port + delay -string +number @@ -2276,20 +2578,159 @@
    Parameters:
    - +

    How long to sleep (in milliseconds).

    + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved after the delay is finished.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + + + + + +
    +
    +

    wait(commands)

    + + +
    +
    + + +
    +

    Wait until a given list of concurrently running commands are complete.

    +

    Note: This is a helper method to make it easier to wait for concurrent commands to complete.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + - + - - + + + + + +
    NameTypeDescription
    xcommands -number +Array.<Promise.<any>> @@ -2300,44 +2741,159 @@
    Parameters:
    -

    Array of executing commands.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved after the commands are finished.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    waitForDeviceAtPort(portName)

    + + +
    +
    + + +
    +

    Retrieves the device attached to a given port, waiting until one is attached if there isn't one.

    +

    Note: If a device is never attached, the returned promise may never resolve.

    +
    - - - y - + - - - -number + + + +
    Parameters:
    + + + + + + + - - + - + - + - - + + + + - + + @@ -2366,15 +2922,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -2401,8 +2948,8 @@
    Parameters:
    @@ -2427,6 +2974,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved once a device is attached, or resolved immediately if a device is already attached.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -2435,7 +3006,7 @@
    Parameters:

    -

    attach

    +

    waitForDeviceByType(deviceType)

    @@ -2443,7 +3014,8 @@

    attach

    -

    Emits when a device is attached to the Hub.

    +

    Retrieves the first device attached of the specified type, waiting until one is attached if there isn't one.

    +

    Note: If a device is never attached, the returned promise may never resolve.

    @@ -2478,13 +3050,13 @@
    Parameters:
    - + + @@ -2540,7 +3112,7 @@
    Parameters:
    @@ -2565,11 +3137,43 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved once a device is attached, or resolved immediately if a device is already attached.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + - + + + + + + +

    Events

    + +

    @@ -2581,7 +3185,7 @@

    attach

    -

    Emits when a device is detached from the Hub.

    +

    Emits when a device is attached to the Hub.

    @@ -2622,7 +3226,7 @@
    Parameters:
    - + - + + - -
    NameType
    Description
    zportName -number +string @@ -2348,7 +2904,7 @@
    Parameters:
    -

    The name of the port to retrieve the device from.

    devicedeviceType -Device +number @@ -2495,7 +3067,7 @@
    Parameters:
    -

    The device type to lookup.

    -Device +Device @@ -2678,7 +3282,7 @@
    Parameters:
    @@ -2849,7 +3453,7 @@
    Parameters:
    @@ -2882,7 +3486,7 @@
    Parameters:

    -

    disconnect

    +

    detach

    @@ -2890,7 +3494,7 @@

    disconnect

    -

    Emits when the hub is disconnected.

    +

    Emits when a device is detached from the Hub.

    @@ -2900,6 +3504,56 @@

    disconnect

    +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    device + + +Device + + + + +
    + +
    @@ -2937,7 +3591,7 @@

    disconnect

    @@ -2970,7 +3624,7 @@

    disconnect


    -

    tilt

    +

    disconnect

    @@ -2978,7 +3632,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when the hub is disconnected.

    @@ -2988,80 +3642,6 @@

    tilt

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    x - - -number - - - - -
    y - - -number - - - - -
    - -
    @@ -3072,15 +3652,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -3107,8 +3678,8 @@
    Parameters:
    @@ -3187,7 +3758,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/HubLED.html b/docs/HubLED.html new file mode 100644 index 00000000..5c68025b --- /dev/null +++ b/docs/HubLED.html @@ -0,0 +1,1492 @@ + + + + + + + node-poweredup Class: HubLED + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: HubLED

    +
    + +
    + +

    + HubLED +

    + + +
    + + +
    +
    + + +
    +
    +

    new HubLED()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    setColor(color)

    + + +
    +
    + + +
    +

    Set the color of the LED on the Hub via a color value.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    color + + +Color + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setRGB(red, green, blue)

    + + +
    +
    + + +
    +

    Set the color of the LED on the Hub via RGB values.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    red + + +number + + + + +
    green + + +number + + + + +
    blue + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/LPF2Hub.html b/docs/LPF2Hub.html index 9a8d0b46..0410915d 100644 --- a/docs/LPF2Hub.html +++ b/docs/LPF2Hub.html @@ -33,14 +33,14 @@ @@ -497,7 +497,7 @@
    Properties:
    firmwareVersionhardwareVersion @@ -1360,7 +1360,7 @@

    Methods


    -

    setName(name)

    +

    createVirtualPort(firstPortName, secondPortName)

    @@ -1368,7 +1368,8 @@

    setName(name)<
    -

    Set the name of the Hub.

    +

    Combines two ports with into a single virtual port.

    +

    Note: The devices attached to the ports must be of the same device type.

    @@ -1403,7 +1404,7 @@

    Parameters:
    namefirstPortName @@ -1420,126 +1421,38 @@
    Parameters:
    -

    New name of the hub (14 characters or less, ASCII only).

    First port name

    - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - + + + secondPortName -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - -
    -
    + + + +string - - -
    + + - -
    -
    -

    shutdown()

    - - -
    -
    - -
    -

    Shutdown the Hub.

    -
    - + - +

    Second port name

    + + + -
    @@ -1577,7 +1490,7 @@

    shutdown()
  • hubs/lpf2hub.js, - line 44 + line 88
  • @@ -1606,7 +1519,7 @@
    Returns:
    -

    Resolved upon successful disconnect.

    +

    Resolved upon successful issuance of command.

    @@ -1630,19 +1543,11 @@
    Returns:
    -
    - - - - - -

    Events

    - -
    +
    -

    accel

    +

    setName(name)

    @@ -1650,7 +1555,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Set the name of the Hub.

    @@ -1685,7 +1590,7 @@
    Parameters:
    - port + name @@ -1702,79 +1607,7 @@
    Parameters:
    - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z - - - - - -number - - - - - - - - - - - +

    New name of the hub (14 characters or less, ASCII only).

    @@ -1818,8 +1651,8 @@
    Parameters:
    @@ -1844,6 +1677,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful issuance of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -1852,7 +1709,7 @@
    Parameters:

    -

    button

    +

    shutdown()

    @@ -1860,7 +1717,7 @@

    button

    -

    Emits when a button is pressed.

    +

    Shutdown the Hub.

    @@ -1870,80 +1727,6 @@

    button

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    button - - -string - - - - -
    state - - -ButtonState - - - - -
    - -
    @@ -1981,7 +1764,7 @@
    Parameters:
    @@ -2006,15 +1789,47 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful disconnect.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + - +
    + + + + + +

    Events

    + +

    -

    tilt

    +

    button

    @@ -2022,7 +1837,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a button is pressed.

    @@ -2057,13 +1872,13 @@
    Parameters:
    - x + button -number +string @@ -2081,13 +1896,13 @@
    Parameters:
    - y + state -number +ButtonState @@ -2142,8 +1957,8 @@
    Parameters:
    @@ -2222,7 +2037,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/Light.html b/docs/Light.html new file mode 100644 index 00000000..5314026e --- /dev/null +++ b/docs/Light.html @@ -0,0 +1,1492 @@ + + + + + + + node-poweredup Class: Light + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: Light

    +
    + +
    + +

    + Light +

    + + +
    + + +
    +
    + + +
    +
    +

    new Light()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    rampBrightness(fromBrightness, toBrightness, time)

    + + +
    +
    + + +
    +

    Ramp the light brightness.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    fromBrightness + + +number + + + + +

    Brightness value between 0-100 (0 is off)

    toBrightness + + +number + + + + +

    Brightness value between 0-100 (0 is off)

    time + + +number + + + + +

    How long the ramp should last (in milliseconds).

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrightness(brightness)

    + + +
    +
    + + +
    +

    Set the light brightness.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    brightness + + +number + + + + +

    Brightness value between 0-100 (0 is off)

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/MediumLinearMotor.html b/docs/MediumLinearMotor.html new file mode 100644 index 00000000..4e32bc70 --- /dev/null +++ b/docs/MediumLinearMotor.html @@ -0,0 +1,1051 @@ + + + + + + + node-poweredup Class: MediumLinearMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: MediumLinearMotor

    +
    + +
    + +

    + MediumLinearMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new MediumLinearMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + + + +

    Methods

    + +
    + +
    +
    +

    rotateByDegrees(degrees [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given amount of degrees.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    degrees + + +number + + + + + + + + + + + + +

    How much the motor should be rotated (in degrees).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrakingStyle(style)

    + + +
    +
    + + +
    +

    Set the braking style of the motor.

    +

    Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    style + + +number + + + + +

    Either BRAKE or HOLD

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    setSpeed(speed)

    + + +
    +
    + + +
    +

    Set the motor speed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    speed + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/MotionSensor.html b/docs/MotionSensor.html new file mode 100644 index 00000000..7601a206 --- /dev/null +++ b/docs/MotionSensor.html @@ -0,0 +1,1264 @@ + + + + + + + node-poweredup Class: MotionSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: MotionSensor

    +
    + +
    + +

    + MotionSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new MotionSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    distance

    + + +
    +
    + + +
    +

    Emits when a distance sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    distance + + +number + + + + +

    Distance, in millimeters.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/MoveHub.html b/docs/MoveHub.html index 1dc1a1f0..a454da0e 100644 --- a/docs/MoveHub.html +++ b/docs/MoveHub.html @@ -33,14 +33,14 @@ @@ -513,7 +513,7 @@
    Properties:
    - firmwareVersion + hardwareVersion @@ -1411,7 +1411,7 @@

    Methods


    -

    setName(name)

    +

    createVirtualPort(firstPortName, secondPortName)

    @@ -1419,7 +1419,8 @@

    setName(name)<
    -

    Set the name of the Hub.

    +

    Combines two ports with into a single virtual port.

    +

    Note: The devices attached to the ports must be of the same device type.

    @@ -1454,7 +1455,7 @@

    Parameters:
    - name + firstPortName @@ -1471,135 +1472,38 @@
    Parameters:
    -

    New name of the hub (14 characters or less, ASCII only).

    +

    First port name

    - - - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - -
    Returns:
    - + + + secondPortName -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - + + + +string -
    -
    - - - - + + - -
    -
    -

    shutdown()

    - - -
    -
    - -
    -

    Shutdown the Hub.

    -
    - + - +

    Second port name

    + + + -
    @@ -1614,7 +1518,7 @@

    shutdown()

    @@ -1646,7 +1550,7 @@

    shutdown()
  • hubs/lpf2hub.js, - line 44 + line 88
  • @@ -1675,7 +1579,7 @@

    Returns:
    -

    Resolved upon successful disconnect.

    +

    Resolved upon successful issuance of command.

    @@ -1699,19 +1603,11 @@
    Returns:
    -
    - - - - - -

    Events

    - -
    +
    -

    accel

    +

    setName(name)

    @@ -1719,7 +1615,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Set the name of the Hub.

    @@ -1754,7 +1650,7 @@
    Parameters:
    - port + name @@ -1771,79 +1667,7 @@
    Parameters:
    - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z - - - - - -number - - - - - - - - - - - +

    New name of the hub (14 characters or less, ASCII only).

    @@ -1865,7 +1689,7 @@
    Parameters:
    @@ -1896,8 +1720,8 @@
    Parameters:
    @@ -1922,6 +1746,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful issuance of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -1930,7 +1778,7 @@
    Parameters:

    -

    button

    +

    shutdown()

    @@ -1938,7 +1786,7 @@

    button

    -

    Emits when a button is pressed.

    +

    Shutdown the Hub.

    @@ -1948,80 +1796,6 @@

    button

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    button - - -string - - - - -
    state - - -ButtonState - - - - -
    - -
    @@ -2036,7 +1810,7 @@
    Parameters:
    @@ -2068,7 +1842,7 @@
    Parameters:
    @@ -2093,15 +1867,47 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful disconnect.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + - +
    + + + + + +

    Events

    + +

    -

    tilt

    +

    button

    @@ -2109,7 +1915,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a button is pressed.

    @@ -2144,13 +1950,13 @@
    Parameters:
    - x + button -number +string @@ -2168,13 +1974,13 @@
    Parameters:
    - y + state -number +ButtonState @@ -2207,7 +2013,7 @@
    Parameters:
    @@ -2238,8 +2044,8 @@
    Parameters:
    @@ -2318,7 +2124,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/MoveHubMediumLinearMotor.html b/docs/MoveHubMediumLinearMotor.html new file mode 100644 index 00000000..295c0526 --- /dev/null +++ b/docs/MoveHubMediumLinearMotor.html @@ -0,0 +1,1051 @@ + + + + + + + node-poweredup Class: MoveHubMediumLinearMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: MoveHubMediumLinearMotor

    +
    + +
    + +

    + MoveHubMediumLinearMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new MoveHubMediumLinearMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + + + +

    Methods

    + +
    + +
    +
    +

    rotateByDegrees(degrees [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given amount of degrees.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    degrees + + +number + + + + + + + + + + + + +

    How much the motor should be rotated (in degrees).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrakingStyle(style)

    + + +
    +
    + + +
    +

    Set the braking style of the motor.

    +

    Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    style + + +number + + + + +

    Either BRAKE or HOLD

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    setSpeed(speed)

    + + +
    +
    + + +
    +

    Set the motor speed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    speed + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/MoveHubTiltSensor.html b/docs/MoveHubTiltSensor.html new file mode 100644 index 00000000..c73b163f --- /dev/null +++ b/docs/MoveHubTiltSensor.html @@ -0,0 +1,1288 @@ + + + + + + + node-poweredup Class: MoveHubTiltSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: MoveHubTiltSensor

    +
    + +
    + +

    + MoveHubTiltSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new MoveHubTiltSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    tilt

    + + +
    +
    + + +
    +

    Emits when a tilt sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    x + + +number + + + + +
    y + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/PUPHub.html b/docs/PUPHub.html index fa01e54c..88d3fbda 100644 --- a/docs/PUPHub.html +++ b/docs/PUPHub.html @@ -33,21 +33,14 @@ - - @@ -152,8 +145,8 @@

    new PUPHub() @@ -290,7 +283,7 @@

    Properties:
    @@ -326,8 +319,8 @@
    Properties:
    @@ -347,8 +340,8 @@
    Properties:

    -
    -

    <readonly> current

    +
    +

    <readonly> firmwareVersion

    @@ -387,13 +380,13 @@
    Properties:
    - current + firmwareVersion -number +string @@ -404,7 +397,7 @@
    Properties:
    -

    Current usage of the hub (Milliamps)

    +

    Firmware version of the hub

    @@ -423,7 +416,7 @@
    Properties:
    @@ -432,7 +425,7 @@
    Properties:
    Overrides:
    @@ -459,8 +452,8 @@
    Properties:
    @@ -480,8 +473,8 @@
    Properties:

    -
    -

    <readonly> firmwareVersion

    +
    +

    <readonly> hardwareVersion

    @@ -520,7 +513,7 @@
    Properties:
    - firmwareVersion + hardwareVersion @@ -537,7 +530,7 @@
    Properties:
    -

    Firmware version of the hub

    +

    Hardware version of the hub

    @@ -556,7 +549,7 @@
    Properties:
    @@ -565,7 +558,7 @@
    Properties:
    Overrides:
    @@ -592,8 +585,8 @@
    Properties:
    @@ -613,8 +606,8 @@
    Properties:

    -
    -

    <readonly> hardwareVersion

    +
    +

    <readonly> name

    @@ -653,7 +646,7 @@
    Properties:
    - firmwareVersion + name @@ -670,7 +663,7 @@
    Properties:
    -

    Hardware version of the hub

    +

    Name of the hub

    @@ -689,7 +682,7 @@
    Properties:
    @@ -698,7 +691,7 @@
    Properties:
    Overrides:
    @@ -725,8 +718,8 @@
    Properties:
    @@ -746,8 +739,8 @@
    Properties:

    -
    -

    <readonly> name

    +
    +

    <readonly> ports

    @@ -786,13 +779,13 @@
    Properties:
    - name + ports -string +Array.<string> @@ -803,7 +796,7 @@
    Properties:
    -

    Name of the hub

    +

    Array of port names

    @@ -822,7 +815,7 @@
    Properties:
    @@ -831,7 +824,7 @@
    Properties:
    Overrides:
    @@ -858,8 +851,8 @@
    Properties:
    @@ -955,7 +948,7 @@
    Properties:
    @@ -991,8 +984,8 @@
    Properties:
    @@ -1088,7 +1081,7 @@
    Properties:
    @@ -1124,8 +1117,8 @@
    Properties:
    @@ -1145,8 +1138,8 @@
    Properties:

    -
    -

    <readonly> uuid

    +
    +

    <readonly> type

    @@ -1185,7 +1178,7 @@
    Properties:
    - uuid + type @@ -1202,7 +1195,7 @@
    Properties:
    -

    UUID of the hub

    +

    Hub type

    @@ -1221,7 +1214,7 @@
    Properties:
    @@ -1230,7 +1223,7 @@
    Properties:
    Overrides:
    @@ -1257,8 +1250,8 @@
    Properties:
    @@ -1278,8 +1271,8 @@
    Properties:

    -
    -

    <readonly> voltage

    +
    +

    <readonly> uuid

    @@ -1318,13 +1311,13 @@
    Properties:
    - voltage + uuid -number +string @@ -1335,7 +1328,7 @@
    Properties:
    -

    Voltage of the hub (Volts)

    +

    UUID of the hub

    @@ -1354,7 +1347,7 @@
    Properties:
    @@ -1363,7 +1356,7 @@
    Properties:
    Overrides:
    @@ -1390,8 +1383,8 @@
    Properties:
    @@ -1418,7 +1411,7 @@

    Methods


    -

    brakeMotor(port)

    +

    connect()

    @@ -1426,7 +1419,7 @@

    brakeMotor(
    -

    Fully (hard) stop the motor on a given port.

    +

    Connect to the Hub.

    @@ -1436,56 +1429,6 @@

    brakeMotor( -

    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    - -
    @@ -1496,7 +1439,21 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + + +
    Overrides:
    +
    @@ -1522,8 +1479,8 @@
    Parameters:
    @@ -1552,7 +1509,7 @@
    Returns:
    -

    Resolved upon successful completion of command.

    +

    Resolved upon successful connect.

    @@ -1580,7 +1537,7 @@
    Returns:

    -

    connect()

    +

    createVirtualPort(firstPortName, secondPortName)

    @@ -1588,7 +1545,8 @@

    connect()

    -

    Connect to the Hub.

    +

    Combines two ports with into a single virtual port.

    +

    Note: The devices attached to the ports must be of the same device type.

    @@ -1598,6 +1556,80 @@

    connect()

    +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    firstPortName + + +string + + + + +

    First port name

    secondPortName + + +string + + + + +

    Second port name

    + +
    @@ -1612,7 +1644,7 @@

    connect()

    @@ -1621,7 +1653,7 @@

    connect()

    Overrides:
    @@ -1648,8 +1680,8 @@

    connect()

    @@ -1678,7 +1710,7 @@
    Returns:
    -

    Resolved upon successful connect.

    +

    Resolved upon successful issuance of command.

    @@ -1744,11 +1776,6 @@

    disconnect( - -
    Overrides:
    -
    @@ -1774,8 +1801,8 @@

    disconnect(
    @@ -1832,7 +1859,7 @@

    Returns:

    -

    getHubType()

    +

    getDeviceAtPort(portName)

    @@ -1840,7 +1867,7 @@

    getHubType(
    -

    Get the hub type.

    +

    Retrieves the device attached to a given port.

    @@ -1850,6 +1877,56 @@

    getHubType( +

    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The name of the port to retrieve the device from.

    + +
    @@ -1864,17 +1941,12 @@

    getHubType(
    - -
    Overrides:
    -
    @@ -1900,8 +1972,8 @@

    getHubType(
    @@ -1929,6 +2001,10 @@

    getHubType(

    Returns:
    +
    +

    The device attached to the port.

    +
    +
    @@ -1937,7 +2013,10 @@
    Returns:
    -HubType +Device +| + +undefined @@ -1954,7 +2033,7 @@
    Returns:

    -

    getPortDeviceType(port)

    +

    getDevices()

    @@ -1962,7 +2041,7 @@

    getP
    -

    Get the device type for a given port.

    +

    Retrieves all attached devices.

    @@ -1972,56 +2051,6 @@

    getP -

    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    - -
    @@ -2036,17 +2065,12 @@
    Parameters:
    - -
    Overrides:
    -
    @@ -2072,8 +2096,8 @@
    Parameters:
    @@ -2101,6 +2125,10 @@
    Parameters:
    Returns:
    +
    +

    Array of all attached devices.

    +
    +
    @@ -2109,7 +2137,7 @@
    Returns:
    -DeviceType +Array.<Device> @@ -2126,7 +2154,7 @@
    Returns:

    -

    rampMotorSpeed(port, fromSpeed, toSpeed, time)

    +

    getDevicesByType(deviceType)

    @@ -2134,7 +2162,7 @@

    rampMot
    -

    Ramp the motor speed on a given port.

    +

    Retrieves an array of devices of the specified type.

    @@ -2169,79 +2197,7 @@

    Parameters:
    - port - - - - - -string - - - - - - - - - - - - - - - - - - fromSpeed - - - - - -number - - - - - - - - - - -

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    - - - - - - - toSpeed - - - - - -number - - - - - - - - - - -

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    - - - - - - - time + deviceType @@ -2258,7 +2214,7 @@
    Parameters:
    -

    How long the ramp should last (in milliseconds).

    +

    The device type to lookup.

    @@ -2276,6 +2232,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -2302,8 +2267,8 @@
    Parameters:
    @@ -2332,3553 +2297,26 @@
    Returns:
    -

    Resolved upon successful completion of command.

    +

    Array of all devices of the specified type.

    -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - - - - - -
    -
    -

    resetAbsolutePosition(port)

    - - -
    -
    - - -
    -

    Reset the current motor position as absolute position zero

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful completion of command (ie. once the motor is finished).

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    setAbsolutePosition(port, pos [, speed])

    - - -
    -
    - - -
    -

    Tell motor to goto an absolute position

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeArgumentDefaultDescription
    port - - -string - - - - - - - - - - - - -
    pos - - -number - - - - - - - - - - - - -

    The position of the motor to go to

    speed - - -number -| - -Array.<number> - - - - - - - <optional>
    - - - - - -
    - - 100 - -

    A value between 1 - 100 should be set (Direction does not apply when going to absolute position)

    - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful completion of command (ie. once the motor is finished).

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    setLEDColor(color)

    - - -
    -
    - - -
    -

    Set the color of the LED on the Hub via a color value.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    color - - -Color - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    setLEDRGB(red, green, blue)

    - - -
    -
    - - -
    -

    Set the color of the LED on the Hub via RGB values.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    red - - -number - - - - -
    green - - -number - - - - -
    blue - - -number - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    setLightBrightness(port, brightness [, time])

    - - -
    -
    - - -
    -

    Set the light brightness on a given port.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeArgumentDescription
    port - - -string - - - - - - - - - - -
    brightness - - -number - - - - - - - - - - -

    Brightness value between 0-100 (0 is off)

    time - - -number - - - - - - - <optional>
    - - - - - -

    How long to turn the light on (in milliseconds). Leave empty to turn the light on indefinitely.

    - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful completion of command. If time is specified, this is once the light is turned off.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    setMotorAngle(port, angle [, speed])

    - - -
    -
    - - -
    -

    Rotate a motor by a given angle.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeArgumentDefaultDescription
    port - - -string - - - - - - - - - - - - -
    angle - - -number - - - - - - - - - - - - -

    How much the motor should be rotated (in degrees).

    speed - - -number -| - -Array.<number> - - - - - - - <optional>
    - - - - - -
    - - 100 - -

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

    - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful completion of command (ie. once the motor is finished).

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    setMotorSpeed(port, speed [, time])

    - - -
    -
    - - -
    -

    Set the motor speed on a given port.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeArgumentDescription
    port - - -string - - - - - - - - - - -
    speed - - -number -| - -Array.<number> - - - - - - - - - - -

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. If you are specifying port AB to control both motors, you can optionally supply a tuple of speeds.

    time - - -number - - - - - - - <optional>
    - - - - - -

    How long to activate the motor for (in milliseconds). Leave empty to turn the motor on indefinitely.

    - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful completion of command. If time is specified, this is once the motor is finished.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    setName(name)

    - - -
    -
    - - -
    -

    Set the name of the Hub.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    name - - -string - - - - -

    New name of the hub (14 characters or less, ASCII only).

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    shutdown()

    - - -
    -
    - - -
    -

    Shutdown the Hub.

    -
    - - - - - - - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful disconnect.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    sleep(delay)

    - - -
    -
    - - -
    -

    Sleep a given amount of time.

    -

    This is a helper method to make it easier to add delays into a chain of commands.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    delay - - -number - - - - -

    How long to sleep (in milliseconds).

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - -
    Overrides:
    -
    - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved after the delay is finished.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    subscribe(port [, mode])

    - - -
    -
    - - -
    -

    Subscribe to sensor notifications on a given port.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeArgumentDescription
    port - - -string - - - - - - - - - - -
    mode - - -number - - - - - - - <optional>
    - - - - - -

    The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - -
    Overrides:
    -
    - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    unsubscribe(port)

    - - -
    -
    - - -
    -

    Unsubscribe to sensor notifications on a given port.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - -
    Overrides:
    -
    - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - - - -
    -
    -

    wait(commands)

    - - -
    -
    - - -
    -

    Wait until a given list of concurrently running commands are complete.

    -

    This is a helper method to make it easier to wait for concurrent commands to complete.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    commands - - -Array.<Promise.<any>> - - - - -

    Array of executing commands.

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - -
    Overrides:
    -
    - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - -
    Returns:
    - - -
    -

    Resolved after the commands are finished.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - - - -
    -
    - - - - - -
    - -
    - - - - - -

    Events

    - -
    - -
    -
    -

    accel

    - - -
    -
    - - -
    -

    Emits when accelerometer detects movement. Measured in mG.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    x - - -number - - - - -
    y - - -number - - - - -
    z - - -number - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    attach

    - - -
    -
    - - -
    -

    Emits when a motor or sensor is attached to the Hub.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    type - - -DeviceType - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - -
    Overrides:
    -
    - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    button

    - - -
    -
    - - -
    -

    Emits when a button is pressed.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    button - - -string - - - - -
    state - - -ButtonState - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    color

    - - -
    -
    - - -
    -

    Emits when a color sensor is activated.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    color - - -Color - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    colorAndDistance

    - - -
    -
    - - -
    -

    A combined color and distance event, emits when the sensor is activated.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    color - - -Color - - - - -
    distance - - -number - - - - -

    Distance, in millimeters.

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - +
    +
    + Type +
    +
    + +Array.<Device> - - - +
    +
    - - +
    @@ -5887,7 +2325,7 @@
    Parameters:

    -

    detach

    +

    setName(name)

    @@ -5895,7 +2333,7 @@

    detach

    -

    Emits when an attached motor or sensor is detached from the Hub.

    +

    Set the name of the Hub.

    @@ -5930,7 +2368,7 @@
    Parameters:
    - port + name @@ -5947,7 +2385,7 @@
    Parameters:
    - +

    New name of the hub (14 characters or less, ASCII only).

    @@ -5969,7 +2407,7 @@
    Parameters:
    @@ -5978,7 +2416,7 @@
    Parameters:
    Overrides:
    @@ -6005,8 +2443,8 @@
    Parameters:
    @@ -6031,6 +2469,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful issuance of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -6039,7 +2501,7 @@
    Parameters:

    -

    disconnect

    +

    shutdown()

    @@ -6047,7 +2509,7 @@

    disconnect

    -

    Emits when the hub is disconnected.

    +

    Shutdown the Hub.

    @@ -6071,7 +2533,7 @@

    disconnect

    @@ -6080,7 +2542,7 @@

    disconnect

    Overrides:
    @@ -6107,8 +2569,8 @@

    disconnect

    @@ -6133,6 +2595,30 @@

    disconnect

    +
    Returns:
    + + +
    +

    Resolved upon successful disconnect.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -6141,7 +2627,7 @@

    disconnect


    -

    distance

    +

    sleep(delay)

    @@ -6149,7 +2635,8 @@

    distance

    -

    Emits when a distance sensor is activated.

    +

    Sleep a given amount of time.

    +

    Note: This is a helper method to make it easier to add delays into a chain of commands.

    @@ -6184,31 +2671,7 @@
    Parameters:
    - port - - - - - -string - - - - - - - - - - - - - - - - - - distance + delay @@ -6225,7 +2688,7 @@
    Parameters:
    -

    Distance, in millimeters.

    +

    How long to sleep (in milliseconds).

    @@ -6247,7 +2710,7 @@
    Parameters:
    @@ -6278,8 +2741,8 @@
    Parameters:
    @@ -6304,6 +2767,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved after the delay is finished.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -6312,7 +2799,7 @@
    Parameters:

    -

    gyro

    +

    wait(commands)

    @@ -6320,7 +2807,8 @@

    gyro

    -

    Emits when gyroscope detects movement. Measured in DPS - degrees per second.

    +

    Wait until a given list of concurrently running commands are complete.

    +

    Note: This is a helper method to make it easier to wait for concurrent commands to complete.

    @@ -6355,13 +2843,13 @@
    Parameters:
    - port + commands -string +Array.<Promise.<any>> @@ -6372,68 +2860,168 @@
    Parameters:
    - +

    Array of executing commands.

    + + - - - x - + - - - -number + +
    + + + - - + +
    Inherited From:
    +
    + +
    + - + - + - - + - - - y + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + +
    +

    Resolved after the commands are finished.

    +
    - + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + + + + - -number +
    +
    +

    waitForDeviceAtPort(portName)

    + + +
    +
    + + +
    +

    Retrieves the device attached to a given port, waiting until one is attached if there isn't one.

    +

    Note: If a device is never attached, the returned promise may never resolve.

    +
    + + + - - + + +
    Parameters:
    + - + + + + + + - + - - + + + + + + + + - + + @@ -6466,7 +3054,7 @@
    Parameters:
    @@ -6497,8 +3085,8 @@
    Parameters:
    @@ -6523,6 +3111,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved once a device is attached, or resolved immediately if a device is already attached.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -6531,7 +3143,7 @@
    Parameters:

    -

    rotate

    +

    waitForDeviceByType(deviceType)

    @@ -6539,7 +3151,8 @@

    rotate

    -

    Emits when a rotation sensor is activated.

    +

    Retrieves the first device attached of the specified type, waiting until one is attached if there isn't one.

    +

    Note: If a device is never attached, the returned promise may never resolve.

    @@ -6574,31 +3187,7 @@
    Parameters:
    - - - - - - - - - - - - - - - - - + + @@ -6637,7 +3226,7 @@
    Parameters:
    @@ -6668,8 +3257,8 @@
    Parameters:
    @@ -6694,15 +3283,47 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved once a device is attached, or resolved immediately if a device is already attached.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + - + + + + + + +

    Events

    + +

    -

    speed

    +

    attach

    @@ -6710,7 +3331,7 @@

    speed

    -

    Emits on a speed change.

    +

    Emits when a device is attached to the Hub.

    @@ -6745,37 +3366,13 @@
    Parameters:
    - - - - - - - - - - - - - - - - - + - + + - + - + + + +
    NameType
    Description
    zportName -number +string @@ -6444,7 +3032,7 @@
    Parameters:
    -

    The name of the port to retrieve the device from.

    port - - -string - - - - -
    rotationdeviceType @@ -6615,7 +3204,7 @@
    Parameters:
    -

    The device type to lookup.

    port - - -string - - - - -
    speeddevice -number +Device @@ -6808,7 +3405,7 @@
    Parameters:
    @@ -6839,8 +3436,8 @@
    Parameters:
    @@ -6873,7 +3470,7 @@
    Parameters:

    -

    temp

    +

    button

    @@ -6881,7 +3478,7 @@

    temp

    -

    Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

    +

    Emits when a button is pressed.

    @@ -6916,7 +3513,7 @@
    Parameters:
    portbutton @@ -6933,20 +3530,20 @@
    Parameters:
    -

    For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

    tempstate -number +ButtonState @@ -6979,12 +3576,17 @@
    Parameters:
    + +
    Overrides:
    +
    @@ -7010,8 +3612,8 @@
    Parameters:
    @@ -7044,7 +3646,7 @@
    Parameters:

    -

    tilt

    +

    detach

    @@ -7052,7 +3654,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a device is detached from the Hub.

    @@ -7087,13 +3689,13 @@
    Parameters:
    portdevice -string +Device @@ -7104,88 +3706,113 @@
    Parameters:
    -

    If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

    - - - x - + - - - -number + +
    + + + - - + +
    Inherited From:
    +
    + +
    + - + - + - - + - - - y - + - - - -number + + + - - + - - - - + +
    Source:
    +
    + +
    + + + + + + + +
    + - - - z - + - - - -number + + + - - + - + +
    + +
    +
    +

    disconnect

    + + +
    +
    -

    (Only available when using a Control+ Hub)

    - + +
    +

    Emits when the hub is disconnected.

    +
    + - - + + +
    @@ -7198,7 +3825,7 @@
    Parameters:
    @@ -7229,8 +3856,8 @@
    Parameters:
    @@ -7309,7 +3936,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/PUPRemote.html b/docs/PUPRemote.html index 06ab35c2..10d29b70 100644 --- a/docs/PUPRemote.html +++ b/docs/PUPRemote.html @@ -33,21 +33,14 @@ - - @@ -152,8 +145,8 @@

    new PUPRemot
    @@ -290,7 +283,7 @@

    Properties:
    @@ -326,8 +319,8 @@
    Properties:
    @@ -347,8 +340,8 @@
    Properties:

    -
    -

    <readonly> current

    +
    +

    <readonly> firmwareVersion

    @@ -387,13 +380,13 @@
    Properties:
    - current + firmwareVersion -number +string @@ -404,7 +397,7 @@
    Properties:
    -

    Current usage of the hub (Milliamps)

    +

    Firmware version of the hub

    @@ -423,7 +416,7 @@
    Properties:
    @@ -432,7 +425,7 @@
    Properties:
    Overrides:
    @@ -459,8 +452,8 @@
    Properties:
    @@ -480,8 +473,8 @@
    Properties:

    -
    -

    <readonly> firmwareVersion

    +
    +

    <readonly> hardwareVersion

    @@ -520,7 +513,7 @@
    Properties:
    - firmwareVersion + hardwareVersion @@ -537,7 +530,7 @@
    Properties:
    -

    Firmware version of the hub

    +

    Hardware version of the hub

    @@ -556,7 +549,7 @@
    Properties:
    @@ -565,7 +558,7 @@
    Properties:
    Overrides:
    @@ -592,8 +585,8 @@
    Properties:
    @@ -613,8 +606,8 @@
    Properties:

    -
    -

    <readonly> hardwareVersion

    +
    +

    <readonly> name

    @@ -653,7 +646,7 @@
    Properties:
    - firmwareVersion + name @@ -670,7 +663,7 @@
    Properties:
    -

    Hardware version of the hub

    +

    Name of the hub

    @@ -689,7 +682,7 @@
    Properties:
    @@ -698,7 +691,7 @@
    Properties:
    Overrides:
    @@ -725,8 +718,8 @@
    Properties:
    @@ -746,8 +739,8 @@
    Properties:

    -
    -

    <readonly> name

    +
    +

    <readonly> ports

    @@ -786,13 +779,13 @@
    Properties:
    - name + ports -string +Array.<string> @@ -803,7 +796,7 @@
    Properties:
    -

    Name of the hub

    +

    Array of port names

    @@ -822,7 +815,7 @@
    Properties:
    @@ -831,7 +824,7 @@
    Properties:
    Overrides:
    @@ -858,8 +851,8 @@
    Properties:
    @@ -955,7 +948,7 @@
    Properties:
    @@ -991,8 +984,8 @@
    Properties:
    @@ -1088,7 +1081,7 @@
    Properties:
    @@ -1124,8 +1117,8 @@
    Properties:
    @@ -1145,8 +1138,8 @@
    Properties:

    -
    -

    <readonly> uuid

    +
    +

    <readonly> type

    @@ -1185,7 +1178,7 @@
    Properties:
    - uuid + type @@ -1202,7 +1195,7 @@
    Properties:
    -

    UUID of the hub

    +

    Hub type

    @@ -1221,7 +1214,7 @@
    Properties:
    @@ -1230,7 +1223,7 @@
    Properties:
    Overrides:
    @@ -1257,8 +1250,8 @@
    Properties:
    @@ -1278,8 +1271,8 @@
    Properties:

    -
    -

    <readonly> voltage

    +
    +

    <readonly> uuid

    @@ -1318,13 +1311,13 @@
    Properties:
    - voltage + uuid -number +string @@ -1335,7 +1328,7 @@
    Properties:
    -

    Voltage of the hub (Volts)

    +

    UUID of the hub

    @@ -1354,7 +1347,7 @@
    Properties:
    @@ -1363,7 +1356,7 @@
    Properties:
    Overrides:
    @@ -1390,8 +1383,8 @@
    Properties:
    @@ -1486,8 +1479,8 @@

    connect()

    @@ -1544,7 +1537,7 @@
    Returns:

    -

    disconnect()

    +

    createVirtualPort(firstPortName, secondPortName)

    @@ -1552,7 +1545,8 @@

    disconnect(
    -

    Disconnect the Hub.

    +

    Combines two ports with into a single virtual port.

    +

    Note: The devices attached to the ports must be of the same device type.

    @@ -1562,6 +1556,80 @@

    disconnect( +

    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    firstPortName + + +string + + + + +

    First port name

    secondPortName + + +string + + + + +

    Second port name

    + +
    @@ -1576,7 +1644,7 @@

    disconnect(
    @@ -1585,7 +1653,7 @@

    disconnect(
    Overrides:
    @@ -1612,8 +1680,8 @@

    disconnect(
    @@ -1642,7 +1710,7 @@

    Returns:
    -

    Resolved upon successful disconnect.

    +

    Resolved upon successful issuance of command.

    @@ -1670,7 +1738,7 @@
    Returns:

    -

    getHubType()

    +

    disconnect()

    @@ -1678,7 +1746,7 @@

    getHubType(
    -

    Get the hub type.

    +

    Disconnect the Hub.

    @@ -1702,17 +1770,12 @@

    getHubType(
    - -
    Overrides:
    -
    @@ -1738,8 +1801,8 @@

    getHubType(
    @@ -1767,6 +1830,10 @@

    getHubType(

    Returns:
    +
    +

    Resolved upon successful disconnect.

    +
    +
    @@ -1775,7 +1842,7 @@
    Returns:
    -HubType +Promise @@ -1792,7 +1859,7 @@
    Returns:

    -

    getPortDeviceType(port)

    +

    getDeviceAtPort(portName)

    @@ -1800,7 +1867,7 @@

    getP
    -

    Get the device type for a given port.

    +

    Retrieves the device attached to a given port.

    @@ -1835,7 +1902,7 @@

    Parameters:
    - port + portName @@ -1852,7 +1919,7 @@
    Parameters:
    - +

    The name of the port to retrieve the device from.

    @@ -1874,17 +1941,12 @@
    Parameters:
    - -
    Overrides:
    -
    @@ -1910,8 +1972,8 @@
    Parameters:
    @@ -1939,6 +2001,10 @@
    Parameters:
    Returns:
    +
    +

    The device attached to the port.

    +
    +
    @@ -1947,7 +2013,10 @@
    Returns:
    -DeviceType +Device +| + +undefined @@ -1964,7 +2033,7 @@
    Returns:

    -

    setLEDColor(color)

    +

    getDevices()

    @@ -1972,7 +2041,7 @@

    setLEDColo
    -

    Set the color of the LED on the Hub via a color value.

    +

    Retrieves all attached devices.

    @@ -1982,56 +2051,6 @@

    setLEDColo -

    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    color - - -Color - - - - -
    - -
    @@ -2046,7 +2065,7 @@
    Parameters:
    @@ -2077,8 +2096,8 @@
    Parameters:
    @@ -2107,7 +2126,7 @@
    Returns:
    -

    Resolved upon successful issuance of command.

    +

    Array of all attached devices.

    @@ -2118,7 +2137,7 @@
    Returns:
    -Promise +Array.<Device> @@ -2135,7 +2154,7 @@
    Returns:

    -

    setLEDRGB(red, green, blue)

    +

    getDevicesByType(deviceType)

    @@ -2143,7 +2162,7 @@

    setLEDRGB(re
    -

    Set the color of the LED on the Hub via RGB values.

    +

    Retrieves an array of devices of the specified type.

    @@ -2178,55 +2197,7 @@

    Parameters:
    - red - - - - - -number - - - - - - - - - - - - - - - - - - green - - - - - -number - - - - - - - - - - - - - - - - - - blue + deviceType @@ -2243,7 +2214,7 @@
    Parameters:
    - +

    The device type to lookup.

    @@ -2265,7 +2236,7 @@
    Parameters:
    @@ -2296,8 +2267,8 @@
    Parameters:
    @@ -2326,7 +2297,7 @@
    Returns:
    -

    Resolved upon successful issuance of command.

    +

    Array of all devices of the specified type.

    @@ -2337,7 +2308,7 @@
    Returns:
    -Promise +Array.<Device> @@ -2442,6 +2413,11 @@
    Parameters:
    + +
    Overrides:
    +
    @@ -2467,8 +2443,8 @@
    Parameters:
    @@ -2563,6 +2539,11 @@

    shutdown() + +
    Overrides:
    +
    @@ -2588,8 +2569,8 @@

    shutdown()

    @@ -2655,7 +2636,7 @@

    sleep(delay)

    Sleep a given amount of time.

    -

    This is a helper method to make it easier to add delays into a chain of commands.

    +

    Note: This is a helper method to make it easier to add delays into a chain of commands.

    @@ -2735,11 +2716,6 @@
    Parameters:
    - -
    Overrides:
    -
    @@ -2765,8 +2741,8 @@
    Parameters:
    @@ -2823,7 +2799,7 @@
    Returns:

    -

    subscribe(port [, mode])

    +

    wait(commands)

    @@ -2831,7 +2807,8 @@

    subscribe(po
    -

    Subscribe to sensor notifications on a given port.

    +

    Wait until a given list of concurrently running commands are complete.

    +

    Note: This is a helper method to make it easier to wait for concurrent commands to complete.

    @@ -2854,8 +2831,6 @@

    Parameters:
    Type - Argument - @@ -2868,45 +2843,13 @@
    Parameters:
    - port - - - - - -string - - - - - - - - - - - - - - - - - - - - - - - - - - mode + commands -number +Array.<Promise.<any>> @@ -2914,20 +2857,10 @@
    Parameters:
    - - - <optional>
    - - - - - - - -

    The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.

    +

    Array of executing commands.

    @@ -2949,17 +2882,12 @@
    Parameters:
    - -
    Overrides:
    -
    @@ -2985,8 +2913,8 @@
    Parameters:
    @@ -3015,7 +2943,7 @@
    Returns:
    -

    Resolved upon successful issuance of command.

    +

    Resolved after the commands are finished.

    @@ -3043,7 +2971,7 @@
    Returns:

    -

    unsubscribe(port)

    +

    waitForDeviceAtPort(portName)

    @@ -3051,7 +2979,8 @@

    unsubscrib
    -

    Unsubscribe to sensor notifications on a given port.

    +

    Retrieves the device attached to a given port, waiting until one is attached if there isn't one.

    +

    Note: If a device is never attached, the returned promise may never resolve.

    @@ -3086,7 +3015,7 @@

    Parameters:
    - port + portName @@ -3103,7 +3032,7 @@
    Parameters:
    - +

    The name of the port to retrieve the device from.

    @@ -3125,17 +3054,12 @@
    Parameters:
    - -
    Overrides:
    -
    @@ -3161,8 +3085,8 @@
    Parameters:
    @@ -3191,7 +3115,7 @@
    Returns:
    -

    Resolved upon successful issuance of command.

    +

    Resolved once a device is attached, or resolved immediately if a device is already attached.

    @@ -3219,7 +3143,7 @@
    Returns:

    -

    wait(commands)

    +

    waitForDeviceByType(deviceType)

    @@ -3227,8 +3151,8 @@

    wait(commands) -

    Wait until a given list of concurrently running commands are complete.

    -

    This is a helper method to make it easier to wait for concurrent commands to complete.

    +

    Retrieves the first device attached of the specified type, waiting until one is attached if there isn't one.

    +

    Note: If a device is never attached, the returned promise may never resolve.

    @@ -3263,13 +3187,13 @@

    Parameters:
    - commands + deviceType -Array.<Promise.<any>> +number @@ -3280,7 +3204,7 @@
    Parameters:
    -

    Array of executing commands.

    +

    The device type to lookup.

    @@ -3302,17 +3226,12 @@
    Parameters:
    - -
    Overrides:
    -
    @@ -3338,8 +3257,8 @@
    Parameters:
    @@ -3368,7 +3287,7 @@
    Returns:
    -

    Resolved after the commands are finished.

    +

    Resolved once a device is attached, or resolved immediately if a device is already attached.

    @@ -3404,7 +3323,7 @@

    Events


    -

    accel

    +

    attach

    @@ -3412,7 +3331,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Emits when a device is attached to the Hub.

    @@ -3447,85 +3366,13 @@
    Parameters:
    - port - - - - - -string - - - - - - - - - - - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z + device -number +Device @@ -3558,7 +3405,7 @@
    Parameters:
    @@ -3589,8 +3436,8 @@
    Parameters:
    @@ -3623,7 +3470,7 @@
    Parameters:

    -

    attach

    +

    button

    @@ -3631,7 +3478,7 @@

    attach

    -

    Emits when a motor or sensor is attached to the Hub.

    +

    Emits when a button is pressed.

    @@ -3666,7 +3513,7 @@
    Parameters:
    - port + button @@ -3690,13 +3537,13 @@
    Parameters:
    - type + state -DeviceType +ButtonState @@ -3729,7 +3576,7 @@
    Parameters:
    @@ -3738,7 +3585,7 @@
    Parameters:
    Overrides:
    @@ -3765,8 +3612,8 @@
    Parameters:
    @@ -3799,7 +3646,7 @@
    Parameters:

    -

    button

    +

    detach

    @@ -3807,7 +3654,7 @@

    button

    -

    Emits when a button is pressed.

    +

    Emits when a device is detached from the Hub.

    @@ -3842,37 +3689,13 @@
    Parameters:
    - button - - - - - -string - - - - - - - - - - - - - - - - - - state + device -ButtonState +Device @@ -3905,7 +3728,7 @@
    Parameters:
    @@ -3936,8 +3759,8 @@
    Parameters:
    @@ -3970,7 +3793,7 @@
    Parameters:

    -

    color

    +

    disconnect

    @@ -3978,7 +3801,7 @@

    color

    -

    Emits when a color sensor is activated.

    +

    Emits when the hub is disconnected.

    @@ -3988,80 +3811,6 @@

    color

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    color - - -Color - - - - -
    - -
    @@ -4076,1578 +3825,7 @@
    Parameters:
    -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - - - - - -
    -
    -

    colorAndDistance

    - - -
    -
    - - -
    -

    A combined color and distance event, emits when the sensor is activated.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    color - - -Color - - - - -
    distance - - -number - - - - -

    Distance, in millimeters.

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    detach

    - - -
    -
    - - -
    -

    Emits when an attached motor or sensor is detached from the Hub.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - -
    Overrides:
    -
    - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    disconnect

    - - -
    -
    - - -
    -

    Emits when the hub is disconnected.

    -
    - - - - - - - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - -
    Overrides:
    -
    - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    distance

    - - -
    -
    - - -
    -

    Emits when a distance sensor is activated.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    distance - - -number - - - - -

    Distance, in millimeters.

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    gyro

    - - -
    -
    - - -
    -

    Emits when gyroscope detects movement. Measured in DPS - degrees per second.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    x - - -number - - - - -
    y - - -number - - - - -
    z - - -number - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    rotate

    - - -
    -
    - - -
    -

    Emits when a rotation sensor is activated.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    rotation - - -number - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    speed

    - - -
    -
    - - -
    -

    Emits on a speed change.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -
    speed - - -number - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    temp

    - - -
    -
    - - -
    -

    Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -

    For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.

    temp - - -number - - - - -
    - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - - - - -
    - - - -
    -
    -

    tilt

    - - -
    -
    - - -
    -

    Emits when a tilt sensor is activated.

    -
    - - - - - - - - -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    port - - -string - - - - -

    If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.

    x - - -number - - - - -
    y - - -number - - - - -
    z - - -number - - - - -

    (Only available when using a Control+ Hub)

    - - - - -
    - - - - - - - -
    Inherited From:
    -
    -
    @@ -5678,8 +3856,8 @@
    Parameters:
    @@ -5758,7 +3936,7 @@ Documentation generated by JSDoc 3.6.3 - on Tue Dec 3rd 2019 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/PiezoBuzzer.html b/docs/PiezoBuzzer.html new file mode 100644 index 00000000..4e69e3dd --- /dev/null +++ b/docs/PiezoBuzzer.html @@ -0,0 +1,1306 @@ + + + + + + + node-poweredup Class: PiezoBuzzer + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: PiezoBuzzer

    +
    + +
    + +

    + PiezoBuzzer +

    + + +
    + + +
    +
    + + +
    +
    +

    new PiezoBuzzer()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    playTone(frequency, time)

    + + +
    +
    + + +
    +

    Play a tone on the Hub's in-built buzzer

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    frequency + + +number + + + + +
    time + + +number + + + + +

    How long the tone should play for (in milliseconds).

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the tone has finished playing).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/RemoteControl.html b/docs/RemoteControl.html index 21f65a42..18723317 100644 --- a/docs/RemoteControl.html +++ b/docs/RemoteControl.html @@ -33,14 +33,14 @@ @@ -513,7 +513,7 @@
    Properties:
    - firmwareVersion + hardwareVersion @@ -1411,7 +1411,7 @@

    Methods


    -

    setName(name)

    +

    createVirtualPort(firstPortName, secondPortName)

    @@ -1419,7 +1419,8 @@

    setName(name)<
    -

    Set the name of the Hub.

    +

    Combines two ports with into a single virtual port.

    +

    Note: The devices attached to the ports must be of the same device type.

    @@ -1454,7 +1455,7 @@

    Parameters:
    - name + firstPortName @@ -1471,135 +1472,38 @@
    Parameters:
    -

    New name of the hub (14 characters or less, ASCII only).

    +

    First port name

    - - - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - -
    Returns:
    - + + + secondPortName -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - + + + +string -
    -
    - - - -
    + + - -
    -
    -

    shutdown()

    - - -
    -
    - -
    -

    Shutdown the Hub.

    -
    - + - +

    Second port name

    + + + -
    @@ -1614,7 +1518,7 @@

    shutdown()

    @@ -1646,7 +1550,7 @@

    shutdown()
  • hubs/lpf2hub.js, - line 44 + line 88
  • @@ -1675,7 +1579,7 @@

    Returns:
    -

    Resolved upon successful disconnect.

    +

    Resolved upon successful issuance of command.

    @@ -1699,19 +1603,11 @@
    Returns:
    -
    - - - - - -

    Events

    - -
    +
    -

    accel

    +

    setName(name)

    @@ -1719,7 +1615,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Set the name of the Hub.

    @@ -1754,7 +1650,7 @@
    Parameters:
    - port + name @@ -1771,79 +1667,7 @@
    Parameters:
    - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z - - - - - -number - - - - - - - - - - - +

    New name of the hub (14 characters or less, ASCII only).

    @@ -1865,7 +1689,7 @@
    Parameters:
    @@ -1896,8 +1720,8 @@
    Parameters:
    @@ -1922,6 +1746,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful issuance of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -1930,7 +1778,7 @@
    Parameters:

    -

    button

    +

    shutdown()

    @@ -1938,7 +1786,7 @@

    button

    -

    Emits when a button is pressed.

    +

    Shutdown the Hub.

    @@ -1948,80 +1796,6 @@

    button

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    button - - -string - - - - -
    state - - -ButtonState - - - - -
    - -
    @@ -2036,7 +1810,7 @@
    Parameters:
    @@ -2068,7 +1842,7 @@
    Parameters:
    @@ -2093,15 +1867,47 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful disconnect.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + - +
    + + + + + +

    Events

    + +

    -

    tilt

    +

    button

    @@ -2109,7 +1915,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a button is pressed.

    @@ -2144,13 +1950,13 @@
    Parameters:
    - x + button -number +string @@ -2168,13 +1974,13 @@
    Parameters:
    - y + state -number +ButtonState @@ -2207,7 +2013,7 @@
    Parameters:
    @@ -2238,8 +2044,8 @@
    Parameters:
    @@ -2318,7 +2124,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/RemoteControlButton.html b/docs/RemoteControlButton.html new file mode 100644 index 00000000..0e540b1c --- /dev/null +++ b/docs/RemoteControlButton.html @@ -0,0 +1,1264 @@ + + + + + + + node-poweredup Class: RemoteControlButton + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: RemoteControlButton

    +
    + +
    + +

    + RemoteControlButton +

    + + +
    + + +
    +
    + + +
    +
    +

    new RemoteControlButton()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    button

    + + +
    +
    + + +
    +

    Emits when a button on the remote is pressed or released.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    event + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/SimpleMediumLinearMotor.html b/docs/SimpleMediumLinearMotor.html new file mode 100644 index 00000000..ddd20056 --- /dev/null +++ b/docs/SimpleMediumLinearMotor.html @@ -0,0 +1,1116 @@ + + + + + + + node-poweredup Class: SimpleMediumLinearMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: SimpleMediumLinearMotor

    +
    + +
    + +

    + SimpleMediumLinearMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new SimpleMediumLinearMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TachoMotor.html b/docs/TachoMotor.html new file mode 100644 index 00000000..c4301ef9 --- /dev/null +++ b/docs/TachoMotor.html @@ -0,0 +1,2419 @@ + + + + + + + node-poweredup Class: TachoMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TachoMotor

    +
    + +
    + +

    + TachoMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TachoMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    brake()

    + + +
    +
    + + +
    +

    Brake the motor.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rampPower(fromPower, toPower, time)

    + + +
    +
    + + +
    +

    Ramp the motor power.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    fromPower + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    toPower + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    time + + +number + + + + +

    How long the ramp should last (in milliseconds).

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rotateByDegrees(degrees [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given amount of degrees.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    degrees + + +number + + + + + + + + + + + + +

    How much the motor should be rotated (in degrees).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrakingStyle(style)

    + + +
    +
    + + +
    +

    Set the braking style of the motor.

    +

    Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    style + + +number + + + + +

    Either BRAKE or HOLD

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    setPower(power)

    + + +
    +
    + + +
    +

    Set the motor power.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    power + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setSpeed(speed)

    + + +
    +
    + + +
    +

    Set the motor speed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    speed + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    stop()

    + + +
    +
    + + +
    +

    Stop the motor.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicColorSensor.html b/docs/TechnicColorSensor.html new file mode 100644 index 00000000..fff63e9b --- /dev/null +++ b/docs/TechnicColorSensor.html @@ -0,0 +1,1552 @@ + + + + + + + node-poweredup Class: TechnicColorSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicColorSensor

    +
    + +
    + +

    + TechnicColorSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicColorSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    ambient

    + + +
    +
    + + +
    +

    Emits when the ambient light changes.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    ambient + + +number + + + + +

    Percentage, from 0 to 100.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    color

    + + +
    +
    + + +
    +

    Emits when a color sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    color + + +Color + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    reflect

    + + +
    +
    + + +
    +

    Emits when the light reflectivity changes.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    reflect + + +number + + + + +

    Percentage, from 0 to 100.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicDistanceSensor.html b/docs/TechnicDistanceSensor.html new file mode 100644 index 00000000..611d3ca3 --- /dev/null +++ b/docs/TechnicDistanceSensor.html @@ -0,0 +1,1646 @@ + + + + + + + node-poweredup Class: TechnicDistanceSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicDistanceSensor

    +
    + +
    + +

    + TechnicDistanceSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicDistanceSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    setBrightness(topLeft, bottomLeft, topRight, bottomRight)

    + + +
    +
    + + +
    +

    Set the brightness (or turn on/off) the lights around the eyes.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    topLeft + + +number + + + + +

    Top left quadrant (above left eye). 0-100 brightness.

    bottomLeft + + +number + + + + +

    Bottom left quadrant (below left eye). 0-100 brightness.

    topRight + + +number + + + + +

    Top right quadrant (above right eye). 0-100 brightness.

    bottomRight + + +number + + + + +

    Bottom right quadrant (below right eye). 0-100 brightness.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    distance

    + + +
    +
    + + +
    +

    Emits when the detected distance changes (Slow sampling covers 40mm to 2500mm).

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    distance + + +number + + + + +

    Distance, from 40 to 2500mm

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    fastDistance

    + + +
    +
    + + +
    +

    Emits when the detected distance changes (Fast sampling covers 50mm to 320mm).

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    fastDistance + + +number + + + + +

    Distance, from 50 to 320mm

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicForceSensor.html b/docs/TechnicForceSensor.html new file mode 100644 index 00000000..18b6c57b --- /dev/null +++ b/docs/TechnicForceSensor.html @@ -0,0 +1,1552 @@ + + + + + + + node-poweredup Class: TechnicForceSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicForceSensor

    +
    + +
    + +

    + TechnicForceSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicForceSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    force

    + + +
    +
    + + +
    +

    Emits when force is applied.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    force + + +number + + + + +

    Force, in newtons (0-10).

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    tapped

    + + +
    +
    + + +
    +

    Emits when the sensor is tapped.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    tapped + + +number + + + + +

    How hard the sensor was tapped, from 0-3.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    touch

    + + +
    +
    + + +
    +

    Emits when the sensor is touched.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    touch + + +boolean + + + + +

    Touched on/off (boolean).

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicLargeAngularMotor.html b/docs/TechnicLargeAngularMotor.html new file mode 100644 index 00000000..8e6d4e87 --- /dev/null +++ b/docs/TechnicLargeAngularMotor.html @@ -0,0 +1,1431 @@ + + + + + + + node-poweredup Class: TechnicLargeAngularMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicLargeAngularMotor

    +
    + +
    + +

    + TechnicLargeAngularMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicLargeAngularMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + + + +

    Methods

    + +
    + +
    +
    +

    gotoAngle(angle [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given angle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    angle + + +number + + + + + + + + + + + + +

    Absolute position the motor should go to (degrees from 0).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rotateByDegrees(degrees [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given amount of degrees.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    degrees + + +number + + + + + + + + + + + + +

    How much the motor should be rotated (in degrees).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrakingStyle(style)

    + + +
    +
    + + +
    +

    Set the braking style of the motor.

    +

    Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    style + + +number + + + + +

    Either BRAKE or HOLD

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    setSpeed(speed)

    + + +
    +
    + + +
    +

    Set the motor speed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    speed + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    absolute

    + + +
    +
    + + +
    +

    Emits when a the motors absolute position is changed.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    absolute + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicLargeLinearMotor.html b/docs/TechnicLargeLinearMotor.html new file mode 100644 index 00000000..da906765 --- /dev/null +++ b/docs/TechnicLargeLinearMotor.html @@ -0,0 +1,1431 @@ + + + + + + + node-poweredup Class: TechnicLargeLinearMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicLargeLinearMotor

    +
    + +
    + +

    + TechnicLargeLinearMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicLargeLinearMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + + + +

    Methods

    + +
    + +
    +
    +

    gotoAngle(angle [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given angle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    angle + + +number + + + + + + + + + + + + +

    Absolute position the motor should go to (degrees from 0).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rotateByDegrees(degrees [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given amount of degrees.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    degrees + + +number + + + + + + + + + + + + +

    How much the motor should be rotated (in degrees).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrakingStyle(style)

    + + +
    +
    + + +
    +

    Set the braking style of the motor.

    +

    Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    style + + +number + + + + +

    Either BRAKE or HOLD

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    setSpeed(speed)

    + + +
    +
    + + +
    +

    Set the motor speed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    speed + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    absolute

    + + +
    +
    + + +
    +

    Emits when a the motors absolute position is changed.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    absolute + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicMediumAngularMotor.html b/docs/TechnicMediumAngularMotor.html new file mode 100644 index 00000000..103dd324 --- /dev/null +++ b/docs/TechnicMediumAngularMotor.html @@ -0,0 +1,1431 @@ + + + + + + + node-poweredup Class: TechnicMediumAngularMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicMediumAngularMotor

    +
    + +
    + +

    + TechnicMediumAngularMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicMediumAngularMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + + + +

    Methods

    + +
    + +
    +
    +

    gotoAngle(angle [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given angle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    angle + + +number + + + + + + + + + + + + +

    Absolute position the motor should go to (degrees from 0).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rotateByDegrees(degrees [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given amount of degrees.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    degrees + + +number + + + + + + + + + + + + +

    How much the motor should be rotated (in degrees).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrakingStyle(style)

    + + +
    +
    + + +
    +

    Set the braking style of the motor.

    +

    Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    style + + +number + + + + +

    Either BRAKE or HOLD

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    setSpeed(speed)

    + + +
    +
    + + +
    +

    Set the motor speed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    speed + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    absolute

    + + +
    +
    + + +
    +

    Emits when a the motors absolute position is changed.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    absolute + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicMediumHub.html b/docs/TechnicMediumHub.html index 9c221df6..777a1cb0 100644 --- a/docs/TechnicMediumHub.html +++ b/docs/TechnicMediumHub.html @@ -33,14 +33,14 @@ @@ -513,7 +513,7 @@
    Properties:
    - firmwareVersion + hardwareVersion @@ -1411,7 +1411,7 @@

    Methods


    -

    setName(name)

    +

    createVirtualPort(firstPortName, secondPortName)

    @@ -1419,7 +1419,8 @@

    setName(name)<
    -

    Set the name of the Hub.

    +

    Combines two ports with into a single virtual port.

    +

    Note: The devices attached to the ports must be of the same device type.

    @@ -1454,7 +1455,7 @@

    Parameters:
    - name + firstPortName @@ -1471,135 +1472,38 @@
    Parameters:
    -

    New name of the hub (14 characters or less, ASCII only).

    +

    First port name

    - - - - - - -
    - - - - - - - -
    Inherited From:
    -
    - -
    - - - - - - - - - - - - - - - - - - - - - - - -
    Source:
    -
    - -
    - - - - - - - -
    - - - - - - - - - - - - -
    Returns:
    - + + + secondPortName -
    -

    Resolved upon successful issuance of command.

    -
    - - - -
    -
    - Type -
    -
    - -Promise - + + + +string -
    -
    - - - - + + - -
    -
    -

    shutdown()

    - - -
    -
    - -
    -

    Shutdown the Hub.

    -
    - + - +

    Second port name

    + + + -
    @@ -1614,7 +1518,7 @@

    shutdown()

    @@ -1646,7 +1550,7 @@

    shutdown()
  • hubs/lpf2hub.js, - line 44 + line 88
  • @@ -1675,7 +1579,7 @@

    Returns:
    -

    Resolved upon successful disconnect.

    +

    Resolved upon successful issuance of command.

    @@ -1699,19 +1603,11 @@
    Returns:
    -
    - - - - - -

    Events

    - -
    +
    -

    accel

    +

    setName(name)

    @@ -1719,7 +1615,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Set the name of the Hub.

    @@ -1754,7 +1650,7 @@
    Parameters:
    - port + name @@ -1771,79 +1667,7 @@
    Parameters:
    - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z - - - - - -number - - - - - - - - - - - +

    New name of the hub (14 characters or less, ASCII only).

    @@ -1865,7 +1689,7 @@
    Parameters:
    @@ -1896,8 +1720,8 @@
    Parameters:
    @@ -1922,6 +1746,30 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful issuance of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + @@ -1930,7 +1778,7 @@
    Parameters:

    -

    button

    +

    shutdown()

    @@ -1938,7 +1786,7 @@

    button

    -

    Emits when a button is pressed.

    +

    Shutdown the Hub.

    @@ -1948,80 +1796,6 @@

    button

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    button - - -string - - - - -
    state - - -ButtonState - - - - -
    - -
    @@ -2036,7 +1810,7 @@
    Parameters:
    @@ -2068,7 +1842,7 @@
    Parameters:
    @@ -2093,15 +1867,47 @@
    Parameters:
    +
    Returns:
    + + +
    +

    Resolved upon successful disconnect.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + - +
    + + + + + +

    Events

    + +

    -

    tilt

    +

    button

    @@ -2109,7 +1915,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a button is pressed.

    @@ -2144,13 +1950,13 @@
    Parameters:
    - x + button -number +string @@ -2168,13 +1974,13 @@
    Parameters:
    - y + state -number +ButtonState @@ -2207,7 +2013,7 @@
    Parameters:
    @@ -2238,8 +2044,8 @@
    Parameters:
    @@ -2318,7 +2124,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/TechnicMediumHubAccelerometerSensor.html b/docs/TechnicMediumHubAccelerometerSensor.html new file mode 100644 index 00000000..8fcb059c --- /dev/null +++ b/docs/TechnicMediumHubAccelerometerSensor.html @@ -0,0 +1,1312 @@ + + + + + + + node-poweredup Class: TechnicMediumHubAccelerometerSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicMediumHubAccelerometerSensor

    +
    + +
    + +

    + TechnicMediumHubAccelerometerSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicMediumHubAccelerometerSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    accel

    + + +
    +
    + + +
    +

    Emits when accelerometer detects movement. Measured in mG.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    x + + +number + + + + +
    y + + +number + + + + +
    z + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicMediumHubGyroSensor.html b/docs/TechnicMediumHubGyroSensor.html new file mode 100644 index 00000000..dce01083 --- /dev/null +++ b/docs/TechnicMediumHubGyroSensor.html @@ -0,0 +1,1312 @@ + + + + + + + node-poweredup Class: TechnicMediumHubGyroSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicMediumHubGyroSensor

    +
    + +
    + +

    + TechnicMediumHubGyroSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicMediumHubGyroSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    gyro

    + + +
    +
    + + +
    +

    Emits when gyroscope detects movement. Measured in DPS - degrees per second.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    x + + +number + + + + +
    y + + +number + + + + +
    z + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicMediumHubTiltSensor.html b/docs/TechnicMediumHubTiltSensor.html new file mode 100644 index 00000000..507708fb --- /dev/null +++ b/docs/TechnicMediumHubTiltSensor.html @@ -0,0 +1,1312 @@ + + + + + + + node-poweredup Class: TechnicMediumHubTiltSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicMediumHubTiltSensor

    +
    + +
    + +

    + TechnicMediumHubTiltSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicMediumHubTiltSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    tilt

    + + +
    +
    + + +
    +

    Emits when a tilt sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    x + + +number + + + + +
    y + + +number + + + + +
    z + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TechnicXLargeLinearMotor.html b/docs/TechnicXLargeLinearMotor.html new file mode 100644 index 00000000..55636fbf --- /dev/null +++ b/docs/TechnicXLargeLinearMotor.html @@ -0,0 +1,1431 @@ + + + + + + + node-poweredup Class: TechnicXLargeLinearMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TechnicXLargeLinearMotor

    +
    + +
    + +

    + TechnicXLargeLinearMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TechnicXLargeLinearMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + + + +

    Methods

    + +
    + +
    +
    +

    gotoAngle(angle [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given angle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    angle + + +number + + + + + + + + + + + + +

    Absolute position the motor should go to (degrees from 0).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rotateByDegrees(degrees [, speed])

    + + +
    +
    + + +
    +

    Rotate a motor by a given amount of degrees.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeArgumentDefaultDescription
    degrees + + +number + + + + + + + + + + + + +

    How much the motor should be rotated (in degrees).

    speed + + +number + + + + + + + <optional>
    + + + + + +
    + + 100 + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command (ie. once the motor is finished).

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setBrakingStyle(style)

    + + +
    +
    + + +
    +

    Set the braking style of the motor.

    +

    Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    style + + +number + + + + +

    Either BRAKE or HOLD

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    setSpeed(speed)

    + + +
    +
    + + +
    +

    Set the motor speed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    speed + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +

    Events

    + +
    + +
    +
    +

    absolute

    + + +
    +
    + + +
    +

    Emits when a the motors absolute position is changed.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    absolute + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TiltSensor.html b/docs/TiltSensor.html new file mode 100644 index 00000000..f079ef9b --- /dev/null +++ b/docs/TiltSensor.html @@ -0,0 +1,1288 @@ + + + + + + + node-poweredup Class: TiltSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TiltSensor

    +
    + +
    + +

    + TiltSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TiltSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    tilt

    + + +
    +
    + + +
    +

    Emits when a tilt sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    x + + +number + + + + +
    y + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/TrainMotor.html b/docs/TrainMotor.html new file mode 100644 index 00000000..a3c62cc5 --- /dev/null +++ b/docs/TrainMotor.html @@ -0,0 +1,1752 @@ + + + + + + + node-poweredup Class: TrainMotor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: TrainMotor

    +
    + +
    + +

    + TrainMotor +

    + + +
    + + +
    +
    + + +
    +
    +

    new TrainMotor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + +

    Methods

    + +
    + +
    +
    +

    brake()

    + + +
    +
    + + +
    +

    Brake the motor.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    rampPower(fromPower, toPower, time)

    + + +
    +
    + + +
    +

    Ramp the motor power.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    fromPower + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    toPower + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    time + + +number + + + + +

    How long the ramp should last (in milliseconds).

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful completion of command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    setPower(power)

    + + +
    +
    + + +
    +

    Set the motor power.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    power + + +number + + + + +

    For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.

    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + + + +
    +
    +

    stop()

    + + +
    +
    + + +
    +

    Stop the motor.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + +
    Returns:
    + + +
    +

    Resolved upon successful issuance of the command.

    +
    + + + +
    +
    + Type +
    +
    + +Promise + + + +
    +
    + + + + + +
    + +
    + + + + + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/VoltageSensor.html b/docs/VoltageSensor.html new file mode 100644 index 00000000..33779a0f --- /dev/null +++ b/docs/VoltageSensor.html @@ -0,0 +1,1264 @@ + + + + + + + node-poweredup Class: VoltageSensor + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Class: VoltageSensor

    +
    + +
    + +

    + VoltageSensor +

    + + +
    + + +
    +
    + + +
    +
    +

    new VoltageSensor()

    + + +
    +
    + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + +
    + + +

    Extends

    + + + + + + + + + + + + + + + + + + +

    Members

    + +
    + +
    +
    +

    <readonly> connected

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    connected + + +boolean + + + + +

    Check if the device is still attached.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> hub

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    hub + + +Hub + + + + +

    The Hub the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> isVirtualPort

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    isVirtualPort + + +boolean + + + + +

    Is this device attached to a virtual port (ie. a combined device)

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> mode

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    mode + + +number + + + + +

    The mode the device is currently in

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> portName

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    portName + + +string + + + + +

    The port the device is attached to.

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + + + +
    +
    +

    <readonly> type

    + + +
    +
    + + + + + +
    + + +
    Properties:
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    type + + +number + + + + +

    The type of the device

    +
    + + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + +
    + +
    + + + + + + + +

    Events

    + +
    + +
    +
    +

    voltage

    + + +
    +
    + + +
    +

    Emits when a voltage change is detected.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    voltage + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + +
    + +
    + +
    + + + + +
    +
    + +
    + + +
    + +
    + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/WeDo2SmartHub.html b/docs/WeDo2SmartHub.html index 5479d686..b4f195f1 100644 --- a/docs/WeDo2SmartHub.html +++ b/docs/WeDo2SmartHub.html @@ -33,14 +33,14 @@ @@ -501,7 +501,7 @@
    Properties:
    - firmwareVersion + hardwareVersion @@ -1854,7 +1854,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/classes.list.html b/docs/classes.list.html index 5d56af0e..d5c59b75 100644 --- a/docs/classes.list.html +++ b/docs/classes.list.html @@ -33,14 +33,14 @@ @@ -142,27 +142,135 @@

    Classes

    +
    AbsoluteMotor
    +
    +
    BaseHub
    +
    BasicMotor
    +
    + +
    BoostMoveHub
    +
    + +
    ColorDistanceSensor
    +
    + +
    ControlPlusHub
    +
    + +
    CurrentSensor
    +
    + +
    Device
    +
    +
    DuploTrainBase
    +
    DuploTrainBaseColorSensor
    +
    + +
    DuploTrainBaseMotor
    +
    + +
    DuploTrainBaseSpeaker
    +
    + +
    DuploTraniBaseSpeedometer
    +
    +
    Hub
    +
    HubLED
    +
    + +
    Light
    +
    +
    LPF2Hub
    +
    MediumLinearMotor
    +
    + +
    MotionSensor
    +
    +
    MoveHub
    +
    MoveHubMediumLinearMotor
    +
    + +
    MoveHubTiltSensor
    +
    + +
    PiezoBuzzer
    +
    + +
    PUPHub
    +
    + +
    PUPRemote
    +
    +
    RemoteControl
    +
    RemoteControlButton
    +
    + +
    SimpleMediumLinearMotor
    +
    + +
    TachoMotor
    +
    + +
    TechnicColorSensor
    +
    + +
    TechnicDistanceSensor
    +
    + +
    TechnicForceSensor
    +
    + +
    TechnicLargeAngularMotor
    +
    + +
    TechnicLargeLinearMotor
    +
    + +
    TechnicMediumAngularMotor
    +
    +
    TechnicMediumHub
    +
    TechnicMediumHubAccelerometerSensor
    +
    + +
    TechnicMediumHubGyroSensor
    +
    + +
    TechnicMediumHubTiltSensor
    +
    + +
    TechnicXLargeLinearMotor
    +
    + +
    TiltSensor
    +
    + +
    TrainMotor
    +
    + +
    VoltageSensor
    +
    +
    WeDo2SmartHub
    @@ -200,6 +308,12 @@

    absolute

    +
    Type: +object + + +
    + @@ -290,7 +404,7 @@
    Parameters:
    @@ -323,7 +437,7 @@
    Parameters:

    -

    color

    +

    rotate

    @@ -331,13 +445,19 @@

    color

    -

    Emits when a color sensor is activated.

    +

    Emits when a rotation sensor is activated.

    +
    Type: +object + + +
    + @@ -366,13 +486,13 @@
    Parameters:
    - color + rotation -Color +number @@ -401,6 +521,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -427,8 +556,8 @@
    Parameters:
    @@ -461,7 +590,7 @@
    Parameters:

    -

    colorAndDistance

    +

    attach

    @@ -469,7 +598,7 @@

    colorAndDistance

    -

    A combined color and distance event, emits when the sensor is activated.

    +

    Emits when a device is attached to the Hub.

    @@ -504,13 +633,13 @@
    Parameters:
    - color + device -Color +Device @@ -525,30 +654,6 @@
    Parameters:
    - - - - distance - - - - - -number - - - - - - - - - - -

    Distance, in millimeters.

    - - - @@ -563,6 +668,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -589,8 +703,8 @@
    Parameters:
    @@ -623,7 +737,7 @@
    Parameters:

    -

    distance

    +

    button

    @@ -631,7 +745,7 @@

    distance

    -

    Emits when a distance sensor is activated.

    +

    Emits when a button is pressed.

    @@ -666,13 +780,13 @@
    Parameters:
    - distance + button -number +string @@ -683,7 +797,31 @@
    Parameters:
    -

    Distance, in millimeters.

    + + + + + + + + state + + + + + +ButtonState + + + + + + + + + + + @@ -701,6 +839,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -727,8 +874,8 @@
    Parameters:
    @@ -761,7 +908,7 @@
    Parameters:

    -

    current

    +

    detach

    @@ -769,7 +916,7 @@

    current

    -

    Emits when a current change is detected.

    +

    Emits when a device is detached from the Hub.

    @@ -804,13 +951,13 @@
    Parameters:
    - current + device -number +Device @@ -839,6 +986,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -865,8 +1021,8 @@
    Parameters:
    @@ -899,7 +1055,7 @@
    Parameters:

    -

    accel

    +

    disconnect

    @@ -907,7 +1063,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Emits when the hub is disconnected.

    @@ -917,110 +1073,141 @@

    accel

    -
    Parameters:
    - - - - - - - + +
    + -
    + - + - + +
    Inherited From:
    +
    + +
    + - - - + - - - - - + - + - + - + - - - - - - - + +
    Source:
    +
    + +
    + - - + - + - - + - - - - + - + +
    Type: +object - - +
    + - - + + +
    Parameters:
    + + +
    NameTypeDescription
    port - - -string + + + - -
    x - - -number + + + + - -
    y + + + + + + - -number +
    +
    +

    color

    + + +
    +
    + +
    +

    Emits when a color sensor is activated.

    +
    + + - -
    + + + + + + + + + + + + + + + + - + - + - + + @@ -1220,15 +1404,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -1255,8 +1430,8 @@
    Parameters:
    @@ -1289,7 +1464,7 @@
    Parameters:

    -

    tilt

    +

    distance

    @@ -1297,13 +1472,19 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a distance sensor is activated.

    +
    Type: +object + + +
    + @@ -1332,31 +1513,7 @@
    Parameters:
    - - - - - - - - - - - - - - - - - + + @@ -1391,15 +1548,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -1426,8 +1574,8 @@
    Parameters:
    @@ -1460,7 +1608,7 @@
    Parameters:

    -

    color

    +

    attach

    @@ -1468,7 +1616,7 @@

    color

    -

    Emits when a color sensor is activated.

    +

    Emits when a device is attached to the Hub.

    @@ -1503,13 +1651,13 @@
    Parameters:
    - + - + + + + + + + + + + + + + + + + + - + + +
    NameTypeDescription
    zcolor -number +Color @@ -1049,15 +1236,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -1084,8 +1262,8 @@
    Parameters:
    @@ -1118,7 +1296,7 @@
    Parameters:

    -

    button

    +

    colorAndDistance

    @@ -1126,13 +1304,19 @@

    button

    -

    Emits when a button is pressed.

    +

    A combined color and distance event, emits when the sensor is activated.

    +
    Type: +object + + +
    + @@ -1161,13 +1345,13 @@
    Parameters:
    buttoncolor -string +Color @@ -1185,13 +1369,13 @@
    Parameters:
    statedistance -ButtonState +number @@ -1202,7 +1386,7 @@
    Parameters:
    -

    Distance, in millimeters.

    x - - -number - - - - -
    ydistance @@ -1373,7 +1530,7 @@
    Parameters:
    -

    Distance, in millimeters.

    colordevice -Color +Device @@ -1538,6 +1686,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -1564,8 +1721,8 @@
    Parameters:
    @@ -1598,7 +1755,7 @@
    Parameters:

    -

    speed

    +

    button

    @@ -1606,7 +1763,7 @@

    speed

    -

    Emits on a speed change.

    +

    Emits when a button is pressed.

    @@ -1641,13 +1798,37 @@
    Parameters:
    speedbutton -number +string + + + + +
    state + + +ButtonState @@ -1676,6 +1857,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -1702,8 +1892,8 @@
    Parameters:
    @@ -1736,7 +1926,7 @@
    Parameters:

    -

    accel

    +

    detach

    @@ -1744,7 +1934,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Emits when a device is detached from the Hub.

    @@ -1779,13 +1969,13 @@
    Parameters:
    portdevice -string +Device @@ -1800,97 +1990,25 @@
    Parameters:
    - - - x - + - - - -number + +
    + + - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z - - - - - -number - - - - - - - - - - - - - - - - - - - - -
    - - - - - +
    Inherited From:
    @@ -1921,8 +2039,8 @@
    Parameters:
    @@ -1955,7 +2073,7 @@
    Parameters:

    -

    attach

    +

    disconnect

    @@ -1963,7 +2081,7 @@

    attach

    -

    Emits when a device is attached to the Hub.

    +

    Emits when the hub is disconnected.

    @@ -1973,56 +2091,6 @@

    attach

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    device - - -Device - - - - -
    - -
    @@ -2033,6 +2101,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -2060,7 +2137,7 @@
    Parameters:
    @@ -2093,7 +2170,7 @@
    Parameters:

    -

    attach

    +

    current

    @@ -2101,13 +2178,19 @@

    attach

    -

    Emits when a device is detached from the Hub.

    +

    Emits when a current change is detected.

    +
    Type: +object + + +
    + @@ -2136,13 +2219,13 @@
    Parameters:
    - device + current -Device +number @@ -2197,8 +2280,8 @@
    Parameters:
    @@ -2369,7 +2452,7 @@
    Parameters:
    @@ -2402,7 +2485,7 @@
    Parameters:

    -

    disconnect

    +

    color

    @@ -2410,15 +2493,71 @@

    disconnect

    -

    Emits when the hub is disconnected.

    +

    Emits when a color sensor is activated.

    +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    color + + +Color + + + + +
    + @@ -2456,8 +2595,8 @@

    disconnect

    @@ -2490,7 +2629,7 @@

    disconnect


    -

    tilt

    +

    speed

    @@ -2498,13 +2637,19 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits on a speed change.

    +
    Type: +object + + +
    + @@ -2533,31 +2678,7 @@
    Parameters:
    - x - - - - - -number - - - - - - - - - - - - - - - - - - y + speed @@ -2592,15 +2713,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -2627,8 +2739,8 @@
    Parameters:
    @@ -2661,7 +2773,7 @@
    Parameters:

    -

    accel

    +

    attach

    @@ -2669,7 +2781,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Emits when a device is attached to the Hub.

    @@ -2704,85 +2816,13 @@
    Parameters:
    - port + device -string - - - - - - - - - - - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z - - - - - -number +Device @@ -2837,8 +2877,8 @@
    Parameters:
    @@ -2973,6 +3013,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -3000,7 +3049,7 @@
    Parameters:
    @@ -3033,7 +3082,7 @@
    Parameters:

    -

    tilt

    +

    detach

    @@ -3041,7 +3090,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a device is detached from the Hub.

    @@ -3076,37 +3125,13 @@
    Parameters:
    - x - - - - - -number - - - - - - - - - - - - - - - - - - y + device -number +Device @@ -3161,8 +3186,8 @@
    Parameters:
    @@ -3195,7 +3220,7 @@
    Parameters:

    -

    distance

    +

    disconnect

    @@ -3203,7 +3228,7 @@

    distance

    -

    Emits when a distance sensor is activated.

    +

    Emits when the hub is disconnected.

    @@ -3213,56 +3238,6 @@

    distance

    -
    Parameters:
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    NameTypeDescription
    distance - - -number - - - - -

    Distance, in millimeters.

    - -
    @@ -3299,8 +3274,8 @@
    Parameters:
    @@ -3333,7 +3308,7 @@
    Parameters:

    -

    accel

    +

    button

    @@ -3341,7 +3316,7 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Emits when a button is pressed.

    @@ -3376,7 +3351,7 @@
    Parameters:
    - port + button @@ -3400,61 +3375,13 @@
    Parameters:
    - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z + state -number +ButtonState @@ -3483,15 +3410,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -3518,8 +3436,8 @@
    Parameters:
    @@ -3552,7 +3470,7 @@
    Parameters:

    -

    button

    +

    rotate

    @@ -3560,13 +3478,19 @@

    button

    -

    Emits when a button is pressed.

    +

    Emits when a rotation sensor is activated.

    +
    Type: +object + + +
    + @@ -3595,13 +3519,13 @@
    Parameters:
    - button + rotation -string +number @@ -3616,34 +3540,10 @@
    Parameters:
    + + - - - state - - - - - -ButtonState - - - - - - - - - - - - - - - - - - +
    @@ -3658,7 +3558,7 @@
    Parameters:
    @@ -3689,8 +3589,8 @@
    Parameters:
    @@ -3723,7 +3623,7 @@
    Parameters:

    -

    tilt

    +

    distance

    @@ -3731,13 +3631,19 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a distance sensor is activated.

    +
    Type: +object + + +
    + @@ -3766,31 +3672,7 @@
    Parameters:
    - x - - - - - -number - - - - - - - - - - - - - - - - - - y + distance @@ -3807,7 +3689,7 @@
    Parameters:
    - +

    Distance, in millimeters.

    @@ -3825,15 +3707,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -3860,8 +3733,8 @@
    Parameters:
    @@ -3894,7 +3767,7 @@
    Parameters:

    -

    tilt

    +

    button

    @@ -3902,7 +3775,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a button is pressed.

    @@ -3937,13 +3810,13 @@
    Parameters:
    - x + button -number +string @@ -3961,13 +3834,13 @@
    Parameters:
    - y + state -number +ButtonState @@ -3996,6 +3869,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -4022,8 +3904,8 @@
    Parameters:
    @@ -4056,7 +3938,7 @@
    Parameters:

    -

    accel

    +

    rotate

    @@ -4064,13 +3946,19 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Emits when a rotation sensor is activated.

    +
    Type: +object + + +
    + @@ -4099,79 +3987,7 @@
    Parameters:
    - port - - - - - -string - - - - - - - - - - - - - - - - - - x - - - - - -number - - - - - - - - - - - - - - - - - - y - - - - - -number - - - - - - - - - - - - - - - - - - z + rotation @@ -4210,7 +4026,7 @@
    Parameters:
    @@ -4241,8 +4057,8 @@
    Parameters:
    @@ -4275,7 +4091,7 @@
    Parameters:

    -

    button

    +

    tilt

    @@ -4283,13 +4099,19 @@

    button

    -

    Emits when a button is pressed.

    +

    Emits when a tilt sensor is activated.

    +
    Type: +object + + +
    + @@ -4318,13 +4140,13 @@
    Parameters:
    - button + x -string +number @@ -4342,13 +4164,13 @@
    Parameters:
    - state + y -ButtonState +number @@ -4377,15 +4199,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -4412,8 +4225,8 @@
    Parameters:
    @@ -4446,7 +4259,7 @@
    Parameters:

    -

    tilt

    +

    attach

    @@ -4454,7 +4267,7 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a device is attached to the Hub.

    @@ -4489,13 +4302,13 @@
    Parameters:
    - x + device -number +Device @@ -4510,10 +4323,2305 @@
    Parameters:
    + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + + + + + +
    +
    +

    button

    + + +
    +
    + + +
    +

    Emits when a button is pressed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    button + + +string + + + + +
    state + + +ButtonState + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + +
    Overrides:
    +
    + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    detach

    + + +
    +
    + + +
    +

    Emits when a device is detached from the Hub.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    device + + +Device + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    disconnect

    + + +
    +
    + + +
    +

    Emits when the hub is disconnected.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    attach

    + + +
    +
    + + +
    +

    Emits when a device is attached to the Hub.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    device + + +Device + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    button

    + + +
    +
    + + +
    +

    Emits when a button is pressed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    button + + +string + + + + +
    state + + +ButtonState + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + +
    Overrides:
    +
    + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    detach

    + + +
    +
    + + +
    +

    Emits when a device is detached from the Hub.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    device + + +Device + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    disconnect

    + + +
    +
    + + +
    +

    Emits when the hub is disconnected.

    +
    + + + + + + + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    button

    + + +
    +
    + + +
    +

    Emits when a button is pressed.

    +
    + + + + + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    button + + +string + + + + +
    state + + +ButtonState + + + + +
    + + + + +
    + + + + + + + +
    Inherited From:
    +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    button

    + + +
    +
    + + +
    +

    Emits when a button on the remote is pressed or released.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    event + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    rotate

    + + +
    +
    + + +
    +

    Emits when a rotation sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    rotation + + +number + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    ambient

    + + +
    +
    + + +
    +

    Emits when the ambient light changes.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    ambient + + +number + + + + +

    Percentage, from 0 to 100.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    color

    + + +
    +
    + + +
    +

    Emits when a color sensor is activated.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    color + + +Color + + + + +
    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    reflect

    + + +
    +
    + + +
    +

    Emits when the light reflectivity changes.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    reflect + + +number + + + + +

    Percentage, from 0 to 100.

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    distance

    + + +
    +
    + + +
    +

    Emits when the detected distance changes (Slow sampling covers 40mm to 2500mm).

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    distance + + +number + + + + +

    Distance, from 40 to 2500mm

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    fastDistance

    + + +
    +
    + + +
    +

    Emits when the detected distance changes (Fast sampling covers 50mm to 320mm).

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    fastDistance + + +number + + + + +

    Distance, from 50 to 320mm

    + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Source:
    +
    + +
    + + + + + + + +
    + + + + + + + + + + + + + + + +
    + + + +
    +
    +

    force

    + + +
    +
    + + +
    +

    Emits when force is applied.

    +
    + + + + + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + - + + @@ -4548,15 +6656,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -4583,8 +6682,8 @@
    Parameters:
    @@ -4617,7 +6716,7 @@
    Parameters:

    -

    button

    +

    tapped

    @@ -4625,13 +6724,19 @@

    button

    -

    Emits when a button on the remote is pressed or released.

    +

    Emits when the sensor is tapped.

    +
    Type: +object + + +
    + @@ -4660,7 +6765,7 @@
    Parameters:
    - + + @@ -4721,8 +6826,8 @@
    Parameters:
    @@ -4755,7 +6860,7 @@
    Parameters:

    -

    rotate

    +

    touch

    @@ -4763,13 +6868,19 @@

    rotate

    -

    Emits when a rotation sensor is activated.

    +

    Emits when the sensor is touched.

    +
    Type: +object + + +
    + @@ -4798,13 +6909,13 @@
    Parameters:
    - + + @@ -4859,8 +6970,8 @@
    Parameters:
    @@ -4893,7 +7004,7 @@
    Parameters:

    -

    from 0 to 100.

    +

    absolute

    @@ -4901,13 +7012,19 @@

    from 0 to 100.

    -

    Emits when the ambient light changes.

    +

    Emits when a the motors absolute position is changed.

    +
    Type: +object + + +
    + @@ -4936,7 +7053,7 @@
    Parameters:
    - + - + - + - + - + - + - + + + + + + + + + + + + + + + + +
    NameTypeDescription
    yforce @@ -4530,7 +6638,7 @@
    Parameters:
    -

    Force, in newtons (0-10).

    eventtapped @@ -4677,7 +6782,7 @@
    Parameters:
    -

    How hard the sensor was tapped, from 0-3.

    rotationtouch -number +boolean @@ -4815,7 +6926,7 @@
    Parameters:
    -

    Touched on/off (boolean).

    ambientabsolute @@ -4971,6 +7088,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -4997,8 +7123,8 @@
    Parameters:
    @@ -5031,7 +7157,7 @@
    Parameters:

    -

    color

    +

    rotate

    @@ -5039,13 +7165,19 @@

    color

    -

    Emits when a color sensor is activated.

    +

    Emits when a rotation sensor is activated.

    +
    Type: +object + + +
    + @@ -5074,13 +7206,13 @@
    Parameters:
    colorrotation -Color +number @@ -5109,6 +7241,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -5135,8 +7276,8 @@
    Parameters:
    @@ -5169,7 +7310,7 @@
    Parameters:

    -

    from 0 to 100.

    +

    absolute

    @@ -5177,13 +7318,19 @@

    from 0 to 100.

    -

    Emits when the light reflectivity changes.

    +

    Emits when a the motors absolute position is changed.

    +
    Type: +object + + +
    + @@ -5212,7 +7359,7 @@
    Parameters:
    reflectabsolute @@ -5247,6 +7394,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -5273,8 +7429,8 @@
    Parameters:
    @@ -5307,7 +7463,7 @@
    Parameters:

    -

    from 40 to 2500mm

    +

    rotate

    @@ -5315,13 +7471,19 @@

    from 40 to 2500mm

    -

    Emits when the detected distance changes (Slow sampling covers 40mm to 2500mm).

    +

    Emits when a rotation sensor is activated.

    +
    Type: +object + + +
    + @@ -5350,7 +7512,7 @@
    Parameters:
    distancerotation @@ -5385,6 +7547,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -5411,8 +7582,8 @@
    Parameters:
    @@ -5445,7 +7616,7 @@
    Parameters:

    -

    from 50 to 320mm

    +

    absolute

    @@ -5453,13 +7624,19 @@

    from 50 to 320mm

    -

    Emits when the detected distance changes (Fast sampling covers 50mm to 320mm).

    +

    Emits when a the motors absolute position is changed.

    +
    Type: +object + + +
    + @@ -5488,7 +7665,7 @@
    Parameters:
    fastDistanceabsolute @@ -5523,6 +7700,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -5549,8 +7735,8 @@
    Parameters:
    @@ -5583,7 +7769,7 @@
    Parameters:

    -

    touch Touched on/off (boolean).

    +

    rotate

    @@ -5591,13 +7777,19 @@

    touch Touched on/off (b
    -

    Emits when the sensor is touched.

    +

    Emits when a rotation sensor is activated.

    +

    Type: +object + + +
    + @@ -5626,13 +7818,13 @@
    Parameters:
    touchrotation -boolean +number @@ -5661,6 +7853,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -5687,8 +7888,8 @@
    Parameters:
    @@ -5721,7 +7922,7 @@
    Parameters:

    -

    in newtons (0-10).

    +

    button

    @@ -5729,7 +7930,7 @@

    in newtons (0-10).

    -

    Emits when force is applied.

    +

    Emits when a button is pressed.

    @@ -5764,13 +7965,37 @@
    Parameters:
    forcebutton -number +string + + + + +
    state + + +ButtonState @@ -5799,6 +8024,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -5825,8 +8059,8 @@
    Parameters:
    @@ -5859,7 +8093,7 @@
    Parameters:

    -

    from 0-3.

    +

    accel

    @@ -5867,42 +8101,96 @@

    from 0-3.

    -

    Emits when the sensor is tapped.

    +

    Emits when accelerometer detects movement. Measured in mG.

    - + +
    Type: +object + + +
    + + + + +
    Parameters:
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescription
    x + + +number + + + + +
    y + + +number - - -
    Parameters:
    - - - - - - - - + + - + - + - - - + + - - + - - - - - - - - - - - - - - - - @@ -6147,15 +8417,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -6182,8 +8443,8 @@
    Parameters:
    @@ -6216,7 +8477,7 @@
    Parameters:

    -

    button

    +

    tilt

    @@ -6224,13 +8485,19 @@

    button

    -

    Emits when a button is pressed.

    +

    Emits when a tilt sensor is activated.

    +
    Type: +object + + +
    + @@ -6259,13 +8526,13 @@
    Parameters:
    - + - + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - @@ -6896,8 +9109,8 @@
    Parameters:
    @@ -6945,6 +9158,12 @@

    voltage

    +
    Type: +object + + +
    + @@ -7035,7 +9254,7 @@
    Parameters:
    @@ -7276,7 +9495,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_absolutemotor.js.html b/docs/devices_absolutemotor.js.html index b326086a..e877cbab 100644 --- a/docs/devices_absolutemotor.js.html +++ b/docs/devices_absolutemotor.js.html @@ -33,14 +33,14 @@ @@ -89,6 +89,10 @@

    Source: devices/absolutemotor.js

    const tachomotor_1 = require("./tachomotor"); const Consts = __importStar(require("../consts")); const utils_1 = require("../utils"); +/** + * @class AbsoluteMotor + * @extends TachoMotor + */ class AbsoluteMotor extends tachomotor_1.TachoMotor { constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) { super(hub, portId, Object.assign({}, modeMap, exports.ModeMap), type); @@ -101,6 +105,7 @@

    Source: devices/absolutemotor.js

    /** * Emits when a the motors absolute position is changed. * @event AbsoluteMotor#absolute + * @type {object} * @param {number} absolute */ this.notify("absolute", { angle }); @@ -202,7 +207,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_basicmotor.js.html b/docs/devices_basicmotor.js.html index 5a8203b0..a3c3a8e9 100644 --- a/docs/devices_basicmotor.js.html +++ b/docs/devices_basicmotor.js.html @@ -33,14 +33,14 @@ @@ -89,6 +89,10 @@

    Source: devices/basicmotor.js

    const device_1 = require("./device"); const Consts = __importStar(require("../consts")); const utils_1 = require("../utils"); +/** + * @class BasicMotor + * @extends Device + */ class BasicMotor extends device_1.Device { constructor(hub, portId, modeMap, type = Consts.DeviceType.UNKNOWN) { super(hub, portId, modeMap, type); @@ -97,7 +101,7 @@

    Source: devices/basicmotor.js

    * Set the motor power. * @method BasicMotor#setPower * @param {number} power For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @returns {Promise} Resolved upon successful completion of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ setPower(power, interrupt = true) { if (interrupt) { @@ -129,7 +133,7 @@

    Source: devices/basicmotor.js

    /** * Stop the motor. * @method BasicMotor#stop - * @returns {Promise} Resolved upon successful completion of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ stop() { this.cancelEventTimer(); @@ -138,7 +142,7 @@

    Source: devices/basicmotor.js

    /** * Brake the motor. * @method BasicMotor#brake - * @returns {Promise} Resolved upon successful completion of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ brake() { this.cancelEventTimer(); @@ -191,7 +195,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_colordistancesensor.js.html b/docs/devices_colordistancesensor.js.html index ecbb8489..a78c7ffd 100644 --- a/docs/devices_colordistancesensor.js.html +++ b/docs/devices_colordistancesensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/colordistancesensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class ColorDistanceSensor + * @extends Device + */ class ColorDistanceSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR); @@ -101,6 +105,7 @@

    Source: devices/colordistancesensor.js

    /** * Emits when a color sensor is activated. * @event ColorDistanceSensor#color + * @type {object} * @param {Color} color */ this.notify("color", { color }); @@ -115,6 +120,7 @@

    Source: devices/colordistancesensor.js

    /** * Emits when a distance sensor is activated. * @event ColorDistanceSensor#distance + * @type {object} * @param {number} distance Distance, in millimeters. */ this.notify("distance", { distance }); @@ -133,6 +139,7 @@

    Source: devices/colordistancesensor.js

    /** * A combined color and distance event, emits when the sensor is activated. * @event ColorDistanceSensor#colorAndDistance + * @type {object} * @param {Color} color * @param {number} distance Distance, in millimeters. */ @@ -201,7 +208,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_currentsensor.js.html b/docs/devices_currentsensor.js.html index 1505e38a..bae46607 100644 --- a/docs/devices_currentsensor.js.html +++ b/docs/devices_currentsensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/currentsensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class CurrentSensor + * @extends Device + */ class CurrentSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.CURRENT_SENSOR); @@ -113,6 +117,7 @@

    Source: devices/currentsensor.js

    /** * Emits when a current change is detected. * @event CurrentSensor#current + * @type {object} * @param {number} current */ this.notify("current", { current }); @@ -181,7 +186,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_device.js.html b/docs/devices_device.js.html new file mode 100644 index 00000000..cba74fbc --- /dev/null +++ b/docs/devices_device.js.html @@ -0,0 +1,379 @@ + + + + + + + node-poweredup Source: devices/device.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/device.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const events_1 = require("events");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class Device
    + * @extends EventEmitter
    + */
    +class Device extends events_1.EventEmitter {
    +    constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) {
    +        super();
    +        this.autoSubscribe = true;
    +        this.values = {};
    +        this._busy = false;
    +        this._connected = true;
    +        this._modeMap = {};
    +        this._isVirtualPort = false;
    +        this._eventTimer = null;
    +        this._hub = hub;
    +        this._portId = portId;
    +        this._type = type;
    +        this._modeMap = modeMap;
    +        this._isWeDo2SmartHub = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB);
    +        this._isVirtualPort = this.hub.isPortVirtual(portId);
    +        const eventAttachListener = (event) => {
    +            if (event === "detach") {
    +                return;
    +            }
    +            if (this.autoSubscribe) {
    +                if (this._modeMap[event] !== undefined) {
    +                    this.subscribe(this._modeMap[event]);
    +                }
    +            }
    +        };
    +        const deviceDetachListener = (device) => {
    +            if (device.portId === this.portId) {
    +                this._connected = false;
    +                this.hub.removeListener("detach", deviceDetachListener);
    +                this.emit("detach");
    +            }
    +        };
    +        for (const event in this._modeMap) {
    +            if (this.hub.listenerCount(event) > 0) {
    +                eventAttachListener(event);
    +            }
    +        }
    +        this.hub.on("newListener", eventAttachListener);
    +        this.on("newListener", eventAttachListener);
    +        this.hub.on("detach", deviceDetachListener);
    +    }
    +    /**
    +     * @readonly
    +     * @property {boolean} connected Check if the device is still attached.
    +     */
    +    get connected() {
    +        return this._connected;
    +    }
    +    /**
    +     * @readonly
    +     * @property {Hub} hub The Hub the device is attached to.
    +     */
    +    get hub() {
    +        return this._hub;
    +    }
    +    get portId() {
    +        return this._portId;
    +    }
    +    /**
    +     * @readonly
    +     * @property {string} portName The port the device is attached to.
    +     */
    +    get portName() {
    +        return this.hub.getPortNameForPortId(this.portId);
    +    }
    +    /**
    +     * @readonly
    +     * @property {number} type The type of the device
    +     */
    +    get type() {
    +        return this._type;
    +    }
    +    get typeName() {
    +        return Consts.DeviceTypeNames[this.type];
    +    }
    +    /**
    +     * @readonly
    +     * @property {number} mode The mode the device is currently in
    +     */
    +    get mode() {
    +        return this._mode;
    +    }
    +    get isWeDo2SmartHub() {
    +        return this._isWeDo2SmartHub;
    +    }
    +    /**
    +     * @readonly
    +     * @property {boolean} isVirtualPort Is this device attached to a virtual port (ie. a combined device)
    +     */
    +    get isVirtualPort() {
    +        return this._isVirtualPort;
    +    }
    +    writeDirect(mode, data, callback) {
    +        if (this.isWeDo2SmartHub) {
    +            this.send(Buffer.concat([Buffer.from([this.portId, 0x01, 0x02]), data]), Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
    +        }
    +        else {
    +            this.send(Buffer.concat([Buffer.from([0x81, this.portId, 0x11, 0x51, mode]), data]), Consts.BLECharacteristic.LPF2_ALL, callback);
    +        }
    +    }
    +    send(data, characteristic = Consts.BLECharacteristic.LPF2_ALL, callback) {
    +        this._ensureConnected();
    +        this.hub.send(data, characteristic, callback);
    +    }
    +    subscribe(mode) {
    +        this._ensureConnected();
    +        if (mode !== this._mode) {
    +            this._mode = mode;
    +            this.hub.subscribe(this.portId, this.type, mode);
    +        }
    +    }
    +    unsubscribe(mode) {
    +        this._ensureConnected();
    +    }
    +    receive(message) {
    +        this.notify("receive", { message });
    +    }
    +    notify(event, values) {
    +        this.values[event] = values;
    +        this.emit(event, values);
    +        if (this.hub.listenerCount(event) > 0) {
    +            this.hub.emit(event, this, values);
    +        }
    +    }
    +    finish() {
    +        this._busy = false;
    +        if (this._finished) {
    +            this._finished();
    +            this._finished = undefined;
    +        }
    +    }
    +    setEventTimer(timer) {
    +        this._eventTimer = timer;
    +    }
    +    cancelEventTimer() {
    +        if (this._eventTimer) {
    +            clearTimeout(this._eventTimer);
    +            this._eventTimer = null;
    +        }
    +    }
    +    _ensureConnected() {
    +        if (!this.connected) {
    +            throw new Error("Device is not connected");
    +        }
    +    }
    +}
    +exports.Device = Device;
    +//# sourceMappingURL=device.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_duplotrainbasecolorsensor.js.html b/docs/devices_duplotrainbasecolorsensor.js.html index 8a4df3ad..44591a83 100644 --- a/docs/devices_duplotrainbasecolorsensor.js.html +++ b/docs/devices_duplotrainbasecolorsensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/duplotrainbasecolorsensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class DuploTrainBaseColorSensor + * @extends Device + */ class DuploTrainBaseColorSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR); @@ -101,6 +105,7 @@

    Source: devices/duplotrainbasecolorsensor.js

    /** * Emits when a color sensor is activated. * @event DuploTrainBaseColorSensor#color + * @type {object} * @param {Color} color */ this.notify("color", { color }); @@ -162,7 +167,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_duplotrainbasemotor.js.html b/docs/devices_duplotrainbasemotor.js.html new file mode 100644 index 00000000..50d36a5f --- /dev/null +++ b/docs/devices_duplotrainbasemotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/duplotrainbasemotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/duplotrainbasemotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const basicmotor_1 = require("./basicmotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class DuploTrainBaseMotor
    + * @extends BasicMotor
    + */
    +class DuploTrainBaseMotor extends basicmotor_1.BasicMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR);
    +    }
    +}
    +exports.DuploTrainBaseMotor = DuploTrainBaseMotor;
    +//# sourceMappingURL=duplotrainbasemotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_duplotrainbasespeaker.js.html b/docs/devices_duplotrainbasespeaker.js.html index f7985b85..3c631ee3 100644 --- a/docs/devices_duplotrainbasespeaker.js.html +++ b/docs/devices_duplotrainbasespeaker.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/duplotrainbasespeaker.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class DuploTrainBaseSpeaker + * @extends Device + */ class DuploTrainBaseSpeaker extends device_1.Device { constructor(hub, portId) { super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER); @@ -96,7 +100,7 @@

    Source: devices/duplotrainbasespeaker.js

    * Play a built-in train sound. * @method DuploTrainBaseSpeaker#playSound * @param {DuploTrainBaseSound} sound - * @returns {Promise} Resolved upon successful issuance of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ playSound(sound) { return new Promise((resolve, reject) => { @@ -156,7 +160,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_duplotrainbasespeedometer.js.html b/docs/devices_duplotrainbasespeedometer.js.html index 6be089b3..37908aac 100644 --- a/docs/devices_duplotrainbasespeedometer.js.html +++ b/docs/devices_duplotrainbasespeedometer.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/duplotrainbasespeedometer.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class DuploTraniBaseSpeedometer + * @extends Device + */ class DuploTrainBaseSpeedometer extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER); @@ -100,6 +104,7 @@

    Source: devices/duplotrainbasespeedometer.js

    /** * Emits on a speed change. * @event DuploTrainBaseSpeedometer#speed + * @type {object} * @param {number} speed */ this.notify("speed", { speed }); @@ -160,7 +165,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_hubled.js.html b/docs/devices_hubled.js.html index 33402631..f98b5c5c 100644 --- a/docs/devices_hubled.js.html +++ b/docs/devices_hubled.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/hubled.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class HubLED + * @extends Device + */ class HubLED extends device_1.Device { constructor(hub, portId) { super(hub, portId, {}, Consts.DeviceType.HUB_LED); @@ -96,7 +100,7 @@

    Source: devices/hubled.js

    * Set the color of the LED on the Hub via a color value. * @method HubLED#setColor * @param {Color} color - * @returns {Promise} Resolved upon successful issuance of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ setColor(color) { return new Promise((resolve, reject) => { @@ -120,7 +124,7 @@

    Source: devices/hubled.js

    * @param {number} red * @param {number} green * @param {number} blue - * @returns {Promise} Resolved upon successful issuance of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ setRGB(red, green, blue) { return new Promise((resolve, reject) => { @@ -187,7 +191,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_light.js.html b/docs/devices_light.js.html index 87521bc8..1968a200 100644 --- a/docs/devices_light.js.html +++ b/docs/devices_light.js.html @@ -33,14 +33,14 @@ @@ -89,6 +89,10 @@

    Source: devices/light.js

    const device_1 = require("./device"); const Consts = __importStar(require("../consts")); const utils_1 = require("../utils"); +/** + * @class Light + * @extends Device + */ class Light extends device_1.Device { constructor(hub, portId) { super(hub, portId, {}, Consts.DeviceType.LIGHT); @@ -173,7 +177,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_mediumlinearmotor.js.html b/docs/devices_mediumlinearmotor.js.html new file mode 100644 index 00000000..da4432b9 --- /dev/null +++ b/docs/devices_mediumlinearmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/mediumlinearmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/mediumlinearmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const tachomotor_1 = require("./tachomotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class MediumLinearMotor
    + * @extends TachoMotor
    + */
    +class MediumLinearMotor extends tachomotor_1.TachoMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.MEDIUM_LINEAR_MOTOR);
    +    }
    +}
    +exports.MediumLinearMotor = MediumLinearMotor;
    +//# sourceMappingURL=mediumlinearmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_motionsensor.js.html b/docs/devices_motionsensor.js.html index c4f16b10..6b139e66 100644 --- a/docs/devices_motionsensor.js.html +++ b/docs/devices_motionsensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/motionsensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class MotionSensor + * @extends Device + */ class MotionSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.MOTION_SENSOR); @@ -104,6 +108,7 @@

    Source: devices/motionsensor.js

    /** * Emits when a distance sensor is activated. * @event MotionSensor#distance + * @type {object} * @param {number} distance Distance, in millimeters. */ this.notify("distance", { distance }); @@ -164,7 +169,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_movehubmediumlinearmotor.js.html b/docs/devices_movehubmediumlinearmotor.js.html new file mode 100644 index 00000000..98a55653 --- /dev/null +++ b/docs/devices_movehubmediumlinearmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/movehubmediumlinearmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/movehubmediumlinearmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const tachomotor_1 = require("./tachomotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class MoveHubMediumLinearMotor
    + * @extends TachoMotor
    + */
    +class MoveHubMediumLinearMotor extends tachomotor_1.TachoMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR);
    +    }
    +}
    +exports.MoveHubMediumLinearMotor = MoveHubMediumLinearMotor;
    +//# sourceMappingURL=movehubmediumlinearmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_movehubtiltsensor.js.html b/docs/devices_movehubtiltsensor.js.html index c3bdc7c8..0ee58b72 100644 --- a/docs/devices_movehubtiltsensor.js.html +++ b/docs/devices_movehubtiltsensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/movehubtiltsensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class MoveHubTiltSensor + * @extends Device + */ class MoveHubTiltSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.MOVE_HUB_TILT_SENSOR); @@ -99,6 +103,7 @@

    Source: devices/movehubtiltsensor.js

    /** * Emits when a tilt sensor is activated. * @event MoveHubTiltSensor#tilt + * @type {object} * @param {number} x * @param {number} y */ @@ -162,7 +167,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_piezobuzzer.js.html b/docs/devices_piezobuzzer.js.html index 9edb6bd7..846d4d5f 100644 --- a/docs/devices_piezobuzzer.js.html +++ b/docs/devices_piezobuzzer.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/piezobuzzer.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class PiezoBuzzer + * @extends Device + */ class PiezoBuzzer extends device_1.Device { constructor(hub, portId) { super(hub, portId, {}, Consts.DeviceType.PIEZO_BUZZER); @@ -155,7 +159,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_remotecontrolbutton.js.html b/docs/devices_remotecontrolbutton.js.html index db272343..eabf2927 100644 --- a/docs/devices_remotecontrolbutton.js.html +++ b/docs/devices_remotecontrolbutton.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/remotecontrolbutton.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class RemoteControlButton + * @extends Device + */ class RemoteControlButton extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.REMOTE_CONTROL_BUTTON); @@ -99,6 +103,7 @@

    Source: devices/remotecontrolbutton.js

    /** * Emits when a button on the remote is pressed or released. * @event RemoteControlButton#button + * @type {object} * @param {number} event */ const event = message[4]; @@ -166,7 +171,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_simplemediumlinearmotor.js.html b/docs/devices_simplemediumlinearmotor.js.html new file mode 100644 index 00000000..1dc20e4c --- /dev/null +++ b/docs/devices_simplemediumlinearmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/simplemediumlinearmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/simplemediumlinearmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const basicmotor_1 = require("./basicmotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class SimpleMediumLinearMotor
    + * @extends Device
    + */
    +class SimpleMediumLinearMotor extends basicmotor_1.BasicMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR);
    +    }
    +}
    +exports.SimpleMediumLinearMotor = SimpleMediumLinearMotor;
    +//# sourceMappingURL=simplemediumlinearmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_tachomotor.js.html b/docs/devices_tachomotor.js.html index 1e2ce115..e19b3478 100644 --- a/docs/devices_tachomotor.js.html +++ b/docs/devices_tachomotor.js.html @@ -33,14 +33,14 @@ @@ -89,6 +89,10 @@

    Source: devices/tachomotor.js

    const basicmotor_1 = require("./basicmotor"); const Consts = __importStar(require("../consts")); const utils_1 = require("../utils"); +/** + * @class TachoMotor + * @extends BasicMotor + */ class TachoMotor extends basicmotor_1.BasicMotor { constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) { super(hub, portId, Object.assign({}, modeMap, exports.ModeMap), type); @@ -102,12 +106,20 @@

    Source: devices/tachomotor.js

    /** * Emits when a rotation sensor is activated. * @event TachoMotor#rotate + * @type {object} * @param {number} rotation */ this.notify("rotate", { degrees }); break; } } + /** + * Set the braking style of the motor. + * + * Note: This applies to setSpeed, rotateByDegrees, and gotoAngle. + * @method TachoMotor#setBrakingStyle + * @param {number} style Either BRAKE or HOLD + */ setBrakingStyle(style) { this._brakeStyle = style; } @@ -115,7 +127,7 @@

    Source: devices/tachomotor.js

    * Set the motor speed. * @method TachoMotor#setSpeed * @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0. - * @returns {Promise} Resolved upon successful completion of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ setSpeed(speed, time) { if (!this.isVirtualPort && speed instanceof Array) { @@ -242,7 +254,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_techniccolorsensor.js.html b/docs/devices_techniccolorsensor.js.html index 104ed63c..603acc68 100644 --- a/docs/devices_techniccolorsensor.js.html +++ b/docs/devices_techniccolorsensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/techniccolorsensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class TechnicColorSensor + * @extends Device + */ class TechnicColorSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_COLOR_SENSOR); @@ -101,6 +105,7 @@

    Source: devices/techniccolorsensor.js

    /** * Emits when a color sensor is activated. * @event TechnicColorSensor#color + * @type {object} * @param {Color} color */ this.notify("color", { color }); @@ -110,8 +115,9 @@

    Source: devices/techniccolorsensor.js

    const reflect = message[4]; /** * Emits when the light reflectivity changes. - * @event TechnicColorSensor#reflect Percentage, from 0 to 100. - * @param {number} reflect + * @event TechnicColorSensor#reflect + * @type {object} + * @param {number} reflect Percentage, from 0 to 100. */ this.notify("reflect", { reflect }); break; @@ -119,8 +125,9 @@

    Source: devices/techniccolorsensor.js

    const ambient = message[4]; /** * Emits when the ambient light changes. - * @event TechnicColorSensor#ambient Percentage, from 0 to 100. - * @param {number} ambient + * @event TechnicColorSensor#ambient + * @type {object} + * @param {number} ambient Percentage, from 0 to 100. */ this.notify("ambient", { ambient }); break; @@ -184,7 +191,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_technicdistancesensor.js.html b/docs/devices_technicdistancesensor.js.html index ede059c7..525d89a0 100644 --- a/docs/devices_technicdistancesensor.js.html +++ b/docs/devices_technicdistancesensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/technicdistancesensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class TechnicDistanceSensor + * @extends Device + */ class TechnicDistanceSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_DISTANCE_SENSOR); @@ -99,8 +103,9 @@

    Source: devices/technicdistancesensor.js

    const distance = message.readUInt16LE(4); /** * Emits when the detected distance changes (Slow sampling covers 40mm to 2500mm). - * @event TechnicDistanceSensor#distance Distance, from 40 to 2500mm - * @param {number} distance + * @event TechnicDistanceSensor#distance + * @type {object} + * @param {number} distance Distance, from 40 to 2500mm */ this.notify("distance", { distance }); break; @@ -108,8 +113,9 @@

    Source: devices/technicdistancesensor.js

    const fastDistance = message.readUInt16LE(4); /** * Emits when the detected distance changes (Fast sampling covers 50mm to 320mm). - * @event TechnicDistanceSensor#fastDistance Distance, from 50 to 320mm - * @param {number} fastDistance + * @event TechnicDistanceSensor#fastDistance + * @type {object} + * @param {number} fastDistance Distance, from 50 to 320mm */ this.notify("fastDistance", { fastDistance }); break; @@ -122,7 +128,7 @@

    Source: devices/technicdistancesensor.js

    * @param {number} bottomLeft Bottom left quadrant (below left eye). 0-100 brightness. * @param {number} topRight Top right quadrant (above right eye). 0-100 brightness. * @param {number} bottomRight Bottom right quadrant (below right eye). 0-100 brightness. - * @returns {Promise} Resolved upon successful completion of command. + * @returns {Promise} Resolved upon successful issuance of the command. */ setBrightness(topLeft, bottomLeft, topRight, bottomRight) { this.writeDirect(0x05, Buffer.from([topLeft, topRight, bottomLeft, bottomRight])); @@ -183,7 +189,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_technicforcesensor.js.html b/docs/devices_technicforcesensor.js.html index 3d2c10e7..994bb0d6 100644 --- a/docs/devices_technicforcesensor.js.html +++ b/docs/devices_technicforcesensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/technicforcesensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class TechnicForceSensor + * @extends Device + */ class TechnicForceSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_FORCE_SENSOR); @@ -99,8 +103,9 @@

    Source: devices/technicforcesensor.js

    const force = message[4] / 10; /** * Emits when force is applied. - * @event TechnicForceSensor#force Force, in newtons (0-10). - * @param {number} force + * @event TechnicForceSensor#force + * @type {object} + * @param {number} force Force, in newtons (0-10). */ this.notify("force", { force }); break; @@ -108,8 +113,9 @@

    Source: devices/technicforcesensor.js

    const touched = message[4] ? true : false; /** * Emits when the sensor is touched. - * @event TechnicForceSensor#touch Touched on/off (boolean). - * @param {boolean} touch + * @event TechnicForceSensor#touch + * @type {object} + * @param {boolean} touch Touched on/off (boolean). */ this.notify("touched", { touched }); break; @@ -117,8 +123,9 @@

    Source: devices/technicforcesensor.js

    const tapped = message[4]; /** * Emits when the sensor is tapped. - * @event TechnicForceSensor#tapped How hard the sensor was tapped, from 0-3. - * @param {number} tapped + * @event TechnicForceSensor#tapped + * @type {object} + * @param {number} tapped How hard the sensor was tapped, from 0-3. */ this.notify("tapped", { tapped }); break; @@ -182,7 +189,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_techniclargeangularmotor.js.html b/docs/devices_techniclargeangularmotor.js.html new file mode 100644 index 00000000..0e6571a6 --- /dev/null +++ b/docs/devices_techniclargeangularmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/techniclargeangularmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/techniclargeangularmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const absolutemotor_1 = require("./absolutemotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class TechnicLargeAngularMotor
    + * @extends AbsoluteMotor
    + */
    +class TechnicLargeAngularMotor extends absolutemotor_1.AbsoluteMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.TECHNIC_LARGE_ANGULAR_MOTOR);
    +    }
    +}
    +exports.TechnicLargeAngularMotor = TechnicLargeAngularMotor;
    +//# sourceMappingURL=techniclargeangularmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_techniclargelinearmotor.js.html b/docs/devices_techniclargelinearmotor.js.html new file mode 100644 index 00000000..83ab2d8b --- /dev/null +++ b/docs/devices_techniclargelinearmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/techniclargelinearmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/techniclargelinearmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const absolutemotor_1 = require("./absolutemotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class TechnicLargeLinearMotor
    + * @extends AbsoluteMotor
    + */
    +class TechnicLargeLinearMotor extends absolutemotor_1.AbsoluteMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR);
    +    }
    +}
    +exports.TechnicLargeLinearMotor = TechnicLargeLinearMotor;
    +//# sourceMappingURL=techniclargelinearmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_technicmediumangularmotor.js.html b/docs/devices_technicmediumangularmotor.js.html new file mode 100644 index 00000000..22a2e714 --- /dev/null +++ b/docs/devices_technicmediumangularmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/technicmediumangularmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/technicmediumangularmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const absolutemotor_1 = require("./absolutemotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class TechnicMediumAngularMotor
    + * @extends AbsoluteMotor
    + */
    +class TechnicMediumAngularMotor extends absolutemotor_1.AbsoluteMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.TECHNIC_MEDIUM_ANGULAR_MOTOR);
    +    }
    +}
    +exports.TechnicMediumAngularMotor = TechnicMediumAngularMotor;
    +//# sourceMappingURL=technicmediumangularmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_technicmediumhubaccelerometersensor.js.html b/docs/devices_technicmediumhubaccelerometersensor.js.html index a5b737f9..85f89c08 100644 --- a/docs/devices_technicmediumhubaccelerometersensor.js.html +++ b/docs/devices_technicmediumhubaccelerometersensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/technicmediumhubaccelerometersensor.jsSource: devices/technicmediumhubaccelerometersensor.jsSearch results

    Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_technicmediumhubgyrosensor.js.html b/docs/devices_technicmediumhubgyrosensor.js.html index b2674324..9ad8e5e7 100644 --- a/docs/devices_technicmediumhubgyrosensor.js.html +++ b/docs/devices_technicmediumhubgyrosensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/technicmediumhubgyrosensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class TechnicMediumHubGyroSensor + * @extends Device + */ class TechnicMediumHubGyroSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR); @@ -99,6 +103,7 @@

    Source: devices/technicmediumhubgyrosensor.js

    /** * Emits when gyroscope detects movement. Measured in DPS - degrees per second. * @event TechnicMediumHubGyroSensor#gyro + * @type {object} * @param {number} x * @param {number} y * @param {number} z @@ -164,7 +169,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_technicmediumhubtiltsensor.js.html b/docs/devices_technicmediumhubtiltsensor.js.html index 0453b844..f6bbe48a 100644 --- a/docs/devices_technicmediumhubtiltsensor.js.html +++ b/docs/devices_technicmediumhubtiltsensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/technicmediumhubtiltsensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class TechnicMediumHubTiltSensor + * @extends Device + */ class TechnicMediumHubTiltSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR); @@ -99,6 +103,7 @@

    Source: devices/technicmediumhubtiltsensor.js

    /** * Emits when a tilt sensor is activated. * @event TechnicMediumHubTiltSensor#tilt + * @type {object} * @param {number} x * @param {number} y * @param {number} z @@ -164,7 +169,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_technicxlargelinearmotor.js.html b/docs/devices_technicxlargelinearmotor.js.html new file mode 100644 index 00000000..b3d40cad --- /dev/null +++ b/docs/devices_technicxlargelinearmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/technicxlargelinearmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/technicxlargelinearmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const absolutemotor_1 = require("./absolutemotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class TechnicXLargeLinearMotor
    + * @extends AbsoluteMotor
    + */
    +class TechnicXLargeLinearMotor extends absolutemotor_1.AbsoluteMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR);
    +    }
    +}
    +exports.TechnicXLargeLinearMotor = TechnicXLargeLinearMotor;
    +//# sourceMappingURL=technicxlargelinearmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_tiltsensor.js.html b/docs/devices_tiltsensor.js.html index 4c098820..5112b01d 100644 --- a/docs/devices_tiltsensor.js.html +++ b/docs/devices_tiltsensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/tiltsensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class TiltSensor + * @extends Device + */ class TiltSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.TILT_SENSOR); @@ -100,7 +104,8 @@

    Source: devices/tiltsensor.js

    const y = message.readInt8(this.isWeDo2SmartHub ? 3 : 5); /** * Emits when a tilt sensor is activated. - * @event LPF2Hub#tilt + * @event TiltSensor#tilt + * @type {object} * @param {number} x * @param {number} y */ @@ -162,7 +167,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/devices_trainmotor.js.html b/docs/devices_trainmotor.js.html new file mode 100644 index 00000000..6ae86791 --- /dev/null +++ b/docs/devices_trainmotor.js.html @@ -0,0 +1,237 @@ + + + + + + + node-poweredup Source: devices/trainmotor.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: devices/trainmotor.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const basicmotor_1 = require("./basicmotor");
    +const Consts = __importStar(require("../consts"));
    +/**
    + * @class TrainMotor
    + * @extends BasicMotor
    + */
    +class TrainMotor extends basicmotor_1.BasicMotor {
    +    constructor(hub, portId) {
    +        super(hub, portId, {}, Consts.DeviceType.TRAIN_MOTOR);
    +    }
    +}
    +exports.TrainMotor = TrainMotor;
    +//# sourceMappingURL=trainmotor.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/devices_voltagesensor.js.html b/docs/devices_voltagesensor.js.html index a341a208..e509767c 100644 --- a/docs/devices_voltagesensor.js.html +++ b/docs/devices_voltagesensor.js.html @@ -33,14 +33,14 @@ @@ -88,6 +88,10 @@

    Source: devices/voltagesensor.js

    Object.defineProperty(exports, "__esModule", { value: true }); const device_1 = require("./device"); const Consts = __importStar(require("../consts")); +/** + * @class VoltageSensor + * @extends Device + */ class VoltageSensor extends device_1.Device { constructor(hub, portId) { super(hub, portId, exports.ModeMap, Consts.DeviceType.VOLTAGE_SENSOR); @@ -113,6 +117,7 @@

    Source: devices/voltagesensor.js

    /** * Emits when a voltage change is detected. * @event VoltageSensor#voltage + * @type {object} * @param {number} voltage */ this.notify("voltage", { voltage }); @@ -185,7 +190,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_basehub.js.html b/docs/hubs_basehub.js.html index af7ab7af..910438d2 100644 --- a/docs/hubs_basehub.js.html +++ b/docs/hubs_basehub.js.html @@ -33,14 +33,14 @@ @@ -178,7 +178,7 @@

    Source: hubs/basehub.js

    } /** * @readonly - * @property {string} firmwareVersion Hardware version of the hub + * @property {string} hardwareVersion Hardware version of the hub */ get hardwareVersion() { return this._hardwareVersion; @@ -236,6 +236,12 @@

    Source: hubs/basehub.js

    disconnect() { return this._bleDevice.disconnect(); } + /** + * Retrieves the device attached to a given port. + * @method Hub#getDeviceAtPort + * @param {string} portName The name of the port to retrieve the device from. + * @returns {Device | undefined} The device attached to the port. + */ getDeviceAtPort(portName) { const portId = this._portMap[portName]; if (portId !== undefined) { @@ -245,6 +251,14 @@

    Source: hubs/basehub.js

    return undefined; } } + /** + * Retrieves the device attached to a given port, waiting until one is attached if there isn't one. + * + * Note: If a device is never attached, the returned promise may never resolve. + * @method Hub#waitForDeviceAtPort + * @param {string} portName The name of the port to retrieve the device from. + * @returns {Promise} Resolved once a device is attached, or resolved immediately if a device is already attached. + */ waitForDeviceAtPort(portName) { return new Promise((resolve) => { const existingDevice = this.getDeviceAtPort(portName); @@ -262,12 +276,31 @@

    Source: hubs/basehub.js

    }); }); } + /** + * Retrieves all attached devices. + * @method Hub#getDevices + * @returns {Device[]} Array of all attached devices. + */ getDevices() { return Object.values(this._attachedDevices); } + /** + * Retrieves an array of devices of the specified type. + * @method Hub#getDevicesByType + * @param {number} deviceType The device type to lookup. + * @returns {Device[]} Array of all devices of the specified type. + */ getDevicesByType(deviceType) { return this.getDevices().filter((device) => device.type === deviceType); } + /** + * Retrieves the first device attached of the specified type, waiting until one is attached if there isn't one. + * + * Note: If a device is never attached, the returned promise may never resolve. + * @method Hub#waitForDeviceByType + * @param {number} deviceType The device type to lookup. + * @returns {Promise} Resolved once a device is attached, or resolved immediately if a device is already attached. + */ waitForDeviceByType(deviceType) { return new Promise((resolve) => { const existingDevices = this.getDevicesByType(deviceType); @@ -299,7 +332,7 @@

    Source: hubs/basehub.js

    /** * Sleep a given amount of time. * - * This is a helper method to make it easier to add delays into a chain of commands. + * Note: This is a helper method to make it easier to add delays into a chain of commands. * @method Hub#sleep * @param {number} delay How long to sleep (in milliseconds). * @returns {Promise} Resolved after the delay is finished. @@ -312,7 +345,7 @@

    Source: hubs/basehub.js

    /** * Wait until a given list of concurrently running commands are complete. * - * This is a helper method to make it easier to wait for concurrent commands to complete. + * Note: This is a helper method to make it easier to wait for concurrent commands to complete. * @method Hub#wait * @param {Array<Promise<any>>} commands Array of executing commands. * @returns {Promise} Resolved after the commands are finished. @@ -352,7 +385,7 @@

    Source: hubs/basehub.js

    delete this._attachedDevices[device.portId]; /** * Emits when a device is detached from the Hub. - * @event Hub#attach + * @event Hub#detach * @param {Device} device */ this.emit("detach", device); @@ -449,7 +482,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_boostmovehub.js.html b/docs/hubs_boostmovehub.js.html new file mode 100644 index 00000000..c4825ba8 --- /dev/null +++ b/docs/hubs_boostmovehub.js.html @@ -0,0 +1,276 @@ + + + + + + + node-poweredup Source: hubs/boostmovehub.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: hubs/boostmovehub.js

    + +
    +
    +
    "use strict";
    +var __importDefault = (this && this.__importDefault) || function (mod) {
    +    return (mod && mod.__esModule) ? mod : { "default": mod };
    +};
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const compare_versions_1 = __importDefault(require("compare-versions"));
    +const lpf2hub_1 = require("./lpf2hub");
    +const Consts = __importStar(require("../consts"));
    +const Debug = require("debug");
    +const debug = Debug("boostmovehub");
    +/**
    + * The BoostMoveHub is emitted if the discovered device is a Boost Move Hub.
    + * @class BoostMoveHub
    + * @extends LPF2Hub
    + * @extends Hub
    + */
    +class BoostMoveHub extends lpf2hub_1.LPF2Hub {
    +    constructor(device) {
    +        super(device, Consts.HubType.BOOST_MOVE_HUB);
    +        this._currentPort = 0x3b;
    +        this._voltagePort = 0x3c;
    +        this._portNames = {
    +            "A": 0,
    +            "B": 1,
    +            "C": 2,
    +            "D": 3,
    +            "TILT": 58
    +        };
    +        debug("Discovered Boost Move Hub");
    +    }
    +    static IsBoostMoveHub(peripheral) {
    +        return (peripheral.advertisement &&
    +            peripheral.advertisement.serviceUuids &&
    +            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
    +            peripheral.advertisement.manufacturerData &&
    +            peripheral.advertisement.manufacturerData.length > 3 &&
    +            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID);
    +    }
    +    connect() {
    +        return new Promise(async (resolve, reject) => {
    +            debug("Connecting to Boost Move Hub");
    +            await super.connect();
    +            debug("Connect completed");
    +            return resolve();
    +        });
    +    }
    +    _checkFirmware(version) {
    +        if (compare_versions_1.default("2.0.00.0017", version) === 1) {
    +            throw new Error(`Your Boost Move Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
    +        }
    +    }
    +}
    +exports.BoostMoveHub = BoostMoveHub;
    +//# sourceMappingURL=boostmovehub.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_controlplushub.js.html b/docs/hubs_controlplushub.js.html new file mode 100644 index 00000000..00708573 --- /dev/null +++ b/docs/hubs_controlplushub.js.html @@ -0,0 +1,273 @@ + + + + + + + node-poweredup Source: hubs/controlplushub.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: hubs/controlplushub.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const lpf2hub_1 = require("./lpf2hub");
    +const Consts = __importStar(require("../consts"));
    +const Debug = require("debug");
    +const debug = Debug("ControlPlusHub");
    +/**
    + * The ControlPlusHub is emitted if the discovered device is a Control+ Hub.
    + * @class ControlPlusHub
    + * @extends LPF2Hub
    + * @extends Hub
    + */
    +class ControlPlusHub extends lpf2hub_1.LPF2Hub {
    +    constructor(device) {
    +        super(device, Consts.HubType.CONTROL_PLUS_HUB);
    +        this._currentPort = 0x3b;
    +        this._currentMaxMA = 4175;
    +        this._voltagePort = 0x3c;
    +        this._voltageMaxRaw = 4095;
    +        this._voltageMaxV = 9.615;
    +        this._portNames = {
    +            "A": 0,
    +            "B": 1,
    +            "C": 2,
    +            "D": 3,
    +            "ACCEL": 97,
    +            "GYRO": 98,
    +            "TILT": 99
    +        };
    +        debug("Discovered Control+ Hub");
    +    }
    +    static IsControlPlusHub(peripheral) {
    +        return (peripheral.advertisement &&
    +            peripheral.advertisement.serviceUuids &&
    +            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
    +            peripheral.advertisement.manufacturerData &&
    +            peripheral.advertisement.manufacturerData.length > 3 &&
    +            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.CONTROL_PLUS_LARGE_HUB);
    +    }
    +    connect() {
    +        return new Promise(async (resolve, reject) => {
    +            debug("Connecting to Control+ Hub");
    +            await super.connect();
    +            this.send(Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01]), Consts.BLECharacteristic.LPF2_ALL); // Temperature
    +            debug("Connect completed");
    +            return resolve();
    +        });
    +    }
    +}
    +exports.ControlPlusHub = ControlPlusHub;
    +//# sourceMappingURL=controlplushub.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_duplotrainbase.js.html b/docs/hubs_duplotrainbase.js.html index 7d4c71bb..5d53f264 100644 --- a/docs/hubs_duplotrainbase.js.html +++ b/docs/hubs_duplotrainbase.js.html @@ -33,14 +33,14 @@ @@ -169,7 +169,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_hub.js.html b/docs/hubs_hub.js.html index bf00f19f..d02c5d43 100644 --- a/docs/hubs_hub.js.html +++ b/docs/hubs_hub.js.html @@ -33,14 +33,14 @@ @@ -181,7 +181,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_lpf2hub.js.html b/docs/hubs_lpf2hub.js.html index 13d46e81..574986c9 100644 --- a/docs/hubs_lpf2hub.js.html +++ b/docs/hubs_lpf2hub.js.html @@ -33,14 +33,14 @@ @@ -164,6 +164,15 @@

    Source: hubs/lpf2hub.js

    unsubscribe(portId, mode) { this.send(Buffer.from([0x41, portId, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), Consts.BLECharacteristic.LPF2_ALL); } + /** + * Combines two ports with into a single virtual port. + * + * Note: The devices attached to the ports must be of the same device type. + * @method LPF2Hub#createVirtualPort + * @param {string} firstPortName First port name + * @param {string} secondPortName Second port name + * @returns {Promise} Resolved upon successful issuance of command. + */ createVirtualPort(firstPortName, secondPortName) { const firstDevice = this.getDeviceAtPort(firstPortName); if (!firstDevice) { @@ -458,7 +467,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_movehub.js.html b/docs/hubs_movehub.js.html index 8e2433e3..6b3ed475 100644 --- a/docs/hubs_movehub.js.html +++ b/docs/hubs_movehub.js.html @@ -33,14 +33,14 @@ @@ -183,7 +183,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_puphub.js.html b/docs/hubs_puphub.js.html new file mode 100644 index 00000000..77b8ee45 --- /dev/null +++ b/docs/hubs_puphub.js.html @@ -0,0 +1,273 @@ + + + + + + + node-poweredup Source: hubs/puphub.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: hubs/puphub.js

    + +
    +
    +
    "use strict";
    +var __importDefault = (this && this.__importDefault) || function (mod) {
    +    return (mod && mod.__esModule) ? mod : { "default": mod };
    +};
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const compare_versions_1 = __importDefault(require("compare-versions"));
    +const lpf2hub_1 = require("./lpf2hub");
    +const Consts = __importStar(require("../consts"));
    +const Debug = require("debug");
    +const debug = Debug("puphub");
    +/**
    + * The PUPHub is emitted if the discovered device is a Powered UP Hub.
    + * @class PUPHub
    + * @extends LPF2Hub
    + * @extends Hub
    + */
    +class PUPHub extends lpf2hub_1.LPF2Hub {
    +    constructor(device) {
    +        super(device, Consts.HubType.POWERED_UP_HUB);
    +        this._currentPort = 0x3b;
    +        this._voltagePort = 0x3c;
    +        this._portNames = {
    +            "A": 0,
    +            "B": 1
    +        };
    +        debug("Discovered Powered UP Hub");
    +    }
    +    static IsPUPHub(peripheral) {
    +        return (peripheral.advertisement &&
    +            peripheral.advertisement.serviceUuids &&
    +            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
    +            peripheral.advertisement.manufacturerData &&
    +            peripheral.advertisement.manufacturerData.length > 3 &&
    +            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID);
    +    }
    +    connect() {
    +        return new Promise(async (resolve, reject) => {
    +            debug("Connecting to Powered UP Hub");
    +            await super.connect();
    +            debug("Connect completed");
    +            return resolve();
    +        });
    +    }
    +    _checkFirmware(version) {
    +        if (compare_versions_1.default("1.1.00.0004", version) === 1) {
    +            throw new Error(`Your Powered Up Hub's (${this.name}) firmware is out of date and unsupported by this library. Please update it via the official Powered Up app.`);
    +        }
    +    }
    +}
    +exports.PUPHub = PUPHub;
    +//# sourceMappingURL=puphub.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_pupremote.js.html b/docs/hubs_pupremote.js.html new file mode 100644 index 00000000..688f36ca --- /dev/null +++ b/docs/hubs_pupremote.js.html @@ -0,0 +1,266 @@ + + + + + + + node-poweredup Source: hubs/pupremote.js + + + + + + + + + + + + + +
    +
    + + +
    + +
    + + +

    Source: hubs/pupremote.js

    + +
    +
    +
    "use strict";
    +var __importStar = (this && this.__importStar) || function (mod) {
    +    if (mod && mod.__esModule) return mod;
    +    var result = {};
    +    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    +    result["default"] = mod;
    +    return result;
    +};
    +Object.defineProperty(exports, "__esModule", { value: true });
    +const lpf2hub_1 = require("./lpf2hub");
    +const Consts = __importStar(require("../consts"));
    +const Debug = require("debug");
    +const debug = Debug("pupremote");
    +/**
    + * The PUPRemote is emitted if the discovered device is a Powered UP Remote.
    + * @class PUPRemote
    + * @extends LPF2Hub
    + * @extends Hub
    + */
    +class PUPRemote extends lpf2hub_1.LPF2Hub {
    +    constructor(device) {
    +        super(device, Consts.HubType.POWERED_UP_REMOTE);
    +        this._ledPort = 0x34;
    +        this._voltagePort = 0x3b;
    +        this._voltageMaxV = 6.4;
    +        this._voltageMaxRaw = 3200;
    +        this._portNames = {
    +            "LEFT": 0,
    +            "RIGHT": 1
    +        };
    +        debug("Discovered Powered UP Remote");
    +    }
    +    static IsPUPRemote(peripheral) {
    +        return (peripheral.advertisement &&
    +            peripheral.advertisement.serviceUuids &&
    +            peripheral.advertisement.serviceUuids.indexOf(Consts.BLEService.LPF2_HUB.replace(/-/g, "")) >= 0 &&
    +            peripheral.advertisement.manufacturerData &&
    +            peripheral.advertisement.manufacturerData.length > 3 &&
    +            peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID);
    +    }
    +    connect() {
    +        return new Promise(async (resolve, reject) => {
    +            debug("Connecting to Powered UP Remote");
    +            await super.connect();
    +            debug("Connect completed");
    +            return resolve();
    +        });
    +    }
    +}
    +exports.PUPRemote = PUPRemote;
    +//# sourceMappingURL=pupremote.js.map
    +
    +
    + + + + + +
    +
    + +
    + + + +
    +
    + + + + + +
    + + + + node-poweredup by Nathan Kellenicki licensed under the MIT license. + + + + Documentation generated by JSDoc 3.6.3 + + on Mon Feb 10th 2020 + + using the DocStrap template. + +
    + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/hubs_remotecontrol.js.html b/docs/hubs_remotecontrol.js.html index e46113af..ded1a630 100644 --- a/docs/hubs_remotecontrol.js.html +++ b/docs/hubs_remotecontrol.js.html @@ -33,14 +33,14 @@ @@ -171,7 +171,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_technicmediumhub.js.html b/docs/hubs_technicmediumhub.js.html index b5791f78..3adc0c06 100644 --- a/docs/hubs_technicmediumhub.js.html +++ b/docs/hubs_technicmediumhub.js.html @@ -33,14 +33,14 @@ @@ -177,7 +177,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/hubs_wedo2smarthub.js.html b/docs/hubs_wedo2smarthub.js.html index 724ce5ec..87aa1ee1 100644 --- a/docs/hubs_wedo2smarthub.js.html +++ b/docs/hubs_wedo2smarthub.js.html @@ -33,14 +33,14 @@ @@ -323,7 +323,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/index.html b/docs/index.html index e99f5cd8..118bca27 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,14 +33,14 @@ @@ -358,7 +358,7 @@ Documentation generated by JSDoc 3.6.3 - on Fri Feb 7th 2020 + on Mon Feb 10th 2020 using the DocStrap template. diff --git a/docs/quicksearch.html b/docs/quicksearch.html index 942391bb..95d71432 100644 --- a/docs/quicksearch.html +++ b/docs/quicksearch.html @@ -7,7 +7,7 @@
    NameTypeDescription
    tappedz @@ -5963,8 +8251,8 @@
    Parameters:
    @@ -5997,7 +8285,7 @@
    Parameters:

    -

    accel

    +

    gyro

    @@ -6005,13 +8293,19 @@

    accel

    -

    Emits when accelerometer detects movement. Measured in mG.

    +

    Emits when gyroscope detects movement. Measured in DPS - degrees per second.

    +
    Type: +object + + +
    + @@ -6038,30 +8332,6 @@
    Parameters:
    port - - -string - - - - -
    x
    buttonx -string +number @@ -6283,13 +8550,37 @@
    Parameters:
    statey -ButtonState +number + + + + +
    z + + +number @@ -6318,15 +8609,6 @@
    Parameters:
    -
    Inherited From:
    -
    - -
    - @@ -6353,8 +8635,8 @@
    Parameters:
    @@ -6387,7 +8669,7 @@
    Parameters:

    -

    tilt

    +

    absolute

    @@ -6395,13 +8677,19 @@

    tilt

    -

    Emits when a tilt sensor is activated.

    +

    Emits when a the motors absolute position is changed.

    +
    Type: +object + + +
    + @@ -6430,31 +8718,7 @@
    Parameters:
    x - - -number - - - - -
    yabsolute @@ -6493,7 +8757,7 @@
    Parameters:
    @@ -6524,8 +8788,8 @@
    Parameters:
    @@ -6558,7 +8822,7 @@
    Parameters:

    -

    gyro

    +

    rotate

    @@ -6566,13 +8830,19 @@

    gyro

    -

    Emits when gyroscope detects movement. Measured in DPS - degrees per second.

    +

    Emits when a rotation sensor is activated.

    +
    Type: +object + + +
    + @@ -6601,55 +8871,7 @@
    Parameters:
    x - - -number - - - - -
    y - - -number - - - - -
    zrotation @@ -6684,6 +8906,15 @@
    Parameters:
    +
    Inherited From:
    +
    + +
    + @@ -6710,8 +8941,8 @@
    Parameters:
    @@ -6759,6 +8990,12 @@

    tilt

    +
    Type: +object + + +
    + @@ -6828,30 +9065,6 @@
    Parameters:
    -
    z - - -number - - - - -