Motion and tilt sensor
This commit is contained in:
parent
81cbdac591
commit
31ac93275c
@ -65,7 +65,7 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
device instanceof PoweredUP.Lights
|
device instanceof PoweredUP.Light
|
||||||
) {
|
) {
|
||||||
const lights = device;
|
const lights = device;
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
|||||||
|
|
||||||
if (device instanceof PoweredUP.ColorDistanceSensor) {
|
if (device instanceof PoweredUP.ColorDistanceSensor) {
|
||||||
const sensor = device;
|
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}`);
|
console.log(`Distance ${distance}`);
|
||||||
});
|
});
|
||||||
sensor.on("color", (color) => {
|
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", () => {
|
hub.on("disconnect", () => {
|
||||||
|
@ -59,8 +59,8 @@ export enum DeviceType {
|
|||||||
CURRENT = 21,
|
CURRENT = 21,
|
||||||
PIEZO_TONE = 22,
|
PIEZO_TONE = 22,
|
||||||
RGB_LIGHT = 23,
|
RGB_LIGHT = 23,
|
||||||
WEDO2_TILT = 34,
|
TILT_SENSOR = 34,
|
||||||
WEDO2_DISTANCE = 35,
|
MOTION_SENSOR = 35,
|
||||||
COLOR_DISTANCE_SENSOR = 37,
|
COLOR_DISTANCE_SENSOR = 37,
|
||||||
MEDIUM_LINEAR_MOTOR = 38,
|
MEDIUM_LINEAR_MOTOR = 38,
|
||||||
MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39,
|
MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39,
|
||||||
|
@ -7,7 +7,7 @@ export class Device extends EventEmitter {
|
|||||||
|
|
||||||
public autoSubscribe: boolean = true;
|
public autoSubscribe: boolean = true;
|
||||||
|
|
||||||
protected _mode: number = 0x00;
|
protected _mode: number | undefined;
|
||||||
protected _busy: boolean = false;
|
protected _busy: boolean = false;
|
||||||
protected _finished: (() => void) | undefined;
|
protected _finished: (() => void) | undefined;
|
||||||
|
|
||||||
|
@ -14,10 +14,12 @@ import { ColorDistanceSensor } from "./colordistancesensor";
|
|||||||
import { Device } from "./device";
|
import { Device } from "./device";
|
||||||
import { Light } from "./light";
|
import { Light } from "./light";
|
||||||
import { MediumLinearMotor } from "./mediumlinearmotor";
|
import { MediumLinearMotor } from "./mediumlinearmotor";
|
||||||
|
import { MotionSensor } from "./motionsensor";
|
||||||
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
|
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
|
||||||
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
|
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
|
||||||
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
|
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
|
||||||
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
|
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
|
||||||
|
import { TiltSensor } from "./tiltsensor";
|
||||||
import { TrainMotor } from "./trainmotor";
|
import { TrainMotor } from "./trainmotor";
|
||||||
|
|
||||||
import { isWebBluetooth } from "./utils";
|
import { isWebBluetooth } from "./utils";
|
||||||
@ -37,10 +39,12 @@ window.PoweredUP = {
|
|||||||
Device,
|
Device,
|
||||||
Light,
|
Light,
|
||||||
MediumLinearMotor,
|
MediumLinearMotor,
|
||||||
|
MotionSensor,
|
||||||
MoveHubMediumLinearMotor,
|
MoveHubMediumLinearMotor,
|
||||||
SimpleMediumLinearMotor,
|
SimpleMediumLinearMotor,
|
||||||
TechnicLargeLinearMotor,
|
TechnicLargeLinearMotor,
|
||||||
TechnicXLargeLinearMotor,
|
TechnicXLargeLinearMotor,
|
||||||
|
TiltSensor,
|
||||||
TrainMotor,
|
TrainMotor,
|
||||||
isWebBluetooth
|
isWebBluetooth
|
||||||
};
|
};
|
||||||
|
@ -14,10 +14,12 @@ import { ColorDistanceSensor } from "./colordistancesensor";
|
|||||||
import { Device } from "./device";
|
import { Device } from "./device";
|
||||||
import { Light } from "./light";
|
import { Light } from "./light";
|
||||||
import { MediumLinearMotor } from "./mediumlinearmotor";
|
import { MediumLinearMotor } from "./mediumlinearmotor";
|
||||||
|
import { MotionSensor } from "./motionsensor";
|
||||||
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
|
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
|
||||||
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
|
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
|
||||||
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
|
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
|
||||||
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
|
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
|
||||||
|
import { TiltSensor } from "./tiltsensor";
|
||||||
import { TrainMotor } from "./trainmotor";
|
import { TrainMotor } from "./trainmotor";
|
||||||
|
|
||||||
import { isWebBluetooth } from "./utils";
|
import { isWebBluetooth } from "./utils";
|
||||||
@ -37,10 +39,12 @@ export {
|
|||||||
Device,
|
Device,
|
||||||
Light,
|
Light,
|
||||||
MediumLinearMotor,
|
MediumLinearMotor,
|
||||||
|
MotionSensor,
|
||||||
MoveHubMediumLinearMotor,
|
MoveHubMediumLinearMotor,
|
||||||
SimpleMediumLinearMotor,
|
SimpleMediumLinearMotor,
|
||||||
TechnicLargeLinearMotor,
|
TechnicLargeLinearMotor,
|
||||||
TechnicXLargeLinearMotor,
|
TechnicXLargeLinearMotor,
|
||||||
|
TiltSensor,
|
||||||
TrainMotor,
|
TrainMotor,
|
||||||
isWebBluetooth
|
isWebBluetooth
|
||||||
};
|
};
|
||||||
|
@ -4,10 +4,12 @@ import { Hub } from "./hub";
|
|||||||
import { ColorDistanceSensor } from "./colordistancesensor";
|
import { ColorDistanceSensor } from "./colordistancesensor";
|
||||||
import { Light } from "./light";
|
import { Light } from "./light";
|
||||||
import { MediumLinearMotor } from "./mediumlinearmotor";
|
import { MediumLinearMotor } from "./mediumlinearmotor";
|
||||||
|
import { MotionSensor } from "./motionsensor";
|
||||||
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
|
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
|
||||||
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
|
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
|
||||||
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
|
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
|
||||||
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
|
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
|
||||||
|
import { TiltSensor } from "./tiltsensor";
|
||||||
import { TrainMotor } from "./trainmotor";
|
import { TrainMotor } from "./trainmotor";
|
||||||
|
|
||||||
import * as Consts from "./consts";
|
import * as Consts from "./consts";
|
||||||
@ -301,6 +303,12 @@ export class LPF2Hub extends Hub {
|
|||||||
case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR:
|
case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR:
|
||||||
device = new MoveHubMediumLinearMotor(this, portId);
|
device = new MoveHubMediumLinearMotor(this, portId);
|
||||||
break;
|
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:
|
case Consts.DeviceType.MEDIUM_LINEAR_MOTOR:
|
||||||
device = new MediumLinearMotor(this, portId);
|
device = new MediumLinearMotor(this, portId);
|
||||||
break;
|
break;
|
||||||
|
41
src/motionsensor.ts
Normal file
41
src/motionsensor.ts
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
src/tiltsensor.ts
Normal file
40
src/tiltsensor.ts
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user