Better common hub code
This commit is contained in:
parent
b0400856cc
commit
ab08218595
58
boosthub.js
58
boosthub.js
@ -171,64 +171,8 @@ class BoostHub extends Hub {
|
||||
}
|
||||
|
||||
port.connected = (data[4] === 1 || data[4] === 2) ? true : false;
|
||||
this._registerDeviceAttachment(port, data[5]);
|
||||
|
||||
if (port.connected) {
|
||||
switch (data[5]) {
|
||||
case Consts.Devices.WEDO2_TILT:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_TILT;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_TILT`);
|
||||
this._activatePortDevice(port.value, port.type, 0x00, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_DISTANCE:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_DISTANCE;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_DISTANCE`);
|
||||
this._activatePortDevice(port.value, port.type, 0x00, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_MOTOR`);
|
||||
this._activatePortDevice(port.value, port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_DISTANCE:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_DISTANCE;
|
||||
debug(`Port ${port.id} connected, detected BOOST_DISTANCE`);
|
||||
this._activatePortDevice(port.value, port.type, 0x08, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_INTERACTIVE_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected BOOST_INTERACTIVE_MOTOR`);
|
||||
this._activatePortDevice(port.value, port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_MOVE_HUB_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_MOVE_HUB_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected BOOST_MOVE_HUB_MOTOR`);
|
||||
this._activatePortDevice(port.value, port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_TILT:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_TILT;
|
||||
debug(`Port ${port.id} connected, detected BOOST_TILT`);
|
||||
this._activatePortDevice(port.value, port.type, 0x04, 0x00);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
port.type = null;
|
||||
debug(`Port ${port.id} disconnected`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
62
hub.js
62
hub.js
@ -95,6 +95,68 @@ class Hub extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
_registerDeviceAttachment (port, type) {
|
||||
|
||||
if (port.connected) {
|
||||
switch (type) {
|
||||
case Consts.Devices.WEDO2_TILT:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_TILT;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_TILT`);
|
||||
this._activatePortDevice(port.value, port.type, 0x00, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_DISTANCE:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_DISTANCE;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_DISTANCE`);
|
||||
this._activatePortDevice(port.value, port.type, 0x00, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_MOTOR`);
|
||||
this._activatePortDevice(port.value, port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_DISTANCE:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_DISTANCE;
|
||||
debug(`Port ${port.id} connected, detected BOOST_DISTANCE`);
|
||||
this._activatePortDevice(port.value, port.type, this.type == Consts.Hubs.WEDO2_SMART_HUB ? 0x00 : 0x08, 0x00); // NK: 0x00 for WeDo 2.0 Smart hub, 0x08 for Boost Move Hub
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_INTERACTIVE_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected BOOST_INTERACTIVE_MOTOR`);
|
||||
this._activatePortDevice(port.value, port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_MOVE_HUB_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_MOVE_HUB_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected BOOST_MOVE_HUB_MOTOR`);
|
||||
this._activatePortDevice(port.value, port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_TILT:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_TILT;
|
||||
debug(`Port ${port.id} connected, detected BOOST_TILT`);
|
||||
this._activatePortDevice(port.value, port.type, 0x04, 0x00);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
port.type = null;
|
||||
debug(`Port ${port.id} disconnected`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
8
test.js
8
test.js
@ -19,9 +19,9 @@ lpf2.on("discover", (hub) => {
|
||||
moveHub.on("distance", (port, distance) => {
|
||||
console.log(`Distance ${distance} received on port ${port}`);
|
||||
if (distance < 90) {
|
||||
moveHub.setMotorSpeed("D", 40);
|
||||
wedoHub.setMotorSpeed("B", 40);
|
||||
} else {
|
||||
moveHub.setMotorSpeed("D", 0);
|
||||
wedoHub.setMotorSpeed("B", 0);
|
||||
}
|
||||
});
|
||||
|
||||
@ -32,9 +32,9 @@ lpf2.on("discover", (hub) => {
|
||||
wedoHub.on("distance", (port, distance) => {
|
||||
console.log(`Distance ${distance} received on port ${port}`);
|
||||
if (distance < 90) {
|
||||
wedoHub.setMotorSpeed("B", 40);
|
||||
moveHub.setMotorSpeed("D", 40);
|
||||
} else {
|
||||
wedoHub.setMotorSpeed("B", 0);
|
||||
moveHub.setMotorSpeed("D", 0);
|
||||
}
|
||||
});
|
||||
|
||||
|
52
wedo2hub.js
52
wedo2hub.js
@ -103,57 +103,7 @@ class WeDo2Hub extends Hub {
|
||||
}
|
||||
|
||||
port.connected = data[1] === 1 ? true : false;
|
||||
|
||||
if (port.connected) {
|
||||
switch (data[3]) {
|
||||
case Consts.Devices.WEDO2_TILT:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_TILT;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_TILT`);
|
||||
this._activatePortDevice(data[0], port.type, 0x00, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_DISTANCE:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_DISTANCE;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_DISTANCE`);
|
||||
this._activatePortDevice(data[0], port.type, 0x00, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.WEDO2_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected WEDO2_MOTOR`);
|
||||
this._activatePortDevice(data[0], port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_DISTANCE:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_DISTANCE;
|
||||
debug(`Port ${port.id} connected, detected BOOST_DISTANCE`);
|
||||
// NK: BOOST_DISTANCE only supports color when connected to WeDo 2.0 Smart Hub
|
||||
this._activatePortDevice(data[0], port.type, 0x00, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_INTERACTIVE_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected BOOST_INTERACTIVE_MOTOR`);
|
||||
this._activatePortDevice(data[0], port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_MOVE_HUB_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_MOVE_HUB_MOTOR;
|
||||
debug(`Port ${port.id} connected, detected BOOST_MOVE_HUB_MOTOR`);
|
||||
this._activatePortDevice(data[0], port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
port.type = null;
|
||||
debug(`Port ${port.id} disconnected`);
|
||||
}
|
||||
this._registerDeviceAttachment(port, data[3]);
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user