Skip to content

Commit

Permalink
Rewritten some functions and added some callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
sbstjn committed Feb 27, 2012
1 parent ec1d83d commit cf5d2e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/board.js
Expand Up @@ -8,25 +8,34 @@ var events = require('events'),
/*
* The main Arduino constructor
* Connect to the serial port and bind
* @param object options object of option key -> value relations
* @param function callback function to be called after connection
*/
var Board = function (options) {
var Board = function (options, callback) {
this.log('info', 'initializing');
this.debug = options && options.debug || false;
this.writeBuffer = [];
this.connected = false;

var self = this;
this.detect(function (err, serial) {
if (err) throw err;
if (err) { return callback(new Error('Adruino not found')); }
self.serial = serial;
self.emit('connected');
callback(null, self);

self.log('info', 'binding serial events');
self.serial.on('data', function(data){
if (self.connected == false) {
self.connected = true;
self.emit('ready');
}

self.log('receive', data.toString().red);
self.emit('data', data);
});

setTimeout(function(){
setTimeout(function() {
self.log('info', 'board ready');
self.sendClearingBytes();

Expand All @@ -46,8 +55,10 @@ var Board = function (options) {
if (self.writeBuffer.length > 0) {
self.processWriteBuffer();
}

self.emit('ready');

self.IAmDave();
setTimeout(function() {self.IAmDave();}, 500);
// self.emit('ready');
}, 500);
});
}
Expand Down Expand Up @@ -97,6 +108,12 @@ Board.prototype.sendClearingBytes = function () {
this.serial.write('00000000');
}

Board.prototype.IAmDave = function() {
this.log('info', 'I\'m Dave!');
this.serial.write('90000000');
this.write('90000000');
}

/*
* Process the writeBuffer (messages attempted before serial was ready)
*/
Expand Down Expand Up @@ -150,7 +167,7 @@ Board.prototype.LOW = '000';
* val == out = 001
* val == in = 000
*/
Board.prototype.pinMode = function (pin, val) {
Board.prototype.pinMode = function (pin, val, callback) {
pin = this.normalizePin(pin);
this.log('info', 'set pin ' + pin + ' mode to ' + val);
val = (
Expand Down Expand Up @@ -186,9 +203,10 @@ Board.prototype.analogWrite = function (pin, val) {
this.log('info', 'analogWrite to pin ' + pin + ': ' + val.green);
this.write('03' + pin + val);
}

Board.prototype.analogRead = function (pin) {
pin = this.normalizePin(pin);
this.log('info', 'analogRead to pin ' + pin + ': ' + val.green);
this.log('info', 'analogRead to pin ' + pin);
this.write('04' + pin + this.normalizeVal(0));
}

Expand Down
5 changes: 5 additions & 0 deletions src/du.ino
Expand Up @@ -46,6 +46,7 @@ void process() {
case 2: dr(pin); break;
case 3: aw(pin,val); break;
case 4: ar(pin); break;
case 90: autoReply(); break;
case 98: handleServo(pin,val,aux); break;
case 99: toggleDebug(val); break;
default: break;
Expand All @@ -66,6 +67,10 @@ void toggleDebug(char *val) {
}
}

void autoReply() {
Serial.println('Is Dave there?');
}

/**
* Set pin mode
* @param char pin identifier for pin
Expand Down

0 comments on commit cf5d2e9

Please sign in to comment.