Skip to content

Commit

Permalink
rudder and thrust work
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Oct 21, 2014
1 parent 2512e40 commit 9aeb3fa
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 50 deletions.
49 changes: 49 additions & 0 deletions examples/powerup_dualshock3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var Cylon = require('cylon');

Cylon.robot({
connections: [
{ name: 'bluetooth', adaptor: 'ble', uuid: '84dd20eb3d89' },
{ name: 'dualshock3', adaptor: 'joystick' }
],
devices: [
{ name: 'controller', driver: 'dualshock-3', connection: 'dualshock3' },
{ name: 'powerup', driver: 'powerup' }
],
work: function(my) {
var thrust = 0;
var rudder = 0;
var canRudder = false;

var cb = function(err) {
if (!!err) {
console.log(err);
} else {
if (canRudder) {
canRudder = false;
my.powerup.setRudder(rudder, cb);
}
else {
my.powerup.setThrust(thrust, cb);
}
}
}

my.powerup.setThrust(thrust, cb);

my.controller.on("left_y:move", function(data) {
if (data < 0) {
thrust = Math.abs(data/32768*254) | 0;
} else {
thrust = 0;
}
});

my.controller.on("right_x:move", function(data) {
var tmp = data/32768*127 | 0;
if (tmp !== rudder) {
rudder = tmp;
canRudder = true;
}
});
}
}).start();
29 changes: 0 additions & 29 deletions lib/adaptor.js

This file was deleted.

15 changes: 3 additions & 12 deletions lib/cylon-powerup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,22 @@
* cylon-powerup
* http://cylonjs.com
*
* Copyright (c) 2014 Your Name Here
* Copyright (c) 2014 The Hybrid Group
* Your License Here
*/

'use strict';

var Cylon = require('cylon');

var Adaptor = require('./adaptor'),
Driver = require('./driver');
var Driver = require('./driver');

module.exports = {
adaptor: function(opts) {
return new Adaptor(opts);
},

driver: function(opts) {
return new Driver(opts);
},

register: function(robot) {
// Bootstrap your adaptor here. For example, with a Sphero, you would call
// the registerAdaptor and registerDriver functions as follows:
//
// robot.registerAdaptor('cylon-sphero', 'sphero');
// robot.registerDriver('cylon-sphero', 'sphero');
robot.registerDriver('cylon-powerup', 'powerup');
}
};
35 changes: 26 additions & 9 deletions lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,50 @@
* cylon-powerup driver
* http://cylonjs.com
*
* Copyright (c) 2014 Your Name Here
* Copyright (c) 2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/

'use strict';

var Cylon = require('cylon');

var MOTOR = '75b64e51f1844ed1921a476090d80ba7',
RUDDER = '75b64e51f1854ed1921a476090d80ba7';

var Driver = module.exports = function Driver() {
Driver.__super__.constructor.apply(this, arguments);

this.serviceId = '75b64e51f1814ed1921a476090d80ba7';

// Include a list of commands that will be made available to the device instance.
// and used in the work block of the robot.
this.commands = {
// This is how you register a command function for the device;
// the command should be added to the prototype, see below.
hello: this.hello
setThrust: this.setThrust,
setRudder: this.setRudder
};
};

Cylon.Utils.subclass(Driver, Cylon.Driver);

Driver.prototype.start = function(callback) {
Driver.__super__.start.apply(this, arguments);
callback();
};

Driver.prototype.halt = function(callback) {
callback();
};

Driver.prototype.setThrust = function(value, callback) {
this._writeServiceCharacteristic(value, MOTOR, callback);
};

Driver.prototype.setRudder = function(value, callback) {
this._writeServiceCharacteristic(value, RUDDER, callback);
};

Driver.prototype.hello = function() {
Cylon.Logger.info('Hello World!');
Driver.prototype._writeServiceCharacteristic = function(value, characteristic, callback) {
this.connection.writeServiceCharacteristic(this.serviceId, characteristic, new Buffer([value]),
function(err) {
if ('function' === typeof(callback)) { callback(err); }
}
);
}

0 comments on commit 9aeb3fa

Please sign in to comment.