Permalink
Browse files

committing i2c updates, examples and read me change

  • Loading branch information...
1 parent 8b7b1e1 commit 9251ce3785d60b20251e0080b6cfa21a7eda495a @jgautier jgautier committed Sep 6, 2011
Showing with 59 additions and 1 deletion.
  1. +26 −0 examples/k22.js
  2. +17 −0 examples/servosweep.js
  3. +12 −0 lib/firmata.js
  4. +4 −1 readme.md
View
@@ -0,0 +1,26 @@
+/**
+ * Sample script to take readings from a k22 co2 sensor.
+ * http://www.co2meter.com/collections/co2-sensors/products/k-22-oc-co2-sensor-module
+ */
+var Board = require('../lib/firmata').Board;
+var board = new Board('/dev/tty.usbmodemfa131',function(){
+ board.sendI2CConfig();
+ board.on('string',function(string){
+ console.log(string);
+ });
+ setInterval(function(){
+ board.sendI2CWriteRequest(0x68,[0x22,0x00,0x08,0x2A]);
+ board.sendI2CReadRequest(0x68,4,function(data){
+ var ppms = 0;
+ ppms |= data[1] & 0xFF;
+ ppms = ppms << 8;
+ ppms |= data[2] & 0xFF;
+ var checksum = data[0] + data[1] + data[2];
+ if(checksum == data[3]){
+ console.log('Current PPMs: '+ppms);
+ } else {
+ console.log('Checksum failure');
+ }
+ });
+ },2000);
+});
@@ -0,0 +1,17 @@
+/**
+ * Sample script to move a servo back and forth.
+ */
+var Board = require('../lib/firmata').Board;
+var board = new Board('/dev/tty.usbmodemfa131', function() {
+ var degrees = 10;
+ var incrementer = 10;
+ board.pinMode(9,board.MODES.SERVO);
+ board.servoWrite(9,0);
+ setInterval(function(){
+ if(degrees >= 180 || degrees === 0){
+ incrementer *= -1;
+ }
+ degrees += incrementer;
+ board.servoWrite(9,degrees);
+ },500)
+});
View
@@ -37,6 +37,7 @@ var PIN_MODE = 0xF4,
/**
* MIDI_RESPONSE contains functions to be called when we receive a MIDI message from the arduino.
* used as a switch object as seen here http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/
+ * @private
*/
var MIDI_RESPONSE = {};
@@ -102,6 +103,7 @@ MIDI_RESPONSE[DIGITAL_MESSAGE] = function(board) {
/**
* SYSEX_RESPONSE contains functions to be called when we receive a SYSEX message from the arduino.
* used as a switch object as seen here http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/
+ * @private
*/
var SYSEX_RESPONSE = {};
@@ -438,6 +440,16 @@ Board.prototype.queryPinState = function(pin, callback) {
this.once('pin-state-' + pin, callback);
this.sp.write([START_SYSEX, PIN_STATE_QUERY, pin, END_SYSEX]);
};
+/**
+ * Sends a I2C config request to the arduino board with an optional
+ * value in microseconds to delay an I2C Read. Must be called before
+ * an I2C Read or Write
+ * @param {number} delay in microseconds to set for I2C Read
+ */
+Board.prototype.sendI2CConfig=function(delay){
+ var delay = delay || 0;
+ this.sp.write([START_SYSEX,I2C_CONFIG,(delay & 0xFF),((delay >> 8) & 0xFF),END_SYSEX]);
+};
/**
* Asks the arduino to send an I2C request to a device
View
@@ -73,6 +73,9 @@ If you run *firmata* from the command line it will prompt you for the usb port.
Read an input for an analog pin. Every time there is data on the pin the callback will be fired with a value argument.
board.servoWrite(pin,degree)
+ Set I2C Config on the arduino
+
+ board.sendI2CConfig(delay)
Write a degree value to a servo pin.
@@ -83,7 +86,7 @@ If you run *firmata* from the command line it will prompt you for the usb port.
board.sendI2CReadRequest(slaveAddress,numBytes,function(data))
Requests a number of bytes from a slave I2C device. When the bytes are received from the I2C device the callback is called with the byte array.
-
+ *note* You must use the latest version of firmata seen here http://firmata.svn.sourceforge.net/viewvc/firmata/arduino/trunk/Firmata/examples/StandardFirmata/ for I2C Support.
## License
(The MIT License)

0 comments on commit 9251ce3

Please sign in to comment.