Joystick - DualShock 3 Controller

For this Cylon example, we'll demonstrate how to get input from a DualShock 3 controller.

You can connect to the controller over Bluetooth, or plug it in via USB and press the 'PS' button to connect it to your system.

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 DualShock controller, and also setting the controller up as a device the robot can talk to:

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

  devices: {
    controller: { driver: 'dualshock-3' }
  },

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 (Square, Circle, X, Triangle) are pressed and released:

    ["square", "circle", "x", "triangle"].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, lets' print their current positions.

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

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

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

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

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

Cylon.start();