Add example using Xbox 360 Controller

This commit is contained in:
Andrew Stewart 2014-01-21 11:11:01 -08:00
parent f174386672
commit 851d7f9032
3 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,29 @@
Cylon = require '../..'
Cylon.robot
connection: { name: 'joystick', adaptor: 'joystick', controller: 'xbox360' }
device: { name: 'controller', driver: 'xbox360' }
work: (my) ->
["a", "b", "x", "y"].forEach (button) ->
my.controller.on "#{button}:press", ->
console.log "Button #{button} pressed."
my.controller.on "#{button}:release", ->
console.log "Button #{button} released."
lastPosition =
left: { x: 0, y: 0 }
right: { x: 0, y: 0 }
my.controller.on "left:move", (pos) ->
last = lastPosition.left
unless pos.x is last.x and pos.y is last.y
console.log "Left Stick:", pos
my.controller.on "right:move", (pos) ->
last = lastPosition.right
unless pos.x is last.x and pos.y is last.y
console.log "Right Stick:", pos
Cylon.start()

View File

@ -0,0 +1,39 @@
var Cylon = require('../..');
Cylon.robot({
connection: { name: 'joystick', adaptor: 'joystick', controller: 'xbox360' },
device: { name: 'controller', driver: 'xbox360' },
work: function(my) {
["a", "b", "x", "y"].forEach(function(button) {
my.controller.on(button + ":press", function() {
console.log("Button " + button + " pressed.");
});
my.controller.on(button + ":release", function() {
console.log("Button " + button + " released.");
});
});
var lastPosition = {
left: { x: 0, y: 0 },
right: { x: 0, y: 0 }
};
my.controller.on("left:move", function(pos) {
var last = lastPosition.left;
if (!(pos.x === last.x && pos.y === last.y)) {
console.log("Left Stick:", pos);
}
});
my.controller.on("right:move", function(pos) {
var last = lastPosition.right;
if (!(pos.x === last.x && pos.y === last.y)) {
console.log("Right Stick:", pos);
}
});
}
});
Cylon.start();

View File

@ -0,0 +1,66 @@
# Joystick - Xbox 360 Controller
For this Cylon example, we'll demonstrate how to get input from a wired Xbox 360
controller.
Before we get started, make sure you've got `cylon-joystick` installed via NPM
so we can connect to the controller.
First, let's load up Cylon:
Cylon = require '../..'
With that done, we can start defining our new robot.
Cylon.robot
We'll be setting up a connection to our 360 controller, and also setting the
controller up as a device the robot can talk to:
connection:
name: 'joystick'
adaptor: 'joystick'
controller: 'xbox360'
device:
name: 'controller'
driver: 'xbox360'
With our connection to the controller established, we'll tell it what to do:
work: (my) ->
Let's ask our robot to tell us when the face buttons on the controller (A, B, X,
Y) are pressed and released:
["a", "b", "x", "y"].forEach (button) ->
my.controller.on "#{button}:press", ->
console.log "Button #{button} pressed."
my.controller.on "#{button}:release", ->
console.log "Button #{button} released."
Next up, when someone moves the left and right analog sticks, lets' print their
current positions.
Since the Xbox 360 controller's adaptor emits the `left:move`
and `right:move` events even when the sticks aren't moved, we'll prevent it from
emitting duplicate events, too:
lastPosition =
left: { x: 0, y: 0 }
right: { x: 0, y: 0 }
my.controller.on "left:move", (pos) ->
last = lastPosition.left
unless pos.x is last.x and pos.y is last.y
console.log "Left Stick:", pos
my.controller.on "right:move", (pos) ->
last = lastPosition.right
unless pos.x is last.x and pos.y is last.y
console.log "Right Stick:", pos
And with our work defined, we can tell Cylon to start up our Robot:
Cylon.start()