_writeMessage function for LPF2Hub
This commit is contained in:
parent
c6395e109b
commit
d515fcd662
88
lpf2hub.ts
88
lpf2hub.ts
@ -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));
|
||||||
characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]), false);
|
this._writeMessage(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]));
|
||||||
debug("Connect completed");
|
debug("Connect completed");
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
@ -88,32 +88,18 @@ 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) => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
|
||||||
if (characteristic) {
|
this._writeMessage(data);
|
||||||
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
|
if (color === false) {
|
||||||
characteristic.write(data, false);
|
color = 0;
|
||||||
if (color === false) {
|
|
||||||
color = 0;
|
|
||||||
}
|
|
||||||
data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
|
||||||
characteristic.write(data, false);
|
|
||||||
}
|
}
|
||||||
|
data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
||||||
|
this._writeMessage(data);
|
||||||
return resolve();
|
return resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// setLEDRGB (red, green, blue) {
|
|
||||||
// const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
|
|
||||||
// if (characteristic) {
|
|
||||||
// let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x03]);
|
|
||||||
// characteristic.write(data);
|
|
||||||
// data = Buffer.from([0x0a, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, red, green, blue]);
|
|
||||||
// characteristic.write(data);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the motor speed on a given port.
|
* Set the motor speed on a given port.
|
||||||
* @method LPF2Hub#setMotorSpeed
|
* @method LPF2Hub#setMotorSpeed
|
||||||
@ -124,22 +110,19 @@ export class LPF2Hub extends Hub {
|
|||||||
*/
|
*/
|
||||||
public setMotorSpeed (port: string, speed: number, time: number) {
|
public setMotorSpeed (port: string, speed: number, time: number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const portObj = this._ports[port];
|
||||||
if (characteristic) {
|
if (time) {
|
||||||
const portObj = this._ports[port];
|
portObj.busy = true;
|
||||||
if (time) {
|
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(data);
|
||||||
data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
|
portObj.finished = () => {
|
||||||
characteristic.write(data, false);
|
|
||||||
portObj.finished = () => {
|
|
||||||
return resolve();
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
|
||||||
characteristic.write(data, false);
|
|
||||||
return resolve();
|
return resolve();
|
||||||
}
|
};
|
||||||
|
} else {
|
||||||
|
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||||
|
this._writeMessage(data);
|
||||||
|
return resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -155,34 +138,33 @@ export class LPF2Hub extends Hub {
|
|||||||
*/
|
*/
|
||||||
public setMotorAngle (port: string, angle: number, speed: number = 100) {
|
public setMotorAngle (port: string, angle: number, speed: number = 100) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const portObj = this._ports[port];
|
||||||
if (characteristic) {
|
portObj.busy = true;
|
||||||
const portObj = this._ports[port];
|
const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
|
||||||
portObj.busy = true;
|
data.writeUInt32LE(angle, 6);
|
||||||
const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
|
data.writeUInt8(this._mapSpeed(speed), 10);
|
||||||
data.writeUInt32LE(angle, 6);
|
this._writeMessage(data);
|
||||||
data.writeUInt8(this._mapSpeed(speed), 10);
|
portObj.finished = () => {
|
||||||
characteristic.write(data, false);
|
return resolve();
|
||||||
portObj.finished = () => {
|
};
|
||||||
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.BOOST_ALL];
|
this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
|
||||||
if (characteristic) {
|
|
||||||
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 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) {
|
||||||
|
this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private _writeMessage (message: Buffer, callback?: () => void) {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), false, callback);
|
characteristic.write(message, false, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user