Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/devices: parsing sensor #65

Closed
wants to merge 12 commits into from
87 changes: 0 additions & 87 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,3 @@
/**
* @typedef HubType
* @property {number} UNKNOWN 0
* @property {number} WEDO2_SMART_HUB 1
* @property {number} MOVE_HUB 2
* @property {number} POWERED_UP_HUB 3
* @property {number} POWERED_UP_REMOTE 4
* @property {number} DUPLO_TRAIN_HUB 5
* @property {number} CONTROL_PLUS_HUB 6
*/
export enum HubType {
UNKNOWN = 0,
WEDO2_SMART_HUB = 1,
MOVE_HUB = 2,
HUB = 3,
REMOTE_CONTROL = 4,
DUPLO_TRAIN_BASE = 5,
TECHNIC_MEDIUM_HUB = 6
}


// tslint:disable-next-line
export const HubTypeNames = HubType;


/**
* @typedef DeviceType
* @property {number} UNKNOWN 0
* @property {number} SIMPLE_MEDIUM_LINEAR_MOTOR 1
* @property {number} TRAIN_MOTOR 2
* @property {number} LED_LIGHTS 8
* @property {number} VOLTAGE 20
* @property {number} CURRENT 21
* @property {number} PIEZO_TONE 22
* @property {number} RGB_LIGHT 23
* @property {number} WEDO2_TILT 34
* @property {number} WEDO2_DISTANCE 35
* @property {number} COLOR_DISTANCE_SENSOR 37
* @property {number} MEDIUM_LINEAR_MOTOR 38
* @property {number} MOVE_HUB_MEDIUM_LINEAR_MOTOR 39
* @property {number} BOOST_TILT 40
* @property {number} DUPLO_TRAIN_BASE_MOTOR 41
* @property {number} DUPLO_TRAIN_BASE_SPEAKER 42
* @property {number} DUPLO_TRAIN_BASE_COLOR 43
* @property {number} DUPLO_TRAIN_BASE_SPEEDOMETER 44
* @property {number} CONTROL_PLUS_LARGE_MOTOR 46
* @property {number} CONTROL_PLUS_XLARGE_MOTOR 47
* @property {number} POWERED_UP_REMOTE_BUTTON 55
* @property {number} RSSI 56
* @property {number} CONTROL_PLUS_ACCELEROMETER 58
* @property {number} CONTROL_PLUS_TILT 59
*/
export enum DeviceType {
UNKNOWN = 0,
SIMPLE_MEDIUM_LINEAR_MOTOR = 1,
TRAIN_MOTOR = 2,
LIGHT = 8,
VOLTAGE_SENSOR = 20,
CURRENT_SENSOR = 21,
PIEZO_BUZZER = 22,
HUB_LED = 23,
TILT_SENSOR = 34,
MOTION_SENSOR = 35,
COLOR_DISTANCE_SENSOR = 37,
MEDIUM_LINEAR_MOTOR = 38,
MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39,
MOVE_HUB_TILT_SENSOR = 40,
DUPLO_TRAIN_BASE_MOTOR = 41,
DUPLO_TRAIN_BASE_SPEAKER = 42,
DUPLO_TRAIN_BASE_COLOR_SENSOR = 43,
DUPLO_TRAIN_BASE_SPEEDOMETER = 44,
TECHNIC_LARGE_LINEAR_MOTOR = 46,
TECHNIC_XLARGE_LINEAR_MOTOR = 47,
TECHNIC_MEDIUM_HUB_GEST_SENSOR = 54,
REMOTE_CONTROL_BUTTON = 55,
REMOTE_CONTROL_RSSI = 56,
TECHNIC_MEDIUM_HUB_ACCELEROMETER = 57,
TECHNIC_MEDIUM_HUB_GYRO_SENSOR = 58,
TECHNIC_MEDIUM_HUB_TILT_SENSOR = 59,
TECHNIC_MEDIUM_HUB_TEMPERATURE_SENSOR = 60,
}


// tslint:disable-next-line
export const DeviceTypeNames = DeviceType;


/**
* @typedef Color
* @property {number} BLACK 0
Expand Down
93 changes: 93 additions & 0 deletions src/devices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Device } from "./devices/generic/device";
export { Device };

import { ColorDistanceSensor } from "./devices/colordistancesensor";
import { CurrentSensor } from "./devices/currentsensor";
import { DuploTrainBaseColorSensor } from "./devices/duplotrainbasecolorsensor";
import { DuploTrainBaseMotor } from "./devices/duplotrainbasemotor";
import { DuploTrainBaseSpeaker } from "./devices/duplotrainbasespeaker";
import { DuploTrainBaseSpeedometer } from "./devices/duplotrainbasespeedometer";
import { HubLED } from "./devices/hubled";
import { Light } from "./devices/light";
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
import { MotionSensor } from "./devices/motionsensor";
import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor";
import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
import { PiezoBuzzer } from "./devices/piezobuzzer";
import { RemoteControlButton } from "./devices/remotecontrolbutton";
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
import { TechnicMediumHubAccelerometerSensor } from "./devices/technicmediumhubaccelerometersensor";
import { TechnicMediumHubGyroSensor } from "./devices/technicmediumhubgyrosensor";
import { TechnicMediumHubTiltSensor } from "./devices/technicmediumhubtiltsensor";
import { TechnicXLargeLinearMotor } from "./devices/technicxlargelinearmotor";
import { TiltSensor } from "./devices/tiltsensor";
import { TrainMotor } from "./devices/trainmotor";
import { VoltageSensor } from "./devices/voltagesensor";

export {
ColorDistanceSensor,
CurrentSensor,
DuploTrainBaseColorSensor,
DuploTrainBaseMotor,
DuploTrainBaseSpeaker,
DuploTrainBaseSpeedometer,
HubLED,
Light,
MediumLinearMotor,
MotionSensor,
MoveHubMediumLinearMotor,
MoveHubTiltSensor,
PiezoBuzzer,
RemoteControlButton,
SimpleMediumLinearMotor,
TechnicLargeLinearMotor,
TechnicMediumHubAccelerometerSensor,
TechnicMediumHubGyroSensor,
TechnicMediumHubTiltSensor,
TechnicXLargeLinearMotor,
TiltSensor,
TrainMotor,
VoltageSensor,
};

export const devices: {[type: number]: typeof Device} = {
[SimpleMediumLinearMotor.type]: SimpleMediumLinearMotor,
[TrainMotor.type]: TrainMotor,
[Light.type]: Light,
[VoltageSensor.type]: VoltageSensor,
[CurrentSensor.type]: CurrentSensor,
[PiezoBuzzer.type]: PiezoBuzzer,
[HubLED.type]: HubLED,
[TiltSensor.type]: TiltSensor,
[MotionSensor.type]: MotionSensor,
[ColorDistanceSensor.type]: ColorDistanceSensor,
[MediumLinearMotor.type]: MediumLinearMotor,
[MoveHubMediumLinearMotor.type]: MoveHubMediumLinearMotor,
[MoveHubTiltSensor.type]: MoveHubTiltSensor,
[DuploTrainBaseMotor.type]: DuploTrainBaseMotor,
[DuploTrainBaseSpeaker.type]: DuploTrainBaseSpeaker,
[DuploTrainBaseColorSensor.type]: DuploTrainBaseColorSensor,
[DuploTrainBaseSpeedometer.type]: DuploTrainBaseSpeedometer,
[TechnicLargeLinearMotor.type]: TechnicLargeLinearMotor,
[TechnicXLargeLinearMotor.type]: TechnicXLargeLinearMotor,
[RemoteControlButton.type]: RemoteControlButton,
[TechnicMediumHubAccelerometerSensor.type]: TechnicMediumHubAccelerometerSensor,
[TechnicMediumHubGyroSensor.type]: TechnicMediumHubGyroSensor,
[TechnicMediumHubTiltSensor.type]: TechnicMediumHubTiltSensor,
};

export const deviceType: {[typeName: string]: number} = {
UNKNOW: 0,
};
export const deviceTypeNames: {[typeName: number]: string } = {
0: "UNKNOW",
};

for (const type in devices) {
if (devices[+type]) {
const device = devices[+type];
deviceType[device.typeName] = device.type;
deviceTypeNames[device.type] = device.typeName;
}
}
Comment on lines +80 to +93
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are no longer enum and I think it is not very good.

11 changes: 7 additions & 4 deletions src/devices/colordistancesensor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Device } from "./device";
import { Device, DeviceVersion } from "./generic/device";

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

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

export class ColorDistanceSensor extends Device {
constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, ColorDistanceSensor.modes, DeviceType.COLOR_DISTANCE_SENSOR);
protected static _type = 37;
protected static _typeName: string = "COLOR_DISTANCE_SENSOR";

constructor (hub: IDeviceInterface, portId: number, versions: DeviceVersion) {
super(hub, portId, versions, ColorDistanceSensor.modes);
}

public receive (message: Buffer) {
Expand Down
9 changes: 5 additions & 4 deletions src/devices/currentsensor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Device } from "./device";
import { Device, DeviceVersion } from "./generic/device";

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

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

export class CurrentSensor extends Device {
protected static _type = 21;

constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, CurrentSensor.modes, DeviceType.CURRENT_SENSOR);
constructor (hub: IDeviceInterface, portId: number, versions: DeviceVersion) {
super(hub, portId, versions, CurrentSensor.modes);
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/devices/duplotrainbasecolorsensor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Device } from "./device";
import { Device, DeviceVersion } from "./generic/device";

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

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

export class DuploTrainBaseColorSensor extends Device {
protected static _type = 43;

constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, DuploTrainBaseColorSensor.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR_SENSOR);
constructor (hub: IDeviceInterface, portId: number, versions: DeviceVersion) {
super(hub, portId, versions, DuploTrainBaseColorSensor.ModeMap);
}

public receive (message: Buffer) {
Expand Down
8 changes: 5 additions & 3 deletions src/devices/duplotrainbasemotor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { BasicMotor } from "./basicmotor";
import { BasicMotor } from "./generic/basicmotor";
import { DeviceVersion } from "./generic/device";

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

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

export class DuploTrainBaseMotor extends BasicMotor {
protected static _type = 41;

constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_MOTOR);
constructor (hub: IDeviceInterface, portId: number, versions: DeviceVersion) {
super(hub, portId, versions, {});
}

}
7 changes: 4 additions & 3 deletions src/devices/duplotrainbasespeaker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Device } from "./device";
import { Device, DeviceVersion } from "./generic/device";

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

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

export class DuploTrainBaseSpeaker extends Device {
protected static _type = 42;


constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, {}, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEAKER);
constructor (hub: IDeviceInterface, portId: number, versions: DeviceVersion) {
super(hub, portId, versions, {});
}


Expand Down
9 changes: 4 additions & 5 deletions src/devices/duplotrainbasespeedometer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Device } from "./device";
import { Device, DeviceVersion } from "./generic/device";

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

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

export class DuploTrainBaseSpeedometer extends Device {
protected static _type = 44;

constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, DuploTrainBaseSpeedometer.ModeMap, Consts.DeviceType.DUPLO_TRAIN_BASE_SPEEDOMETER);
constructor (hub: IDeviceInterface, portId: number, versions: DeviceVersion) {
super(hub, portId, versions, DuploTrainBaseSpeedometer.ModeMap);
}

public receive (message: Buffer) {
Expand Down
12 changes: 5 additions & 7 deletions src/devices/basicmotor.ts → src/devices/generic/basicmotor.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Device } from "./device";
import { Device, DeviceVersion } from "./device";

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

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

import { mapSpeed } from "../utils";
import { mapSpeed } from "../../utils";

export class BasicMotor extends Device {


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


Expand Down
Loading