Testing
This commit is contained in:
parent
2106586377
commit
0e00bfd653
18
examples/test.js
Normal file
18
examples/test.js
Normal 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);
|
||||||
|
|
||||||
|
});
|
@ -11,11 +11,11 @@ let remote = null;
|
|||||||
lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote
|
lpf2.on("discover", async (hub) => { // Wait to discover Vernie and Remote
|
||||||
|
|
||||||
if (hub.type === LPF2.Consts.Hubs.BOOST_MOVE_HUB) {
|
if (hub.type === LPF2.Consts.Hubs.BOOST_MOVE_HUB) {
|
||||||
|
|
||||||
vernie = hub;
|
vernie = hub;
|
||||||
await vernie.connect();
|
await vernie.connect();
|
||||||
console.log("Connected to Vernie!");
|
console.log("Connected to Vernie!");
|
||||||
|
|
||||||
|
|
||||||
} else if (hub.type === LPF2.Consts.Hubs.POWERED_UP_REMOTE) {
|
} else if (hub.type === LPF2.Consts.Hubs.POWERED_UP_REMOTE) {
|
||||||
remote = hub;
|
remote = hub;
|
||||||
|
|
||||||
|
8
hub.ts
8
hub.ts
@ -131,9 +131,9 @@ export class Hub extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
public subscribe (port: string, mode?: number) {
|
public subscribe (port: string, mode?: number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let newMode = 0x00;
|
let newMode = this._getModeForDeviceType(this._ports[port].type);
|
||||||
if (mode && !(typeof mode === "number")) {
|
if (mode) {
|
||||||
newMode = this._getModeForDeviceType(this._ports[port].type);
|
newMode = mode;
|
||||||
}
|
}
|
||||||
this._activatePortDevice(this._ports[port].value, this._ports[port].type, newMode, 0x00, () => {
|
this._activatePortDevice(this._ports[port].value, this._ports[port].type, newMode, 0x00, () => {
|
||||||
return resolve();
|
return resolve();
|
||||||
@ -216,7 +216,7 @@ export class Hub extends EventEmitter {
|
|||||||
if (port.connected) {
|
if (port.connected) {
|
||||||
port.type = type;
|
port.type = type;
|
||||||
if (this.autoSubscribe) {
|
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.
|
* Emits when a motor or sensor is attached to the Hub.
|
||||||
* @event Hub#attach
|
* @event Hub#attach
|
||||||
|
30
lpf2hub.ts
30
lpf2hub.ts
@ -120,13 +120,23 @@ export class LPF2Hub extends Hub {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const portObj = this._ports[port];
|
const portObj = this._ports[port];
|
||||||
if (time) {
|
if (time) {
|
||||||
portObj.busy = true;
|
if (portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) {
|
||||||
const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
portObj.busy = true;
|
||||||
data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
|
const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
|
data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
|
||||||
portObj.finished = () => {
|
this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
|
||||||
return resolve();
|
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 {
|
} else {
|
||||||
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
|
||||||
@ -170,6 +180,8 @@ export class LPF2Hub extends Hub {
|
|||||||
|
|
||||||
|
|
||||||
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
|
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
|
||||||
|
console.log("OUT");
|
||||||
|
console.log(message);
|
||||||
const characteristic = this._characteristics[uuid];
|
const characteristic = this._characteristics[uuid];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
characteristic.write(message, false, callback);
|
characteristic.write(message, false, callback);
|
||||||
@ -179,6 +191,9 @@ export class LPF2Hub extends Hub {
|
|||||||
|
|
||||||
private _parseMessage (data?: Buffer) {
|
private _parseMessage (data?: Buffer) {
|
||||||
|
|
||||||
|
console.log("IN");
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
this._incomingData = Buffer.concat([this._incomingData, 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;
|
port.connected = (data[4] === 1 || data[4] === 2) ? true : false;
|
||||||
this._registerDeviceAttachment(port, data[5]);
|
this._registerDeviceAttachment(port, data[5]);
|
||||||
|
console.log(port);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,10 @@ export class WeDo2Hub extends Hub {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)]));
|
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)]));
|
||||||
if (time) {
|
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 {
|
} else {
|
||||||
return resolve();
|
return resolve();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user