From afe4eadf0867059b04cea25bd8eec3cd4ff319bd Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Fri, 12 Jun 2020 12:37:07 -0700 Subject: [PATCH] Fixed docs, updated force sensor for WeDo, added Mindstorms motors --- README.md | 14 ++++----- examples/sensor_test.js | 40 ++++++++++++++---------- package-lock.json | 2 +- package.json | 2 +- src/consts.ts | 4 ++- src/devices/colordistancesensor.ts | 6 +++- src/devices/technicforcesensor.ts | 2 +- src/devices/techniclargeangularmotor.ts | 4 +-- src/devices/technicmediumangularmotor.ts | 4 +-- src/hubs/basehub.ts | 2 +- 10 files changed, 47 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index f642368..096467b 100644 --- a/README.md +++ b/README.md @@ -40,11 +40,11 @@ While most Powered Up components and Hubs are compatible with each other, there | Powered Up LED Lights | 88005 | Light | Yes | Yes | Yes | Yes | 88005 | | Control+ Large Motor | 22169 | Motor/Sensor | *Partial* | No | Yes | Yes | 42099
42100 | | Control+ XLarge Motor | 22172 | Motor/Sensor | *Partial* | No | Yes | Yes | 42099
42100 | -| SPIKE Prime Medium Motor | 45678 | Motor/Sensor | *Partial* | Yes | Yes | Yes | 45678 | -| SPIKE Prime Large Motor | 45678 | Motor/Sensor | *Partial* | Yes | Yes | Yes | 45678 | -| SPIKE Prime Color Sensor | 45678 | Sensor | No | Yes | Yes | Yes | 45678 | -| SPIKE Prime Distance Sensor | 45678 | Sensor | No | Yes | Yes | Yes | 45678 | -| SPIKE Prime Force Sensor | 45678 | Sensor | No | Yes | Yes | Yes | 45678 | +| SPIKE Prime Medium Motor | 45678 | Motor/Sensor | *Partial* | No | Yes | Yes | 45678 | +| SPIKE Prime Large Motor | 45678 | Motor/Sensor | *Partial* | No | Yes | Yes | 45678 | +| SPIKE Prime Color Sensor | 45678 | Sensor | No | No | Yes | Yes | 45678 | +| SPIKE Prime Distance Sensor | 45678 | Sensor | No | No | Yes | Yes | 45678 | +| SPIKE Prime Force Sensor | 45678 | Sensor | *Partial* | Yes | Yes | Yes | 45678 | In addition, the Hubs themselves have certain built-in features which this library exposes. @@ -62,9 +62,9 @@ In addition, the Hubs themselves have certain built-in features which this libra * The WeDo 2.0 Smart Hub uses an older firmware which is no longer being updated. As a result, only certain motors and sensors work with it. See the table above. -* When used with the Boost Move Hub, the Control+ Motors do not currently accept commands (This is a known but which requires a firmware update from Lego to fix) +* When used with the Boost Move Hub, the Control+ Motors and SPIKE Prime Motors/Sensors do not currently accept commands (This is a known bug which requires a firmware update from Lego to fix) -* The SPIKE Prime Hub does not use Bluetooth Low Energy, so is not supported via this library. It is recommended you use MicroPython and Bluetooth Classic to develop for this Hub. +* The SPIKE Prime Hub does not use Bluetooth Low Energy, so is not supported via this library. It is recommended you use MicroPython to develop for this Hub using the officially provided tools and software. ### Documentation diff --git a/examples/sensor_test.js b/examples/sensor_test.js index 42e0758..770b1f7 100644 --- a/examples/sensor_test.js +++ b/examples/sensor_test.js @@ -23,36 +23,44 @@ poweredUP.on("discover", async (hub) => { console.log(`Disconnected ${hub.name}`); }) - hub.on("tilt", (port, x, y, z) => { - console.log(`Tilt detected on port ${port} (X: ${x}, Y: ${y}${z !== "undefined" ? `, Z: ${z}`: ""})`); + hub.on("tilt", (device, { x, y, z }) => { + console.log(`Tilt detected on port ${device.portName} (X: ${x}, Y: ${y}${z !== "undefined" ? `, Z: ${z}`: ""})`); }); - hub.on("accel", (port, x, y, z) => { - console.log(`Accelerometer detected on port ${port} (X: ${x}, Y: ${y}, Z: ${z})`); + hub.on("accel", (device, { x, y, z }) => { + console.log(`Accelerometer detected on port ${device.portName} (X: ${x}, Y: ${y}, Z: ${z})`); }); - hub.on("distance", (port, distance) => { - console.log(`Motion detected on port ${port} (Distance: ${distance})`); + hub.on("distance", (device, { distance }) => { + console.log(`Motion detected on port ${device.portName} (Distance: ${distance})`); }); - hub.on("color", (port, color) => { - console.log(`Color detected on port ${port} (Color: ${color})`); + hub.on("color", (device, { color }) => { + console.log(`Color detected on port ${device.portName} (Color: ${color})`); }); - hub.on("rotate", (port, rotation) => { - console.log(`Rotation detected on port ${port} (Rotation: ${rotation})`); + hub.on("rotate", (device, { degrees }) => { + console.log(`Rotation detected on port ${device.portName} (Rotation: ${degrees})`); }); - hub.on("button", (button, state) => { - console.log(`Button press detected (Button: ${button}, State: ${state})`); + hub.on("force", (device, { force }) => { + console.log(`Force detected on port ${device.portName} (Force: ${force})`); }); - hub.on("attach", (port, device) => { - console.log(`Device attached to port ${port} (Device ID: ${device})`) ; + hub.on("button", ({ event }) => { + console.log(`Green button press detected (Event: ${event})`); }); - hub.on("detach", (port) => { - console.log(`Device detached from port ${port}`) ; + hub.on("remoteButton", (device, { event }) => { + console.log(`Remote control button press detected on port ${device.portName} (Event: ${event})`); + }); + + hub.on("attach", (device) => { + console.log(`Device attached to port ${device.portName} (Device ID: ${device.type})`) ; + }); + + hub.on("detach", (device) => { + console.log(`Device detached from port ${device.portName}`) ; }); }); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d599c1f..db027d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-poweredup", - "version": "6.5.3", + "version": "6.6.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index db99a5f..81d576c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-poweredup", - "version": "6.5.3", + "version": "6.6.0", "description": "A Javascript module to interface with LEGO Powered Up components.", "homepage": "https://github.com/nathankellenicki/node-poweredup/", "main": "dist/node/index-node.js", diff --git a/src/consts.ts b/src/consts.ts index d5395d4..a06f70a 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -82,7 +82,9 @@ export enum DeviceType { TECHNIC_MEDIUM_HUB_TEMPERATURE_SENSOR = 60, TECHNIC_COLOR_SENSOR = 61, // Spike Prime TECHNIC_DISTANCE_SENSOR = 62, // Spike Prime - TECHNIC_FORCE_SENSOR = 63 // Spike Prime + TECHNIC_FORCE_SENSOR = 63, // Spike Prime + TECHNIC_MEDIUM_ANGULAR_MOTOR_GREY = 75, // Mindstorms + TECHNIC_LARGE_ANGULAR_MOTOR_GREY = 76, // Mindstorms } diff --git a/src/devices/colordistancesensor.ts b/src/devices/colordistancesensor.ts index f7560be..67868cc 100644 --- a/src/devices/colordistancesensor.ts +++ b/src/devices/colordistancesensor.ts @@ -37,7 +37,11 @@ export class ColorDistanceSensor extends Device { break; } if (message[4] <= 10) { - const distance = Math.floor(message[4] * 25.4) - 20; + let distance = Math.floor(message[4] * 25.4); + + if (distance < 0) { + distance = 0; + } /** * Emits when a distance sensor is activated. diff --git a/src/devices/technicforcesensor.ts b/src/devices/technicforcesensor.ts index d2f01aa..3065237 100644 --- a/src/devices/technicforcesensor.ts +++ b/src/devices/technicforcesensor.ts @@ -19,7 +19,7 @@ export class TechnicForceSensor extends Device { switch (mode) { case Mode.FORCE: - const force = message[4] / 10; + const force = message[this.isWeDo2SmartHub ? 2 : 4] / 10; /** * Emits when force is applied. diff --git a/src/devices/techniclargeangularmotor.ts b/src/devices/techniclargeangularmotor.ts index ce71958..689b8f5 100644 --- a/src/devices/techniclargeangularmotor.ts +++ b/src/devices/techniclargeangularmotor.ts @@ -10,8 +10,8 @@ import * as Consts from "../consts"; */ export class TechnicLargeAngularMotor extends AbsoluteMotor { - constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, {}, Consts.DeviceType.TECHNIC_LARGE_ANGULAR_MOTOR); + constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.TECHNIC_LARGE_ANGULAR_MOTOR) { + super(hub, portId, {}, type); } } diff --git a/src/devices/technicmediumangularmotor.ts b/src/devices/technicmediumangularmotor.ts index eeb5f1b..d97d48b 100644 --- a/src/devices/technicmediumangularmotor.ts +++ b/src/devices/technicmediumangularmotor.ts @@ -10,8 +10,8 @@ import * as Consts from "../consts"; */ export class TechnicMediumAngularMotor extends AbsoluteMotor { - constructor (hub: IDeviceInterface, portId: number) { - super(hub, portId, {}, Consts.DeviceType.TECHNIC_MEDIUM_ANGULAR_MOTOR); + constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number} = {}, type: Consts.DeviceType = Consts.DeviceType.TECHNIC_MEDIUM_ANGULAR_MOTOR) { + super(hub, portId, {}, type); } } diff --git a/src/hubs/basehub.ts b/src/hubs/basehub.ts index 17ce350..a6c06b1 100644 --- a/src/hubs/basehub.ts +++ b/src/hubs/basehub.ts @@ -422,7 +422,7 @@ export class BaseHub extends EventEmitter { constructor = deviceConstructors[deviceType as Consts.DeviceType]; if (constructor) { - return new constructor(this, portId); + return new constructor(this, portId, undefined, deviceType); } else { return new Device(this, portId, undefined, deviceType); }