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:

var Cylon = require('cylon');

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:

  connections: {
    joystick: { adaptor: 'joystick' }
  },

  devices: {
    controller: { driver: 'xbox-360' }
  },

With our connection to the controller established, we'll tell it what to do:

  work: function(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(function(button) {
      my.controller.on(button + ":press", function() {
        console.log("Button " + button + " pressed.");
      });

      my.controller.on(button + ":release", function() {
        console.log("Button " + button + " released.");
      });
    });

Next up, when someone moves the left and right analog sticks, or presses the triggers, let's print their current positions.

    my.controller.on("left_x:move", function(pos) {
      console.log("Left Stick - X:", pos);
    });

    my.controller.on("left_y:move", function(pos) {
      console.log("Left Stick - Y:", pos);
    });

    my.controller.on("right_x:move", function(pos) {
      console.log("Right Stick - X:", pos);
    });

    my.controller.on("right_y:move", function(pos) {
      console.log("Right Stick - Y:", pos);
    });

    my.controller.on("lt:move", function(pos) {
      console.log("Left Trigger: ", pos);
    });

    my.controller.on("rt:move", function(pos) {
      console.log("Right Trigger: ", pos);
    });
  }
});

And with our work defined, we can tell Cylon to start up our Robot:

Cylon.start();