Better handling of message lengths when sending packets
This commit is contained in:
parent
a39352703b
commit
24037d537c
@ -59,7 +59,7 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
if (color === false) {
|
||||
color = 0;
|
||||
}
|
||||
const data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
||||
const data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
return resolve();
|
||||
});
|
||||
@ -87,25 +87,25 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
let data = null;
|
||||
if (portObj.id === "AB") {
|
||||
if (speed instanceof Array) {
|
||||
data = Buffer.from([0x0d, 0x00, 0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||
} else {
|
||||
data = Buffer.from([0x0d, 0x00, 0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore: The type of speed is properly checked at the start
|
||||
data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([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, 4);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
portObj.finished = () => {
|
||||
return resolve();
|
||||
};
|
||||
} else {
|
||||
// @ts-ignore: The type of speed is properly checked at the start
|
||||
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
setTimeout(() => {
|
||||
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
|
||||
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
return resolve();
|
||||
}, time);
|
||||
@ -118,13 +118,13 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
let data = null;
|
||||
if (portObj.id === "AB") {
|
||||
if (speed instanceof Array) {
|
||||
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||
} else {
|
||||
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore: The type of speed is properly checked at the start
|
||||
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
}
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
portObj.finished = () => {
|
||||
@ -132,7 +132,7 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
};
|
||||
} else {
|
||||
// @ts-ignore: The type of speed is properly checked at the start
|
||||
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||
const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
}
|
||||
|
||||
@ -162,15 +162,15 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
let data = null;
|
||||
if (portObj.id === "AB") {
|
||||
if (speed instanceof Array) {
|
||||
data = Buffer.from([0x0f, 0x00, 0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
|
||||
} else {
|
||||
data = Buffer.from([0x0f, 0x00, 0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore: The type of speed is properly checked at the start
|
||||
data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
|
||||
}
|
||||
data.writeUInt32LE(angle, 6);
|
||||
data.writeUInt32LE(angle, 4);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
portObj.finished = () => {
|
||||
return resolve();
|
||||
|
15
lpf2hub.ts
15
lpf2hub.ts
@ -39,9 +39,9 @@ export class LPF2Hub extends Hub {
|
||||
await super.connect();
|
||||
const characteristic = this._characteristics[Consts.BLECharacteristics.LPF2_ALL];
|
||||
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); // Activate button reports
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
@ -58,9 +58,8 @@ export class LPF2Hub extends Hub {
|
||||
throw new Error("Name must be 14 characters or less");
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
let data = Buffer.from([0x00, 0x00, 0x01, 0x01, 0x01]);
|
||||
let data = Buffer.from([0x01, 0x01, 0x01]);
|
||||
data = Buffer.concat([data, Buffer.from(name, "ascii")]);
|
||||
data[0] = data.length;
|
||||
// Send this twice, as sometimes the first time doesn't take
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
@ -71,18 +70,20 @@ export class LPF2Hub extends Hub {
|
||||
|
||||
|
||||
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
|
||||
}
|
||||
|
||||
|
||||
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback);
|
||||
}
|
||||
|
||||
|
||||
protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
|
||||
const characteristic = this._characteristics[uuid];
|
||||
if (characteristic) {
|
||||
message = Buffer.concat([Buffer.alloc(2), message]);
|
||||
message[0] = message.length;
|
||||
characteristic.write(message, false, callback);
|
||||
}
|
||||
}
|
||||
|
18
puphub.ts
18
puphub.ts
@ -64,7 +64,7 @@ export class PUPHub extends LPF2Hub {
|
||||
if (color === false) {
|
||||
color = 0;
|
||||
}
|
||||
const data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
||||
const data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
return resolve();
|
||||
});
|
||||
@ -96,21 +96,21 @@ export class PUPHub extends LPF2Hub {
|
||||
let data = null;
|
||||
if (portObj.id === "AB") {
|
||||
if (speed instanceof Array) {
|
||||
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
|
||||
} else {
|
||||
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore: The type of speed is properly checked at the start
|
||||
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
||||
}
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
setTimeout(() => {
|
||||
let data = null;
|
||||
if (portObj.id === "AB") {
|
||||
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, 0x00]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00]);
|
||||
} else {
|
||||
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]);
|
||||
}
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
return resolve();
|
||||
@ -119,13 +119,13 @@ export class PUPHub extends LPF2Hub {
|
||||
let data = null;
|
||||
if (portObj.id === "AB") {
|
||||
if (speed instanceof Array) {
|
||||
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
|
||||
} else {
|
||||
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore: The type of speed is properly checked at the start
|
||||
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
||||
data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
||||
}
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
return resolve();
|
||||
|
@ -88,7 +88,7 @@ export class PUPRemote extends LPF2Hub {
|
||||
if (color === false) {
|
||||
color = 0;
|
||||
}
|
||||
const data = Buffer.from([0x08, 0x00, 0x81, 0x34, 0x11, 0x51, 0x00, color]);
|
||||
const data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x00, color]);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
return resolve();
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user