This commit is contained in:
parent
c1b8697a56
commit
2e06a17e5d
58
src/devices/currentsensor.ts
Normal file
58
src/devices/currentsensor.ts
Normal 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,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -31,8 +31,6 @@ export class BoostMoveHub extends LPF2Hub {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _currentPort = 0x3b;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB);
|
super(device, BoostMoveHub.PortMap, Consts.HubType.BOOST_MOVE_HUB);
|
||||||
debug("Discovered Boost Move Hub");
|
debug("Discovered Boost Move Hub");
|
||||||
|
@ -30,9 +30,6 @@ export class ControlPlusHub extends LPF2Hub {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _currentPort = 0x3b;
|
|
||||||
protected _currentMaxMA = 4175;
|
|
||||||
|
|
||||||
constructor (device: IBLEAbstraction) {
|
constructor (device: IBLEAbstraction) {
|
||||||
super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB);
|
super(device, ControlPlusHub.PortMap, Consts.HubType.CONTROL_PLUS_HUB);
|
||||||
debug("Discovered Control+ Hub");
|
debug("Discovered Control+ Hub");
|
||||||
|
@ -3,6 +3,7 @@ import { EventEmitter } from "events";
|
|||||||
import { IBLEAbstraction } from "../interfaces";
|
import { IBLEAbstraction } from "../interfaces";
|
||||||
|
|
||||||
import { ColorDistanceSensor } from "../devices/colordistancesensor";
|
import { ColorDistanceSensor } from "../devices/colordistancesensor";
|
||||||
|
import { CurrentSensor } from "../devices/currentsensor";
|
||||||
import { Device } from "../devices/device";
|
import { Device } from "../devices/device";
|
||||||
import { Light } from "../devices/light";
|
import { Light } from "../devices/light";
|
||||||
import { MediumLinearMotor } from "../devices/mediumlinearmotor";
|
import { MediumLinearMotor } from "../devices/mediumlinearmotor";
|
||||||
@ -35,7 +36,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 _current: number = 0;
|
|
||||||
protected _rssi: number = -60;
|
protected _rssi: number = -60;
|
||||||
|
|
||||||
protected _bleDevice: IBLEAbstraction;
|
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.
|
* Connect to the Hub.
|
||||||
* @method Hub#connect
|
* @method Hub#connect
|
||||||
@ -340,6 +331,9 @@ export class Hub extends EventEmitter {
|
|||||||
case Consts.DeviceType.VOLTAGE_SENSOR:
|
case Consts.DeviceType.VOLTAGE_SENSOR:
|
||||||
device = new VoltageSensor(this, portId);
|
device = new VoltageSensor(this, portId);
|
||||||
break;
|
break;
|
||||||
|
case Consts.DeviceType.CURRENT_SENSOR:
|
||||||
|
device = new CurrentSensor(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 _currentPort: number | undefined;
|
|
||||||
protected _currentMaxMA: number = 2444;
|
|
||||||
protected _currentMaxRaw: number = 4095;
|
|
||||||
|
|
||||||
private _lastTiltX: number = 0;
|
private _lastTiltX: number = 0;
|
||||||
private _lastTiltY: number = 0;
|
private _lastTiltY: number = 0;
|
||||||
@ -32,9 +29,6 @@ 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._currentPort !== undefined) {
|
|
||||||
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
|
||||||
this.send(Buffer.from([0x01, 0x03, 0x05]), Consts.BLECharacteristic.LPF2_ALL); // Request firmware version
|
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);
|
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
|
// 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.
|
// * Emits when a change is detected on a temperature sensor. Measured in degrees centigrade.
|
||||||
|
@ -11,6 +11,7 @@ import { PUPRemote } from "./hubs/pupremote";
|
|||||||
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||||
|
|
||||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||||
|
import { CurrentSensor } from "./devices/currentsensor";
|
||||||
import { Device } from "./devices/device";
|
import { Device } from "./devices/device";
|
||||||
import { Light } from "./devices/light";
|
import { Light } from "./devices/light";
|
||||||
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
||||||
@ -48,6 +49,7 @@ window.PoweredUP = {
|
|||||||
TiltSensor,
|
TiltSensor,
|
||||||
TrainMotor,
|
TrainMotor,
|
||||||
VoltageSensor,
|
VoltageSensor,
|
||||||
|
CurrentSensor,
|
||||||
isWebBluetooth
|
isWebBluetooth
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import { PUPRemote } from "./hubs/pupremote";
|
|||||||
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||||
|
|
||||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||||
|
import { CurrentSensor } from "./devices/currentsensor";
|
||||||
import { Device } from "./devices/device";
|
import { Device } from "./devices/device";
|
||||||
import { Light } from "./devices/light";
|
import { Light } from "./devices/light";
|
||||||
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
||||||
@ -48,5 +49,6 @@ export {
|
|||||||
TiltSensor,
|
TiltSensor,
|
||||||
TrainMotor,
|
TrainMotor,
|
||||||
VoltageSensor,
|
VoltageSensor,
|
||||||
|
CurrentSensor,
|
||||||
isWebBluetooth
|
isWebBluetooth
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user