diff --git a/boostmovehub.ts b/boostmovehub.ts index 27df0e8..3a04fd8 100644 --- a/boostmovehub.ts +++ b/boostmovehub.ts @@ -27,7 +27,7 @@ export class BoostMoveHub extends LPF2Hub { public static IsBoostMoveHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.BOOST_MOVE_HUB_ID); } diff --git a/consts.ts b/consts.ts index 8972fe9..f300b9b 100644 --- a/consts.ts +++ b/consts.ts @@ -69,22 +69,22 @@ export enum BLEManufacturerData { export enum BLEServices { - WEDO2_SMART_HUB = "000015231212efde1523785feabcd123", - LPF2_HUB = "000016231212efde1623785feabcd123" + WEDO2_SMART_HUB = "00001523-1212-efde-1523-785feabcd123", + LPF2_HUB = "00001623-1212-efde-1623-785feabcd123" } export enum BLECharacteristics { WEDO2_BATTERY = "2a19", - WEDO2_BUTTON = "000015261212efde1523785feabcd123", // "1526" - WEDO2_PORT_TYPE = "000015271212efde1523785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub - WEDO2_LOW_VOLTAGE_ALERT = "000015281212efde1523785feabcd123", // "1528" - WEDO2_HIGH_CURRENT_ALERT = "000015291212efde1523785feabcd123", // "1529" - WEDO2_LOW_SIGNAL_ALERT = "0000152a1212efde1523785feabcd123", // "152a" - WEDO2_SENSOR_VALUE = "000015601212efde1523785feabcd123", // "1560" - WEDO2_VALUE_FORMAT = "000015611212efde1523785feabcd123", // "1561" - WEDO2_PORT_TYPE_WRITE = "000015631212efde1523785feabcd123", // "1563" - WEDO2_MOTOR_VALUE_WRITE = "000015651212efde1523785feabcd123", // "1565" - WEDO2_NAME_ID = "000015241212efde1523785feabcd123", // "1524" - LPF2_ALL = "000016241212efde1623785feabcd123" + WEDO2_BUTTON = "00001526-1212-efde-1523-785feabcd123", // "1526" + WEDO2_PORT_TYPE = "00001527-1212-efde-1523-785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub + WEDO2_LOW_VOLTAGE_ALERT = "00001528-1212-efde-1523-785feabcd123", // "1528" + WEDO2_HIGH_CURRENT_ALERT = "00001529-1212-efde-1523-785feabcd123", // "1529" + WEDO2_LOW_SIGNAL_ALERT = "0000152a-1212-efde-1523-785feabcd123", // "152a" + WEDO2_SENSOR_VALUE = "00001560-1212-efde-1523-785feabcd123", // "1560" + WEDO2_VALUE_FORMAT = "00001561-1212-efde-1523-785feabcd123", // "1561" + WEDO2_PORT_TYPE_WRITE = "00001563-1212-efde-1523-785feabcd123", // "1563" + WEDO2_MOTOR_VALUE_WRITE = "00001565-1212-efde-1523-785feabcd123", // "1565" + WEDO2_NAME_ID = "00001524-1212-efde-1523-785feabcd123", // "1524" + LPF2_ALL = "00001624-1212-efde-1623-785feabcd123" } diff --git a/duplotrainbase.ts b/duplotrainbase.ts index 7a4a117..79ede2b 100644 --- a/duplotrainbase.ts +++ b/duplotrainbase.ts @@ -52,7 +52,7 @@ export class DuploTrainBase extends LPF2Hub { public static IsDuploTrainBase (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.DUPLO_TRAIN_HUB_ID); } diff --git a/examples/vernie_avoidance.js b/examples/vernie_avoidance.js new file mode 100644 index 0000000..698d111 --- /dev/null +++ b/examples/vernie_avoidance.js @@ -0,0 +1,45 @@ +/* + * + * This example allows you to connect Vernie and a Powered UP Remote Control to your laptop, and enables the control of Vernie with the Remote. + * + */ + +const PoweredUP = require(".."); + +const poweredUP = new PoweredUP.PoweredUP(); +poweredUP.scan(); // Start scanning + +console.log("Looking for Vernie..."); + +const Modes = { + AVOIDING: 0, + MOVING: 1 +} + +poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote + + const vernie = hub; + await vernie.connect(); + console.log("Connected to Vernie!"); + + let mode = Modes.MOVING; + + vernie.setMotorSpeed("AB", 50); + + vernie.on("distance", async (port, distance) => { + + if (distance < 180 && mode === Modes.MOVING) { + mode = Modes.AVOIDING; + await vernie.setMotorSpeed("AB", 0, 1000); + await vernie.setMotorSpeed("AB", -20, 1000); + await vernie.setMotorSpeed("AB", 0, 1000); + vernie.setMotorSpeed("A", 30, 500); + await vernie.setMotorSpeed("B", -30, 500); + await vernie.setMotorSpeed("AB", 0, 1000); + vernie.setMotorSpeed("AB", 50); + mode = Modes.MOVING; + } + + }); + +}); \ No newline at end of file diff --git a/hub.ts b/hub.ts index 611b6e4..69562c2 100644 --- a/hub.ts +++ b/hub.ts @@ -216,6 +216,11 @@ 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); diff --git a/lpf2hub.ts b/lpf2hub.ts index 4f3fd92..56fd40f 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -37,7 +37,7 @@ export class LPF2Hub extends Hub { public connect () { return new Promise(async (resolve, reject) => { await super.connect(); - const characteristic = this._characteristics[Consts.BLECharacteristics.LPF2_ALL]; + const characteristic = this._getCharacteristic(Consts.BLECharacteristics.LPF2_ALL); this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports @@ -101,7 +101,7 @@ export class LPF2Hub extends Hub { protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) { - const characteristic = this._characteristics[uuid]; + const characteristic = this._getCharacteristic(uuid); if (characteristic) { message = Buffer.concat([Buffer.alloc(2), message]); message[0] = message.length; diff --git a/puphub.ts b/puphub.ts index 30e6a4b..1b6c726 100644 --- a/puphub.ts +++ b/puphub.ts @@ -32,7 +32,7 @@ export class PUPHub extends LPF2Hub { public static IsPUPHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_HUB_ID); } diff --git a/pupremote.ts b/pupremote.ts index 1e7d837..ce8ccf4 100644 --- a/pupremote.ts +++ b/pupremote.ts @@ -57,7 +57,7 @@ export class PUPRemote extends LPF2Hub { public static IsPUPRemote (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.LPF2_HUB.replace(/-/g, "")) >= 0 && peripheral.advertisement.manufacturerData[3] === Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID); } diff --git a/wedo2smarthub.ts b/wedo2smarthub.ts index 0aed878..99b7923 100644 --- a/wedo2smarthub.ts +++ b/wedo2smarthub.ts @@ -26,7 +26,7 @@ export class WeDo2SmartHub extends Hub { public static IsWeDo2SmartHub (peripheral: Peripheral) { - return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB) >= 0); + return (peripheral.advertisement.serviceUuids.indexOf(Consts.BLEServices.WEDO2_SMART_HUB.replace(/-/g, "")) >= 0); } @@ -49,12 +49,12 @@ export class WeDo2SmartHub extends Hub { return new Promise(async (resolve, reject) => { debug("Connecting to WeDo 2.0 Smart Hub"); await super.connect(); - this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE], this._parsePortMessage.bind(this)); - this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_SENSOR_VALUE], this._parseSensorMessage.bind(this)); - this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_BUTTON], this._parseSensorMessage.bind(this)); - this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_BATTERY], this._parseBatteryMessage.bind(this)); - this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_HIGH_CURRENT_ALERT], this._parseHighCurrentAlert.bind(this)); - this._characteristics[Consts.BLECharacteristics.WEDO2_BATTERY].read((err, data) => { + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_PORT_TYPE), this._parsePortMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_SENSOR_VALUE), this._parseSensorMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BUTTON), this._parseSensorMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY), this._parseBatteryMessage.bind(this)); + this._subscribeToCharacteristic(this._getCharacteristic(Consts.BLECharacteristics.WEDO2_HIGH_CURRENT_ALERT), this._parseHighCurrentAlert.bind(this)); + this._getCharacteristic(Consts.BLECharacteristics.WEDO2_BATTERY).read((err, data) => { this._parseBatteryMessage(data); }); debug("Connect completed"); @@ -237,7 +237,7 @@ export class WeDo2SmartHub extends Hub { private _writeMessage (uuid: string, message: Buffer, callback?: () => void) { - const characteristic = this._characteristics[uuid]; + const characteristic = this._getCharacteristic(uuid); if (characteristic) { characteristic.write(message, false, callback); }