From 8ff0664641b1edfbd93df32a67c8951bda077f86 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 11 Nov 2019 11:01:10 -0800 Subject: [PATCH] Made Control+ tilt and accelerometer behave like normal devices --- examples/42099_ds4_remote.html | 89 ++++++++++++++++++++++++++++++++++ examples/sensor_test.js | 8 ++- examples/web_bluetooth.html | 9 +++- src/consts.ts | 6 ++- src/controlplushub.ts | 5 +- src/hub.ts | 4 ++ src/lpf2hub.ts | 52 ++++++++++---------- 7 files changed, 138 insertions(+), 35 deletions(-) create mode 100644 examples/42099_ds4_remote.html diff --git a/examples/42099_ds4_remote.html b/examples/42099_ds4_remote.html new file mode 100644 index 0000000..c2c50c1 --- /dev/null +++ b/examples/42099_ds4_remote.html @@ -0,0 +1,89 @@ + + + + +4x4 Crawler / PlayStation DualShock 4 Remote Control + + + + + + + +

4x4 Crawler / PlayStation DualShock 4 Remote Control

+
+Scan for car +
+
+Current Color:
 
+
+ + + \ No newline at end of file diff --git a/examples/sensor_test.js b/examples/sensor_test.js index 3f21714..42e0758 100644 --- a/examples/sensor_test.js +++ b/examples/sensor_test.js @@ -23,8 +23,12 @@ poweredUP.on("discover", async (hub) => { console.log(`Disconnected ${hub.name}`); }) - hub.on("tilt", (port, x, y) => { - console.log(`Tilt detected on port ${port} (X: ${x}, Y: ${y})`); + hub.on("tilt", (port, x, y, z) => { + console.log(`Tilt detected on port ${port} (X: ${x}, Y: ${y}${z !== "undefined" ? `, Z: ${z}`: ""})`); + }); + + hub.on("accel", (port, x, y, z) => { + console.log(`Accelerometer detected on port ${port} (X: ${x}, Y: ${y}, Z: ${z})`); }); hub.on("distance", (port, distance) => { diff --git a/examples/web_bluetooth.html b/examples/web_bluetooth.html index 9f66394..5e63c35 100644 --- a/examples/web_bluetooth.html +++ b/examples/web_bluetooth.html @@ -19,10 +19,15 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs log(`Disconnected ${hub.name}`); }) - hub.on("tilt", (port, x, y) => { - log(`Tilt detected on port ${port} (X: ${x}, Y: ${y})`); + hub.on("tilt", (port, x, y, z) => { + log(`Tilt detected on port ${port} (X: ${x}, Y: ${y}${z !== "undefined" ? `, Z: ${z}`: ""})`); }); + hub.on("accel", (port, x, y, z) => { + log(`Accelerometer detected on port ${port} (X: ${x}, Y: ${y}, Z: ${z})`); + }); + + hub.on("distance", (port, distance) => { log(`Motion detected on port ${port} (Distance: ${distance})`); }); diff --git a/src/consts.ts b/src/consts.ts index 6158b95..0c70471 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -47,6 +47,8 @@ export let HubTypeNames = Object.keys(HubType).reduce((result: {[hubType: string * @property {number} CONTROL_PLUS_LARGE_MOTOR 46 * @property {number} CONTROL_PLUS_XLARGE_MOTOR 47 * @property {number} POWERED_UP_REMOTE_BUTTON 55 + * @property {number} CONTROL_PLUS_ACCELEROMETER 58 + * @property {number} CONTROL_PLUS_TILT 59 */ export enum DeviceType { UNKNOWN = 0, @@ -66,7 +68,9 @@ export enum DeviceType { DUPLO_TRAIN_BASE_SPEEDOMETER = 44, CONTROL_PLUS_LARGE_MOTOR = 46, CONTROL_PLUS_XLARGE_MOTOR = 47, - POWERED_UP_REMOTE_BUTTON = 55 + POWERED_UP_REMOTE_BUTTON = 55, + CONTROL_PLUS_ACCELEROMETER = 58, + CONTROL_PLUS_TILT = 59 } diff --git a/src/controlplushub.ts b/src/controlplushub.ts index bdcf7a1..533ea84 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -39,7 +39,8 @@ export class ControlPlusHub extends LPF2Hub { "B": new Port("B", 1), "C": new Port("C", 2), "D": new Port("D", 3), - // "TILT": new Port("TILT", 60) + "ACCEL": new Port("ACCEL", 98), + "TILT": new Port("TILT", 99) }; this.on("attach", (port, type) => { this._combinePorts(port, type); @@ -52,8 +53,6 @@ export class ControlPlusHub extends LPF2Hub { return new Promise(async (resolve, reject) => { debug("Connecting to Control+ Hub"); 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"); return resolve(); diff --git a/src/hub.ts b/src/hub.ts index f08661e..339a63e 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -395,6 +395,10 @@ export class Hub extends EventEmitter { return 0x02; case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR: return 0x02; + case Consts.DeviceType.CONTROL_PLUS_TILT: + return 0x00; + case Consts.DeviceType.CONTROL_PLUS_ACCELEROMETER: + return 0x00; case Consts.DeviceType.BOOST_DISTANCE: return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08); case Consts.DeviceType.BOOST_TILT: diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index ab9adda..9cdaa3f 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -407,33 +407,6 @@ export class LPF2Hub extends Hub { 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. @@ -544,6 +517,31 @@ export class LPF2Hub extends Hub { this.emit("rotate", port.id, rotation); break; } + case Consts.DeviceType.CONTROL_PLUS_TILT: { + 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); + break; + } + case Consts.DeviceType.CONTROL_PLUS_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); + break; + } case Consts.DeviceType.BOOST_TILT: { const tiltX = data.readInt8(4); const tiltY = data.readInt8(5);