CurrentSensor
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Nathan Kellenicki 2019-12-18 12:04:39 -08:00
parent c1b8697a56
commit 2e06a17e5d
7 changed files with 66 additions and 27 deletions

View File

@ -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,
}
}

View File

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

View File

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

View File

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

View File

@ -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.

View File

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

View File

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