diff --git a/src/consts.ts b/src/consts.ts index 3ab80cb..88c56bd 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -55,8 +55,8 @@ export enum DeviceType { SIMPLE_MEDIUM_LINEAR_MOTOR = 1, TRAIN_MOTOR = 2, LIGHT = 8, - VOLTAGE = 20, - CURRENT = 21, + VOLTAGE_SENSOR = 20, + CURRENT_SENSOR = 21, PIEZO_TONE = 22, RGB_LIGHT = 23, TILT_SENSOR = 34, diff --git a/src/devices/voltagesensor.ts b/src/devices/voltagesensor.ts new file mode 100644 index 0000000..a1a85ca --- /dev/null +++ b/src/devices/voltagesensor.ts @@ -0,0 +1,62 @@ +import { Device } from "./device"; + +import { IDeviceInterface } from "../interfaces"; + +import * as Consts from "../consts"; + +export class VoltageSensor extends Device { + + constructor (hub: IDeviceInterface, portId: number) { + super(hub, portId, VoltageSensor.ModeMap, Consts.DeviceType.VOLTAGE_SENSOR); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case VoltageSensor.Mode.VOLTAGE: + let maxVoltageValue = VoltageSensor.MaxVoltageValue[this.hub.type]; + if (maxVoltageValue === undefined) { + maxVoltageValue = VoltageSensor.MaxVoltageValue[Consts.HubType.UNKNOWN]; + } + let maxVoltageRaw = VoltageSensor.MaxVoltageRaw[this.hub.type]; + if (maxVoltageRaw === undefined) { + maxVoltageRaw = VoltageSensor.MaxVoltageRaw[Consts.HubType.UNKNOWN]; + } + const voltage = message.readUInt16LE(4) * maxVoltageValue / maxVoltageRaw; + /** + * Emits when a voltage change is detected. + * @event VoltageSensor#voltage + * @param {number} voltage + */ + this.emit("voltage", voltage); + break; + } + } + +} + +export namespace VoltageSensor { + + export enum Mode { + VOLTAGE = 0x00 + } + + export const ModeMap: {[event: string]: number} = { + "voltage": VoltageSensor.Mode.VOLTAGE + } + + export const MaxVoltageValue: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 9.615, + [Consts.HubType.DUPLO_TRAIN_HUB]: 6.4, + [Consts.HubType.POWERED_UP_REMOTE]: 6.4, + } + + export const MaxVoltageRaw: {[hubType: number]: number} = { + [Consts.HubType.UNKNOWN]: 3893, + [Consts.HubType.DUPLO_TRAIN_HUB]: 3047, + [Consts.HubType.POWERED_UP_REMOTE]: 3200, + [Consts.HubType.CONTROL_PLUS_HUB]: 4095, + } + +} \ No newline at end of file diff --git a/src/hubs/boostmovehub.ts b/src/hubs/boostmovehub.ts index 0a88b84..66282a3 100644 --- a/src/hubs/boostmovehub.ts +++ b/src/hubs/boostmovehub.ts @@ -32,7 +32,6 @@ export class BoostMoveHub extends LPF2Hub { } protected _currentPort = 0x3b; - protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB); diff --git a/src/hubs/controlplushub.ts b/src/hubs/controlplushub.ts index b088d1d..29985ba 100644 --- a/src/hubs/controlplushub.ts +++ b/src/hubs/controlplushub.ts @@ -32,9 +32,6 @@ export class ControlPlusHub extends LPF2Hub { protected _currentPort = 0x3b; protected _currentMaxMA = 4175; - protected _voltagePort = 0x3c; - protected _voltageMaxRaw = 4095; - protected _voltageMaxV = 9.615; constructor (device: IBLEAbstraction) { super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB); diff --git a/src/hubs/duplotrainbase.ts b/src/hubs/duplotrainbase.ts index fd8d529..98eda91 100644 --- a/src/hubs/duplotrainbase.ts +++ b/src/hubs/duplotrainbase.ts @@ -31,9 +31,6 @@ export class DuploTrainBase extends LPF2Hub { protected _ledPort = 0x11; - protected _voltagePort = 0x14; - protected _voltageMaxV = 6.4; - protected _voltageMaxRaw = 3047; constructor (device: IBLEAbstraction) { super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB); diff --git a/src/hubs/hub.ts b/src/hubs/hub.ts index a063d97..b431125 100644 --- a/src/hubs/hub.ts +++ b/src/hubs/hub.ts @@ -13,6 +13,7 @@ import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor"; import { TiltSensor } from "../devices/tiltsensor"; import { TrainMotor } from "../devices/trainmotor"; +import { VoltageSensor } from "../devices/voltagesensor"; import * as Consts from "../consts"; @@ -34,7 +35,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 _voltage: number = 0; protected _current: number = 0; protected _rssi: number = -60; @@ -140,15 +140,6 @@ export class Hub extends EventEmitter { } - /** - * @readonly - * @property {number} voltage Voltage of the hub (Volts) - */ - public get voltage () { - return this._voltage; - } - - /** * @readonly * @property {number} current Current usage of the hub (Milliamps) @@ -346,6 +337,9 @@ export class Hub extends EventEmitter { case Consts.DeviceType.COLOR_DISTANCE_SENSOR: device = new ColorDistanceSensor(this, portId); break; + case Consts.DeviceType.VOLTAGE_SENSOR: + device = new VoltageSensor(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 4975dd9..f76eceb 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 _voltagePort: number | undefined; - protected _voltageMaxV: number = 9.6; - protected _voltageMaxRaw: number = 3893; protected _currentPort: number | undefined; protected _currentMaxMA: number = 2444; protected _currentMaxRaw: number = 4095; @@ -35,11 +32,8 @@ 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._voltagePort !== undefined) { - this.subscribe(this._voltagePort, Consts.DeviceType.VOLTAGE, 0x00); // Activate voltage reports - } if (this._currentPort !== undefined) { - this.subscribe(this._currentPort, Consts.DeviceType.CURRENT, 0x00); // Activate currrent reports + 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 @@ -413,11 +407,7 @@ export class LPF2Hub extends Hub { device.receive(message); } - // if (data[3] === this._voltagePort) { - // const voltageRaw = data.readUInt16LE(4); - // this._voltage = voltageRaw * this._voltageMaxV / this._voltageMaxRaw; - // return; - // } else if (data[3] === this._currentPort) { + // if (data[3] === this._currentPort) { // const currentRaw = data.readUInt16LE(4); // this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw; // return; diff --git a/src/hubs/puphub.ts b/src/hubs/puphub.ts index cd66c53..c376856 100644 --- a/src/hubs/puphub.ts +++ b/src/hubs/puphub.ts @@ -32,7 +32,6 @@ export class PUPHub extends LPF2Hub { } protected _currentPort = 0x3b; - protected _voltagePort = 0x3c; constructor (device: IBLEAbstraction) { super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB); diff --git a/src/hubs/pupremote.ts b/src/hubs/pupremote.ts index 96b93bf..7197f16 100644 --- a/src/hubs/pupremote.ts +++ b/src/hubs/pupremote.ts @@ -32,9 +32,6 @@ export class PUPRemote extends LPF2Hub { protected _ledPort = 0x34; - protected _voltagePort = 0x3b; - protected _voltageMaxV = 6.4; - protected _voltageMaxRaw = 3200; constructor (device: IBLEAbstraction) { diff --git a/src/index-browser.ts b/src/index-browser.ts index 3eb7e07..f6aeeee 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -21,6 +21,7 @@ import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; import { TiltSensor } from "./devices/tiltsensor"; import { TrainMotor } from "./devices/trainmotor"; +import { VoltageSensor } from "./devices/voltagesensor"; import { isWebBluetooth } from "./utils"; @@ -46,6 +47,7 @@ window.PoweredUP = { TechnicXLargeLinearMotor, TiltSensor, TrainMotor, + VoltageSensor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index b4f4eeb..729b2eb 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -21,6 +21,7 @@ import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor"; import { TiltSensor } from "./devices/tiltsensor"; import { TrainMotor } from "./devices/trainmotor"; +import { VoltageSensor } from "./devices/voltagesensor"; import { isWebBluetooth } from "./utils"; @@ -46,5 +47,6 @@ export { TechnicXLargeLinearMotor, TiltSensor, TrainMotor, + VoltageSensor, isWebBluetooth };