This commit is contained in:
parent
de4feb0b89
commit
c1b8697a56
@ -55,8 +55,8 @@ export enum DeviceType {
|
|||||||
SIMPLE_MEDIUM_LINEAR_MOTOR = 1,
|
SIMPLE_MEDIUM_LINEAR_MOTOR = 1,
|
||||||
TRAIN_MOTOR = 2,
|
TRAIN_MOTOR = 2,
|
||||||
LIGHT = 8,
|
LIGHT = 8,
|
||||||
VOLTAGE = 20,
|
VOLTAGE_SENSOR = 20,
|
||||||
CURRENT = 21,
|
CURRENT_SENSOR = 21,
|
||||||
PIEZO_TONE = 22,
|
PIEZO_TONE = 22,
|
||||||
RGB_LIGHT = 23,
|
RGB_LIGHT = 23,
|
||||||
TILT_SENSOR = 34,
|
TILT_SENSOR = 34,
|
||||||
|
62
src/devices/voltagesensor.ts
Normal file
62
src/devices/voltagesensor.ts
Normal file
@ -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,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -32,7 +32,6 @@ export class BoostMoveHub extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected _currentPort = 0x3b;
|
protected _currentPort = 0x3b;
|
||||||
protected _voltagePort = 0x3c;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB);
|
super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB);
|
||||||
|
@ -32,9 +32,6 @@ export class ControlPlusHub extends LPF2Hub {
|
|||||||
|
|
||||||
protected _currentPort = 0x3b;
|
protected _currentPort = 0x3b;
|
||||||
protected _currentMaxMA = 4175;
|
protected _currentMaxMA = 4175;
|
||||||
protected _voltagePort = 0x3c;
|
|
||||||
protected _voltageMaxRaw = 4095;
|
|
||||||
protected _voltageMaxV = 9.615;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB);
|
super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB);
|
||||||
|
@ -31,9 +31,6 @@ export class DuploTrainBase extends LPF2Hub {
|
|||||||
|
|
||||||
|
|
||||||
protected _ledPort = 0x11;
|
protected _ledPort = 0x11;
|
||||||
protected _voltagePort = 0x14;
|
|
||||||
protected _voltageMaxV = 6.4;
|
|
||||||
protected _voltageMaxRaw = 3047;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB);
|
super(device, DuploTrainBase.PortMap, Consts.HubType.DUPLO_TRAIN_HUB);
|
||||||
|
@ -13,6 +13,7 @@ import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor";
|
|||||||
import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor";
|
import { TechnicXLargeLinearMotor } from "../devices/technicxlargelinearmotor";
|
||||||
import { TiltSensor } from "../devices/tiltsensor";
|
import { TiltSensor } from "../devices/tiltsensor";
|
||||||
import { TrainMotor } from "../devices/trainmotor";
|
import { TrainMotor } from "../devices/trainmotor";
|
||||||
|
import { VoltageSensor } from "../devices/voltagesensor";
|
||||||
|
|
||||||
import * as Consts from "../consts";
|
import * as Consts from "../consts";
|
||||||
|
|
||||||
@ -34,7 +35,6 @@ export class Hub extends EventEmitter {
|
|||||||
protected _hardwareVersion: string = "0.0.00.0000";
|
protected _hardwareVersion: string = "0.0.00.0000";
|
||||||
protected _primaryMACAddress: string = "00:00:00:00:00:00";
|
protected _primaryMACAddress: string = "00:00:00:00:00:00";
|
||||||
protected _batteryLevel: number = 100;
|
protected _batteryLevel: number = 100;
|
||||||
protected _voltage: number = 0;
|
|
||||||
protected _current: number = 0;
|
protected _current: number = 0;
|
||||||
protected _rssi: number = -60;
|
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
|
* @readonly
|
||||||
* @property {number} current Current usage of the hub (Milliamps)
|
* @property {number} current Current usage of the hub (Milliamps)
|
||||||
@ -346,6 +337,9 @@ export class Hub extends EventEmitter {
|
|||||||
case Consts.DeviceType.COLOR_DISTANCE_SENSOR:
|
case Consts.DeviceType.COLOR_DISTANCE_SENSOR:
|
||||||
device = new ColorDistanceSensor(this, portId);
|
device = new ColorDistanceSensor(this, portId);
|
||||||
break;
|
break;
|
||||||
|
case Consts.DeviceType.VOLTAGE_SENSOR:
|
||||||
|
device = new VoltageSensor(this, portId);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
device = new Device(this, portId, undefined, deviceType);
|
device = new Device(this, portId, undefined, deviceType);
|
||||||
break;
|
break;
|
||||||
|
@ -16,9 +16,6 @@ const modeInfoDebug = Debug("lpf2hubmodeinfo");
|
|||||||
export class LPF2Hub extends Hub {
|
export class LPF2Hub extends Hub {
|
||||||
|
|
||||||
protected _ledPort: number = 0x32;
|
protected _ledPort: number = 0x32;
|
||||||
protected _voltagePort: number | undefined;
|
|
||||||
protected _voltageMaxV: number = 9.6;
|
|
||||||
protected _voltageMaxRaw: number = 3893;
|
|
||||||
protected _currentPort: number | undefined;
|
protected _currentPort: number | undefined;
|
||||||
protected _currentMaxMA: number = 2444;
|
protected _currentMaxMA: number = 2444;
|
||||||
protected _currentMaxRaw: number = 4095;
|
protected _currentMaxRaw: number = 4095;
|
||||||
@ -35,11 +32,8 @@ export class LPF2Hub extends Hub {
|
|||||||
await super.connect();
|
await super.connect();
|
||||||
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB);
|
await this._bleDevice.discoverCharacteristicsForService(Consts.BLEService.LPF2_HUB);
|
||||||
this._bleDevice.subscribeToCharacteristic(Consts.BLECharacteristic.LPF2_ALL, this._parseMessage.bind(this));
|
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) {
|
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);
|
await this.sleep(100);
|
||||||
this.send(Buffer.from([0x01, 0x02, 0x02]), Consts.BLECharacteristic.LPF2_ALL); // Activate button reports
|
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);
|
device.receive(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (data[3] === this._voltagePort) {
|
// if (data[3] === this._currentPort) {
|
||||||
// const voltageRaw = data.readUInt16LE(4);
|
|
||||||
// this._voltage = voltageRaw * this._voltageMaxV / this._voltageMaxRaw;
|
|
||||||
// return;
|
|
||||||
// } else if (data[3] === this._currentPort) {
|
|
||||||
// const currentRaw = data.readUInt16LE(4);
|
// const currentRaw = data.readUInt16LE(4);
|
||||||
// this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw;
|
// this._current = this._currentMaxMA * currentRaw / this._currentMaxRaw;
|
||||||
// return;
|
// return;
|
||||||
|
@ -32,7 +32,6 @@ export class PUPHub extends LPF2Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected _currentPort = 0x3b;
|
protected _currentPort = 0x3b;
|
||||||
protected _voltagePort = 0x3c;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB);
|
super(device, PUPHub.PortMap, Consts.HubType.POWERED_UP_HUB);
|
||||||
|
@ -32,9 +32,6 @@ export class PUPRemote extends LPF2Hub {
|
|||||||
|
|
||||||
|
|
||||||
protected _ledPort = 0x34;
|
protected _ledPort = 0x34;
|
||||||
protected _voltagePort = 0x3b;
|
|
||||||
protected _voltageMaxV = 6.4;
|
|
||||||
protected _voltageMaxRaw = 3200;
|
|
||||||
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
|
@ -21,6 +21,7 @@ import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
|||||||
import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor";
|
import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor";
|
||||||
import { TiltSensor } from "./devices/tiltsensor";
|
import { TiltSensor } from "./devices/tiltsensor";
|
||||||
import { TrainMotor } from "./devices/trainmotor";
|
import { TrainMotor } from "./devices/trainmotor";
|
||||||
|
import { VoltageSensor } from "./devices/voltagesensor";
|
||||||
|
|
||||||
import { isWebBluetooth } from "./utils";
|
import { isWebBluetooth } from "./utils";
|
||||||
|
|
||||||
@ -46,6 +47,7 @@ window.PoweredUP = {
|
|||||||
TechnicXLargeLinearMotor,
|
TechnicXLargeLinearMotor,
|
||||||
TiltSensor,
|
TiltSensor,
|
||||||
TrainMotor,
|
TrainMotor,
|
||||||
|
VoltageSensor,
|
||||||
isWebBluetooth
|
isWebBluetooth
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
|||||||
import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor";
|
import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor";
|
||||||
import { TiltSensor } from "./devices/tiltsensor";
|
import { TiltSensor } from "./devices/tiltsensor";
|
||||||
import { TrainMotor } from "./devices/trainmotor";
|
import { TrainMotor } from "./devices/trainmotor";
|
||||||
|
import { VoltageSensor } from "./devices/voltagesensor";
|
||||||
|
|
||||||
import { isWebBluetooth } from "./utils";
|
import { isWebBluetooth } from "./utils";
|
||||||
|
|
||||||
@ -46,5 +47,6 @@ export {
|
|||||||
TechnicXLargeLinearMotor,
|
TechnicXLargeLinearMotor,
|
||||||
TiltSensor,
|
TiltSensor,
|
||||||
TrainMotor,
|
TrainMotor,
|
||||||
|
VoltageSensor,
|
||||||
isWebBluetooth
|
isWebBluetooth
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user