Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

took out pin array default

  • Loading branch information...
commit 132eff037a506d574d3c8b92954e2e4dbb0098e7 1 parent c531256
@jgautier jgautier authored
Showing with 34 additions and 43 deletions.
  1. +33 −41 lib/firmata.js
  2. +1 −2  test/firmata.js
View
74 lib/firmata.js
@@ -1,6 +1,6 @@
var SerialPort=require('serialport').SerialPort
, sys=require('sys')
-, events=require('events')
+, events=require('events');
const PIN_MODE=0xF4
, REPORT_DIGITAL=0xD0
, REPORT_ANALOG=0xC0
@@ -28,20 +28,14 @@ var Board=function(port,callback){
};
this.HIGH=1;
this.LOW=0;
- this.pins=new Array(128);
+ //the first two pins are for serial communication
+ this.pins=[];
this.analogPins=[];
this.version={};
this.firmware={};
this.timeoutId = setTimeout(function(){
//callback('Time Out');
},10000);
- for(i=0;i<128;i++){
- this.pins[i]={
- value:0,
- supportedModes:[],
- mode:255
- };
- }
this.currentBuffer=[];
this.sp=new SerialPort(port,{baudrate:57600,buffersize:1});
this.sp.on('data',function(data){
@@ -58,28 +52,30 @@ var Board=function(port,callback){
board.sp.write([REPORT_DIGITAL | i,1]);
board.sp.write([REPORT_ANALOG | i,1]);
}
- }else if(cmd == ANALOG_MESSAGE){
- var value =board.currentBuffer[1]|(board.currentBuffer[2] << 7);
- var port = board.currentBuffer[0]&0x0F;
- if(board.pins[board.analogPins[port]]){
- board.pins[board.analogPins[port]].value = value;
- }
- board.emit('analog-read-'+port,value);
- board.emit('analog-read',{pin:port,data:value});
- }else if(cmd == DIGITAL_MESSAGE){
- var port = (board.currentBuffer[0]&0x0F);
- var lsb = board.currentBuffer[1];
- var msb = board.currentBuffer[2];
- var mask = (msb << 7) + lsb;
- for(var i = 0; i < 8; i++){
- var pinNumber = 8*port+i;
- var pin = board.pins[pinNumber];
- if(pin.mode == board.MODES.INPUT){
- pin.value = ((mask & 1) << pinNumber)>0;
- board.emit('digital-read-'+pinNumber,pin.value);
- board.emit('digital-read',{pin: pinNumber,value:pin.value});
- }
- }
+ }else if(board.pins.length > 2) {
+ if(cmd == ANALOG_MESSAGE){
+ var value =board.currentBuffer[1]|(board.currentBuffer[2] << 7);
+ var port = board.currentBuffer[0]&0x0F;
+ if(board.pins[board.analogPins[port]]){
+ board.pins[board.analogPins[port]].value = value;
+ }
+ board.emit('analog-read-'+port,value);
+ board.emit('analog-read',{pin:port,data:value});
+ }else if(cmd == DIGITAL_MESSAGE){
+ var port = (board.currentBuffer[0]&0x0F);
+ var lsb = board.currentBuffer[1];
+ var msb = board.currentBuffer[2];
+ var mask = (msb << 7) + lsb;
+ for(var i = 0; i < 8; i++){
+ var pinNumber = 8*port+i;
+ var pin = board.pins[pinNumber];
+ if(pin.mode == board.MODES.INPUT){
+ pin.value = ((mask & 1) << pinNumber)>0;
+ board.emit('digital-read-'+pinNumber,pin.value);
+ board.emit('digital-read',{pin: pinNumber,value:pin.value});
+ }
+ }
+ }
}
board.currentBuffer=[];
}else if (board.currentBuffer[0]==START_SYSEX && board.currentBuffer[board.currentBuffer.length-1]==END_SYSEX){
@@ -96,17 +92,18 @@ var Board=function(port,callback){
board.emit('queryfirmware');
break;
case CAPABILITY_RESPONSE:
- var pin = 0;
var supportedModes=0;
+ var modesArray;
for(i = 2 , n=0; i < board.currentBuffer.length-1; i++){
- if(board.currentBuffer[i]==127){
+ if(board.currentBuffer[i]==127){
+ modesArray=[];
Object.keys(board.MODES).forEach(function(mode){
if(supportedModes & (1 << board.MODES[mode])){
- board.pins[pin].supportedModes.push(board.MODES[mode]);
+ modesArray.push(board.MODES[mode]);
}
});
+ board.pins.push({supportedModes:modesArray,mode:board.MODES.OUTPUT});
supportedModes=0;
- pin++;
n=0;
continue;
}
@@ -163,11 +160,7 @@ var Board=function(port,callback){
for(i = 0; i < pinsToQuery.length;i++){
if(i == pinsToQuery.length - 1){
board.queryPinState(pinsToQuery[i],function(){
- if(board.timeoutId){
- clearTimeout(board.timeoutId);
- board.timeoutId = null;
- }
- callback(null);
+ callback();
});
}
else{
@@ -201,7 +194,6 @@ Board.prototype.analogRead=function(pin,callback){
};
Board.prototype.analogWrite=function(pin,value){
this.pins[pin]=value;
- console.log('pin '+ pin +' value '+value);
this.sp.write([ANALOG_MESSAGE|pin,value & 0x7F,(value >> 7) & 0x7F]);
};
Board.prototype.pinMode=function(number,state){
View
3  test/firmata.js
@@ -90,7 +90,7 @@ var board=new firmata.Board('/dev/ttyS2',function(error){
console.log('Put Your hand over light sensor');
board.pinMode(8,board.MODES.INPUT);
board.digitalRead(8,function(value){
- if(value == 0){
+ if(value === 0){
if(!callbackCalled){
callbackCalled=true;
callback(null,'2');
@@ -135,7 +135,6 @@ var board=new firmata.Board('/dev/ttyS2',function(error){
,analogWrite
,readYes
],function(error,results){
- console.log(results);
assert.ok(true,'Analog Read Succesful');
assert.ok(true,'Digital Read Successful');
assert.ok(results[3] == 'Y\n','Digital Write Successful');
Please sign in to comment.
Something went wrong with that request. Please try again.