Compatible with Powered Up Hub and Remote

This commit is contained in:
Nathan Kunicki 2018-06-30 19:18:47 +01:00
parent 184df80120
commit 73e7f89f6f
5 changed files with 60 additions and 18 deletions

View File

@ -23,8 +23,8 @@ While most LPF2 components and Hubs are compatible with each other, there are ex
| WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a><br /> <a href="https://brickset.com/sets/76112-1/">76112</a> |
| Boost Color and Distance Sensor | Sensor | *Partial (<a href="#compatibility-note-1">1</a>)* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Boost Interactive Motor | Motor/Sensor | *Partial (<a href="#compatibility-note-2">2</a>)* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Powered Up Train Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> |
| Powered Up LED Lights | Light | Yes | Yes | Yes | <a href="https://brickset.com/sets/88005-1/">88005</a> |
| Powered Up Train Motor | Motor | No | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> |
| Powered Up LED Lights | Light | Unknown | Unknown | Unknown | <a href="https://brickset.com/sets/88005-1/">88005</a> |
<a name="compatibility-note-1"></a><sub>(1) Only color mode is supported on the WeDo 2.0 Smart Hub at this point.</sub>

View File

@ -23,8 +23,8 @@ While most LPF2 components and Hubs are compatible with each other, there are ex
| WeDo 2.0 Medium Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/45300-1/">45300</a><br /> <a href="https://brickset.com/sets/76112-1/">76112</a> |
| Boost Color and Distance Sensor | Sensor | *Partial (<a href="#compatibility-note-1">1</a>)* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Boost Interactive Motor | Motor/Sensor | *Partial (<a href="#compatibility-note-2">2</a>)* | Yes | Yes | <a href="https://brickset.com/sets/17101-1/">17101</a> |
| Powered Up Train Motor | Motor | Yes | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> |
| Powered Up LED Lights | Light | Yes | Yes | Yes | <a href="https://brickset.com/sets/88005-1/">88005</a> |
| Powered Up Train Motor | Motor | No | Yes | Yes | <a href="https://brickset.com/sets/60197-1/">60197</a><br /><a href="https://brickset.com/sets/60198-1/">60198</a> |
| Powered Up LED Lights | Light | Unknown | Unknown | Unknown | <a href="https://brickset.com/sets/88005-1/">88005</a> |
<a name="compatibility-note-1"></a><sub>(1) Only color mode is supported on the WeDo 2.0 Smart Hub at this point.</sub>

View File

@ -9,13 +9,15 @@ export enum Hubs {
export enum Devices {
UNKNOWN = 0,
BASIC_MOTOR = 1,
TRAIN_MOTOR = 2,
BOOST_LED = 22,
WEDO2_TILT = 34,
WEDO2_DISTANCE = 35,
BOOST_DISTANCE = 37,
BOOST_INTERACTIVE_MOTOR = 38,
BOOST_MOVE_HUB_MOTOR = 39,
BOOST_TILT = 40
BOOST_TILT = 40,
REMOTE_BUTTON = 55
}
@ -35,14 +37,16 @@ export enum Colors {
export enum ButtonStates {
PRESSED = 0,
RELEASED = 1
RELEASED = 1,
UP = 2,
DOWN = 3,
STOP = 4
}
export enum BLENames {
WEDO2_SMART_HUB_NAME = "LPF2 Smart Hub 2 I/O",
BOOST_MOVE_HUB_NAME = "LEGO Move Hub",
POWERED_UP_HUB_NAME = "HUB NO.4",
POWERED_UP_REMOTE_NAME = "Handset"
export enum BLEManufacturerData {
BOOST_MOVE_HUB_ID = 64,
POWERED_UP_HUB_ID = 65,
POWERED_UP_REMOTE_ID = 66
}
export enum BLEServices {

4
hub.ts
View File

@ -276,6 +276,8 @@ export class Hub extends EventEmitter {
switch (type) {
case Consts.Devices.BASIC_MOTOR:
return 0x02;
case Consts.Devices.TRAIN_MOTOR:
return 0x02;
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
return 0x02;
case Consts.Devices.BOOST_MOVE_HUB_MOTOR:
@ -284,6 +286,8 @@ export class Hub extends EventEmitter {
return (this.type === Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08);
case Consts.Devices.BOOST_TILT:
return 0x04;
case Consts.Devices.REMOTE_BUTTON:
return 0x00;
default:
return 0x00;
}

View File

@ -31,21 +31,25 @@ export class LPF2Hub extends Hub {
constructor (peripheral: Peripheral, autoSubscribe: boolean = true) {
super(peripheral, autoSubscribe);
switch (peripheral.advertisement.localName) {
case Consts.BLENames.POWERED_UP_HUB_NAME:
switch (peripheral.advertisement.manufacturerData[3]) {
case Consts.BLEManufacturerData.POWERED_UP_HUB_ID:
{
this.type = Consts.Hubs.POWERED_UP_HUB;
this._ports = {
"A": new Port("A", 55),
"B": new Port("B", 56),
"A": new Port("A", 0),
"B": new Port("B", 1),
"AB": new Port("AB", 57)
};
debug("Discovered Powered Up Hub");
break;
}
case Consts.BLENames.POWERED_UP_REMOTE_NAME:
case Consts.BLEManufacturerData.POWERED_UP_REMOTE_ID:
{
this.type = Consts.Hubs.POWERED_UP_REMOTE;
this._ports = {
"LEFT": new Port("LEFT", 0),
"RIGHT": new Port("RIGHT", 1)
};
debug("Discovered Powered Up Remote");
break;
}
@ -69,7 +73,7 @@ export class LPF2Hub extends Hub {
public connect () {
return new Promise(async (resolve, reject) => {
debug("Connecting to Boost Move Hub");
debug("Connecting to Hub");
await super.connect();
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
@ -111,7 +115,11 @@ export class LPF2Hub extends Hub {
public setMotorSpeed (port: string, speed: number, time: number) {
return new Promise((resolve, reject) => {
const portObj = this._ports[port];
if (time) {
if (portObj.type === Consts.Devices.TRAIN_MOTOR) {
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data);
return resolve();
} else if (time) {
portObj.busy = true;
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);
@ -359,6 +367,32 @@ export class LPF2Hub extends Hub {
this.emit("tilt", port.id, tiltX, tiltY);
break;
}
case Consts.Devices.REMOTE_BUTTON:
{
switch (data[4]) {
case 0x01:
{
this.emit("button", port.id, Consts.ButtonStates.UP);
break;
}
case 0xff:
{
this.emit("button", port.id, Consts.ButtonStates.DOWN);
break;
}
case 0x7f:
{
this.emit("button", port.id, Consts.ButtonStates.STOP);
break;
}
case 0x00:
{
this.emit("button", port.id, Consts.ButtonStates.RELEASED);
break;
}
}
break;
}
}
}