diff --git a/README.md b/README.md index b6233ff..a87538c 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,8 @@ Node Arduino ============ -Version: 0.2.3 - Released March 20, 2011 - -***** - This package is designed to allow you to control and program an arduino chipset with JavaScript. It leverages the node-serialport code base, so ensure that you have that installed. In order to see what this package can do, please watch [this presentation from JSConf EU 2010](http://jsconf.eu/2010/speaker/livingroombindmotion_function.html) by [Nikolai Onken](http://twitter.com/nonken) and [Jörn Zaefferer](http://bassistance.de/). -***** - How To Use ========== @@ -18,24 +12,21 @@ To Install ----------
-  npm install serialport arduino
+npm install git://github.com/kyungw00k/node-arduino.git
 
To Use ------ - 1. Opening an arduino board: -
-  var arduino = require("arduino");
-  var myBoard = arduino.connect("/dev/tty-usbserial1");
+var arduino = require("arduino") 
+  , myBoard = arduino.connect("/dev/tty-usbserial1");
 
- -2. Getting pin 7 digital value +2. Getting pin 7 digital value
-  var arduino = require("arduino");
-  var myBoard = arduino.connect("/dev/tty-usbserial1");
+var arduino = require("arduino")
+  , myBoard = arduino.connect("/dev/tty-usbserial1");
 
 myBoard.pinMode(7, arduino.INPUT);
 
@@ -45,8 +36,3 @@ setInterval(function () {
   });
 }, 100);
 
- -Alpha ------ - -This code is still very much alpha and early stage, but that just makes it more fun to work with. If you find an issue/problem, fork it and issue a pull request. No matter what, enjoy and do cool things with this code. \ No newline at end of file diff --git a/lib/arduino.js b/lib/arduino.js index bd0caa4..d8a45a7 100644 --- a/lib/arduino.js +++ b/lib/arduino.js @@ -6,7 +6,8 @@ */ var sys = require('sys'), - SerialPort = require('serialport').SerialPort; + SerialPort = require('serialport').SerialPort, + parsers = require('serialport').parsers; const SERIAL_BAUDRATE = 9600; @@ -31,40 +32,30 @@ exports.DEFAULT = 0x01; exports.INTERNAL = 0x03; Board = function (path) { - 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); - } - } - }); + // Callback Function Map for read functions + var callback = {}; + + this.sp = new SerialPort(path, {baudrate: SERIAL_BAUDRATE, parser: parsers.readline('\n')}); + + var onData = (function(callback) { + return function( data ) { + var pin, value; + data = +data; + if ( data && data > 1 ) { + pin = data >> 16; + value = data & 0xFFFF; + + // There is no callback function. + if ( !callback['pin'+pin] ) { + sys.puts('no callback for pin '+pin); + } else { + callback['pin'+pin](value); + } + } + }; + })(callback); + + this.sp.on( "data", onData); }; Board.prototype = { diff --git a/src/node.pde b/src/node.pde deleted file mode 100644 index 004e37d..0000000 --- a/src/node.pde +++ /dev/null @@ -1,66 +0,0 @@ -/* - * node-arduino: Control your Arduino with Node - * - * Copyright (c) 2010 Tobias Schneider - * node-arduino is freely distributable under the terms of the MIT license. - */ - -#define SERIAL_BAUDRATE 9600 - -#define OPC_PIN_MODE 0x01 -#define OPC_DIGITAL_READ 0x02 -#define OPC_DIGITAL_WRITE 0x03 -#define OPC_ANALOG_REFERENCE 0x04 -#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: { - pinMode(Serial.read(), Serial.read()); - break; - } - case OPC_DIGITAL_READ: { - delay(50); - pinVal = Serial.read(); - inpVal = digitalRead(pinVal); - outVal = pinVal << 16 | inpVal; - Serial.print(outVal); - Serial.print("#"); - break; - } - case OPC_DIGITAL_WRITE: { - digitalWrite(Serial.read(), Serial.read()); - break; - } - case OPC_ANALOG_REFERENCE: { - analogReference(Serial.read()); - break; - } - case OPC_ANALOG_READ: { - delay(50); - pinVal = Serial.read(); - inpVal = analogRead(pinVal); - outVal = pinVal << 16 | inpVal; - Serial.print(outVal); - Serial.print("#"); - break; - } - case OPC_ANALOG_WRITE: { - analogWrite(Serial.read(), Serial.read()); - break; - } - } - } -}