From c6f8015154e81254aa9fe73985058d9a4ae1d318 Mon Sep 17 00:00:00 2001 From: Nathan Kellenicki Date: Wed, 29 Jan 2020 14:54:14 -0800 Subject: [PATCH] Added example of slow speed train control with a Medium Linear Motor (Boost Motor) --- examples/slow_train_remote.js | 78 +++++++++++++++++++++++++++++++++++ src/consts.ts | 10 ++--- 2 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 examples/slow_train_remote.js diff --git a/examples/slow_train_remote.js b/examples/slow_train_remote.js new file mode 100644 index 0000000..22075f1 --- /dev/null +++ b/examples/slow_train_remote.js @@ -0,0 +1,78 @@ +/* + * + * This example allows you to connect Vernie and a Powered UP Remote Control to your laptop, and enables the control of Vernie with the Remote. + * + */ + +const PoweredUP = require(".."); + +const poweredUP = new PoweredUP.PoweredUP(); +poweredUP.scan(); // Start scanning + +console.log("Looking for train and remote..."); + +const TRAIN_LED_COLOR = PoweredUP.Consts.Color.PURPLE; + +let trainHub = null; +let trainMotor = null; +let remoteHub = null; +let remoteButtonLeft = null; +let remoteButtonRight = null; + +let currentSpeed = 0; + +poweredUP.on("discover", async (hub) => { // Wait to discover Vernie and Remote + + if (hub.type === PoweredUP.Consts.HubType.HUB) { + + trainHub = hub; + await trainHub.connect(); + const led = await trainHub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED); + trainMotor = await trainHub.waitForDeviceByType(PoweredUP.Consts.DeviceType.MEDIUM_LINEAR_MOTOR); + led.setColor(TRAIN_LED_COLOR); + console.log(`Connected to train (${trainHub.name})!`); + if (trainHub && remoteHub) { + console.log("You're now ready to go!"); + } + + } else if (hub.type === PoweredUP.Consts.HubType.REMOTE_CONTROL) { + remoteHub = hub; + await remoteHub.connect(); + const led = await remoteHub.waitForDeviceByType(PoweredUP.Consts.DeviceType.HUB_LED); + remoteButtonLeft = await remoteHub.waitForDeviceAtPort("LEFT"); + remoteButtonRight = await remoteHub.waitForDeviceAtPort("RIGHT"); + led.setColor(TRAIN_LED_COLOR); + + remoteButtonLeft.on("remoteButton", ({ event }) => { + if (trainMotor) { + if (event === PoweredUP.Consts.ButtonState.UP) { + currentSpeed += 10; + } else if (event === PoweredUP.Consts.ButtonState.DOWN) { + currentSpeed -= 10; + } else if (event === PoweredUP.Consts.ButtonState.STOP) { + currentSpeed = 0; + } + trainMotor.setSpeed(currentSpeed); + } + }); + + remoteButtonRight.on("remoteButton", ({ event }) => { + if (trainMotor) { + if (event === PoweredUP.Consts.ButtonState.UP) { + currentSpeed += 1; + } else if (event === PoweredUP.Consts.ButtonState.DOWN) { + currentSpeed -= 1; + } else if (event === PoweredUP.Consts.ButtonState.STOP) { + currentSpeed = 0; + } + trainMotor.setSpeed(currentSpeed); + } + }); + + console.log(`Connected to remote (${remoteHub.name})!`); + if (trainHub && remoteHub) { + console.log("You're now ready to go!"); + } + } + +}); \ No newline at end of file diff --git a/src/consts.ts b/src/consts.ts index 196d312..549b66c 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -134,11 +134,11 @@ export const ColorNames = Color; * @property {number} STOP 4 */ export enum ButtonState { - PRESSED = 0, - RELEASED = 1, - UP = 2, - DOWN = 3, - STOP = 4 + PRESSED = 2, + RELEASED = 0, + UP = 1, + DOWN = 255, + STOP = 127 }