Skip to content
This repository has been archived by the owner on May 30, 2018. It is now read-only.

Commit

Permalink
implement read functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kyungw00k committed Jul 31, 2011
1 parent 12db338 commit 4bfca42
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 38 deletions.
107 changes: 73 additions & 34 deletions lib/arduino.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* node-arduino is freely distributable under the terms of the MIT license.
*/

var sys = require('sys')
, SerialPort = require('serialport').SerialPort
;
var sys = require('sys'),
SerialPort = require('serialport').SerialPort;

const SERIAL_BAUDRATE = 9600;

Expand All @@ -32,40 +31,80 @@ exports.DEFAULT = 0x01;
exports.INTERNAL = 0x03;

Board = function (path) {
this.sp = new SerialPort(path, {baudrate: SERIAL_BAUDRATE});
}
var receivedDataQueue = [], // Data Temporary Stored
callback = {}; // Callback Function Map for read functions

this.callback = callback;

this.sp = new SerialPort(path, {baudrate: SERIAL_BAUDRATE});

this.sp.on( "data", function( data ) {
var pin,
value;

receivedDataQueue.push(data);

if ( (data+'').indexOf('#') == -1 ) {
return ;
}

data = receivedDataQueue.join('').split('#')[0];
receivedDataQueue.length = 0;

data = +data;

if ( data && data > 1 ) {
pin = data >> 16;
value = data & 0xFFFF;

if ( !callback['pin'+pin] ) {
sys.puts('no callback');
sys.puts('pin : '+pin+', value : '+value);
} else {
callback['pin'+pin](value);
}
}
});
};

Board.prototype = {
pinMode : function (pin, mode) {
this.sp.write(new Buffer([OPC_PIN_MODE, pin, mode]), 3);
}

, digitalRead : function (pin) {
// TODO
}

, digitalWrite : function (pin, val) {
this.sp.write(new Buffer([OPC_DIGITAL_WRITE, pin, val]), 3);
}

, analogReference : function (type) {
this.sp.write(new Buffer([OPC_ANALOG_REFERENCE, type]), 2);
}

, analogRead : function (pin) {
// TODO
}

, analogWrite : function (pin, val) {
this.sp.write(new Buffer([OPC_ANALOG_WRITE, pin, val]), 3);
},

close: function () {
this.sp.close()
}

pinMode : function (pin, mode) {
this.sp.write(new Buffer([OPC_PIN_MODE, pin, mode]), 3);
},

digitalRead : function (pin, callback) {
this.sp.write(new Buffer([OPC_DIGITAL_READ, pin]), 2);

if ( typeof callback == 'function' ) {
this.callback['pin'+pin] = callback;
}
},

digitalWrite : function (pin, val) {
this.sp.write(new Buffer([OPC_DIGITAL_WRITE, pin, val]), 3);
},

analogReference : function (type) {
this.sp.write(new Buffer([OPC_ANALOG_REFERENCE, type]), 2);
},

analogRead : function (pin, callback) {
this.sp.write(new Buffer([OPC_ANALOG_READ, pin]), 2);

if ( typeof callback == 'function' ) {
this.callback['pin'+pin] = callback;
}
},

analogWrite : function (pin, val) {
this.sp.write(new Buffer([OPC_ANALOG_WRITE, pin, val]), 3);
},

close: function () {
this.sp.close();
}
};

exports.connect = function (path) {
return new Board(path);
return new Board(path);
};
22 changes: 18 additions & 4 deletions src/node.pde
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,33 @@
#define OPC_ANALOG_READ 0x05
#define OPC_ANALOG_WRITE 0x06

long pinVal = 0;
long inpVal = 0;
long outVal = 0;


void setup() {
Serial.begin(SERIAL_BAUDRATE);
}

void loop() {
pinVal = 0, inpVal = 0, outVal = 0;
while (Serial.available() > 0) {
switch (Serial.read()) {
case OPC_PIN_MODE: {
Serial.println("pinMode");
pinMode(Serial.read(), Serial.read());
break;
}
case OPC_DIGITAL_READ: {
digitalRead(Serial.read());
delay(50);
pinVal = Serial.read();
inpVal = digitalRead(pinVal);
outVal = pinVal << 16 | inpVal;
Serial.print(outVal);
Serial.print("#");
break;
}
case OPC_DIGITAL_WRITE: {
Serial.println("digitalWrite");
digitalWrite(Serial.read(), Serial.read());
break;
}
Expand All @@ -40,7 +49,12 @@ void loop() {
break;
}
case OPC_ANALOG_READ: {
analogRead(Serial.read());
delay(50);
pinVal = Serial.read();
inpVal = analogRead(pinVal);
outVal = pinVal << 16 | inpVal;
Serial.print(outVal);
Serial.print("#");
break;
}
case OPC_ANALOG_WRITE: {
Expand Down

0 comments on commit 4bfca42

Please sign in to comment.