diff --git a/src/boostmovehub.ts b/src/boostmovehub.ts index 3c1c9f1..93c1222 100644 --- a/src/boostmovehub.ts +++ b/src/boostmovehub.ts @@ -31,6 +31,8 @@ export class BoostMoveHub extends LPF2Hub { ); } + protected _currentPort = 0x3b; + protected _voltagePort = 0x3c; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/consts.ts b/src/consts.ts index 0c70471..4a853e0 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -20,11 +20,7 @@ export enum HubType { // tslint:disable-next-line -export let HubTypeNames = Object.keys(HubType).reduce((result: {[hubType: string]: string}, item) => { - // @ts-ignore - result[HubType[item]] = item; - return result; -}, {}); +export const HubTypeNames = HubType; /** @@ -33,7 +29,10 @@ export let HubTypeNames = Object.keys(HubType).reduce((result: {[hubType: string * @property {number} BASIC_MOTOR 1 * @property {number} TRAIN_MOTOR 2 * @property {number} LED_LIGHTS 8 - * @property {number} BOOST_LED 22 + * @property {number} VOLTAGE 20 + * @property {number} CURRENT 21 + * @property {number} PIEZO_TONE 22 + * @property {number} RGB_LIGHT 23 * @property {number} WEDO2_TILT 34 * @property {number} WEDO2_DISTANCE 35 * @property {number} BOOST_DISTANCE 37 @@ -47,6 +46,7 @@ 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} RSSI 56 * @property {number} CONTROL_PLUS_ACCELEROMETER 58 * @property {number} CONTROL_PLUS_TILT 59 */ @@ -55,7 +55,10 @@ export enum DeviceType { BASIC_MOTOR = 1, TRAIN_MOTOR = 2, LED_LIGHTS = 8, - BOOST_LED = 22, + VOLTAGE = 20, + CURRENT = 21, + PIEZO_TONE = 22, + RGB_LIGHT = 23, WEDO2_TILT = 34, WEDO2_DISTANCE = 35, BOOST_DISTANCE = 37, @@ -69,17 +72,14 @@ export enum DeviceType { CONTROL_PLUS_LARGE_MOTOR = 46, CONTROL_PLUS_XLARGE_MOTOR = 47, POWERED_UP_REMOTE_BUTTON = 55, + RSSI = 56, CONTROL_PLUS_ACCELEROMETER = 58, CONTROL_PLUS_TILT = 59 } // tslint:disable-next-line -export let DeviceTypeNames = Object.keys(DeviceType).reduce((result: {[deviceType: string]: string}, item) => { - // @ts-ignore - result[DeviceType[item]] = item; - return result; -}, {}); +export const DeviceTypeNames = DeviceType; /** @@ -114,11 +114,7 @@ export enum Color { // tslint:disable-next-line -export let ColorNames = Object.keys(Color).reduce((result: {[color: string]: string}, item) => { - // @ts-ignore - result[Color[item]] = item; - return result; -}, {}); +export const ColorNames = Color; /** diff --git a/src/controlplushub.ts b/src/controlplushub.ts index 533ea84..03298e9 100644 --- a/src/controlplushub.ts +++ b/src/controlplushub.ts @@ -30,6 +30,9 @@ export class ControlPlusHub extends LPF2Hub { ); } + protected _currentPort = 0x3b; + protected _voltagePort = 0x3c; + protected _voltageMaxRaw = 4095; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/duplotrainbase.ts b/src/duplotrainbase.ts index 1503d1d..c52ca0d 100644 --- a/src/duplotrainbase.ts +++ b/src/duplotrainbase.ts @@ -32,7 +32,9 @@ export class DuploTrainBase extends LPF2Hub { protected _ledPort = 0x11; - + protected _voltagePort = 0x14; + protected _voltageMaxV = 6.4; + protected _voltageMaxRaw = 3047; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/hub.ts b/src/hub.ts index 28cb63a..0169563 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -32,7 +32,6 @@ export class Hub extends EventEmitter { protected _current: number = 0; protected _bleDevice: IBLEDevice; - private _rssi: number = -100; private _isConnecting = false; private _isConnected = false; @@ -92,15 +91,6 @@ export class Hub extends EventEmitter { } - /** - * @readonly - * @property {number} rssi Signal strength of the hub - */ - public get rssi () { - return this._rssi; - } - - /** * @readonly * @property {number} batteryLevel Battery level of the hub (Percentage between 0-100) @@ -382,15 +372,6 @@ export class Hub extends EventEmitter { return port; } - - protected _lpad (str: string, length: number) { - while (str.length < length) { - str = "0" + str; - } - return str; - } - - private _getModeForDeviceType (type: Consts.DeviceType) { switch (type) { case Consts.DeviceType.BASIC_MOTOR: diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index be68cb2..5f71a2f 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -4,6 +4,7 @@ import { Hub } from "./hub"; import { Port } from "./port"; import * as Consts from "./consts"; +import { toBin, toHex } from "./utils"; import Debug = require("debug"); const debug = Debug("lpf2hub"); @@ -21,10 +22,16 @@ export class LPF2Hub extends Hub { } private static decodeMacAddress(v: Uint8Array) { - return Array.from(v).map((n) => n.toString(16).padStart(2, "0")).join(":"); + return Array.from(v).map((n) => toHex(n, 2)).join(":"); } protected _ledPort: number = 0x32; + protected _voltagePort: number | undefined; + protected _voltageMaxV: number = 9.6; + protected _voltageMaxRaw: number = 3893; + protected _currentPort: number | undefined; + protected _currentMaxMA: number = 2444; + protected _currentMaxRaw: number = 4095; private _lastTiltX: number = 0; private _lastTiltY: number = 0; @@ -43,8 +50,12 @@ export class LPF2Hub extends Hub { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x04, 0x05])); // Request hardware version this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x06, 0x02])); // Activate battery level reports this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x0d, 0x05])); // Request primary MAC address - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + if (this._voltagePort !== undefined) { + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._voltagePort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports + } + if (this._currentPort !== undefined) { + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, this._currentPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports + } if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01])); } @@ -272,12 +283,17 @@ export class LPF2Hub extends Hub { } - private _parsePortMessage (data: Buffer) { let port = this._getPortForPortNumber(data[3]); + const type = data[4] ? data.readUInt16LE(5) : 0; - if (data[4] === 0x01 && process.env["PORT_DEBUG_INFO"]) { + if (data[4] === 0x01 && modeInfoDebug.enabled) { + const typeName = Consts.DeviceTypeNames[data[5]] || "unknown"; + modeInfoDebug(`Port ${toHex(data[3])}, type ${toHex(type, 4)} (${typeName})`); + const hwVersion = LPF2Hub.decodeVersion(data.readInt32LE(7)); + const swVersion = LPF2Hub.decodeVersion(data.readInt32LE(11)); + modeInfoDebug(`Port ${toHex(data[3])}, hardware version ${hwVersion}, software version ${swVersion}`); this._sendPortInformationRequest(data[3]); } @@ -290,7 +306,7 @@ export class LPF2Hub extends Hub { port = this._getPortForPortNumber(data[3]); if (port) { port.connected = true; - this._registerDeviceAttachment(port, data[5]); + this._registerDeviceAttachment(port, type); } else { return; } @@ -302,7 +318,7 @@ export class LPF2Hub extends Hub { } } else { port.connected = (data[4] === 0x01 || data[4] === 0x02) ? true : false; - this._registerDeviceAttachment(port, data[5]); + this._registerDeviceAttachment(port, type); } } @@ -310,15 +326,24 @@ export class LPF2Hub extends Hub { private _sendPortInformationRequest (port: number) { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x01])); + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x21, port, 0x02])); // Mode combinations } private _parsePortInformationResponse (data: Buffer) { const port = data[3]; + if (data[4] === 2) { + const modeCombinationMasks: number[] = []; + for (let i = 5; i < data.length; i += 2) { + modeCombinationMasks.push(data.readUInt16LE(i)); + } + modeInfoDebug(`Port ${toHex(port)}, mode combinations [${modeCombinationMasks.map((c) => toBin(c, 0)).join(", ")}]`); + return; + } const count = data[6]; - const input = data.readUInt16LE(7); - const output = data.readUInt16LE(9); - modeInfoDebug(`Port ${port}, total modes ${count}, input modes ${input.toString(2)}, output modes ${output.toString(2)}`); + const input = toBin(data.readUInt16LE(7), count); + const output = toBin(data.readUInt16LE(9), count); + modeInfoDebug(`Port ${toHex(port)}, total modes ${count}, input modes ${input}, output modes ${output}`); for (let i = 0; i < count; i++) { this._sendModeInformationRequest(port, i, 0x00); // Mode Name @@ -326,6 +351,7 @@ export class LPF2Hub extends Hub { this._sendModeInformationRequest(port, i, 0x02); // PCT Range this._sendModeInformationRequest(port, i, 0x03); // SI Range this._sendModeInformationRequest(port, i, 0x04); // SI Symbol + this._sendModeInformationRequest(port, i, 0x80); // Value Format } } @@ -336,7 +362,7 @@ export class LPF2Hub extends Hub { private _parseModeInformationResponse (data: Buffer) { - const port = data[3]; + const port = toHex(data[3]); const mode = data[4]; const type = data[5]; switch (type) { @@ -355,6 +381,12 @@ export class LPF2Hub extends Hub { case 0x04: // SI Symbol modeInfoDebug(`Port ${port}, mode ${mode}, SI symbol ${data.slice(6, data.length).toString()}`); break; + case 0x80: // Value Format + const numValues = data[6]; + const dataType = ["8bit", "16bit", "32bit", "float"][data[7]]; + const totalFigures = data[8]; + const decimals = data[9]; + modeInfoDebug(`Port ${port}, mode ${mode}, Value ${numValues} x ${dataType}, Decimal format ${totalFigures}.${decimals}`); } } @@ -378,42 +410,15 @@ export class LPF2Hub extends Hub { } - private _padMessage (data: Buffer, len: number) { - if (data.length < len) { - data = Buffer.concat([data, Buffer.alloc(len - data.length)]); - } - return data; - } - - private _parseSensorMessage (data: Buffer) { - if ((data[3] === 0x3b && this.type === Consts.HubType.POWERED_UP_REMOTE)) { // Voltage (PUP Remote) - data = this._padMessage(data, 6); - const voltage = data.readUInt16LE(4); - this._voltage = 6400.0 * voltage / 3200.0 / 1000.0; + if (data[3] === this._voltagePort) { + const voltageRaw = data.readUInt16LE(4); + this._voltage = voltageRaw * this._voltageMaxV / this._voltageMaxRaw; return; - } else if ((data[3] === 0x3c && this.type === Consts.HubType.POWERED_UP_HUB)) { // Voltage (PUP Hub) - data = this._padMessage(data, 6); - const voltage = data.readUInt16LE(4); - this._voltage = 9620.0 * voltage / 3893.0 / 1000.0; - 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) - data = this._padMessage(data, 6); - const voltage = data.readUInt16LE(4); - this._voltage = 9600.0 * voltage / 3893.0 / 1000.0; - return; - } else if (data[3] === 0x3c && this.type === Consts.HubType.POWERED_UP_REMOTE) { // RSSI (PUP Remote) - return; - } else if (data[3] === 0x3b) { // Current (Others) - data = this._padMessage(data, 6); - const current = data.readUInt16LE(4); - this._current = 2444 * current / 4095.0; + } else if (data[3] === this._currentPort) { + const currentRaw = data.readUInt16LE(4); + this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; return; } diff --git a/src/puphub.ts b/src/puphub.ts index e85bd93..a305511 100644 --- a/src/puphub.ts +++ b/src/puphub.ts @@ -31,6 +31,8 @@ export class PUPHub extends LPF2Hub { ); } + protected _currentPort = 0x3b; + protected _voltagePort = 0x3c; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { super(device, autoSubscribe); diff --git a/src/pupremote.ts b/src/pupremote.ts index 888728f..d01c3f2 100644 --- a/src/pupremote.ts +++ b/src/pupremote.ts @@ -32,6 +32,9 @@ export class PUPRemote extends LPF2Hub { protected _ledPort = 0x34; + protected _voltagePort = 0x3b; + protected _voltageMaxV = 6.4; + protected _voltageMaxRaw = 3200; constructor (device: IBLEDevice, autoSubscribe: boolean = true) { diff --git a/src/utils.ts b/src/utils.ts index 629b5ef..83650aa 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,2 +1,9 @@ // @ts-ignore export const isWebBluetooth = (typeof navigator !== "undefined" && navigator && navigator.bluetooth); + +export function toHex (value: number, length: number = 2) { + return value.toString(16).padStart(length, "0"); +} +export function toBin (value: number, length: number = 8) { + return value.toString(2).padStart(length, "0"); +}