More reliable battery level reports and voltage reports on LPF2 hubs

This commit is contained in:
Nathan Kellenicki 2019-01-18 15:14:11 -08:00
parent 47a0f56d0d
commit bf7e023697
3 changed files with 120 additions and 9 deletions

48
DOCS.md
View File

@ -463,6 +463,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [BoostMoveHub](#BoostMoveHub) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### boostMoveHub.voltage
**Kind**: instance property of [<code>BoostMoveHub</code>](#BoostMoveHub)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### boostMoveHub.current
@ -876,6 +888,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [PUPHub](#PUPHub) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### pupHub.voltage
**Kind**: instance property of [<code>PUPHub</code>](#PUPHub)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### pupHub.current
@ -1261,6 +1285,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [PUPRemote](#PUPRemote) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### pupRemote.voltage
**Kind**: instance property of [<code>PUPRemote</code>](#PUPRemote)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### pupRemote.current
@ -1522,6 +1558,7 @@ A combined color and distance event, emits when the sensor is activated.
* [DuploTrainBase](#DuploTrainBase) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### duploTrainBase.voltage
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### duploTrainBase.current

View File

@ -556,6 +556,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [BoostMoveHub](#BoostMoveHub) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### boostMoveHub.voltage
**Kind**: instance property of [<code>BoostMoveHub</code>](#BoostMoveHub)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### boostMoveHub.current
@ -969,6 +981,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [PUPHub](#PUPHub) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### pupHub.voltage
**Kind**: instance property of [<code>PUPHub</code>](#PUPHub)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### pupHub.current
@ -1354,6 +1378,7 @@ Emits when an attached motor or sensor is detached from the Hub.
* [PUPRemote](#PUPRemote) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### pupRemote.voltage
**Kind**: instance property of [<code>PUPRemote</code>](#PUPRemote)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### pupRemote.current
@ -1615,6 +1651,7 @@ A combined color and distance event, emits when the sensor is activated.
* [DuploTrainBase](#DuploTrainBase) ⇐ <code>LPF2Hub</code>
* [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.
<a name="LPF2Hub+voltage"></a>
### duploTrainBase.voltage
**Kind**: instance property of [<code>DuploTrainBase</code>](#DuploTrainBase)
**Read only**: true
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| voltage | <code>number</code> | Voltage of the hub (Volts) |
<a name="LPF2Hub+current"></a>
### duploTrainBase.current

View File

@ -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);