WeDo 2.0 Smart Hub and Boost Move Hub working
This commit is contained in:
parent
4f698a0e30
commit
00a2ed4bae
228
boosthub.js
Normal file
228
boosthub.js
Normal file
@ -0,0 +1,228 @@
|
||||
const debug = require("debug")("lpf2"),
|
||||
EventEmitter = require("events").EventEmitter;
|
||||
|
||||
const Hub = require("./hub.js"),
|
||||
Port = require("./port.js"),
|
||||
Consts = require("./consts.js");
|
||||
|
||||
|
||||
class BoostHub extends Hub {
|
||||
|
||||
|
||||
constructor (peripheral) {
|
||||
super(peripheral);
|
||||
this.type = Consts.Hubs.BOOST_MOVE_HUB;
|
||||
this.ports = {
|
||||
"A": new Port("A", 55),
|
||||
"B": new Port("B", 56),
|
||||
"C": new Port("C", 1),
|
||||
"D": new Port("D", 2)
|
||||
};
|
||||
this._lastTiltX = 0;
|
||||
this._lastTiltY = 0;
|
||||
debug("Discovered Boost Move Hub");
|
||||
}
|
||||
|
||||
|
||||
static isBoostHub (peripheral) {
|
||||
return (peripheral.advertisement.localName === Consts.BLE.Name.BOOST_MOVE_HUB_NAME && peripheral.advertisement.serviceUuids.indexOf(Consts.BLE.Services.BOOST_MOVE_HUB) >= 0);
|
||||
}
|
||||
|
||||
|
||||
connect (callback) {
|
||||
debug("Connecting to Boost Move Hub");
|
||||
super.connect(() => {
|
||||
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.Boost.ALL], this._parseMessage.bind(this));
|
||||
debug("Connect completed");
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
setMotorSpeed (port, speed, seconds) {
|
||||
const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
|
||||
if (characteristic) {
|
||||
const data = Buffer.from([0x0c, 0x00, 0x81, this.ports[port].value, 0x11, 0x09, 0xff, 0xff, speed, 0x64, 0x7f, 0x03]);
|
||||
if (seconds) {
|
||||
data.writeUInt16LE(seconds * 1000, 6);
|
||||
}
|
||||
characteristic.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_parseMessage (data) {
|
||||
|
||||
switch (data[2]) {
|
||||
case 0x04:
|
||||
{
|
||||
this._parsePortMessage(data);
|
||||
break;
|
||||
}
|
||||
case 0x45:
|
||||
{
|
||||
this._parseSensorMessage(data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_parsePortMessage (data) {
|
||||
|
||||
let port = null;
|
||||
|
||||
if (data[4] !== 1) { // NK: This doesn't support groups...yet.
|
||||
return;
|
||||
}
|
||||
|
||||
if (data[3] === 1) {
|
||||
port = this.ports["C"];
|
||||
} else if (data[3] === 2) {
|
||||
port = this.ports["D"];
|
||||
} else if (data[3] === 55) {
|
||||
port = this.ports["A"];
|
||||
} else if (data[3] === 56) {
|
||||
port = this.ports["B"];
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
port.connected = data[4] === 1 ? true : false;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
port.type = null;
|
||||
debug(`Port ${port.id} disconnected`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
_activatePortDevice (port, type, mode, format, callback) {
|
||||
const characteristic = this._characteristics[Consts.BLE.Characteristics.Boost.ALL];
|
||||
if (characteristic) {
|
||||
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_parseSensorMessage (data) {
|
||||
|
||||
let port = null;
|
||||
|
||||
if (data[3] === 1) {
|
||||
port = this.ports["C"];
|
||||
} else if (data[3] === 2) {
|
||||
port = this.ports["D"];
|
||||
} else if (data[3] === 55) {
|
||||
port = this.ports["A"];
|
||||
} else if (data[3] === 56) {
|
||||
port = this.ports["B"];
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (port && port.connected) {
|
||||
switch (port.type) {
|
||||
case Consts.Devices.WEDO2_DISTANCE:
|
||||
{
|
||||
let distance = data[4];
|
||||
if (data[5] === 1) {
|
||||
distance = data[4] + 255;
|
||||
}
|
||||
this.emit("distance", port.id, distance * 10);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_DISTANCE:
|
||||
{
|
||||
|
||||
this.emit("color", port.id, data[4]);
|
||||
|
||||
let distance;
|
||||
if (data[7] > 0 && data[5] < 2) {
|
||||
distance = Math.floor(20 - (data[7] * 2.85));
|
||||
} else if (data[5] > 9) {
|
||||
distance = 10;
|
||||
} else {
|
||||
distance = Math.floor((20 + (data[5] * 18)));
|
||||
}
|
||||
|
||||
if (distance > 130) {
|
||||
distance = 130;
|
||||
}
|
||||
|
||||
this.emit("distance", port.id, distance);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_TILT:
|
||||
{
|
||||
this._lastTiltX = data[4];
|
||||
if (this._lastTiltX > 100) {
|
||||
this._lastTiltX = -(255 - this._lastTiltX);
|
||||
}
|
||||
this._lastTiltY = data[5];
|
||||
if (this._lastTiltY > 100) {
|
||||
this._lastTiltY = -(255 - this._lastTiltY);
|
||||
}
|
||||
this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
|
||||
{
|
||||
const rotation = data.readInt32LE(2);
|
||||
this.emit("rotate", port.id, rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = BoostHub;
|
42
consts.js
42
consts.js
@ -1,8 +1,9 @@
|
||||
const Consts = {
|
||||
Hubs: {
|
||||
WEDO2_SMART_HUB: 0,
|
||||
BOOST_MOVE_HUB: 1,
|
||||
POWERED_UP_HUB: 2
|
||||
UNKNOWN: 0,
|
||||
WEDO2_SMART_HUB: 1,
|
||||
BOOST_MOVE_HUB: 2,
|
||||
POWERED_UP_HUB: 3
|
||||
},
|
||||
Devices: {
|
||||
WEDO2_MOTOR: 1,
|
||||
@ -11,28 +12,35 @@ const Consts = {
|
||||
WEDO2_DISTANCE: 35,
|
||||
BOOST_DISTANCE: 37,
|
||||
BOOST_INTERACTIVE_MOTOR: 38,
|
||||
BOOST_MOTOR: 39,
|
||||
BOOST_MOVE_HUB_MOTOR: 39,
|
||||
BOOST_TILT: 40
|
||||
},
|
||||
BLE: {
|
||||
Name: {
|
||||
WEDO2_SMART_HUB_NAME: "LPF2 Smart Hub 2 I/O"
|
||||
WEDO2_SMART_HUB_NAME: "LPF2 Smart Hub 2 I/O",
|
||||
BOOST_MOVE_HUB_NAME: "LEGO Move Hub"
|
||||
},
|
||||
Services: {
|
||||
WEDO2_SMART_HUB: "000015231212efde1523785feabcd123"
|
||||
WEDO2_SMART_HUB: "000015231212efde1523785feabcd123",
|
||||
BOOST_MOVE_HUB: "000016231212efde1623785feabcd123"
|
||||
},
|
||||
Characteristics: {
|
||||
BATTERY: "2a19",
|
||||
BUTTON: "000015261212efde1523785feabcd123", // "1526"
|
||||
PORT_TYPE: "000015271212efde1523785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub
|
||||
LOW_VOLTAGE_ALERT: "000015281212efde1523785feabcd123", // "1528"
|
||||
HIGH_CURRENT_ALERT: "000015291212efde1523785feabcd123", // "1529"
|
||||
LOW_SIGNAL_ALERT: "0000152a1212efde1523785feabcd123", // "152a"
|
||||
SENSOR_VALUE: "000015601212efde1523785feabcd123", // "1560"
|
||||
VALUE_FORMAT: "000015611212efde1523785feabcd123", // "1561"
|
||||
PORT_TYPE_WRITE: "000015631212efde1523785feabcd123", // "1563"
|
||||
MOTOR_VALUE_WRITE: "000015651212efde1523785feabcd123", // "1565"
|
||||
NAME_ID: "000015241212efde1523785feabcd123", // "1524"
|
||||
WeDo2: {
|
||||
BATTERY: "2a19",
|
||||
BUTTON: "000015261212efde1523785feabcd123", // "1526"
|
||||
PORT_TYPE: "000015271212efde1523785feabcd123", // "1527" // Handles plugging and unplugging of devices on WeDo 2.0 Smart Hub
|
||||
LOW_VOLTAGE_ALERT: "000015281212efde1523785feabcd123", // "1528"
|
||||
HIGH_CURRENT_ALERT: "000015291212efde1523785feabcd123", // "1529"
|
||||
LOW_SIGNAL_ALERT: "0000152a1212efde1523785feabcd123", // "152a"
|
||||
SENSOR_VALUE: "000015601212efde1523785feabcd123", // "1560"
|
||||
VALUE_FORMAT: "000015611212efde1523785feabcd123", // "1561"
|
||||
PORT_TYPE_WRITE: "000015631212efde1523785feabcd123", // "1563"
|
||||
MOTOR_VALUE_WRITE: "000015651212efde1523785feabcd123", // "1565"
|
||||
NAME_ID: "000015241212efde1523785feabcd123", // "1524"
|
||||
},
|
||||
Boost: {
|
||||
ALL: "000016241212efde1623785feabcd123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
167
hub.js
167
hub.js
@ -10,21 +10,13 @@ class Hub extends EventEmitter {
|
||||
|
||||
constructor (peripheral) {
|
||||
super();
|
||||
|
||||
this.deviceType = "WeDo 2.0 Smart Hub";
|
||||
|
||||
this._peripheral = peripheral;
|
||||
this._characteristics = {};
|
||||
this._batteryLevel = 100;
|
||||
this._rssi = -100; // Initialize as -100 - no signal
|
||||
|
||||
this._portTypeWriteCharacteristic = null;
|
||||
this._motorValueWriteCharacteristic = null;
|
||||
|
||||
this._lastTiltX = 0;
|
||||
this._lastTiltY = 0;
|
||||
|
||||
this.ports = [new Port(0), new Port(1)];
|
||||
|
||||
this._ports = {};
|
||||
this.type = Consts.Hubs.UNKNOWN;
|
||||
this.uuid = peripheral.uuid;
|
||||
}
|
||||
|
||||
|
||||
@ -69,20 +61,7 @@ class Hub extends EventEmitter {
|
||||
|
||||
service.discoverCharacteristics([], (err, characteristics) => {
|
||||
characteristics.forEach((characteristic) => {
|
||||
switch (characteristic.uuid) {
|
||||
case Consts.BLE.Characteristics.PORT_TYPE:
|
||||
self._subscribeToCharacteristic(characteristic, self._parsePortMessage.bind(self));
|
||||
break;
|
||||
case Consts.BLE.Characteristics.PORT_TYPE_WRITE:
|
||||
this._portTypeWriteCharacteristic = characteristic;
|
||||
break;
|
||||
case Consts.BLE.Characteristics.MOTOR_VALUE_WRITE:
|
||||
this._motorValueWriteCharacteristic = characteristic;
|
||||
break;
|
||||
case Consts.BLE.Characteristics.SENSOR_VALUE:
|
||||
this._subscribeToCharacteristic(characteristic, self._parseSensorMessage.bind(self));
|
||||
break;
|
||||
}
|
||||
this._characteristics[characteristic.uuid] = characteristic;
|
||||
});
|
||||
return resolve();
|
||||
});
|
||||
@ -92,6 +71,7 @@ class Hub extends EventEmitter {
|
||||
});
|
||||
|
||||
Promise.all(servicePromises).then(() => {
|
||||
debug("Service/characteristic discovery finished");
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
@ -104,21 +84,6 @@ class Hub extends EventEmitter {
|
||||
}
|
||||
|
||||
|
||||
setMotorSpeed (port, speed) {
|
||||
if (this._motorValueWriteCharacteristic) {
|
||||
let newSpeed = 0;
|
||||
if (speed > 1 && speed <= 100) {
|
||||
newSpeed = parseInt(this._speedMapping(speed, 1, 100, 15, 97));
|
||||
} else if (speed < -1 && speed >= -100) {
|
||||
newSpeed = parseInt(this._speedMapping(speed, -100, -1, 160, 245));
|
||||
} else {
|
||||
newSpeed = 0;
|
||||
}
|
||||
this._motorValueWriteCharacteristic.write(Buffer.from([port + 1, 0x01, 0x02, speed]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_subscribeToCharacteristic (characteristic, callback) {
|
||||
characteristic.on("data", (data, isNotification) => {
|
||||
return callback(data);
|
||||
@ -130,126 +95,6 @@ class Hub extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
_parseSensorMessage (data) {
|
||||
|
||||
console.log(data);
|
||||
|
||||
const port = this.ports[data[1] - 1];
|
||||
|
||||
if (port && port.connected) {
|
||||
switch (port.type) {
|
||||
case Consts.Devices.WEDO2_DISTANCE:
|
||||
{
|
||||
let distance = data[2];
|
||||
if (data[3] === 1) {
|
||||
distance = data[2] + 255;
|
||||
}
|
||||
this.emit("distance", data[1] - 1, distance);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_DISTANCE:
|
||||
{
|
||||
let distance = data[2];
|
||||
this.emit("distance", data[1] - 1, distance);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_TILT:
|
||||
{
|
||||
this._lastTiltX = data[2];
|
||||
if (this._lastTiltX > 100) {
|
||||
this._lastTiltX = -(255 - this._lastTiltX);
|
||||
}
|
||||
this._lastTiltY = data[3];
|
||||
if (this._lastTiltY > 100) {
|
||||
this._lastTiltY = -(255 - this._lastTiltY);
|
||||
}
|
||||
this.emit("tilt", port, this._lastTiltX, this._lastTiltX);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
|
||||
{
|
||||
const rotation = data.readInt32LE(2);
|
||||
console.log(rotation);
|
||||
this.emit("rotate", port, rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
_parsePortMessage (data) {
|
||||
|
||||
if (data[0] === 1 || data[0] === 2) {
|
||||
const port = this.ports[data[0] - 1];
|
||||
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 ${data[0] - 1} 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 ${data[0] - 1} 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 ${data[0] - 1} 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 ${data[0] - 1} connected, detected BOOST_DISTANCE`);
|
||||
this._activatePortDevice(data[0], port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
|
||||
{
|
||||
port.type = Consts.Devices.BOOST_INTERACTIVE_MOTOR;
|
||||
debug(`Port ${data[0] - 1} connected, detected BOOST_INTERACTIVE_MOTOR`);
|
||||
this._activatePortDevice(data[0], port.type, 0x02, 0x00);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
port.type = null;
|
||||
debug(`Port ${data[0]} disconnected`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
_speedMapping (speed, inMin, inMax, outMin, outMax) {
|
||||
if (speed > inMax) {
|
||||
speed = inMax;
|
||||
}
|
||||
if (speed < inMin) {
|
||||
speed = inMax;
|
||||
}
|
||||
return (speed - inMax) * (outMax - outMin) / (inMax - inMin) + outMin;
|
||||
}
|
||||
|
||||
|
||||
_activatePortDevice (port, type, mode, format, callback) {
|
||||
if (this._portTypeWriteCharacteristic) {
|
||||
this._portTypeWriteCharacteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
63
lpf2.js
63
lpf2.js
@ -2,7 +2,8 @@ const noble = require("noble"),
|
||||
debug = require("debug")("lpf2"),
|
||||
EventEmitter = require("events").EventEmitter;
|
||||
|
||||
const Hub = require("./hub.js"),
|
||||
const WeDo2Hub = require("./wedo2hub.js"),
|
||||
BoostHub = require("./boosthub.js"),
|
||||
Consts = require("./consts.js");
|
||||
|
||||
let ready = false,
|
||||
@ -12,6 +13,7 @@ noble.on("stateChange", (state) => {
|
||||
ready = (state === "poweredOn");
|
||||
if (ready) {
|
||||
if (wantScan) {
|
||||
debug("Scanning started");
|
||||
noble.startScanning();
|
||||
}
|
||||
} else {
|
||||
@ -33,38 +35,43 @@ class LPF2 extends EventEmitter {
|
||||
|
||||
noble.on("discover", (peripheral) => {
|
||||
|
||||
let advertisement = peripheral.advertisement;
|
||||
|
||||
if (advertisement.localName === Consts.BLE.Name.WEDO2_SMART_HUB_NAME && advertisement.serviceUuids.indexOf(Consts.BLE.Services.WEDO2_SMART_HUB) >= 0) {
|
||||
|
||||
peripheral.removeAllListeners();
|
||||
noble.stopScanning();
|
||||
noble.startScanning();
|
||||
|
||||
const hub = new Hub(peripheral);
|
||||
|
||||
hub._peripheral.on("connect", () => {
|
||||
debug("Hub connected");
|
||||
this._connectedDevices[hub.uuid] = hub;
|
||||
});
|
||||
|
||||
hub._peripheral.on("disconnect", () => {
|
||||
debug("Peripheral disconnected");
|
||||
delete this._connectedDevices[hub.uuid];
|
||||
|
||||
if (wantScan) {
|
||||
noble.startScanning();
|
||||
}
|
||||
|
||||
hub.emit("disconnect");
|
||||
});
|
||||
|
||||
this.emit("discover", hub);
|
||||
let hub = null;
|
||||
|
||||
if (WeDo2Hub.isWeDo2Hub(peripheral)) {
|
||||
hub = new WeDo2Hub(peripheral);
|
||||
} else if (BoostHub.isBoostHub(peripheral)) {
|
||||
hub = new BoostHub(peripheral);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
peripheral.removeAllListeners();
|
||||
noble.stopScanning();
|
||||
noble.startScanning();
|
||||
|
||||
hub._peripheral.on("connect", () => {
|
||||
debug(`Hub ${hub.uuid} connected`);
|
||||
this._connectedDevices[hub.uuid] = hub;
|
||||
});
|
||||
|
||||
hub._peripheral.on("disconnect", () => {
|
||||
debug(`Hub ${hub.uuid} disconnected`);
|
||||
delete this._connectedDevices[hub.uuid];
|
||||
|
||||
if (wantScan) {
|
||||
noble.startScanning();
|
||||
}
|
||||
|
||||
hub.emit("disconnect");
|
||||
});
|
||||
|
||||
debug(`Hub ${hub.uuid} discovered`);
|
||||
this.emit("discover", hub);
|
||||
|
||||
});
|
||||
|
||||
if (ready) {
|
||||
debug("Scanning started");
|
||||
noble.startScanning();
|
||||
}
|
||||
}
|
||||
|
3
port.js
3
port.js
@ -1,7 +1,8 @@
|
||||
class Port {
|
||||
|
||||
constructor (id) {
|
||||
constructor (id, value) {
|
||||
this.id = id;
|
||||
this.value = value;
|
||||
this.connected = false;
|
||||
this.type = null;
|
||||
}
|
||||
|
27
test.js
27
test.js
@ -6,13 +6,22 @@ lpf2.scan();
|
||||
|
||||
lpf2.on("discover", (hub) => {
|
||||
hub.connect();
|
||||
/*hub.on("tilt", (port, x, y) => {
|
||||
hub.setMotorSpeed(0, y);
|
||||
});*/
|
||||
let speed = 0;
|
||||
// setInterval(() => {
|
||||
// console.log(speed);
|
||||
// hub.setMotorSpeed(0, speed);
|
||||
// speed += 1;
|
||||
// }, 500);
|
||||
hub.on("distance", (port, distance) => {
|
||||
console.log(`Distance ${distance} received on port ${port}`);
|
||||
});
|
||||
hub.on("color", (port, color) => {
|
||||
console.log(`Color ${color} received on port ${port}`);
|
||||
});
|
||||
hub.on("tilt", (port, x, y) => {
|
||||
console.log(`Tilt ${x}, ${y} received on port ${port}`);
|
||||
});
|
||||
hub.on("rotate", (port, rotate) => {
|
||||
console.log(`Rotate ${rotate} received on port ${port}`);
|
||||
});
|
||||
// setTimeout(() => {
|
||||
// hub.setMotorSpeed("C", 30);
|
||||
// setTimeout(() => {
|
||||
// hub.setMotorSpeed("C", 0);
|
||||
// }, 3000);
|
||||
// }, 3000);
|
||||
});
|
179
wedo2hub.js
Normal file
179
wedo2hub.js
Normal file
@ -0,0 +1,179 @@
|
||||
const debug = require("debug")("lpf2"),
|
||||
EventEmitter = require("events").EventEmitter;
|
||||
|
||||
const Hub = require("./hub.js"),
|
||||
Port = require("./port.js"),
|
||||
Consts = require("./consts.js");
|
||||
|
||||
|
||||
class WeDo2Hub extends Hub {
|
||||
|
||||
|
||||
constructor (peripheral) {
|
||||
super(peripheral);
|
||||
this.type = Consts.Hubs.WEDO2_SMART_HUB;
|
||||
this.ports = {
|
||||
"A": new Port("A", 1),
|
||||
"B": new Port("B", 2)
|
||||
};
|
||||
this._lastTiltX = 0;
|
||||
this._lastTiltY = 0;
|
||||
debug("Discovered WeDo 2.0 Smart Hub");
|
||||
}
|
||||
|
||||
|
||||
static isWeDo2Hub (peripheral) {
|
||||
return (peripheral.advertisement.localName === Consts.BLE.Name.WEDO2_SMART_HUB_NAME && peripheral.advertisement.serviceUuids.indexOf(Consts.BLE.Services.WEDO2_SMART_HUB) >= 0);
|
||||
}
|
||||
|
||||
|
||||
connect (callback) {
|
||||
debug("Connecting to WeDo 2.0 Smart Hub");
|
||||
super.connect(() => {
|
||||
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE], this._parsePortMessage.bind(this));
|
||||
this._subscribeToCharacteristic(this._characteristics[Consts.BLE.Characteristics.WeDo2.SENSOR_VALUE], this._parseSensorMessage.bind(this));
|
||||
debug("Connect completed");
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
setMotorSpeed (port, speed) {
|
||||
const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.MOTOR_VALUE_WRITE];
|
||||
if (characteristic) {
|
||||
characteristic.write(Buffer.from([this.ports[port].value, 0x01, 0x02, speed]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_parsePortMessage (data) {
|
||||
|
||||
let port = null;
|
||||
|
||||
if (data[0] === 1) {
|
||||
port = this.ports["A"];
|
||||
} else if (data[0] === 2) {
|
||||
port = this.ports["B"];
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
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`);
|
||||
this._activatePortDevice(data[0], port.type, 0x02, 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`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
_activatePortDevice (port, type, mode, format, callback) {
|
||||
const characteristic = this._characteristics[Consts.BLE.Characteristics.WeDo2.PORT_TYPE_WRITE];
|
||||
if (characteristic) {
|
||||
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_parseSensorMessage (data) {
|
||||
|
||||
let port = null;
|
||||
|
||||
if (data[1] === 1) {
|
||||
port = this.ports["A"];
|
||||
} else if (data[1] === 2) {
|
||||
port = this.ports["B"];
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (port && port.connected) {
|
||||
switch (port.type) {
|
||||
case Consts.Devices.WEDO2_DISTANCE:
|
||||
{
|
||||
let distance = data[2];
|
||||
if (data[3] === 1) {
|
||||
distance = data[2] + 255;
|
||||
}
|
||||
this.emit("distance", port.id, distance);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_DISTANCE:
|
||||
{
|
||||
let distance = data[2];
|
||||
this.emit("distance", port.id, distance);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.WEDO2_TILT:
|
||||
{
|
||||
this._lastTiltX = data[2];
|
||||
if (this._lastTiltX > 100) {
|
||||
this._lastTiltX = -(255 - this._lastTiltX);
|
||||
}
|
||||
this._lastTiltY = data[3];
|
||||
if (this._lastTiltY > 100) {
|
||||
this._lastTiltY = -(255 - this._lastTiltY);
|
||||
}
|
||||
this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY);
|
||||
break;
|
||||
}
|
||||
case Consts.Devices.BOOST_INTERACTIVE_MOTOR:
|
||||
{
|
||||
const rotation = data.readInt32LE(2);
|
||||
this.emit("rotate", port.id, rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = WeDo2Hub;
|
Loading…
x
Reference in New Issue
Block a user