Skip to content
21 changes: 21 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,24 @@ export enum BLECharacteristic {
WEDO2_NAME_ID = "00001524-1212-efde-1523-785feabcd123", // "1524"
LPF2_ALL = "00001624-1212-efde-1623-785feabcd123"
}

export enum ValueType {
UInt8 = "UInt8",
Int8 = "Int8",
UInt16 = "UInt16",
Int16 = "Int16",
UInt32 = "UInt32",
Int32 = "Int32",
Float = "Float"
}

// tslint:disable-next-line
export const ValueBits = {
[ValueType.UInt8]: 1,
[ValueType.Int8]: 1,
[ValueType.UInt16]: 2,
[ValueType.Int16]: 2,
[ValueType.UInt32]: 4,
[ValueType.Int32]: 4,
[ValueType.Float]: 4
};
17 changes: 4 additions & 13 deletions src/devices/basicmotor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Device } from "./device";

import { IDeviceInterface } from "../interfaces";
import { IDeviceInterface, IDeviceMode } from "../interfaces";

import * as Consts from "../consts";

Expand All @@ -9,8 +9,8 @@ import { mapSpeed } from "../utils";
export class BasicMotor extends Device {


constructor (hub: IDeviceInterface, portId: number, modeMap: {[event: string]: number}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) {
super(hub, portId, modeMap, type);
constructor (hub: IDeviceInterface, portId: number, modes: {[name: string]: IDeviceMode}, type: Consts.DeviceType = Consts.DeviceType.UNKNOWN) {
super(hub, portId, modes, type);
}


Expand All @@ -21,16 +21,7 @@ export class BasicMotor extends Device {
* @returns {Promise} Resolved upon successful completion of command.
*/
public setPower (power: number) {
return new Promise((resolve) => {
if (this.isWeDo2SmartHub) {
const data = Buffer.from([this.portId, 0x01, 0x02, mapSpeed(power)]);
this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
} else {
const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, mapSpeed(power)]);
this.send(data);
}
return resolve();
});
return this.sendLinearPowerCommand(mapSpeed(power));
}


Expand Down
182 changes: 106 additions & 76 deletions src/devices/colordistancesensor.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,121 @@
import { Device } from "./device";

import { IDeviceInterface } from "../interfaces";
import { IDeviceInterface, IDeviceMode } from "../interfaces";

import * as Consts from "../consts";
import { DeviceType, HubType, ValueType } from "../consts";

export class ColorDistanceSensor extends Device {

constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, ColorDistanceSensor.ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
super(hub, portId, ColorDistanceSensor.modes, DeviceType.COLOR_DISTANCE_SENSOR);
}

public receive (message: Buffer) {
const mode = this._mode;

switch (mode) {
case ColorDistanceSensor.Mode.COLOR:
if (message[this.isWeDo2SmartHub ? 2 : 4] <= 10) {
const color = message[this.isWeDo2SmartHub ? 2 : 4];

/**
* Emits when a color sensor is activated.
* @event ColorDistanceSensor#color
* @param {Color} color
*/
this.emit("color", color);
}
break;

case ColorDistanceSensor.Mode.DISTANCE:
if (this.isWeDo2SmartHub) {
break;
}
if (message[4] <= 10) {
const distance = Math.floor(message[4] * 25.4) - 20;

/**
* Emits when a distance sensor is activated.
* @event ColorDistanceSensor#distance
* @param {number} distance Distance, in millimeters.
*/
this.emit("distance", distance);
}
break;

case ColorDistanceSensor.Mode.COLOR_AND_DISTANCE:
if (this.isWeDo2SmartHub) {
break;
}

let distance = message[5];
const partial = message[7];

if (partial > 0) {
distance += 1.0 / partial;
}

distance = Math.floor(distance * 25.4) - 20;

/**
* A combined color and distance event, emits when the sensor is activated.
* @event ColorDistanceSensor#colorAndDistance
* @param {Color} color
* @param {number} distance Distance, in millimeters.
*/
if (message[4] <= 10) {
const color = message[4];
this.emit("colorAndDistance", color, distance);
}
break;
const data = super.receive(message) || [];

if (this._mode === "SPEC_1") {
this.emit("color", data[0]);
this.emit("distance", data[1]);
this.emit("reflectivity", data[3]);
}
}

return data;
}
}

export namespace ColorDistanceSensor {

export enum Mode {
COLOR = 0x00,
DISTANCE = 0x01,
COLOR_AND_DISTANCE = 0x08
}

export const ModeMap: {[event: string]: number} = {
"color": ColorDistanceSensor.Mode.COLOR,
"distance": ColorDistanceSensor.Mode.DISTANCE,
"colorAndDistance": ColorDistanceSensor.Mode.COLOR_AND_DISTANCE
}

}
export const modes: { [name: string]: IDeviceMode } = {
COLOR: {
/**
* Emits when a color sensor is activated.
* @event ColorDistanceSensor#color
* @param {string} port
* @param {Color} color
*/
input: true,
event: "color",
values: { type: ValueType.UInt8, count: 1, min: 0, max: 255 },
num: {
[HubType.MOVE_HUB]: 0x00,
[HubType.TECHNIC_MEDIUM_HUB]: 0x00,
[HubType.HUB]: 0x00,
[HubType.WEDO2_SMART_HUB]: 0x00
}
},
PROX: {
input: true,
event: "distance",
values: { type: ValueType.UInt8, count: 1, min: 0, max: 10 },
num: {
[HubType.MOVE_HUB]: 0x01,
[HubType.TECHNIC_MEDIUM_HUB]: 0x01,
[HubType.HUB]: 0x01
}
},
COUNT: {
input: true,
event: "count",
values: { type: ValueType.UInt8, count: 1, min: 0 },
num: {
[HubType.MOVE_HUB]: 0x02,
[HubType.TECHNIC_MEDIUM_HUB]: 0x02,
[HubType.HUB]: 0x02
}
},
REFLT: {
input: true,
event: "reflectivity",
values: { type: ValueType.UInt8, count: 1, min: 0, max: 100 },
num: {
[HubType.MOVE_HUB]: 0x03,
[HubType.TECHNIC_MEDIUM_HUB]: 0x03,
[HubType.HUB]: 0x03
}
},
AMBI: {
input: true,
event: "luminosity",
values: { type: ValueType.UInt8, count: 1, min: 0, max: 100 },
num: {
[HubType.MOVE_HUB]: 0x04,
[HubType.TECHNIC_MEDIUM_HUB]: 0x04,
[HubType.HUB]: 0x04
}
},
COL_O: {
input: false,
num: {
[HubType.MOVE_HUB]: 0x05,
[HubType.TECHNIC_MEDIUM_HUB]: 0x05,
[HubType.HUB]: 0x05
}
},
RGB_I: {
input: true,
event: "rgb",
values: { type: ValueType.UInt8, count: 3, min: 0, max: 255 },
num: {
[HubType.MOVE_HUB]: 0x06,
[HubType.TECHNIC_MEDIUM_HUB]: 0x06,
[HubType.HUB]: 0x06
}
},
IR_Tx: {
input: false,
num: {
[HubType.MOVE_HUB]: 0x07,
[HubType.TECHNIC_MEDIUM_HUB]: 0x07,
[HubType.HUB]: 0x07
}
},
SPEC_1: {
input: true,
event: "colorAndDistance",
values: { type: ValueType.UInt8, count: 4, min: 0, max: 255 },
num: {
[HubType.MOVE_HUB]: 0x08,
[HubType.TECHNIC_MEDIUM_HUB]: 0x08,
[HubType.HUB]: 0x08
}
}
};
}
69 changes: 29 additions & 40 deletions src/devices/currentsensor.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,47 @@
import { Device } from "./device";

import { IDeviceInterface } from "../interfaces";
import { IDeviceInterface, IDeviceMode } from "../interfaces";

import * as Consts from "../consts";
import { DeviceType, HubType, ValueType } from "../consts";

export class CurrentSensor extends Device {

constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, CurrentSensor.ModeMap, Consts.DeviceType.CURRENT_SENSOR);
super(hub, portId, CurrentSensor.modes, 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 modes: { [name: string]: IDeviceMode } = {
CURRENT: {
/**
* Emits when a current change is detected.
* @event CurrentSensor#current
* @param {number} current
*/
input: true,
event: "current",
values: { type: ValueType.UInt8, count: 1, min: 0, max: 255 },
num: {
[HubType.MOVE_HUB]: 0x00,
[HubType.TECHNIC_MEDIUM_HUB]: 0x00,
[HubType.HUB]: 0x00,
[HubType.WEDO2_SMART_HUB]: 0x00
},
transform(hubType, data) {
const maxCurrentValue = CurrentSensor.MaxCurrentValue[hubType] || CurrentSensor.MaxCurrentValue[HubType.UNKNOWN];
const maxCurrentRaw = CurrentSensor.MaxCurrentRaw[hubType] || CurrentSensor.MaxCurrentRaw[HubType.UNKNOWN];
return [data[0] * maxCurrentValue / maxCurrentRaw];
}
}
};

export const MaxCurrentValue: {[hubType: number]: number} = {
[Consts.HubType.UNKNOWN]: 2444,
[Consts.HubType.TECHNIC_MEDIUM_HUB]: 4175,
[HubType.UNKNOWN]: 2444,
[HubType.TECHNIC_MEDIUM_HUB]: 4175,
}

export const MaxCurrentRaw: {[hubType: number]: number} = {
[Consts.HubType.UNKNOWN]: 4095,
[HubType.UNKNOWN]: 4095,
}

}
Loading