node-poweredup/src/devices/colordistancesensor.ts
Nathan Kellenicki 0c3ff8b00a
Some checks failed
continuous-integration/drone/push Build is failing
Devices now cache notified values for easy retrieval without events
2020-01-13 12:56:58 -08:00

88 lines
2.6 KiB
TypeScript

import { Device } from "./device";
import { IDeviceInterface } from "../interfaces";
import * as Consts from "../consts";
export class ColorDistanceSensor extends Device {
constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, ModeMap, Consts.DeviceType.COLOR_DISTANCE_SENSOR);
}
public receive (message: Buffer) {
const mode = this._mode;
switch (mode) {
case 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.notify("color", { color });
}
break;
case 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.notify("distance", { distance });
}
break;
case 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.notify("colorAndDistance", { color, distance });
}
break;
}
}
}
export enum Mode {
COLOR = 0x00,
DISTANCE = 0x01,
COLOR_AND_DISTANCE = 0x08
}
export const ModeMap: {[event: string]: number} = {
"color": Mode.COLOR,
"distance": Mode.DISTANCE,
"colorAndDistance": Mode.COLOR_AND_DISTANCE
};