allow RGB led control for the duplo train hub

This commit is contained in:
Michal Szafranski 2019-11-02 15:16:15 +01:00
parent 9a9d3ee4d5
commit 05daa648cb
4 changed files with 11 additions and 61 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "node-poweredup", "name": "node-poweredup",
"version": "2.5.3", "version": "3.5.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -26,6 +26,9 @@ export class DuploTrainBase extends LPF2Hub {
} }
protected _ledPort = 0x11;
constructor (device: IBLEDevice, autoSubscribe: boolean = true) { constructor (device: IBLEDevice, autoSubscribe: boolean = true) {
super(device, autoSubscribe); super(device, autoSubscribe);
this.type = Consts.HubType.DUPLO_TRAIN_HUB; this.type = Consts.HubType.DUPLO_TRAIN_HUB;
@ -47,25 +50,6 @@ export class DuploTrainBase extends LPF2Hub {
}); });
} }
/**
* Set the color of the LED on the train via a color value.
* @method DuploTrainBase#setLEDColor
* @param {Color} color
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => {
if (typeof color === "boolean") {
color = 0;
}
const data = Buffer.from([0x81, 0x11, 0x11, 0x51, 0x00, color]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
return resolve();
});
}
/** /**
* Set the motor speed on a given port. * Set the motor speed on a given port.
* @method DuploTrainBase#setMotorSpeed * @method DuploTrainBase#setMotorSpeed

View File

@ -16,6 +16,8 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo");
*/ */
export class LPF2Hub extends Hub { export class LPF2Hub extends Hub {
protected _ledPort: number = 0x32;
private _lastTiltX: number = 0; private _lastTiltX: number = 0;
private _lastTiltY: number = 0; private _lastTiltY: number = 0;
private _lastTiltZ: number = 0; private _lastTiltZ: number = 0;
@ -111,9 +113,9 @@ export class LPF2Hub 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) => {
let data = Buffer.from([0x41, 0x32, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]); let data = Buffer.from([0x41, this._ledPort, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x01, red, green, blue]); data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x01, red, green, blue]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
return resolve(); return resolve();
}); });

View File

@ -26,6 +26,9 @@ export class PUPRemote extends LPF2Hub {
} }
protected _ledPort = 0x34;
constructor (device: IBLEDevice, autoSubscribe: boolean = true) { constructor (device: IBLEDevice, autoSubscribe: boolean = true) {
super(device, autoSubscribe); super(device, autoSubscribe);
this.type = Consts.HubType.POWERED_UP_REMOTE; this.type = Consts.HubType.POWERED_UP_REMOTE;
@ -47,43 +50,4 @@ export class PUPRemote extends LPF2Hub {
} }
/**
* Set the color of the LED on the Remote via a color value.
* @method PUPRemote#setLEDColor
* @param {Color} color
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => {
let data = Buffer.from([0x41, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
if (typeof color === "boolean") {
color = 0;
}
data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x00, color]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
return resolve();
});
}
/**
* Set the color of the LED on the Hub via RGB values.
* @method PUPRemote#setLEDRGB
* @param {number} red
* @param {number} green
* @param {number} blue
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDRGB (red: number, green: number, blue: number) {
return new Promise((resolve, reject) => {
let data = Buffer.from([0x41, 0x34, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x01, red, green, blue]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
return resolve();
});
}
} }