Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions lib/merge.js

This file was deleted.

124 changes: 22 additions & 102 deletions lib/wii.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,35 @@
'use strict';

var merge = require('./merge'),
util = require('util'),
var util = require('util'),
EventEmitter = require('events').EventEmitter,
serialport = require('serialport'),
SerialPort = serialport.SerialPort,
Q = require('q');


var list = function (callback) {
var deferred = Q.defer();
serialport.list(function (err, ports) {
if(err) {
deferred.reject(err);
} else {
deferred.resolve(ports);
}
});
if('function' === typeof callback) {
deferred.promise.nodeify(callback);
} else {
return deferred.promise;
}
};
when = require('when');

var defaults = {
baudrate: 115200,
databits: 8,
stopbits: 1,
parity: 'none',
parser: serialport.parsers.raw
};

var sendHeader = [0x24, 0x4D, 0x3C];


function Wii(options) {
if(!options) {
options = {};
} else if('string' === typeof options) {
options = { port: options };
function Wii(connection, options) {
if (!connection) {
throw new Error("Connection must be provided that implements a stream interface");
}
this.options = merge(options, defaults);

this.onData = this.onData.bind(this);
this.onError = this.onError.bind(this);
this.options = options || {};
this.socket = connection;

this.messages = {};
this.readMessages = {};
require('./messages').register(this);
}

util.inherits(Wii, EventEmitter);

Wii.prototype.connect = function(port, options) {
if('string' === typeof port) {
port = { comName: port };
} else {
port = port || this.options.port;
}

if(!port) {
throw new Error('no port specified');
}

options = options || {};
options.port = undefined;

var deferred = Q.defer();

this.port = new SerialPort(port.comName, merge(options, this.options));
require('./messages').register(this);

this._onConnect = function () {
this.port.removeListener('open', this._onConnect);
delete this._onConnect;
this.onOpen();
deferred.resolve(this);
}.bind(this);
this.connected = true;

this._onError = function (err) {
this.port.removeListener('error', this._onError);
delete this._onError;
deferred.reject(err);
}.bind(this);
this.onData = this.onData.bind(this);
this.onError = this.onError.bind(this);

this.port.on('error', this._onError);
this.port.on('open', this._onConnect);
this.socket.on('data', this.onData);
this.socket.on('error', this.onError);
}

return deferred.promise;
};
util.inherits(Wii, EventEmitter);

Wii.prototype.onOpen = function () {
this.connected = true;
this.port.on('error', this.onError);
this.port.on('data', this.onData);
};

Wii.prototype.onError = function (err) {
if(err.errno === -1 && err.code === 'UNKNOWN') {
Expand All @@ -106,7 +41,6 @@ Wii.prototype.onError = function (err) {

Wii.prototype.onData = function (buffer) {
this.emit('data', buffer);

var len = buffer[3];
var type = buffer[4];
var data = buffer.slice(5, 5+len);
Expand All @@ -127,14 +61,13 @@ Wii.prototype.addInMessage = function(name, parseFunction) {
}.bind(this);
};

Wii.prototype.read = function(messageName) {
return this.send(this.readMessages[messageName]);
Wii.prototype.read = function(messageName, callback) {
this.send(this.readMessages[messageName], callback);
};

Wii.prototype.send = function(message) {
Wii.prototype.send = function(message, callback) {
var size, len, msgId, data;

var deferred = Q.defer();

if('number' === typeof message) {
msgId = message;
Expand All @@ -155,24 +88,15 @@ Wii.prototype.send = function(message) {
}
msg.writeUInt8(this.calculateChecksum(msg), 5 + len); // checksum
msg.writeUInt8(0, 6 + len); // derp data to force wii to comply
this.port.write(msg, function (err, result) {
if(err) {
deferred.reject(err);
} else {
deferred.resolve();
}
});

return deferred.promise;
this.socket.write(msg, callback);
};

Wii.prototype.disconnect = function () {
if(this.connected) {
this.connected = false;
this.emit('disconnect');
this.port.removeListener('error', this.onError);
this.port.removeListener('data', this.onData);
this.port.close();
this.socket.removeListener('error', this.onError);
this.socket.removeListener('data', this.onData);
}
};

Expand All @@ -194,8 +118,4 @@ Wii.prototype.calculateChecksum = function(buffer) {
}
};

module.exports = {
Wii: Wii,
list: list,
defaults: defaults
};
module.exports = Wii;
19 changes: 14 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "multiwii",
"version": "0.1.0",
"description": "A library for sending and parsing messages to/from a MultiWii device using Node.js",
"version": "1.0.0",
"description": "A library for sending and parsing messages to/from a MultiWii device using Node.js. It is now implemented such that it will use whatever stream you provide it instead of specifically requiring a serialport.",
"main": "index.js",
"scripts": {
"test": "grunt test"
Expand All @@ -14,15 +14,24 @@
"multiwii",
"copter"
],
"author": "Johan Öbrink <[email protected]>",
"author": [
{
"name": "Johan Öbrink",
"email": "[email protected]",
"url": "http://terminaln00b.tumblr.com/"
},
{
"name": "Chris Williams",
"email": "[email protected]",
"url": "http://www.voodootikigod.com"
}
],
"license": "MIT",
"bugs": {
"url": "https://github.com/JohanObrink/node-multiwii/issues"
},
"homepage": "https://github.com/JohanObrink/node-multiwii",
"dependencies": {
"serialport": "~1.3.1",
"q": "~1.0.1"
},
"devDependencies": {
"sinon-chai": "~2.5.0",
Expand Down
33 changes: 0 additions & 33 deletions test/unit/merge.js

This file was deleted.

43 changes: 18 additions & 25 deletions test/unit/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,27 @@
var chai = require('chai'),
expect = chai.expect,
sinon = require('sinon'),
proxyquire = require('proxyquire');
Wii = require('../../');

chai.use(require('sinon-chai'));

describe('\u2b50 messages', function () {
var Wii, multiwii, serialport, sandbox, port, wii;


var proxyConnection, sandbox, wii;

beforeEach(function () {
port = {
proxyConnection = {
on: sinon.stub(),
removeListener: sinon.stub()
removeListener: sinon.stub(),
write: sinon.stub(),
close: sinon.spy()
};
serialport = {
list: sinon.stub(),
parsers: {
raw: {}
},
SerialPort: sinon.stub().returns(port)
};
multiwii = proxyquire('../../lib/wii', {
'serialport': serialport
});
Wii = multiwii.Wii;
sandbox = sinon.sandbox.create();

wii = new Wii();
wii.connect('/dev/mupp');
port.on.withArgs('open').yield();
wii = new Wii(proxyConnection);
});


afterEach(function () {
sandbox.restore();
});
Expand All @@ -39,7 +32,7 @@ describe('\u2b50 messages', function () {
it('parses the message correctly', function () {
var listener = sinon.spy();
wii.on('ident', listener);
port.on.withArgs('data').yield(new Buffer([0, 0, 0, 7, 100, 2, 1, 1, 0, 0, 0, 0]));
proxyConnection.on.withArgs('data').yield(new Buffer([0, 0, 0, 7, 100, 2, 1, 1, 0, 0, 0, 0]));
expect(listener).calledWith({
version: 2,
multitype: 'TRI',
Expand All @@ -59,7 +52,7 @@ describe('\u2b50 messages', function () {
it('parses the message correctly', function () {
var listener = sinon.spy();
wii.on('rc', listener);
port.on.withArgs('data').yield(new Buffer([0, 0, 0, 16, 105,
proxyConnection.on.withArgs('data').yield(new Buffer([0, 0, 0, 16, 105,
0, 1, // roll
1, 1, // pitch
2, 1, // yaw
Expand All @@ -86,7 +79,7 @@ describe('\u2b50 messages', function () {
it('parses the message correctly', function () {
var listener = sinon.spy();
wii.on('status', listener);
port.on.withArgs('data').yield(new Buffer([0, 0, 0, 11, 101,
proxyConnection.on.withArgs('data').yield(new Buffer([0, 0, 0, 11, 101,
0, 1, // cycleTime
1, 1, // i2cErrorCount
2, 1, // sensor
Expand All @@ -105,7 +98,7 @@ describe('\u2b50 messages', function () {
it('parses the message correctly', function () {
var listener = sinon.spy();
wii.on('servo', listener);
port.on.withArgs('data').yield(new Buffer([0, 0, 0, 16, 103,
proxyConnection.on.withArgs('data').yield(new Buffer([0, 0, 0, 16, 103,
0, 1, // servo[0]
1, 1, // servo[1]
2, 1, // servo[2]
Expand All @@ -123,7 +116,7 @@ describe('\u2b50 messages', function () {
it('parses the message correctly', function () {
var listener = sinon.spy();
wii.on('motor', listener);
port.on.withArgs('data').yield(new Buffer([0, 0, 0, 16, 104,
proxyConnection.on.withArgs('data').yield(new Buffer([0, 0, 0, 16, 104,
0, 1, // motor[0]
1, 1, // motor[1]
2, 1, // motor[2]
Expand All @@ -136,5 +129,5 @@ describe('\u2b50 messages', function () {
expect(listener).calledWith([256, 257, 258, 259, 260, 261, 262, 263]);
});
});

});
Loading