@@ -19,7 +19,7 @@ module.exports = function(RED) {
1919 RED . log . warn ( "Info : mcp3xxx : Not running on a Pi - Ignoring node" ) ;
2020 }
2121
22- var mcp3xxx = [ ] ;
22+ // var mcp3xxx = [];
2323
2424 function PiMcpNode ( n ) {
2525 RED . nodes . createNode ( this , n ) ;
@@ -28,15 +28,17 @@ module.exports = function(RED) {
2828 this . dnum = parseInt ( n . dnum || 0 ) ;
2929 this . bus = parseInt ( n . bus || 0 ) ;
3030 this . dev = n . dev || "3008" ;
31+ this . mcp3xxx = [ ] ;
3132 var node = this ;
32- var cb = function ( err ) { if ( err ) { node . error ( "Error: " + err ) ; } } ;
33- var opt = { speedHz :20000 , deviceNumber :node . dnum , busNumber :node . bus } ;
33+ this . cb = function ( err ) { if ( err ) { node . error ( "Error: " + err ) ; } } ;
34+ this . opt = { speedHz :20000 , deviceNumber :node . dnum , busNumber :node . bus } ;
3435 var chans = parseInt ( this . dev . substr ( 3 ) ) ;
36+ var node = this ;
3537
3638 if ( allOK === true ) {
3739 try {
3840 fs . statSync ( "/dev/spidev" + node . bus + "." + node . dnum ) ;
39- if ( mcp3xxx . length === 0 ) {
41+ if ( node . mcp3xxx . length === 0 ) {
4042 for ( var i = 0 ; i < chans ; i ++ ) {
4143 if ( node . dev === "3002" ) { mcp3xxx . push ( mcpadc . openMcp3002 ( i , opt , cb ) ) ; }
4244 if ( node . dev === "3004" ) { mcp3xxx . push ( mcpadc . openMcp3004 ( i , opt , cb ) ) ; }
@@ -57,22 +59,22 @@ module.exports = function(RED) {
5759 }
5860 else { pin = parseInt ( node . pin ) ; }
5961 if ( pin !== null ) {
60- mcp3xxx [ pin ] . read ( function ( err , reading ) {
62+ node . mcp3xxx [ pin ] . read ( function ( err , reading ) {
6163 if ( err ) { node . warn ( "Read error: " + err ) ; }
6264 else { node . send ( { payload :reading . rawValue , topic :"adc/" + pin } ) ; }
6365 } ) ;
6466 }
6567 } ) ;
6668 }
6769 catch ( err ) {
68- node . error ( "Error : Can't find SPI device - is SPI enabled in raspi-config ?" ) ;
70+ node . error ( "Error : Can't find SPI device - is SPI enabled in raspi-config ?" + err ) ;
6971 }
7072
7173 node . on ( "close" , function ( done ) {
72- if ( mcp3xxx . length !== 0 ) {
74+ if ( node . mcp3xxx . length !== 0 ) {
7375 var j = 0 ;
7476 for ( var i = 0 ; i < chans ; i ++ ) {
75- mcp3xxx [ i ] . close ( function ( ) { j += 1 ; if ( j === chans ) { done ( ) } } ) ;
77+ node . mcp3xxx [ i ] . close ( function ( ) { j += 1 ; if ( j === chans ) { done ( ) } } ) ;
7678 }
7779 mcp3xxx = [ ] ;
7880 }
0 commit comments