Permalink
Browse files

refactor keyboardInvoker: use invoker

  • Loading branch information...
ferclaverino committed Dec 9, 2016
1 parent d2a8484 commit 4b654847bb2838d0c3de4a849d23addbed98759f
Showing with 52 additions and 40 deletions.
  1. +3 −9 car/keyboardInvoker.js
  2. +33 −19 car/main.js
  3. +16 −12 test/manual/keyboardInvoker.unit.js
@@ -1,8 +1,8 @@
const keypress = require('keypress');
class KeyboardInvoker {
constructor(commands) {
this.commands = commands;
constructor(invoker) {
this.invoker = invoker;
keypress(process.stdin);
process.stdin.resume();
process.stdin.setEncoding('utf8');
@@ -11,16 +11,10 @@ class KeyboardInvoker {
listen() {
process.stdin.on('keypress', (ch, key) => {
this.execute(key.name);
this.invoker.start(key.name);
});
}
execute(commandName) {
if (this.commands[commandName]) {
this.commands[commandName]();
}
}
}
module.exports = KeyboardInvoker;
@@ -1,4 +1,5 @@
const { Board, Motor } = require('johnny-five');
const Invoker = require('./invoker.js');
const KeyboardInvoker = require('./keyboardInvoker.js');
var board = new Board({
@@ -15,35 +16,48 @@ board.on('ready', () => {
const speed = 255 * 0.8;
const commands = {
'q': () => process.exit(),
'up': () => {
motorL.forward(speed);
motorR.forward(speed);
'q': {
start: () => process.exit()
},
'down': () => {
motorL.reverse(speed);
motorR.reverse(speed);
'up': {
start: () => {
motorL.forward(speed);
motorR.forward(speed);
}
},
'left': () => {
motorL.reverse(speed);
motorR.forward(speed);
'down': {
start: () => {
motorL.reverse(speed);
motorR.reverse(speed);
}
},
'right': () => {
motorL.forward(speed);
motorR.reverse(speed);
'left': {
start: () => {
motorL.reverse(speed);
motorR.forward(speed);
}
},
'space': () => {
// stop
motorL.stop();
motorR.stop();
'right': {
start: () => {
motorL.forward(speed);
motorR.reverse(speed);
}
},
'space': {
start: () => {
// stop
motorL.stop();
motorR.stop();
}
}
};
var keyboardInvoker = new KeyboardInvoker(commands);
var invoker = new Invoker(commands);
var keyboardInvoker = new KeyboardInvoker(invoker);
keyboardInvoker.listen();
board.on('exit', () => {
keyboardInvoker.execute('space');
invoker.execute('space');
});
});
@@ -1,23 +1,27 @@
const Invoker = require('../../car/invoker');
const KeyboardInvoker = require('../../car/keyboardInvoker');
const commands = {
'q': () => process.exit(),
'up': () => {
console.log('up');
'q': {
start: () => process.exit()
},
'down': () => {
console.log('down');
'up': {
start: () => console.log('start up')
},
'left': () => {
console.log('left');
'down': {
start: () => console.log('start down')
},
'right': () => {
console.log('right');
'left': {
start: () => console.log('start left')
},
'space': () => {
console.log('space');
'right': {
start: () => console.log('start right')
},
'space': {
start: () => console.log('start space')
}
};
var keyboardInvoker = new KeyboardInvoker(commands);
var invoker = new Invoker(commands);
var keyboardInvoker = new KeyboardInvoker(invoker);
keyboardInvoker.listen();

0 comments on commit 4b65484

Please sign in to comment.