Added support for Technic Color Sensor and Technic Distance Sensor (Spike Prime)
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
d329ce6191
commit
ef315676b0
@ -80,6 +80,8 @@ export enum DeviceType {
|
||||
TECHNIC_MEDIUM_HUB_GYRO_SENSOR = 58,
|
||||
TECHNIC_MEDIUM_HUB_TILT_SENSOR = 59,
|
||||
TECHNIC_MEDIUM_HUB_TEMPERATURE_SENSOR = 60,
|
||||
TECHNIC_COLOR_SENSOR = 61,
|
||||
TECHNIC_DISTANCE_SENSOR = 62
|
||||
}
|
||||
|
||||
|
||||
|
66
src/devices/techniccolorsensor.ts
Normal file
66
src/devices/techniccolorsensor.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { Device } from "./device";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class TechnicColorSensor extends Device {
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, ModeMap, Consts.DeviceType.TECHNIC_COLOR_SENSOR);
|
||||
}
|
||||
|
||||
public receive (message: Buffer) {
|
||||
const mode = this._mode;
|
||||
|
||||
switch (mode) {
|
||||
case Mode.COLOR:
|
||||
if (message[4] <= 10) {
|
||||
const color = message[4];
|
||||
|
||||
/**
|
||||
* Emits when a color sensor is activated.
|
||||
* @event TechnicColorSensor#color
|
||||
* @param {Color} color
|
||||
*/
|
||||
this.notify("color", { color });
|
||||
}
|
||||
break;
|
||||
|
||||
case Mode.REFLECTIVITY:
|
||||
const reflect = message[4];
|
||||
|
||||
/**
|
||||
* Emits when the light reflectivity changes.
|
||||
* @event TechnicColorSensor#reflect Percentage, from 0 to 100.
|
||||
* @param {Color} reflect
|
||||
*/
|
||||
this.notify("reflect", { reflect });
|
||||
break;
|
||||
|
||||
case Mode.AMBIENT_LIGHT:
|
||||
const ambient = message[4];
|
||||
|
||||
/**
|
||||
* Emits when the ambient light changes.
|
||||
* @event TechnicColorSensor#ambient Percentage, from 0 to 100.
|
||||
* @param {Color} ambient
|
||||
*/
|
||||
this.notify("ambient", { ambient });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export enum Mode {
|
||||
COLOR = 0x00,
|
||||
REFLECTIVITY = 0x01,
|
||||
AMBIENT_LIGHT = 0x02
|
||||
}
|
||||
|
||||
export const ModeMap: {[event: string]: number} = {
|
||||
"color": Mode.COLOR,
|
||||
"reflect": Mode.REFLECTIVITY,
|
||||
"ambient": Mode.AMBIENT_LIGHT
|
||||
};
|
64
src/devices/technicdistancesensor.ts
Normal file
64
src/devices/technicdistancesensor.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { Device } from "./device";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class TechnicDistanceSensor extends Device {
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, ModeMap, Consts.DeviceType.TECHNIC_DISTANCE_SENSOR);
|
||||
}
|
||||
|
||||
public receive (message: Buffer) {
|
||||
const mode = this._mode;
|
||||
|
||||
switch (mode) {
|
||||
case Mode.DISTANCE:
|
||||
const distance = message.readUInt16LE(4);
|
||||
|
||||
/**
|
||||
* Emits when the detected distance changes (Slow sampling covers 40mm to 2500mm).
|
||||
* @event TechnicDistanceSensor#distance Distance, from 40 to 2500mm
|
||||
* @param {Color} distance
|
||||
*/
|
||||
this.notify("distance", { distance });
|
||||
break;
|
||||
|
||||
case Mode.FAST_DISTANCE:
|
||||
const fastDistance = message.readUInt16LE(4);
|
||||
|
||||
/**
|
||||
* Emits when the detected distance changes (Fast sampling covers 50mm to 320mm).
|
||||
* @event TechnicDistanceSensor#fastDistance Distance, from 50 to 320mm
|
||||
* @param {Color} fastDistance
|
||||
*/
|
||||
this.notify("fastDistance", { fastDistance });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the brightness (or turn on/off) the lights around the eyes.
|
||||
* @method TechnicDistanceSensor#setBrightness
|
||||
* @param {number} topLeft Top left quadrant (above left eye). 0-100 brightness.
|
||||
* @param {number} bottomLeft Bottom left quadrant (below left eye). 0-100 brightness.
|
||||
* @param {number} topRight Top right quadrant (above right eye). 0-100 brightness.
|
||||
* @param {number} bottomRight Bottom right quadrant (below right eye). 0-100 brightness.
|
||||
* @returns {Promise} Resolved upon successful completion of command.
|
||||
*/
|
||||
public setBrightness (topLeft: number, bottomLeft: number, topRight: number, bottomRight: number) {
|
||||
this.writeDirect(0x05, Buffer.from([topLeft, topRight, bottomLeft, bottomRight]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export enum Mode {
|
||||
DISTANCE = 0x00,
|
||||
FAST_DISTANCE = 0x01
|
||||
}
|
||||
|
||||
export const ModeMap: {[event: string]: number} = {
|
||||
"distance": Mode.DISTANCE,
|
||||
"fastDistance": Mode.FAST_DISTANCE
|
||||
};
|
@ -20,6 +20,8 @@ import { MoveHubTiltSensor } from "../devices/movehubtiltsensor";
|
||||
import { PiezoBuzzer } from "../devices/piezobuzzer";
|
||||
import { RemoteControlButton } from "../devices/remotecontrolbutton";
|
||||
import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor";
|
||||
import { TechnicColorSensor } from "../devices/techniccolorsensor";
|
||||
import { TechnicDistanceSensor } from "../devices/technicdistancesensor";
|
||||
import { TechnicLargeAngularMotor } from "../devices/techniclargeangularmotor";
|
||||
import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor";
|
||||
import { TechnicMediumAngularMotor } from "../devices/technicmediumangularmotor";
|
||||
@ -346,6 +348,8 @@ export class BaseHub extends EventEmitter {
|
||||
[Consts.DeviceType.TILT_SENSOR]: TiltSensor,
|
||||
[Consts.DeviceType.MOVE_HUB_TILT_SENSOR]: MoveHubTiltSensor,
|
||||
[Consts.DeviceType.PIEZO_BUZZER]: PiezoBuzzer,
|
||||
[Consts.DeviceType.TECHNIC_COLOR_SENSOR]: TechnicColorSensor,
|
||||
[Consts.DeviceType.TECHNIC_DISTANCE_SENSOR]: TechnicDistanceSensor,
|
||||
[Consts.DeviceType.TECHNIC_MEDIUM_HUB_TILT_SENSOR]: TechnicMediumHubTiltSensor,
|
||||
[Consts.DeviceType.TECHNIC_MEDIUM_HUB_GYRO_SENSOR]: TechnicMediumHubGyroSensor,
|
||||
[Consts.DeviceType.TECHNIC_MEDIUM_HUB_ACCELEROMETER]: TechnicMediumHubAccelerometerSensor,
|
||||
|
@ -26,6 +26,8 @@ import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
||||
import { PiezoBuzzer } from "./devices/piezobuzzer";
|
||||
import { RemoteControlButton } from "./devices/remotecontrolbutton";
|
||||
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
||||
import { TechnicColorSensor } from "./devices/techniccolorsensor";
|
||||
import { TechnicDistanceSensor } from "./devices/technicdistancesensor";
|
||||
import { TechnicLargeAngularMotor } from "./devices/techniclargeangularmotor";
|
||||
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
||||
import { TechnicMediumAngularMotor } from "./devices/technicmediumangularmotor";
|
||||
@ -65,6 +67,8 @@ window.PoweredUP = {
|
||||
PiezoBuzzer,
|
||||
RemoteControlButton,
|
||||
SimpleMediumLinearMotor,
|
||||
TechnicColorSensor,
|
||||
TechnicDistanceSensor,
|
||||
TechnicMediumHubAccelerometerSensor,
|
||||
TechnicMediumHubGyroSensor,
|
||||
TechnicMediumHubTiltSensor,
|
||||
|
@ -26,6 +26,8 @@ import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
||||
import { PiezoBuzzer } from "./devices/piezobuzzer";
|
||||
import { RemoteControlButton } from "./devices/remotecontrolbutton";
|
||||
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
||||
import { TechnicColorSensor } from "./devices/techniccolorsensor";
|
||||
import { TechnicDistanceSensor } from "./devices/technicdistancesensor";
|
||||
import { TechnicLargeAngularMotor } from "./devices/techniclargeangularmotor";
|
||||
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
||||
import { TechnicMediumAngularMotor } from "./devices/technicmediumangularmotor";
|
||||
@ -65,6 +67,8 @@ export {
|
||||
PiezoBuzzer,
|
||||
RemoteControlButton,
|
||||
SimpleMediumLinearMotor,
|
||||
TechnicColorSensor,
|
||||
TechnicDistanceSensor,
|
||||
TechnicMediumHubAccelerometerSensor,
|
||||
TechnicMediumHubGyroSensor,
|
||||
TechnicMediumHubTiltSensor,
|
||||
|
Loading…
x
Reference in New Issue
Block a user