From 2e06a17e5d232872c749e184ba652a0371c02e37 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 18 Dec 2019 12:04:39 -0800 Subject: [PATCH] CurrentSensor --- src/devices/currentsensor.ts | 58 ++++++++++++++++++++++++++++++++++++ src/hubs/boostmovehub.ts | 2 -- src/hubs/controlplushub.ts | 3 -- src/hubs/hub.ts | 14 +++------ src/hubs/lpf2hub.ts | 12 -------- src/index-browser.ts | 2 ++ src/index-node.ts | 2 ++ 7 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 src/devices/currentsensor.ts diff --git a/src/devices/currentsensor.ts b/src/devices/currentsensor.ts new file mode 100644 index 0000000..b078454 --- /dev/null +++ b/src/devices/currentsensor.ts @@ -0,0 +1,58 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class CurrentSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, CurrentSensor.ModeMap, Consts.DeviceType.CURRENT_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case CurrentSensor.Mode.CURRENT: + let maxCurrentValue = CurrentSensor.MaxCurrentValue[this.hub.type]; + if (maxCurrentValue === undefined) { + maxCurrentValue = CurrentSensor.MaxCurrentValue[Consts.HubType.UNKNOWN]; + } + let maxCurrentRaw = CurrentSensor.MaxCurrentRaw[this.hub.type]; + if (maxCurrentRaw === undefined) { + maxCurrentRaw = CurrentSensor.MaxCurrentRaw[Consts.HubType.UNKNOWN]; + } + const current = message.readUInt16LE(4) * maxCurrentValue / maxCurrentRaw; + /** + * Emits when a current change is detected. + * @event CurrentSensor#current + * @param {number} current + */ + this.emit("current", current); + break; + } + } + +} + +export namespace CurrentSensor { + + export enum Mode { + CURRENT = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "current": CurrentSensor.Mode.CURRENT + } + + export const MaxCurrentValue: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 2444, + [Consts.HubType.CONTROL_PLUS_HUB]: 4175, + } + + export const MaxCurrentRaw: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 4095, + } + +} \ No newline at end of file diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 66282a3..644b2cf 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -31,8 +31,6 @@ export class BoostMoveHub extends LPF2Hub { ); } - protected _currentPort = 0x3b; - constructor (device: IBLEAbstraction) { super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); debug("Discovered Boost Move Hub"); diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index 29985ba..43929ee 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -30,9 +30,6 @@ export class ControlPlusHub extends LPF2Hub { ); } - protected _currentPort = 0x3b; - protected _currentMaxMA = 4175; - constructor (device: IBLEAbstraction) { super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB); debug("Discovered Control+ Hub"); diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index b431125..6032eda 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -3,6 +3,7 @@ import { EventEmitter } from "events"; import { IBLEAbstraction } from "../interfaces"; import { ColorDistanceSensor } from "../devices/colordistancesensor"; +import { CurrentSensor } from "../devices/currentsensor"; import { Device } from "../devices/device"; import { Light } from "../devices/light"; import { MediumLinearMotor } from "../devices/mediumlinearmotor"; @@ -35,7 +36,6 @@ export class Hub extends EventEmitter { protected _hardwareVersion: string = "0.0.00.0000"; protected _primaryMACAddress: string = "00:00:00:00:00:00"; protected _batteryLevel: number = 100; - protected _current: number = 0; protected _rssi: number = -60; protected _bleDevice: IBLEAbstraction; @@ -140,15 +140,6 @@ export class Hub extends EventEmitter { } - /** - * @readonly - * @property {number} current Current usage of the hub (Milliamps) - */ - public get current () { - return this._current; - } - - /** * Connect to the Hub. * @method Hub#connect @@ -340,6 +331,9 @@ export class Hub extends EventEmitter { case Consts.DeviceType.VOLTAGE_SENSOR: device = new VoltageSensor(this, portId); break; + case Consts.DeviceType.CURRENT_SENSOR: + device = new CurrentSensor(this, portId); + break; default: device = new Device(this, portId, undefined, deviceType); break; diff --git a/src/hubs/lpf2hub.ts b/src/hubs/lpf2hub.ts index f76eceb..aecce97 100644 --- a/src/hubs/lpf2hub.ts +++ b/src/hubs/lpf2hub.ts @@ -16,9 +16,6 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo"); export class LPF2Hub extends Hub { protected _ledPort: number = 0x32; - protected _currentPort: number | undefined; - protected _currentMaxMA: number = 2444; - protected _currentMaxRaw: number = 4095; private _lastTiltX: number = 0; private _lastTiltY: number = 0; @@ -32,9 +29,6 @@ export class LPF2Hub extends Hub { await super.connect(); await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB); this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this)); - if (this._currentPort !== undefined) { - this.subscribe(this._currentPort, Consts.DeviceType.CURRENT_SENSOR, 0x00); // Activate currrent reports - } await this.sleep(100); this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports this.send(Buffer.from([0x01, 0x03, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request firmware version @@ -407,12 +401,6 @@ export class LPF2Hub extends Hub { device.receive(message); } - // if (data[3] === this._currentPort) { - // const currentRaw = data.readUInt16LE(4); - // this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; - // 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. diff --git a/src/index-browser.ts b/src/index-browser.ts index f6aeeee..6018cdf 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -11,6 +11,7 @@ import { PUPRemote } from "./hubs/pupremote"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; +import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; @@ -48,6 +49,7 @@ window.PoweredUP = { TiltSensor, TrainMotor, VoltageSensor, + CurrentSensor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index 729b2eb..0db196c 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -11,6 +11,7 @@ import { PUPRemote } from "./hubs/pupremote"; import { WeDo2SmartHub } from "./hubs/wedo2smarthub"; import { ColorDistanceSensor } from "./devices/colordistancesensor"; +import { CurrentSensor } from "./devices/currentsensor"; import { Device } from "./devices/device"; import { Light } from "./devices/light"; import { MediumLinearMotor } from "./devices/mediumlinearmotor"; @@ -48,5 +49,6 @@ export { TiltSensor, TrainMotor, VoltageSensor, + CurrentSensor, isWebBluetooth };