_writeMessage function for LPF2Hub

This commit is contained in:
Nathan Kunicki 2018-06-28 18:09:29 +01:00
parent b64b852508
commit 15fff746da
2 changed files with 32 additions and 41 deletions

View File

@ -73,7 +73,7 @@ export class LPF2Hub extends Hub {
await super.connect(); await super.connect();
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
this._writeMessage(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]));
debug("Connect completed"); debug("Connect completed");
return resolve(); return resolve();
}); });
@ -89,12 +89,12 @@ export class LPF2Hub extends Hub {
public setLEDColor (color: number | boolean) { public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]); let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
this._writeMessage(data); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
if (color === false) { if (color === false) {
color = 0; color = 0;
} }
data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
this._writeMessage(data); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
return resolve(); return resolve();
}); });
} }
@ -115,13 +115,13 @@ export class LPF2Hub extends Hub {
portObj.busy = true; portObj.busy = true;
const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); 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); data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
this._writeMessage(data); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
portObj.finished = () => { portObj.finished = () => {
return resolve(); return resolve();
}; };
} else { } else {
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
this._writeMessage(data); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
return resolve(); return resolve();
} }
}); });
@ -143,7 +143,7 @@ export class LPF2Hub extends Hub {
const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
data.writeUInt32LE(angle, 6); data.writeUInt32LE(angle, 6);
data.writeUInt8(this._mapSpeed(speed), 10); data.writeUInt8(this._mapSpeed(speed), 10);
this._writeMessage(data); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
portObj.finished = () => { portObj.finished = () => {
return resolve(); return resolve();
}; };
@ -152,17 +152,17 @@ export class LPF2Hub extends Hub {
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
} }
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback);
} }
private _writeMessage (message: Buffer, callback?: () => void) { private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; const characteristic = this._characteristics[uuid];
if (characteristic) { if (characteristic) {
characteristic.write(message, false, callback); characteristic.write(message, false, callback);
} }

View File

@ -58,18 +58,14 @@ export class WeDo2Hub extends Hub {
*/ */
public setLEDColor (color: number | boolean) { public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
if (motorCharacteristic && portCharacteristic) { if (color === false) {
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); color = 0;
portCharacteristic.write(data, false);
if (color === false) {
color = 0;
}
data = Buffer.from([0x06, 0x04, 0x01, color]);
motorCharacteristic.write(data, false);
return resolve();
} }
data = Buffer.from([0x06, 0x04, 0x01, color]);
this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data);
return resolve();
}); });
} }
@ -84,15 +80,11 @@ export class WeDo2Hub extends Hub {
*/ */
public setLEDRGB (red: number, green: number, blue: number) { public setLEDRGB (red: number, green: number, blue: number) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; let data = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]);
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data);
if (motorCharacteristic && portCharacteristic) { data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]); this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data);
portCharacteristic.write(data1, false); return resolve();
const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
motorCharacteristic.write(data2, false);
return resolve();
}
}); });
} }
@ -106,27 +98,26 @@ export class WeDo2Hub extends Hub {
*/ */
public setMotorSpeed (port: string, speed: number) { public setMotorSpeed (port: string, speed: number) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)]));
if (characteristic) { return resolve();
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)]), false);
return resolve();
}
}); });
} }
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback);
if (characteristic) {
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), false, callback);
}
} }
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback);
}
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._characteristics[uuid];
if (characteristic) { if (characteristic) {
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), false, callback); characteristic.write(message, false, callback);
} }
} }