Weekend Build: Sphero DualShock

My friend Steve was gracious enough to lend me his Sphero, after spending a few months serving as a pretty paper weight, I finally started reading the API docs and looking for existing implementations.

Currently the best Node.js API is spheron, it's simple to use and very intuitive, setting the Sphero color for example:

var spheron = require('spheron');
var spheroPort = '/dev/cu.Sphero-RRY-RN-SPP';

sphero.setRGB(spheron.toolbelt.COLORS.RED, false);
sphero.open(spheroPort);

Getting it to move around is equally as simple:

sphero.heading = heading;
sphero.roll(128, heading, 1);

Putting it all Together:

function roll(heading) {
    return function() {
        sphero.heading = heading;
        sphero.roll(128, heading, 1);
    };
}
function stop() {
    sphero.roll(0, sphero.heading || 0, 0);
}
function setColor(color) {
    return function() {
        sphero.setRGB(color, false);
    };
}
var controllerEvents = [{
      controllerEvent: 'dpadUp:press',
      response: roll(0)
  }, {
      controllerEvent: 'dpadUp:release',
      response: stop
  },
  {
      controllerEvent: 'square:press',
      response: setColor(COLORS.PINK)
  }];

The code is in Github

See it working