Merge pull request #33 from nutki/pr-1

allow RGB led control for the duplo train hub
This commit is contained in:
Nathan Kellenicki 2019-11-10 12:19:48 -08:00 committed by GitHub
commit 2797081091
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 62 deletions

View File

@ -31,6 +31,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;
@ -52,25 +55,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

@ -20,6 +20,8 @@ export class LPF2Hub extends Hub {
return [t[0], t[1], t.substring(2, 4), t.substring(4)].join("."); return [t[0], t[1], t.substring(2, 4), t.substring(4)].join(".");
} }
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;
@ -93,12 +95,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([0x41, 0x32, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]); let data = Buffer.from([0x41, this._ledPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
if (typeof color === "boolean") { if (typeof color === "boolean") {
color = 0; color = 0;
} }
data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x00, color]); data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x00, color]);
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, data);
return resolve(); return resolve();
}); });
@ -115,9 +117,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

@ -31,6 +31,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;
@ -52,43 +55,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();
});
}
} }