Support for tilt, accel, temp on Control+ Hub
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Nathan Kellenicki 2019-08-07 11:04:51 -07:00
parent 373737ddaf
commit 6651b79188
31 changed files with 12679 additions and 1933 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"name": "node-poweredup", "name": "node-poweredup",
"version": "3.1.0", "version": "3.2.0",
"description": "A Javascript module to interface with LEGO Powered Up components.", "description": "A Javascript module to interface with LEGO Powered Up components.",
"homepage": "https://github.com/nathankellenicki/node-poweredup/", "homepage": "https://github.com/nathankellenicki/node-poweredup/",
"main": "dist/node/index-node.js", "main": "dist/node/index-node.js",

View File

@ -20,14 +20,6 @@ const debug = Debug("boostmovehub");
export class BoostMoveHub extends LPF2Hub { export class BoostMoveHub extends LPF2Hub {
// We set JSDoc to ignore these events as a Boost Move Hub will never emit them.
/**
* @event BoostMoveHub#speed
* @ignore
*/
public static IsBoostMoveHub (peripheral: Peripheral) { public static IsBoostMoveHub (peripheral: Peripheral) {
return (peripheral.advertisement && return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids &&

View File

@ -20,14 +20,6 @@ const debug = Debug("ControlPlusHub");
export class ControlPlusHub extends LPF2Hub { export class ControlPlusHub extends LPF2Hub {
// We set JSDoc to ignore these events as a Powered UP Remote will never emit them.
/**
* @event ControlPlusHub#speed
* @ignore
*/
public static IsControlPlusHub (peripheral: Peripheral) { public static IsControlPlusHub (peripheral: Peripheral) {
return (peripheral.advertisement && return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids &&
@ -56,6 +48,9 @@ export class ControlPlusHub extends LPF2Hub {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
debug("Connecting to Control+ Hub"); debug("Connecting to Control+ Hub");
await super.connect(); await super.connect();
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x62, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01])); // Accelerometer
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x63, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Gyro/Tilt
this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3d, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x01])); // Temperature
debug("Connect completed"); debug("Connect completed");
return resolve(); return resolve();
}); });

View File

@ -19,44 +19,6 @@ const debug = Debug("duplotrainbase");
export class DuploTrainBase extends LPF2Hub { export class DuploTrainBase extends LPF2Hub {
// We set JSDoc to ignore these events as a Duplo Train Base will never emit them.
/**
* @event DuploTrainBase#distance
* @ignore
*/
/**
* @event DuploTrainBase#colorAndDistance
* @ignore
*/
/**
* @event DuploTrainBase#tilt
* @ignore
*/
/**
* @event DuploTrainBase#rotate
* @ignore
*/
/**
* @event DuploTrainBase#button
* @ignore
*/
/**
* @event DuploTrainBase#attach
* @ignore
*/
/**
* @event DuploTrainBase#detach
* @ignore
*/
public static IsDuploTrainBase (peripheral: Peripheral) { public static IsDuploTrainBase (peripheral: Peripheral) {
return (peripheral.advertisement && return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids &&

View File

@ -18,6 +18,7 @@ export class LPF2Hub extends Hub {
private _lastTiltX: number = 0; private _lastTiltX: number = 0;
private _lastTiltY: number = 0; private _lastTiltY: number = 0;
private _lastTiltZ: number = 0;
private _messageBuffer: Buffer = Buffer.alloc(0); private _messageBuffer: Buffer = Buffer.alloc(0);
@ -380,6 +381,11 @@ export class LPF2Hub extends Hub {
const voltage = data.readUInt16LE(4); const voltage = data.readUInt16LE(4);
this._voltage = 9620.0 * voltage / 3893.0 / 1000.0; this._voltage = 9620.0 * voltage / 3893.0 / 1000.0;
return; return;
} else if ((data[3] === 0x3c && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Voltage (Control+ Hub)
data = this._padMessage(data, 6);
const voltage = data.readUInt16LE(4);
this._voltage = 9615.0 * voltage / 4095.0 / 1000.0;
return;
} else if (data[3] === 0x3c) { // Voltage (Others) } else if (data[3] === 0x3c) { // Voltage (Others)
data = this._padMessage(data, 6); data = this._padMessage(data, 6);
const voltage = data.readUInt16LE(4); const voltage = data.readUInt16LE(4);
@ -394,6 +400,44 @@ export class LPF2Hub extends Hub {
return; return;
} }
if ((data[3] === 0x62 && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Control+ Accelerometer
const accelX = Math.round((data.readInt16LE(4) / 28571) * 2000);
const accelY = Math.round((data.readInt16LE(6) / 28571) * 2000);
const accelZ = Math.round((data.readInt16LE(8) / 28571) * 2000);
/**
* Emits when accelerometer detects movement. Measured in DPS - degrees per second.
* @event LPF2Hub#accel
* @param {string} port
* @param {number} x
* @param {number} y
* @param {number} z
*/
this.emit("accel", "ACCEL", accelX, accelY, accelZ);
return;
}
if ((data[3] === 0x63 && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Control+ Accelerometer
const tiltZ = data.readInt16LE(4);
const tiltY = data.readInt16LE(6);
const tiltX = data.readInt16LE(8);
this._lastTiltX = tiltX;
this._lastTiltY = tiltY;
this._lastTiltZ = tiltZ;
this.emit("tilt", "TILT", this._lastTiltX, this._lastTiltY, this._lastTiltZ);
return;
}
if ((data[3] === 0x3d && this.type === Consts.HubType.CONTROL_PLUS_HUB)) { // Control+ CPU Temperature
/**
* Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.
* @event LPF2Hub#temp
* @param {string} port For Control+ Hubs, port will be "CPU" as the sensor reports CPU temperature.
* @param {number} temp
*/
this.emit("temp", "CPU", ((data.readInt16LE(4) / 900) * 90).toFixed(2));
return;
}
const port = this._getPortForPortNumber(data[3]); const port = this._getPortForPortNumber(data[3]);
if (!port) { if (!port) {
@ -459,11 +503,12 @@ export class LPF2Hub extends Hub {
/** /**
* Emits when a tilt sensor is activated. * Emits when a tilt sensor is activated.
* @event LPF2Hub#tilt * @event LPF2Hub#tilt
* @param {string} port If the event is fired from the Move Hub's in-built tilt sensor, the special port "TILT" is used. * @param {string} port If the event is fired from the Move Hub or Control+ Hub's in-built tilt sensor, the special port "TILT" is used.
* @param {number} x * @param {number} x
* @param {number} y * @param {number} y
* @param {number} z (Only available when using a Control+ Hub)
*/ */
this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY); this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltY);
break; break;
} }
case Consts.DeviceType.BOOST_TACHO_MOTOR: { case Consts.DeviceType.BOOST_TACHO_MOTOR: {
@ -495,7 +540,9 @@ export class LPF2Hub extends Hub {
case Consts.DeviceType.BOOST_TILT: { case Consts.DeviceType.BOOST_TILT: {
const tiltX = data[4] > 160 ? data[4] - 255 : data[4]; const tiltX = data[4] > 160 ? data[4] - 255 : data[4];
const tiltY = data[5] > 160 ? 255 - data[5] : data[5] - (data[5] * 2); const tiltY = data[5] > 160 ? 255 - data[5] : data[5] - (data[5] * 2);
this.emit("tilt", port.id, tiltX, tiltY); this._lastTiltX = tiltX;
this._lastTiltY = tiltY;
this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ);
break; break;
} }
case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: { case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: {

View File

@ -20,14 +20,6 @@ const debug = Debug("puphub");
export class PUPHub extends LPF2Hub { export class PUPHub extends LPF2Hub {
// We set JSDoc to ignore these events as a Powered UP Remote will never emit them.
/**
* @event PUPHub#speed
* @ignore
*/
public static IsPUPHub (peripheral: Peripheral) { public static IsPUPHub (peripheral: Peripheral) {
return (peripheral.advertisement && return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids &&

View File

@ -19,44 +19,6 @@ const debug = Debug("pupremote");
export class PUPRemote extends LPF2Hub { export class PUPRemote extends LPF2Hub {
// We set JSDoc to ignore these events as a Powered UP Remote will never emit them.
/**
* @event PUPRemote#distance
* @ignore
*/
/**
* @event PUPRemote#color
* @ignore
*/
/**
* @event PUPRemote#tilt
* @ignore
*/
/**
* @event PUPRemote#rotate
* @ignore
*/
/**
* @event PUPRemote#speed
* @ignore
*/
/**
* @event PUPRemote#attach
* @ignore
*/
/**
* @event PUPRemote#detach
* @ignore
*/
public static IsPUPRemote (peripheral: Peripheral) { public static IsPUPRemote (peripheral: Peripheral) {
return (peripheral.advertisement && return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids &&

View File

@ -19,14 +19,6 @@ const debug = Debug("wedo2smarthub");
export class WeDo2SmartHub extends Hub { export class WeDo2SmartHub extends Hub {
// We set JSDoc to ignore these events as a WeDo 2.0 Smart Hub will never emit them.
/**
* @event WeDo2SmartHub#speed
* @ignore
*/
public static IsWeDo2SmartHub (peripheral: Peripheral) { public static IsWeDo2SmartHub (peripheral: Peripheral) {
return (peripheral.advertisement && return (peripheral.advertisement &&
peripheral.advertisement.serviceUuids && peripheral.advertisement.serviceUuids &&