Merge branch 'feature/devices' of github.com:nathankellenicki/node-poweredup into feature/devices

This commit is contained in:
Nathan Kellenicki 2019-12-16 10:44:12 -08:00
commit 81cbdac591
13 changed files with 183 additions and 51 deletions

View File

@ -24,18 +24,60 @@ poweredUP.on("discover", async (hub) => { // Wait to discover hubs
console.log(`Detached device ${device.type} from ${device.port}`); console.log(`Detached device ${device.type} from ${device.port}`);
}); });
if (device instanceof PoweredUP.ControlPlusLargeMotor) { if (
// device instanceof PoweredUP.MoveHubMediumLinearMotor ||
device instanceof PoweredUP.MediumLinearMotor ||
device instanceof PoweredUP.TechnicLargeLinearMotor ||
device instanceof PoweredUP.TechnicXLargeLinearMotor
) {
const motor = device; const motor = device;
motor.on("rotate", (angle) => { motor.on("rotate", (angle) => {
console.log(`Rotate ${angle}`); console.log(`Rotate ${angle}`);
}); });
await motor.rotateByAngle(9000, 50); await motor.rotateByAngle(900, 50);
await motor.rotateByAngle(9000, -50); await motor.rotateByAngle(900, -50);
await motor.rotateByAngle(9000, 50); await motor.rotateByAngle(900, 50);
await motor.rotateByAngle(9000, -50); await motor.rotateByAngle(900, -50);
motor.power(100); motor.setPower(20);
}
if (
device instanceof PoweredUP.SimpleMediumLinearMotor ||
device instanceof PoweredUP.TrainMotor
) {
const motor = device;
motor.setPower(20);
await hub.sleep(1000);
motor.setPower(40);
await hub.sleep(1000);
motor.setPower(60);
await hub.sleep(1000);
motor.setPower(80);
await hub.sleep(1000);
motor.setPower(100);
await hub.sleep(1000);
motor.setPower(60);
await hub.sleep(1000);
motor.setPower(20);
}
if (
device instanceof PoweredUP.Lights
) {
const lights = device;
lights.setBrightness(100);
await hub.sleep(1000);
lights.setBrightness(0);
await hub.sleep(1000);
lights.setBrightness(100);
await hub.sleep(1000);
lights.setBrightness(0);
await hub.sleep(1000);
lights.setBrightness(100);
} }
if (device instanceof PoweredUP.ColorDistanceSensor) { if (device instanceof PoweredUP.ColorDistanceSensor) {

View File

@ -0,0 +1,12 @@
import { Hub } from "./hub";
import * as Consts from "./consts";
import { TachoMotor } from "./tachomotor";
export class MoveHubMediumLinearMotor extends TachoMotor {
constructor (hub: Hub, portId: number) {
super(hub, portId, Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR);
}
}

View File

@ -26,7 +26,7 @@ export const HubTypeNames = HubType;
/** /**
* @typedef DeviceType * @typedef DeviceType
* @property {number} UNKNOWN 0 * @property {number} UNKNOWN 0
* @property {number} BASIC_MOTOR 1 * @property {number} SIMPLE_MEDIUM_LINEAR_MOTOR 1
* @property {number} TRAIN_MOTOR 2 * @property {number} TRAIN_MOTOR 2
* @property {number} LED_LIGHTS 8 * @property {number} LED_LIGHTS 8
* @property {number} VOLTAGE 20 * @property {number} VOLTAGE 20
@ -35,9 +35,9 @@ export const HubTypeNames = HubType;
* @property {number} RGB_LIGHT 23 * @property {number} RGB_LIGHT 23
* @property {number} WEDO2_TILT 34 * @property {number} WEDO2_TILT 34
* @property {number} WEDO2_DISTANCE 35 * @property {number} WEDO2_DISTANCE 35
* @property {number} BOOST_DISTANCE 37 * @property {number} COLOR_DISTANCE_SENSOR 37
* @property {number} BOOST_TACHO_MOTOR 38 * @property {number} MEDIUM_LINEAR_MOTOR 38
* @property {number} BOOST_MOVE_HUB_MOTOR 39 * @property {number} MOVE_HUB_MEDIUM_LINEAR_MOTOR 39
* @property {number} BOOST_TILT 40 * @property {number} BOOST_TILT 40
* @property {number} DUPLO_TRAIN_BASE_MOTOR 41 * @property {number} DUPLO_TRAIN_BASE_MOTOR 41
* @property {number} DUPLO_TRAIN_BASE_SPEAKER 42 * @property {number} DUPLO_TRAIN_BASE_SPEAKER 42
@ -54,7 +54,7 @@ export enum DeviceType {
UNKNOWN = 0, UNKNOWN = 0,
SIMPLE_MEDIUM_LINEAR_MOTOR = 1, SIMPLE_MEDIUM_LINEAR_MOTOR = 1,
TRAIN_MOTOR = 2, TRAIN_MOTOR = 2,
LED_LIGHTS = 8, LIGHT = 8,
VOLTAGE = 20, VOLTAGE = 20,
CURRENT = 21, CURRENT = 21,
PIEZO_TONE = 22, PIEZO_TONE = 22,
@ -62,15 +62,15 @@ export enum DeviceType {
WEDO2_TILT = 34, WEDO2_TILT = 34,
WEDO2_DISTANCE = 35, WEDO2_DISTANCE = 35,
COLOR_DISTANCE_SENSOR = 37, COLOR_DISTANCE_SENSOR = 37,
BOOST_TACHO_MOTOR = 38, MEDIUM_LINEAR_MOTOR = 38,
BOOST_MOVE_HUB_MOTOR = 39, MOVE_HUB_MEDIUM_LINEAR_MOTOR = 39,
BOOST_TILT = 40, BOOST_TILT = 40,
DUPLO_TRAIN_BASE_MOTOR = 41, DUPLO_TRAIN_BASE_MOTOR = 41,
DUPLO_TRAIN_BASE_SPEAKER = 42, DUPLO_TRAIN_BASE_SPEAKER = 42,
DUPLO_TRAIN_BASE_COLOR = 43, DUPLO_TRAIN_BASE_COLOR = 43,
DUPLO_TRAIN_BASE_SPEEDOMETER = 44, DUPLO_TRAIN_BASE_SPEEDOMETER = 44,
CONTROL_PLUS_LARGE_MOTOR = 46, TECHNIC_LARGE_LINEAR_MOTOR = 46,
CONTROL_PLUS_XLARGE_MOTOR = 47, TECHNIC_XLARGE_LINEAR_MOTOR = 47,
CONTROL_PLUS_GEST = 54, CONTROL_PLUS_GEST = 54,
POWERED_UP_REMOTE_BUTTON = 55, POWERED_UP_REMOTE_BUTTON = 55,
RSSI = 56, RSSI = 56,

View File

@ -1,13 +0,0 @@
import { BasicMotor } from "./basicmotor";
import { Hub } from "./hub";
import * as Consts from "./consts";
import { TachoMotor } from "./tachomotor";
export class ControlPlusLargeMotor extends TachoMotor {
constructor (hub: Hub, portId: number) {
super(hub, portId, Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR);
}
}

View File

@ -11,8 +11,14 @@ import { PUPRemote } from "./pupremote";
import { WeDo2SmartHub } from "./wedo2smarthub"; import { WeDo2SmartHub } from "./wedo2smarthub";
import { ColorDistanceSensor } from "./colordistancesensor"; import { ColorDistanceSensor } from "./colordistancesensor";
import { ControlPlusLargeMotor } from "./controlpluslargemotor";
import { Device } from "./device"; import { Device } from "./device";
import { Light } from "./light";
import { MediumLinearMotor } from "./mediumlinearmotor";
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
import { TrainMotor } from "./trainmotor";
import { isWebBluetooth } from "./utils"; import { isWebBluetooth } from "./utils";
@ -27,9 +33,15 @@ window.PoweredUP = {
PUPRemote, PUPRemote,
DuploTrainBase, DuploTrainBase,
Consts, Consts,
Device,
ColorDistanceSensor, ColorDistanceSensor,
ControlPlusLargeMotor, Device,
Light,
MediumLinearMotor,
MoveHubMediumLinearMotor,
SimpleMediumLinearMotor,
TechnicLargeLinearMotor,
TechnicXLargeLinearMotor,
TrainMotor,
isWebBluetooth isWebBluetooth
}; };

View File

@ -11,8 +11,14 @@ import { PUPRemote } from "./pupremote";
import { WeDo2SmartHub } from "./wedo2smarthub"; import { WeDo2SmartHub } from "./wedo2smarthub";
import { ColorDistanceSensor } from "./colordistancesensor"; import { ColorDistanceSensor } from "./colordistancesensor";
import { ControlPlusLargeMotor } from "./controlpluslargemotor";
import { Device } from "./device"; import { Device } from "./device";
import { Light } from "./light";
import { MediumLinearMotor } from "./mediumlinearmotor";
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
import { TrainMotor } from "./trainmotor";
import { isWebBluetooth } from "./utils"; import { isWebBluetooth } from "./utils";
@ -27,8 +33,14 @@ export {
PUPRemote, PUPRemote,
DuploTrainBase, DuploTrainBase,
Consts, Consts,
Device,
ColorDistanceSensor, ColorDistanceSensor,
ControlPlusLargeMotor, Device,
Light,
MediumLinearMotor,
MoveHubMediumLinearMotor,
SimpleMediumLinearMotor,
TechnicLargeLinearMotor,
TechnicXLargeLinearMotor,
TrainMotor,
isWebBluetooth isWebBluetooth
}; };

View File

@ -4,11 +4,11 @@ import { WeDo2SmartHub } from "./wedo2smarthub";
import * as Consts from "./consts"; import * as Consts from "./consts";
export class Lights extends Device { export class Light extends Device {
constructor (hub: Hub, portId: number) { constructor (hub: Hub, portId: number) {
super(hub, portId, Consts.DeviceType.LED_LIGHTS); super(hub, portId, Consts.DeviceType.LIGHT);
} }

View File

@ -2,8 +2,13 @@ import { Device } from "./device";
import { Hub } from "./hub"; import { Hub } from "./hub";
import { ColorDistanceSensor } from "./colordistancesensor"; import { ColorDistanceSensor } from "./colordistancesensor";
import { ControlPlusLargeMotor } from "./controlpluslargemotor"; import { Light } from "./light";
import { Lights } from "./lights"; import { MediumLinearMotor } from "./mediumlinearmotor";
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
import { TrainMotor } from "./trainmotor";
import * as Consts from "./consts"; import * as Consts from "./consts";
@ -284,11 +289,26 @@ export class LPF2Hub extends Hub {
let device; let device;
switch (deviceType) { switch (deviceType) {
case Consts.DeviceType.LED_LIGHTS: case Consts.DeviceType.LIGHT:
device = new Lights(this, portId); device = new Light(this, portId);
break; break;
case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR: case Consts.DeviceType.TRAIN_MOTOR:
device = new ControlPlusLargeMotor(this, portId); device = new TrainMotor(this, portId);
break;
case Consts.DeviceType.SIMPLE_MEDIUM_LINEAR_MOTOR:
device = new SimpleMediumLinearMotor(this, portId);
break;
case Consts.DeviceType.MOVE_HUB_MEDIUM_LINEAR_MOTOR:
device = new MoveHubMediumLinearMotor(this, portId);
break;
case Consts.DeviceType.MEDIUM_LINEAR_MOTOR:
device = new MediumLinearMotor(this, portId);
break;
case Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR:
device = new TechnicLargeLinearMotor(this, portId);
break;
case Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR:
device = new TechnicXLargeLinearMotor(this, portId);
break; break;
case Consts.DeviceType.COLOR_DISTANCE_SENSOR: case Consts.DeviceType.COLOR_DISTANCE_SENSOR:
device = new ColorDistanceSensor(this, portId); device = new ColorDistanceSensor(this, portId);

12
src/mediumlinearmotor.ts Normal file
View File

@ -0,0 +1,12 @@
import { Hub } from "./hub";
import * as Consts from "./consts";
import { TachoMotor } from "./tachomotor";
export class MediumLinearMotor extends TachoMotor {
constructor (hub: Hub, portId: number) {
super(hub, portId, Consts.DeviceType.MEDIUM_LINEAR_MOTOR);
}
}

View File

@ -38,7 +38,7 @@ export class TachoMotor extends BasicMotor {
/** /**
* Rotate a motor by a given angle. * Rotate a motor by a given angle.
* @method TachoMotor#setMotorAngle * @method TachoMotor#rotateByAngle
* @param {number} angle How much the motor should be rotated (in degrees). * @param {number} angle How much the motor should be rotated (in degrees).
* @param {number} [power=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. * @param {number} [power=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished). * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).

View File

@ -0,0 +1,12 @@
import { Hub } from "./hub";
import * as Consts from "./consts";
import { TachoMotor } from "./tachomotor";
export class TechnicLargeLinearMotor extends TachoMotor {
constructor (hub: Hub, portId: number) {
super(hub, portId, Consts.DeviceType.TECHNIC_LARGE_LINEAR_MOTOR);
}
}

View File

@ -0,0 +1,12 @@
import { Hub } from "./hub";
import * as Consts from "./consts";
import { TachoMotor } from "./tachomotor";
export class TechnicXLargeLinearMotor extends TachoMotor {
constructor (hub: Hub, portId: number) {
super(hub, portId, Consts.DeviceType.TECHNIC_XLARGE_LINEAR_MOTOR);
}
}

View File

@ -6,8 +6,13 @@ import { Device } from "./device";
import { Hub } from "./hub"; import { Hub } from "./hub";
import { ColorDistanceSensor } from "./colordistancesensor"; import { ColorDistanceSensor } from "./colordistancesensor";
import { ControlPlusLargeMotor } from "./controlpluslargemotor"; import { Light } from "./light";
import { Lights } from "./lights"; import { MediumLinearMotor } from "./mediumlinearmotor";
import { MoveHubMediumLinearMotor } from "./movehubmediumlinearmotor";
import { SimpleMediumLinearMotor } from "./simplemediumlinearmotor";
import { TechnicLargeLinearMotor } from "./techniclargelinearmotor";
import { TechnicXLargeLinearMotor } from "./technicxlargelinearmotor";
import { TrainMotor } from "./trainmotor";
import * as Consts from "./consts"; import * as Consts from "./consts";
@ -276,19 +281,25 @@ export class WeDo2SmartHub extends Hub {
let device; let device;
switch (deviceType) { switch (deviceType) {
case Consts.DeviceType.LED_LIGHTS: case Consts.DeviceType.LIGHT:
device = new Lights(this, portId); device = new Light(this, portId);
break;
case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR:
device = new ControlPlusLargeMotor(this, portId);
break; break;
// case Consts.DeviceType.BOOST_TACHO_MOTOR:
// device = new BoostTachoMotor(this, portId);
// break;
// case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR:
// device = new ControlPlusLargeMotor(this, portId);
// break;
// case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR:
// device = new ControlPlusXLargeMotor(this, portId);
// break;
case Consts.DeviceType.COLOR_DISTANCE_SENSOR: case Consts.DeviceType.COLOR_DISTANCE_SENSOR:
device = new ColorDistanceSensor(this, portId); device = new ColorDistanceSensor(this, portId);
break; break;
default: default:
device = new Device(this, portId, deviceType); device = new Device(this, portId, deviceType);
break; break;
} }
this._attachDevice(device); this._attachDevice(device);