Permalink
Browse files

initial i2c support

  • Loading branch information...
1 parent 57bf9bc commit aa1371ecd1943db0232bf9ad40fd07d0ba21565f @jgautier jgautier committed Aug 11, 2011
Showing with 45 additions and 6 deletions.
  1. +45 −6 lib/firmata.js
View
51 lib/firmata.js
@@ -15,17 +15,27 @@ const PIN_MODE=0xF4
, PIN_STATE_QUERY=0x6D
, PIN_STATE_RESPONSE=0x6E
, ANALOG_MAPPING_QUERY=0x69
-, ANALOG_MAPPING_RESPONSE=0x6A;
+, ANALOG_MAPPING_RESPONSE=0x6A
+, I2C_REQUEST=0x76
+, I2C_REPLY=0x77
+, I2C_CONFIG=0x78
+, STRING_DATA=0x71
var Board=function(port,callback){
events.EventEmitter.call(this);
var board=this;
this.MODES={
- INPUT:0x00,
- OUTPUT:0x01,
- ANALOG:0x02,
- PWM:0x03,
- SERVO:0x04
+ INPUT:0x00
+ ,OUTPUT:0x01
+ ,ANALOG:0x02
+ ,PWM:0x03
+ ,SERVO:0x04
};
+ this.I2C_MODES={
+ WRITE:0x00
+ ,READ:1
+ ,CONTINUOUS_READ:2
+ ,STOP_READING:3
+ }
this.HIGH=1;
this.LOW=0;
//the first two pins are for serial communication
@@ -137,6 +147,18 @@ var Board=function(port,callback){
}
board.emit('analog-mapping-query');
break;
+ case I2C_REPLY:
+ var replyBuffer = [];
+ var slaveAddress=board.currentBuffer[2]|(board.currentBuffer[3] << 7);
+ var register=board.currentBuffer[4]|(board.currentBuffer[5] << 7);
+ for(var i = 6,length = board.currentBuffer.length-1; i < length;i+=2){
+ replyBuffer.push(board.currentBuffer[i]|(board.currentBuffer[i+1] << 7));
+ }
+ board.emit('I2C-reply',replyBuffer);
+ break;
+ case STRING_DATA:
+ console.log(new Buffer(board.currentBuffer.slice(1,-1)).toString('utf8'));
+ break;
}
board.currentBuffer=[];
}
@@ -223,4 +245,21 @@ Board.prototype.queryPinState=function(pin,callback){
this.once('pin-state-'+pin,callback);
this.sp.write([START_SYSEX,PIN_STATE_QUERY,pin,END_SYSEX]);
};
+Board.prototype.sendI2CWriteRequest=function(slaveAddress,bytes){
+ var bytes = bytes || [];
+ var data = [];
+ data.push(START_SYSEX);
+ data.push(I2C_REQUEST);
+ data.push(slaveAddress);
+ data.push(this.I2C_MODES.WRITE<<3);
+ for(var i = 0, length = bytes.length;i < length; i++){
+ data.push(bytes[i] & 0x7F);
+ data.push((bytes[i] >> 7) & 0x7F);
+ }
+ data.push(END_SYSEX);
+ this.sp.write(data);
+};
+Board.prototype.sendI2CReadRequest=function(slaveAddress,numBytes){
+ this.sp.write([START_SYSEX,I2C_REQUEST,slaveAddress,this.I2C_MODES.READ<<3,numBytes & 0x7F,(numBytes >> 7) & 0x7F,END_SYSEX]);
+};
module.exports={Board:Board};

0 comments on commit aa1371e

Please sign in to comment.