From 31ac93275c96c67a5c774795a05e35045599ef98 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Mon, 16 Dec 2019 11:48:09 -0800 Subject: [PATCH] Motion and tilt sensor --- examples/new_device_test.js | 18 ++++++++++++++-- src/consts.ts | 4 ++-- src/device.ts | 2 +- src/index-browser.ts | 4 ++++ src/index-node.ts | 4 ++++ src/lpf2hub.ts | 8 ++++++++ src/motionsensor.ts | 41 +++++++++++++++++++++++++++++++++++++ src/tiltsensor.ts | 40 ++++++++++++++++++++++++++++++++++++ 8 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 src/motionsensor.ts create mode 100644 src/tiltsensor.ts diff --git a/examples/new_device_test.js b/examples/new_device_test.js index fb05ed0..6364a2b 100644 --- a/examples/new_device_test.js +++ b/examples/new_device_test.js @@ -65,7 +65,7 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs } if ( - device instanceof PoweredUP.Lights + device instanceof PoweredUP.Light ) { const lights = device; @@ -82,7 +82,7 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs if (device instanceof PoweredUP.ColorDistanceSensor) { const sensor = device; - sensor.on("distance", (distance) => { // Adding an event handler for distance automatically subscribes to distance notifications + sensor.on("distance", (distance) => { console.log(`Distance ${distance}`); }); sensor.on("color", (color) => { @@ -90,6 +90,20 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs }); } + if (device instanceof PoweredUP.MotionSensor) { + const sensor = device; + sensor.on("distance", (distance) => { + console.log(`Distance ${distance}`); + }); + } + + if (device instanceof PoweredUP.TiltSensor) { + const sensor = device; + sensor.on("tilt", (x, y) => { + console.log(`Tilt ${x} ${y}`); + }); + } + }); hub.on("disconnect", () => { diff --git a/src/consts.ts b/src/consts.ts index 11f7bb7..3ab80cb 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -59,8 +59,8 @@ export enum DeviceType { CURRENT = 21, PIEZO_TONE = 22, RGB_LIGHT = 23, - WEDO2_TILT = 34, - WEDO2_DISTANCE = 35, + TILT_SENSOR = 34, + MOTION_SENSOR = 35, COLOR_DISTANCE_SENSOR = 37, MEDIUM_LINEAR_MOTOR = 38, MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39, diff --git a/src/device.ts b/src/device.ts index 7494a75..8d95041 100644 --- a/src/device.ts +++ b/src/device.ts @@ -7,7 +7,7 @@ export class Device extends EventEmitter { public autoSubscribe: boolean = true; - protected _mode: number = 0x00; + protected _mode: number | undefined; protected _busy: boolean = false; protected _finished: (() => void) | undefined; diff --git a/src/index-browser.ts b/src/index-browser.ts index 0eafb35..595ab5f 100644 --- a/src/index-browser.ts +++ b/src/index-browser.ts @@ -14,10 +14,12 @@ import { ColorDistanceSensor } from "./colordistancesensor"; import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MotionSensor } from "./motionsensor"; import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; +import { TiltSensor } from "./tiltsensor"; import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -37,10 +39,12 @@ window.PoweredUP = { Device, Light, MediumLinearMotor, + MotionSensor, MoveHubMediumLinearMotor, SimpleMediumLinearMotor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, + TiltSensor, TrainMotor, isWebBluetooth }; diff --git a/src/index-node.ts b/src/index-node.ts index ffe988d..48566c0 100644 --- a/src/index-node.ts +++ b/src/index-node.ts @@ -14,10 +14,12 @@ import { ColorDistanceSensor } from "./colordistancesensor"; import { Device } from "./device"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MotionSensor } from "./motionsensor"; import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; +import { TiltSensor } from "./tiltsensor"; import { TrainMotor } from "./trainmotor"; import { isWebBluetooth } from "./utils"; @@ -37,10 +39,12 @@ export { Device, Light, MediumLinearMotor, + MotionSensor, MoveHubMediumLinearMotor, SimpleMediumLinearMotor, TechnicLargeLinearMotor, TechnicXLargeLinearMotor, + TiltSensor, TrainMotor, isWebBluetooth }; diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index b573bb3..bfd167a 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -4,10 +4,12 @@ import { Hub } from "./hub"; import { ColorDistanceSensor } from "./colordistancesensor"; import { Light } from "./light"; import { MediumLinearMotor } from "./mediumlinearmotor"; +import { MotionSensor } from "./motionsensor"; import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor"; import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor"; import { TechnicLargeLinearMotor } from "./techniclargelinearmotor"; import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor"; +import { TiltSensor } from "./tiltsensor"; import { TrainMotor } from "./trainmotor"; import * as Consts from "./consts"; @@ -301,6 +303,12 @@ export class LPF2Hub extends Hub { case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR: device = new MoveHubMediumLinearMotor(this, portId); break; + case Consts.DeviceType.MOTION_SENSOR: + device = new MotionSensor(this, portId); + break; + case Consts.DeviceType.TILT_SENSOR: + device = new TiltSensor(this, portId); + break; case Consts.DeviceType.MEDIUM_LINEAR_MOTOR: device = new MediumLinearMotor(this, portId); break; diff --git a/src/motionsensor.ts b/src/motionsensor.ts new file mode 100644 index 0000000..b68a0b0 --- /dev/null +++ b/src/motionsensor.ts @@ -0,0 +1,41 @@ +import { Device } from "./device"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class MotionSensor extends Device { + + constructor (hub: Hub, 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; + + switch (mode) { + case 0x00: + let distance = message[4]; + if (message[5] === 1) { + distance = message[4] + 255; + } + /** + * Emits when a distance sensor is activated. + * @event MotionSensor#distance + * @param {number} distance Distance, in millimeters. + */ + this.emit("distance", distance * 10); + break; + } + } + +} diff --git a/src/tiltsensor.ts b/src/tiltsensor.ts new file mode 100644 index 0000000..169c88a --- /dev/null +++ b/src/tiltsensor.ts @@ -0,0 +1,40 @@ +import { Device } from "./device"; +import { Hub } from "./hub"; + +import * as Consts from "./consts"; + +export class TiltSensor extends Device { + + constructor (hub: Hub, portId: number) { + super(hub, portId, Consts.DeviceType.TILT_SENSOR); + + this.on("newListener", (event) => { + if (this.autoSubscribe) { + switch (event) { + case "tilt": + this.subscribe(0x00); + break; + } + } + }); + } + + public receive (message: Buffer) { + const mode = this._mode; + + switch (mode) { + case 0x00: + const tiltX = message.readInt8(4); + const tiltY = message.readInt8(5); + /** + * Emits when a tilt sensor is activated. + * @event LPF2Hub#tilt + * @param {number} x + * @param {number} y + */ + this.emit("tilt", tiltX, tiltY); + break; + } + } + +}