diff --git a/examples/test.js b/examples/test.js new file mode 100644 index 0000000..5d0ff4e --- /dev/null +++ b/examples/test.js @@ -0,0 +1,18 @@ +const LPF2 = require("..").LPF2; + +const lpf2 = new LPF2(); +lpf2.scan(); // Start scanning for hubs + +console.log("Looking for Hubs..."); + +lpf2.on("discover", async (hub) => { // Wait to discover hubs + + await hub.connect(); // Connect to hub + console.log("Connected to Hub!"); + + setTimeout(() => { + hub.setMotorSpeed("A", 50, 500); + //hub.setMotorSpeed("B", 50); + }, 3000); + +}); \ No newline at end of file diff --git a/examples/vernie_remote.js b/examples/vernie_remote.js index a1ec73d..d621c37 100644 --- a/examples/vernie_remote.js +++ b/examples/vernie_remote.js @@ -11,11 +11,11 @@ let remote = null; lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote if (hub.type === LPF2.Consts.Hubs.BOOST_MOVE_HUB) { + vernie = hub; await vernie.connect(); console.log("Connected to Vernie!"); - } else if (hub.type === LPF2.Consts.Hubs.POWERED_UP_REMOTE) { remote = hub; diff --git a/hub.ts b/hub.ts index 3d92d2e..2d0f5e7 100644 --- a/hub.ts +++ b/hub.ts @@ -131,9 +131,9 @@ export class Hub extends EventEmitter { */ public subscribe (port: string, mode?: number) { return new Promise((resolve, reject) => { - let newMode = 0x00; - if (mode && !(typeof mode === "number")) { - newMode = this._getModeForDeviceType(this._ports[port].type); + let newMode = this._getModeForDeviceType(this._ports[port].type); + if (mode) { + newMode = mode; } this._activatePortDevice(this._ports[port].value, this._ports[port].type, newMode, 0x00, () => { return resolve(); @@ -216,7 +216,7 @@ export class Hub extends EventEmitter { if (port.connected) { port.type = type; if (this.autoSubscribe) { - this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00); + // this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00); /** * Emits when a motor or sensor is attached to the Hub. * @event Hub#attach diff --git a/lpf2hub.ts b/lpf2hub.ts index 8a56b3d..a420244 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -120,13 +120,23 @@ export class LPF2Hub extends Hub { return new Promise((resolve, reject) => { const portObj = this._ports[port]; if (time) { - portObj.busy = true; - const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - data.writeUInt16LE(time > 65535 ? 65535 : time, 6); - this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); - portObj.finished = () => { - return resolve(); - }; + if (portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) { + portObj.busy = true; + const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); + data.writeUInt16LE(time > 65535 ? 65535 : time, 6); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); + portObj.finished = () => { + return resolve(); + }; + } else { + const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); + setTimeout(() => { + const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); + return resolve(); + }, time); + } } else { const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); @@ -170,6 +180,8 @@ export class LPF2Hub extends Hub { private _writeMessage (uuid: string, message: Buffer, callback?: () => void) { + console.log("OUT"); + console.log(message); const characteristic = this._characteristics[uuid]; if (characteristic) { characteristic.write(message, false, callback); @@ -179,6 +191,9 @@ export class LPF2Hub extends Hub { private _parseMessage (data?: Buffer) { + console.log("IN"); + console.log(data); + if (data) { this._incomingData = Buffer.concat([this._incomingData, data]); } @@ -255,6 +270,7 @@ export class LPF2Hub extends Hub { port.connected = (data[4] === 1 || data[4] === 2) ? true : false; this._registerDeviceAttachment(port, data[5]); + console.log(port); } diff --git a/wedo2hub.ts b/wedo2hub.ts index 58570a1..a5a8266 100644 --- a/wedo2hub.ts +++ b/wedo2hub.ts @@ -102,7 +102,10 @@ export class WeDo2Hub extends Hub { return new Promise((resolve, reject) => { this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)])); if (time) { - setTimeout(resolve, time); + setTimeout(() => { + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, 0x00])); + return resolve(); + }, time); } else { return resolve(); }