Last active
January 5, 2016 17:55
-
-
Save aqualad/b0b4dc1e925396694d7b to your computer and use it in GitHub Desktop.
bot.js where I started to implement a list of currently pressed keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var five = require("johnny-five"); | |
| var Particle = require("particle-io"); | |
| var keydownup = require("keyupdown"); | |
| // Store process.stdin to a local variable | |
| var stdin = process.stdin; | |
| // Store keys currently pressed | |
| var pressed = {}; | |
| // Init the keydown and keyup events | |
| keydownup(stdin); | |
| var board = new five.Board({ | |
| io: new Particle({ | |
| token: '7ae40abd0f7c272888ca95b6740667febd1a29c8', | |
| deviceName: 'cat_mountain' | |
| }) | |
| }); | |
| board.on("ready", function() { | |
| console.log('ready'); | |
| var rightWheel = new five.Motor({ | |
| pins: { pwm: "D0", dir: "D4" }, | |
| invertPWM: true | |
| }); | |
| var leftWheel = new five.Motor({ | |
| pins: { pwm: "D1", dir: "D5" }, | |
| invertPWM: true | |
| }); | |
| var speed = 255; | |
| function reverse() { | |
| leftWheel.rev(speed); | |
| rightWheel.rev(speed); | |
| } | |
| function forward() { | |
| leftWheel.fwd(speed); | |
| rightWheel.fwd(speed); | |
| } | |
| function stop() { | |
| leftWheel.stop(); | |
| rightWheel.stop(); | |
| } | |
| function left() { | |
| if ( movingForward() ) { | |
| leftWheel.fwd(speed * 0.75); | |
| } | |
| leftWheel.rev(speed); | |
| rightWheel.fwd(speed); | |
| } | |
| function right() { | |
| leftWheel.fwd(speed); | |
| rightWheel.rev(speed); | |
| } | |
| function exit() { | |
| leftWheel.rev(0); | |
| rightWheel.rev(0); | |
| setTimeout(process.exit, 1000); | |
| } | |
| function movingForward() { | |
| // Check if we're already moving | |
| if (leftWheel.isOn && leftWheel.direction.value && rightWheel.isOn && rightWheel.direction.value) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| function debug() { | |
| console.log(movingForward() ? true : false); | |
| } | |
| var keyMap = { | |
| 'up': forward, | |
| 'down': reverse, | |
| 'left': left, | |
| 'right': right, | |
| 'space': stop, | |
| 'q': exit, | |
| 'd': debug, | |
| 'c': console.clear | |
| }; | |
| stdin.setRawMode(true); | |
| stdin.resume(); | |
| stdin.on("keydown", function(chunk, key) { | |
| if (!key || !keyMap[key.name]) return; | |
| // Add pressed key to list | |
| pressed[key.name] = true; | |
| console.log(pressed); | |
| keyMap[key.name](); | |
| }); | |
| // Stop when no keys are being pressed | |
| stdin.on("keyup", function(chunk, key) { | |
| // Remove key from list | |
| delete(pressed[key.name]); | |
| console.log(pressed); | |
| // Check if there are any keys currently pressed | |
| if ( ! pressed.length ) { | |
| stop(); | |
| } | |
| }); | |
| // process.on('SIGINT', function() { | |
| // console.log('STOPPING'); | |
| // stop(); | |
| // }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment