This commit is contained in:
parent
6d23179be6
commit
195c0d5123
@ -25,9 +25,10 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs
|
||||
let color = 1;
|
||||
setInterval(() => {
|
||||
|
||||
const hubs = poweredUP.getConnectedHubs(); // Get an array of all connected hubs
|
||||
hubs.forEach((hub) => {
|
||||
hub.setLEDColor(color); // Set the color
|
||||
const hubs = poweredUP.getHubs(); // Get an array of all connected hubs
|
||||
hubs.forEach(async (hub) => {
|
||||
const led = await hub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED);
|
||||
led.setColor(color); // Set the color
|
||||
})
|
||||
color++;
|
||||
if (color > 10) {
|
||||
|
@ -58,13 +58,13 @@ export enum DeviceType {
|
||||
VOLTAGE_SENSOR = 20,
|
||||
CURRENT_SENSOR = 21,
|
||||
PIEZO_TONE = 22,
|
||||
RGB_LIGHT = 23,
|
||||
HUB_LED = 23,
|
||||
TILT_SENSOR = 34,
|
||||
MOTION_SENSOR = 35,
|
||||
COLOR_DISTANCE_SENSOR = 37,
|
||||
MEDIUM_LINEAR_MOTOR = 38,
|
||||
MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39,
|
||||
BOOST_TILT = 40,
|
||||
MOVE_HUB_TILT_SENSOR = 40,
|
||||
DUPLO_TRAIN_BASE_MOTOR = 41,
|
||||
DUPLO_TRAIN_BASE_SPEAKER = 42,
|
||||
DUPLO_TRAIN_BASE_COLOR = 43,
|
||||
|
@ -69,6 +69,10 @@ export class Device extends EventEmitter {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
public get mode () {
|
||||
return this._mode;
|
||||
}
|
||||
|
||||
protected get isWeDo2SmartHub () {
|
||||
return this._isWeDo2SmartHub;
|
||||
}
|
||||
|
84
src/devices/hubled.ts
Normal file
84
src/devices/hubled.ts
Normal file
@ -0,0 +1,84 @@
|
||||
import { Device } from "./device";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class HubLED extends Device {
|
||||
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, {}, Consts.DeviceType.HUB_LED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the color of the LED on the Hub via a color value.
|
||||
* @method HubLED#setColor
|
||||
* @param {Color} color
|
||||
* @returns {Promise} Resolved upon successful issuance of command.
|
||||
*/
|
||||
public setColor (color: number | boolean) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.mode !== HubLED.Mode.COLOR) {
|
||||
this.subscribe(HubLED.Mode.COLOR);
|
||||
}
|
||||
if (typeof color === "boolean") {
|
||||
color = 0;
|
||||
}
|
||||
this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, color]));
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the color of the LED on the Hub via RGB values.
|
||||
* @method HubLED#setRGB
|
||||
* @param {number} red
|
||||
* @param {number} green
|
||||
* @param {number} blue
|
||||
* @returns {Promise} Resolved upon successful issuance of command.
|
||||
*/
|
||||
public setRGB (red: number, green: number, blue: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.mode !== HubLED.Mode.RGB) {
|
||||
this.subscribe(HubLED.Mode.RGB);
|
||||
}
|
||||
this.send(Buffer.from([0x81, this.portId, 0x11, 0x51, 0x01, red, green, blue]));
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set the light brightness.
|
||||
* @method Light#brightness
|
||||
* @param {number} brightness Brightness value between 0-100 (0 is off)
|
||||
* @returns {Promise} Resolved upon successful completion of command.
|
||||
*/
|
||||
public setBrightness (brightness: number) {
|
||||
return new Promise((resolve) => {
|
||||
if (this.isWeDo2SmartHub) {
|
||||
const data = Buffer.from([this.portId, 0x01, 0x02, brightness]);
|
||||
this.send(data, Consts.BLECharacteristic.WEDO2_MOTOR_VALUE_WRITE);
|
||||
} else {
|
||||
const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x00, brightness]);
|
||||
this.send(data);
|
||||
}
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export namespace HubLED {
|
||||
|
||||
export enum Mode {
|
||||
COLOR = 0x00,
|
||||
RGB = 0x01
|
||||
}
|
||||
|
||||
}
|
43
src/devices/movehubtiltsensor.ts
Normal file
43
src/devices/movehubtiltsensor.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { Device } from "./device";
|
||||
|
||||
import { IDeviceInterface } from "../interfaces";
|
||||
|
||||
import * as Consts from "../consts";
|
||||
|
||||
export class MoveHubTiltSensor extends Device {
|
||||
|
||||
constructor (hub: IDeviceInterface, portId: number) {
|
||||
super(hub, portId, MoveHubTiltSensor.ModeMap, Consts.DeviceType.MOVE_HUB_TILT_SENSOR);
|
||||
}
|
||||
|
||||
public receive (message: Buffer) {
|
||||
const mode = this._mode;
|
||||
|
||||
switch (mode) {
|
||||
case MoveHubTiltSensor.Mode.TILT:
|
||||
/**
|
||||
* Emits when a tilt sensor is activated.
|
||||
* @event MoveHubTiltSensor#tilt
|
||||
* @param {number} x
|
||||
* @param {number} y
|
||||
*/
|
||||
const tiltX = -(message.readInt8(4));
|
||||
const tiltY = message.readInt8(5);
|
||||
this.emit("tilt", tiltX, tiltY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export namespace MoveHubTiltSensor {
|
||||
|
||||
export enum Mode {
|
||||
TILT = 0x00
|
||||
}
|
||||
|
||||
export const ModeMap: {[event: string]: number} = {
|
||||
"tilt": MoveHubTiltSensor.Mode.TILT
|
||||
}
|
||||
|
||||
}
|
@ -5,10 +5,12 @@ import { IBLEAbstraction } from "../interfaces";
|
||||
import { ColorDistanceSensor } from "../devices/colordistancesensor";
|
||||
import { CurrentSensor } from "../devices/currentsensor";
|
||||
import { Device } from "../devices/device";
|
||||
import { HubLED } from "../devices/hubled";
|
||||
import { Light } from "../devices/light";
|
||||
import { MediumLinearMotor } from "../devices/mediumlinearmotor";
|
||||
import { MotionSensor } from "../devices/motionsensor";
|
||||
import { MoveHubMediumLinearMotor } from "../devices/movehubmediumlinearmotor";
|
||||
import { MoveHubTiltSensor } from "../devices/movehubtiltsensor";
|
||||
import { PUPRemoteButton } from "../devices/pupremotebutton";
|
||||
import { SimpleMediumLinearMotor } from "../devices/simplemediumlinearmotor";
|
||||
import { TechnicLargeLinearMotor } from "../devices/techniclargelinearmotor";
|
||||
@ -317,6 +319,9 @@ export class Hub extends EventEmitter {
|
||||
case Consts.DeviceType.TILT_SENSOR:
|
||||
device = new TiltSensor(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.MOVE_HUB_TILT_SENSOR:
|
||||
device = new MoveHubTiltSensor(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.MEDIUM_LINEAR_MOTOR:
|
||||
device = new MediumLinearMotor(this, portId);
|
||||
break;
|
||||
@ -338,6 +343,9 @@ export class Hub extends EventEmitter {
|
||||
case Consts.DeviceType.PUP_REMOTE_BUTTON:
|
||||
device = new PUPRemoteButton(this, portId);
|
||||
break;
|
||||
case Consts.DeviceType.HUB_LED:
|
||||
device = new HubLED(this, portId);
|
||||
break;
|
||||
default:
|
||||
device = new Device(this, portId, undefined, deviceType);
|
||||
break;
|
||||
|
@ -78,45 +78,6 @@ export class LPF2Hub extends Hub {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the color of the LED on the Hub via a color value.
|
||||
* @method LPF2Hub#setLEDColor
|
||||
* @param {Color} color
|
||||
* @returns {Promise} Resolved upon successful issuance of command.
|
||||
*/
|
||||
public setLEDColor (color: number | boolean) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let data = Buffer.from([0x41, this._ledPort, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]);
|
||||
this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
||||
if (typeof color === "boolean") {
|
||||
color = 0;
|
||||
}
|
||||
data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x00, color]);
|
||||
this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the color of the LED on the Hub via RGB values.
|
||||
* @method LPF2Hub#setLEDRGB
|
||||
* @param {number} red
|
||||
* @param {number} green
|
||||
* @param {number} blue
|
||||
* @returns {Promise} Resolved upon successful issuance of command.
|
||||
*/
|
||||
public setLEDRGB (red: number, green: number, blue: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let data = Buffer.from([0x41, this._ledPort, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]);
|
||||
this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
||||
data = Buffer.from([0x81, this._ledPort, 0x11, 0x51, 0x01, red, green, blue]);
|
||||
this.send(data, Consts.BLECharacteristic.LPF2_ALL);
|
||||
return resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public send (message: Buffer, uuid: string, callback?: () => void) {
|
||||
message = Buffer.concat([Buffer.alloc(2), message]);
|
||||
message[0] = message.length;
|
||||
@ -460,35 +421,6 @@ export class LPF2Hub extends Hub {
|
||||
// this.emit("accel", "ACCEL", accelX, accelY, accelZ);
|
||||
// break;
|
||||
// }
|
||||
// case Consts.DeviceType.BOOST_TILT: {
|
||||
// const tiltX = data.readInt8(4);
|
||||
// const tiltY = data.readInt8(5);
|
||||
// this._lastTiltX = tiltX;
|
||||
// this._lastTiltY = tiltY;
|
||||
// this.emit("tilt", port.id, this._lastTiltX, this._lastTiltY, this._lastTiltZ);
|
||||
// break;
|
||||
// }
|
||||
// case Consts.DeviceType.POWERED_UP_REMOTE_BUTTON: {
|
||||
// switch (data[4]) {
|
||||
// case 0x01: {
|
||||
// this.emit("button", port.id, Consts.ButtonState.UP);
|
||||
// break;
|
||||
// }
|
||||
// case 0xff: {
|
||||
// this.emit("button", port.id, Consts.ButtonState.DOWN);
|
||||
// break;
|
||||
// }
|
||||
// case 0x7f: {
|
||||
// this.emit("button", port.id, Consts.ButtonState.STOP);
|
||||
// break;
|
||||
// }
|
||||
// case 0x00: {
|
||||
// this.emit("button", port.id, Consts.ButtonState.RELEASED);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
// case Consts.DeviceType.DUPLO_TRAIN_BASE_COLOR: {
|
||||
// if (data[4] <= 10) {
|
||||
// this.emit("color", port.id, data[4]);
|
||||
|
@ -13,10 +13,12 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||
import { CurrentSensor } from "./devices/currentsensor";
|
||||
import { Device } from "./devices/device";
|
||||
import { HubLED } from "./devices/hubled";
|
||||
import { Light } from "./devices/light";
|
||||
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
||||
import { MotionSensor } from "./devices/motionsensor";
|
||||
import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor";
|
||||
import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
||||
import { PUPRemoteButton } from "./devices/pupremotebutton";
|
||||
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
||||
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
||||
@ -40,10 +42,12 @@ window.PoweredUP = {
|
||||
Consts,
|
||||
ColorDistanceSensor,
|
||||
Device,
|
||||
HubLED,
|
||||
Light,
|
||||
MediumLinearMotor,
|
||||
MotionSensor,
|
||||
MoveHubMediumLinearMotor,
|
||||
MoveHubTiltSensor,
|
||||
PUPRemoteButton,
|
||||
SimpleMediumLinearMotor,
|
||||
TechnicLargeLinearMotor,
|
||||
|
@ -13,10 +13,12 @@ import { WeDo2SmartHub } from "./hubs/wedo2smarthub";
|
||||
import { ColorDistanceSensor } from "./devices/colordistancesensor";
|
||||
import { CurrentSensor } from "./devices/currentsensor";
|
||||
import { Device } from "./devices/device";
|
||||
import { HubLED } from "./devices/hubled";
|
||||
import { Light } from "./devices/light";
|
||||
import { MediumLinearMotor } from "./devices/mediumlinearmotor";
|
||||
import { MotionSensor } from "./devices/motionsensor";
|
||||
import { MoveHubMediumLinearMotor } from "./devices/movehubmediumlinearmotor";
|
||||
import { MoveHubTiltSensor } from "./devices/movehubtiltsensor";
|
||||
import { PUPRemoteButton } from "./devices/pupremotebutton";
|
||||
import { SimpleMediumLinearMotor } from "./devices/simplemediumlinearmotor";
|
||||
import { TechnicLargeLinearMotor } from "./devices/techniclargelinearmotor";
|
||||
@ -40,10 +42,12 @@ export {
|
||||
Consts,
|
||||
ColorDistanceSensor,
|
||||
Device,
|
||||
HubLED,
|
||||
Light,
|
||||
MediumLinearMotor,
|
||||
MotionSensor,
|
||||
MoveHubMediumLinearMotor,
|
||||
MoveHubTiltSensor,
|
||||
PUPRemoteButton,
|
||||
SimpleMediumLinearMotor,
|
||||
TechnicLargeLinearMotor,
|
||||
|
Loading…
x
Reference in New Issue
Block a user