Permalink
Browse files

added tests and digital read working

  • Loading branch information...
1 parent d6a9db2 commit 990468c611d8e1175b063cf7d2de5042fe2865a1 @jgautier jgautier committed May 31, 2011
Showing with 92 additions and 131 deletions.
  1. +92 −18 lib/firmata.js
  2. +0 −14 test/photocell.js
  3. +0 −29 test/public/index.html
  4. +0 −29 test/server.js
  5. +0 −24 test/temperature.js
  6. +0 −17 test/test.js
View
@@ -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
@@ -19,16 +19,26 @@ const PIN_MODE=0xF4
var Board=function(port,callback){
events.EventEmitter.call(this);
var board=this;
- this.OUTPUT=1;
- this.INPUT=0;
- this.PWM=3;
+ this.MODES={
+ INPUT:0x00,
+ OUTPUT:0x01,
+ ANALOG:0x02,
+ PWM:0x03,
+ SERVO:0x04
+ };
this.HIGH=1;
this.LOW=0;
this.pins=new Array(128);
+ 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:0,
+ supportedModes:[],
mode:255
};
}
@@ -41,33 +51,67 @@ var Board=function(port,callback){
var cmd=board.currentBuffer[0]&0xF0;
if(board.currentBuffer.length==3 && board.currentBuffer[0] != START_SYSEX){
if(board.currentBuffer[0]==REPORT_VERSION){
- board.emit('reportversion',{major:board.currentBuffer[1],minor:board.currentBuffer[2]});
+ board.version.major = board.currentBuffer[1];
+ board.version.minor = board.currentBuffer[2];
+ board.emit('reportversion');
for(i=0;i<16;i++){
board.sp.write([REPORT_DIGITAL | i,1]);
board.sp.write([REPORT_ANALOG | i,1]);
}
}else if(cmd == ANALOG_MESSAGE){
- var port = board.currentBuffer[0]&0x0F;
- board.emit('analog-read-'+port,board.currentBuffer[1]|(board.currentBuffer[2] << 7));
- board.emit('analog-read',{pin:port,data:board.currentBuffer[1]|(board.currentBuffer[2] << 7)});
+ 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){
- //to do
+ 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){
switch(board.currentBuffer[1]){
case QUERY_FIRMWARE:
+ var firmwareBuf = [];
+ board.firmware.version={};
+ board.firmware.version.major=board.currentBuffer[2];
+ board.firmware.version.minor=board.currentBuffer[3];
+ for(var i = 4, length = board.length-3;i < length;i+=2){
+ firmwareBuf.push((board.currentBuffer[i] & 0x7F)|((board.currentBuffer[i+1] & 0x7F) << 7));
+ }
+ board.firmware.name = new Buffer(firmwareBuf).toString('utf8',0,firmwareBuf.length);
+ board.emit('queryfirmware');
break;
case CAPABILITY_RESPONSE:
var pin = 0;
+ var supportedModes=0;
for(i = 2 , n=0; i < board.currentBuffer.length-1; i++){
if(board.currentBuffer[i]==127){
+ Object.keys(board.MODES).forEach(function(mode){
+ if(supportedModes & (1 << board.MODES[mode])){
+ board.pins[pin].supportedModes.push(board.MODES[mode]);
+ }
+ });
+ supportedModes=0;
pin++;
n=0;
continue;
}
if(n==0){
- board.pins[pin].supportedModes|=(1<<board.currentBuffer[i]);
+ supportedModes|=(1<<board.currentBuffer[i]);
}
n^=1;
}
@@ -87,8 +131,13 @@ var Board=function(port,callback){
break;
case ANALOG_MAPPING_RESPONSE:
var pin = 0;
+ var currentValue;
for(i = 2 ; i < board.currentBuffer.length-1;i++){
- board.pins[pin].analogChannel=board.currentBuffer[i];
+ currentValue = board.currentBuffer[i];
+ board.pins[pin].analogChannel=currentValue;
+ if(currentValue != 127){
+ board.analogPins.push(pin);
+ }
pin++;
}
board.emit('analog-mapping-query');
@@ -98,7 +147,11 @@ var Board=function(port,callback){
}
});
this.sp.on('error',function(string){
- console.log(string);
+ if(board.timeoutId){
+ clearTimeout(board.timeoutId);
+ board.timeoutId == null;
+ }
+ callback(string);
});
this.reportVersion(function(){
board.queryCapabilities(function(){
@@ -109,7 +162,13 @@ var Board=function(port,callback){
}
for(i = 0; i < pinsToQuery.length;i++){
if(i == pinsToQuery.length - 1){
- board.queryPinState(pinsToQuery[i],callback);
+ board.queryPinState(pinsToQuery[i],function(){
+ if(board.timeoutId){
+ clearTimeout(board.timeoutId);
+ board.timeoutId = null;
+ }
+ callback(null);
+ });
}
else{
board.queryPinState(pinsToQuery[i],function(){});
@@ -118,8 +177,17 @@ var Board=function(port,callback){
});
});
});
+ process.on('SIGINT',function(){
+ board.sp.close();
+ });
+ process.on('SIGTERM',function(){
+ board.sp.close();
+ });
}
sys.inherits(Board,events.EventEmitter);
+Board.prototype.reportDigital=function(pin,bit){
+ this.sp.write([REPORT_DIGITAL|pin,1]);
+};
Board.prototype.reportVersion=function(callback){
this.once('reportversion',callback);
this.sp.write(REPORT_VERSION);
@@ -132,21 +200,27 @@ Board.prototype.analogRead=function(pin,callback){
this.addListener('analog-read-'+pin,callback);
};
Board.prototype.analogWrite=function(pin,value){
- this.sp.write([ANALOG_MESSAGE|pin,value&0x7F,(value >> 7)& 0x7F]);
+ 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){
+ this.pins[number].mode=state;
this.sp.write([PIN_MODE,number,state]);
};
Board.prototype.digitalWrite=function(pin,value){
var port = Math.floor(pin/8);
var portValue=0;
this.pins[pin]=value;
- for(i=0;i<8;i++){
+ for(var i=0;i<8;i++){
if(this.pins[8*port+i])
portValue |= (1<<i);
}
- this.sp.write([DIGITAL_MESSAGE|port,portValue & 0x7F,(portValue >> 7)&0x7F]);
-}
+ this.sp.write([DIGITAL_MESSAGE|port,portValue & 0x7F,(portValue >> 7) & 0x7F]);
+};
+Board.prototype.digitalRead=function(pin,callback){
+ this.addListener('digital-read-'+pin,callback);
+};
Board.prototype.queryCapabilities=function(callback){
this.once('capability-query',callback);
this.sp.write([START_SYSEX,CAPABILITY_QUERY,END_SYSEX]);
View
@@ -1,14 +0,0 @@
-var firmata=require('../lib/firmata.js');
-var board=new firmata.Board('/dev/ttyACM0',function(){
-// board.pinMode(0,board.INPUT);
- board.pinMode(17,board.OUTPUT);
- console.log('board ready');
- board.analogRead(0,function(data){
- console.log(data);
- if(data > 500){
- board.digitalWrite(17,0);
- }else{
- board.digitalWrite(17,1);
- }
- });
-});
View
@@ -1,29 +0,0 @@
-<html>
- <head>
- <script src="http://code.jquery.com/jquery-1.5.min.js"></script>
- <script>
- $(function(){
- var onOff="off";
- $("#setOut").click(function(){
- $.get("/pinmode?pin="+$("#txtPin").val(),function(){
- });
- });
- $("#onOff").click(function(){
- if(onOff=="off"){
- onOff="on";
- }else{
- onOff="off";
- }
- $.get("/"+onOff+"?pin="+$("#txtPin").val(),function(){
- });
- $(this).val(onOff);
- });
- });
- </script>
- </head>
- <body>
- <input type="text" id="txtPin" value="">
- <input type="button" id="setOut" value="OUTPUT">
- <input type="button" id="onOff" value="Off" height="3000" width="3000"></input>
- </body>
-</html>
View
@@ -1,29 +0,0 @@
-var firmata = require('../lib/firmata.js')
- , express=require('express')
- , app = express.createServer()
- , board = new firmata.Board('/dev/ttyACM0',function(){
- console.log(board.pins);
- app.use(express.static(__dirname + '/public'));
- app.get('/on', function(req, res){
- board.digitalWrite(req.param('pin'), 1);
- res.send("on");
- });
- app.get('/off',function(req,res){
- board.digitalWrite(req.param('pin'), 0);
- res.send("off");
- });
- app.get('/pinmode',function(req,res){
- board.pinMode(req.param('pin'),board.OUTPUT);
- res.send("pinmode");
- });
- app.listen(3000);
- console.log('listening');
- board.pinMode(9,board.PWM);
- board.analogRead(0,function(data){
- if(data > 500){
- board.analogWrite(9,0);
- }else{
- board.analogWrite(9,255);
- }
- });
-});
View
@@ -1,24 +0,0 @@
-var firmata=require('../lib/firmata.js')
- ,dataPin=12
- ,clockPin=11;
-var board=new firmata.Board('/dev/ttyACM0',function(version){
- console.log(version);
- board.pinMode(dataPin,board.OUTPUT);
- board.pinMode(clockPin,board.OUTPUT);
- board.digitalWrite(dataPin,board.HIGH);
- board.digitalWrite(clockPin,board.HIGH);
- board.digitalWrite(dataPin,board.LOW);
- board.digitalWrite(clockPin,board.LOW);
- board.digitalWrite(clockPin,board.HIGH);
- board.digitalWrite(dataPin,board.HIGH);
- board.digitalWrite(clockPin,board.LOW);
- //try shift out
- tempCommand=00000011;
- for(i=0;i<8;i++){
- board.digitalWrite(dataPin,!!(tempCommand & (1 << (7 - i))));
- board.digitalWrite(clockPin,board.LOW);
- board.digitalWrite(clockPin,board.HIGH);
- }
- board.digitalWrite(clockPin,board.HIGH);
- board.pinMode(dataPin,board.INPUT);
-});
View
@@ -1,17 +0,0 @@
-var firmata=require('../lib/firmata');
-var board=new firmata.Board('/dev/ttyACM0',function(firmwareVersion){
- var pin = 0;
- console.log(firmwareVersion);
- console.dir(board);
- board.setActive(7,1);
- board.pinMode(7,1);
- setInterval(function(){
- if(pin==1){
- pin=0;
- }else{
- pin=1;
- }
- board.digitalWrite(7,1);
- },500);
-// board.queryFirmware();
-});

0 comments on commit 990468c

Please sign in to comment.