node-poweredup/src/motionsensor.ts
Nathan Kellenicki 023b141c4d
Some checks failed
continuous-integration/drone/push Build is failing
Better handling of device initialisation, implemented more modes
2019-12-16 17:07:43 -08:00

44 lines
1.2 KiB
TypeScript

import { Device } from "./device";
import { IDeviceInterface } from "./interfaces";
import * as Consts from "./consts";
export class MotionSensor extends Device {
constructor (hub: IDeviceInterface, portId: number) {
super(hub, portId, Consts.DeviceType.MOTION_SENSOR);
this.on("newListener", (event) => {
if (this.autoSubscribe) {
switch (event) {
case "distance":
this.subscribe(0x00);
break;
}
}
});
}
public receive (message: Buffer) {
const mode = this._mode;
const isWeDo2 = (this.hub.type === Consts.HubType.WEDO2_SMART_HUB);
switch (mode) {
case 0x00:
let distance = message[isWeDo2 ? 2 : 4];
if (message[isWeDo2 ? 3 : 5] === 1) {
distance = distance + 255;
}
/**
* Emits when a distance sensor is activated.
* @event MotionSensor#distance
* @param {number} distance Distance, in millimeters.
*/
this.emit("distance", distance * 10);
break;
}
}
}