This commit is contained in:
Nathan Kunicki 2018-07-31 11:12:10 +01:00
parent 2106586377
commit 0e00bfd653
5 changed files with 50 additions and 13 deletions

18
examples/test.js Normal file
View File

@ -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);
});

View File

@ -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;

8
hub.ts
View File

@ -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

View File

@ -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);
}

View File

@ -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();
}