Comparing version 0.0.6 to 0.0.7
117
lib/board.js
@@ -25,55 +25,44 @@ var Board, Promise, SerialPort, assert, events, rfcontrol, serialport, | ||
Board.prototype.ready = false; | ||
function Board(port, baudrate) { | ||
var openImmediately; | ||
if (baudrate == null) { | ||
baudrate = 9600; | ||
} | ||
this.port = port; | ||
this.baudrate = baudrate != null ? baudrate : 9600; | ||
this._waitForAcknowledge = __bind(this._waitForAcknowledge, this); | ||
this._onAcknowledge = __bind(this._onAcknowledge, this); | ||
this.serialPort = new SerialPort(port, { | ||
baudrate: baudrate, | ||
parser: serialport.parsers.readline("\r\n") | ||
}, openImmediately = false); | ||
this.serialPort.on("data", (function(_this) { | ||
return function(_line) { | ||
var args, cmd, line; | ||
line = _line.replace(/\0/g, '').trim(); | ||
_this.emit("data", line); | ||
if (line === "ready") { | ||
_this.emit('ready'); | ||
return; | ||
} | ||
args = line.split(" "); | ||
assert(args.length >= 1); | ||
cmd = args[0]; | ||
args.splice(0, 1); | ||
switch (cmd) { | ||
case 'ACK': | ||
case 'ERR': | ||
return _this._handleAcknowledge(cmd, args); | ||
case 'RF': | ||
return _this._handleRFControl(cmd, args); | ||
case 'KP': | ||
return _this._handleKeypad(cmd, args); | ||
default: | ||
return console.log("unknown message received: " + line); | ||
} | ||
}; | ||
})(this)); | ||
this._onData = __bind(this._onData, this); | ||
} | ||
Board.prototype.connect = function(timeout) { | ||
Board.prototype.connect = function(timeout, retries) { | ||
if (timeout == null) { | ||
timeout = 20000; | ||
} | ||
return this.pendingConnect = this.serialPort.openAsync().then((function(_this) { | ||
if (retries == null) { | ||
retries = 3; | ||
} | ||
return this.pendingConnect = (this.serialPort != null ? this.serialPort.closeAsync() : Promise.resolve())["finally"]((function(_this) { | ||
return function() { | ||
var resolver; | ||
resolver = null; | ||
return new Promise(function(resolve, reject) { | ||
resolver = resolve; | ||
return _this.once("ready", resolve); | ||
}).timeout(timeout)["catch"](function(err) { | ||
_this.removeListener("ready", resolver); | ||
throw err; | ||
var openImmediately; | ||
_this.ready = false; | ||
_this.serialPort = new SerialPort(_this.port, { | ||
baudrate: _this.baudrate, | ||
parser: serialport.parsers.readline("\r\n") | ||
}, openImmediately = false); | ||
return _this.serialPort.openAsync().then(function() { | ||
var resolver; | ||
_this.serialPort.on("data", _this._onData); | ||
resolver = null; | ||
return new Promise(function(resolve, reject) { | ||
resolver = resolve; | ||
return _this.once("ready", resolver); | ||
}).timeout(timeout)["catch"](function(err) { | ||
_this.removeListener("ready", resolver); | ||
_this.removeListener("data", _this._onData); | ||
if (err.name === "TimeoutError" && retries > 0) { | ||
_this.emit('reconnect', err); | ||
return _this.connect(timeout, retries - 1); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
}); | ||
@@ -84,2 +73,42 @@ }; | ||
Board.prototype.disconnect = function() { | ||
var close; | ||
if (this.serialPort != null) { | ||
close = this.serialPort.closeAsync(); | ||
this.serialPort = null; | ||
return close; | ||
} else { | ||
return Promise.resolve(); | ||
} | ||
}; | ||
Board.prototype._onData = function(_line) { | ||
var args, cmd, line; | ||
line = _line.replace(/\0/g, '').trim(); | ||
this.emit("data", line); | ||
if (/ready$/.test(line)) { | ||
this.ready = true; | ||
this.emit('ready'); | ||
return; | ||
} | ||
if (!this.ready) { | ||
return; | ||
} | ||
args = line.split(" "); | ||
assert(args.length >= 1); | ||
cmd = args[0]; | ||
args.splice(0, 1); | ||
switch (cmd) { | ||
case 'ACK': | ||
case 'ERR': | ||
return this._handleAcknowledge(cmd, args); | ||
case 'RF': | ||
return this._handleRFControl(cmd, args); | ||
case 'KP': | ||
return this._handleKeypad(cmd, args); | ||
default: | ||
return console.log("unknown message received: " + line); | ||
} | ||
}; | ||
Board.prototype.whenReady = function() { | ||
@@ -86,0 +115,0 @@ if (this.pendingConnect == null) { |
{ | ||
"name": "homeduino", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Node.js library for interfacing with the homeduino ardunio library", | ||
@@ -29,10 +29,12 @@ "main": "index.js", | ||
"dependencies": { | ||
"serialport": "~1.4.5", | ||
"bluebird": "~2.2.2", | ||
"rfcontroljs": "0.0.9" | ||
"otaat-repl": "^1.0.1", | ||
"rfcontroljs": "0.0.9", | ||
"serialport": "~1.4.5" | ||
}, | ||
"devDependencies": { | ||
"coffee-script-redux": "~2.0.0-beta8", | ||
"colors": "^0.6.2", | ||
"gulp": "~3.8.6", | ||
"gulp-coffee": "~2.1.1", | ||
"coffee-script-redux": "~2.0.0-beta8", | ||
"gulp-plumber": "~0.6.4", | ||
@@ -39,0 +41,0 @@ "gulp-watch": "~0.6.9" |
@@ -62,2 +62,26 @@ homeduinojs | ||
board.pinMode(1, 0).done() | ||
``` | ||
``` | ||
REPL-Client | ||
----------- | ||
``` | ||
git clone https://github.com/pimatic/homeduinojs && cd homeduinojs && npm install | ||
``` | ||
Start the repl client: | ||
``` | ||
sudo ./client.js /dev/ttyUSB0 115200 | ||
``` | ||
It will connect to the arduino and give you a prompt, where you can enter a javascript command: | ||
``` | ||
connecting to /dev/ttyUSB0 with 115200 | ||
data: "ready" | ||
connected | ||
homeduino> board.rfControlStartReceiving(0) | ||
data: "ACK" | ||
undefined | ||
``` |
Sorry, the diff of this file is not supported yet
54694
12
265
87
4
6
+ Addedotaat-repl@^1.0.1
+ Addedotaat-repl@1.0.2(transitive)