diff --git a/DOCS.md b/DOCS.md index d794037..faa110e 100644 --- a/DOCS.md +++ b/DOCS.md @@ -463,6 +463,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [BoostMoveHub](#BoostMoveHub) ⇐ LPF2Hub * [new BoostMoveHub()](#new_BoostMoveHub_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -499,6 +500,17 @@ Emits when an attached motor or sensor is detached from the Hub. ### new BoostMoveHub() The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. + + +### boostMoveHub.voltage +**Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### boostMoveHub.current @@ -876,6 +888,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [PUPHub](#PUPHub) ⇐ LPF2Hub * [new PUPHub()](#new_PUPHub_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -910,6 +923,17 @@ Emits when an attached motor or sensor is detached from the Hub. ### new PUPHub() The PUPHub is emitted if the discovered device is a Powered UP Hub. + + +### pupHub.voltage +**Kind**: instance property of [PUPHub](#PUPHub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### pupHub.current @@ -1261,6 +1285,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [PUPRemote](#PUPRemote) ⇐ LPF2Hub * [new PUPRemote()](#new_PUPRemote_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -1286,6 +1311,17 @@ Emits when an attached motor or sensor is detached from the Hub. ### new PUPRemote() The PUPRemote is emitted if the discovered device is a Powered UP Remote. + + +### pupRemote.voltage +**Kind**: instance property of [PUPRemote](#PUPRemote) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### pupRemote.current @@ -1522,6 +1558,7 @@ A combined color and distance event, emits when the sensor is activated. * [DuploTrainBase](#DuploTrainBase) ⇐ LPF2Hub * [new DuploTrainBase()](#new_DuploTrainBase_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -1552,6 +1589,17 @@ A combined color and distance event, emits when the sensor is activated. ### new DuploTrainBase() The DuploTrainBase is emitted if the discovered device is a Duplo Train Base. + + +### duploTrainBase.voltage +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### duploTrainBase.current diff --git a/README.md b/README.md index 4920a9e..b452cee 100644 --- a/README.md +++ b/README.md @@ -556,6 +556,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [BoostMoveHub](#BoostMoveHub) ⇐ LPF2Hub * [new BoostMoveHub()](#new_BoostMoveHub_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -592,6 +593,17 @@ Emits when an attached motor or sensor is detached from the Hub. ### new BoostMoveHub() The BoostMoveHub is emitted if the discovered device is a Boost Move Hub. + + +### boostMoveHub.voltage +**Kind**: instance property of [BoostMoveHub](#BoostMoveHub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### boostMoveHub.current @@ -969,6 +981,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [PUPHub](#PUPHub) ⇐ LPF2Hub * [new PUPHub()](#new_PUPHub_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -1003,6 +1016,17 @@ Emits when an attached motor or sensor is detached from the Hub. ### new PUPHub() The PUPHub is emitted if the discovered device is a Powered UP Hub. + + +### pupHub.voltage +**Kind**: instance property of [PUPHub](#PUPHub) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### pupHub.current @@ -1354,6 +1378,7 @@ Emits when an attached motor or sensor is detached from the Hub. * [PUPRemote](#PUPRemote) ⇐ LPF2Hub * [new PUPRemote()](#new_PUPRemote_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -1379,6 +1404,17 @@ Emits when an attached motor or sensor is detached from the Hub. ### new PUPRemote() The PUPRemote is emitted if the discovered device is a Powered UP Remote. + + +### pupRemote.voltage +**Kind**: instance property of [PUPRemote](#PUPRemote) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### pupRemote.current @@ -1615,6 +1651,7 @@ A combined color and distance event, emits when the sensor is activated. * [DuploTrainBase](#DuploTrainBase) ⇐ LPF2Hub * [new DuploTrainBase()](#new_DuploTrainBase_new) + * [.voltage](#LPF2Hub+voltage) * [.current](#LPF2Hub+current) * [.name](#Hub+name) * [.firmwareVersion](#Hub+firmwareVersion) @@ -1645,6 +1682,17 @@ A combined color and distance event, emits when the sensor is activated. ### new DuploTrainBase() The DuploTrainBase is emitted if the discovered device is a Duplo Train Base. + + +### duploTrainBase.voltage +**Kind**: instance property of [DuploTrainBase](#DuploTrainBase) +**Read only**: true +**Properties** + +| Name | Type | Description | +| --- | --- | --- | +| voltage | number | Voltage of the hub (Volts) | + ### duploTrainBase.current diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index b40e5d2..b4a9849 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -6,7 +6,6 @@ import { Port } from "./port"; import * as Consts from "./consts"; import Debug = require("debug"); -import { promises } from "fs"; const debug = Debug("lpf2hub"); @@ -17,7 +16,7 @@ const debug = Debug("lpf2hub"); */ export class LPF2Hub extends Hub { - + protected _voltage: number = 0; protected _current: number = 0; private _lastTiltX: number = 0; @@ -26,6 +25,15 @@ export class LPF2Hub extends Hub { private _messageBuffer: Buffer = Buffer.alloc(0); + /** + * @readonly + * @property {number} voltage Voltage of the hub (Volts) + */ + public get voltage () { + return this._voltage; + } + + /** * @readonly * @property {number} current Current usage of the hub (Amps) @@ -41,8 +49,8 @@ export class LPF2Hub extends Hub { const characteristic = this._getCharacteristic(Consts.BLECharacteristic.LPF2_ALL); this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); setTimeout(() => { - this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x06, 0x02])); // Activate battery level reports this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports @@ -190,7 +198,8 @@ export class LPF2Hub extends Hub { private _parseDeviceInfo (data: Buffer) { - if (data[3] === 2) { + // Button press reports + if (data[3] === 0x02) { if (data[5] === 1) { /** * Emits when a button is pressed. @@ -204,12 +213,18 @@ export class LPF2Hub extends Hub { this.emit("button", "GREEN", Consts.ButtonState.RELEASED); return; } - } else if (data[3] === 3) { + + // Firmware version + } else if (data[3] === 0x03) { const build = data.readUInt16LE(5); const bugFix = data.readUInt8(7); const major = data.readUInt8(8) >>> 4; const minor = data.readUInt8(8) & 0xf; this._firmwareInfo = { major, minor, bugFix, build }; + + // Battery level reports + } else if (data[3] === 0x06) { + this._batteryLevel = data[5]; } } @@ -260,8 +275,8 @@ export class LPF2Hub extends Hub { if ((data[3] === 0x3b && this.type === Consts.HubType.POWERED_UP_REMOTE)) { // Voltage (PUP Remote) data = this._padMessage(data, 6); - const batteryLevel = data.readUInt16LE(4) / 500; - this._batteryLevel = Math.floor(batteryLevel); + const voltage = data.readUInt16LE(4) / 500; + this._voltage = Math.floor(voltage); return; } else if (data[3] === 0x3c && this.type === Consts.HubType.POWERED_UP_REMOTE) { // Current (PUP Remote) data = this._padMessage(data, 6); @@ -270,8 +285,8 @@ export class LPF2Hub extends Hub { return; } else if (data[3] === 0x3c && this.type !== Consts.HubType.POWERED_UP_REMOTE) { // Voltage (Non-PUP Remote) data = this._padMessage(data, 6); - const batteryLevel = data.readUInt16LE(4) / 400; - this._batteryLevel = Math.floor(batteryLevel); + const voltage = data.readUInt16LE(4) / 400; + this._voltage = Math.floor(voltage); return; } else if (data[3] === 0x3b && this.type !== Consts.HubType.POWERED_UP_REMOTE) { // Current (Non-PUP Remote) data = this._padMessage(data, 6);