2016-01-14 21:19:41 +01:00
var events = require ( 'events' ) ,
child = require ( 'child_process' ) ,
util = require ( 'util' ) ,
2016-01-14 21:48:38 +01:00
serial = require ( "serialport" ) ;
2016-01-14 21:19:41 +01:00
var Arduino = function ( baudrate ) {
this . baudrate = baudrate && baudrate || 9600 ;
this . writeBuffer = [ ] ;
}
2016-01-16 21:58:47 +01:00
util . inherits ( Arduino , events . EventEmitter ) ;
Arduino . prototype . HIGH = '255' ;
Arduino . prototype . LOW = '000' ;
2016-01-14 21:19:41 +01:00
Arduino . prototype . setup = function ( ) {
2016-01-14 21:43:45 +01:00
var self = this ;
2016-01-14 21:19:41 +01:00
child . exec ( 'cd ../arduino && ino build && ino upload' , function ( err , stdout , stderr ) {
2016-01-16 21:58:47 +01:00
if ( err ) { console . log ( 'Please install the ino utility and connect arduino!\nIf there are other open programs that communicate with the arduino, close them!' ) ; return ; }
2016-01-14 21:19:41 +01:00
var buff = stdout . slice ( stdout . indexOf ( 'Guessing serial port ... ' ) + 25 ) ;
buff = buff . slice ( 0 , buff . indexOf ( '\n' ) ) ;
console . log ( 'Guessing serial port ... ' + buff ) ;
2016-01-14 21:48:38 +01:00
self . serial = new serial . SerialPort ( buff , {
2016-01-14 21:43:45 +01:00
baudrate : self . baudrate ,
2016-01-14 21:19:41 +01:00
parser : serial . parsers . readline ( '\n' )
} , false ) ;
2016-01-14 21:43:45 +01:00
self . serial . open ( function ( error ) {
2016-01-14 21:19:41 +01:00
if ( error ) {
2016-01-16 21:58:47 +01:00
console . log ( 'Please check arduino!' ) ;
return ;
2016-01-14 21:19:41 +01:00
}
2016-01-16 21:58:47 +01:00
} ) ;
2016-01-14 21:19:41 +01:00
2016-01-16 22:42:59 +01:00
self . serial . on ( 'open' , function ( ) { setTimeout ( function ( ) {
2016-01-14 21:48:38 +01:00
self . serial . write ( new Buffer ( '00000000' ) ) ;
2016-01-21 22:01:57 +01:00
self . serial . write ( new Buffer ( 'Zn000000' , 'ascii' ) ) ;
2016-01-16 21:58:47 +01:00
self . emit ( 'success' ) ;
self . serial . on ( 'data' , function ( data ) {
2016-01-18 22:35:01 +01:00
console . log ( data ) ;
2016-01-16 21:58:47 +01:00
self . emit ( 'data' , data ) ;
} ) ;
2016-01-14 21:58:44 +01:00
self . processWriteBuffer ( ) ;
2016-01-16 22:42:59 +01:00
} , 5000 ) ; } ) ;
2016-01-14 21:43:45 +01:00
2016-01-14 21:19:41 +01:00
} ) ;
}
2016-01-16 21:58:47 +01:00
Arduino . prototype . write = function ( message ) {
2016-01-14 21:43:45 +01:00
if ( this . serial ) {
2016-01-16 21:58:47 +01:00
console . log ( 'sending ' + message ) ;
this . serial . write ( message ) ;
2016-01-14 21:19:41 +01:00
} else {
2016-01-16 21:58:47 +01:00
this . writeBuffer . push ( message ) ;
2016-01-14 21:19:41 +01:00
console . log ( 'Board isn\'t available (yet?).' )
}
}
Arduino . prototype . processWriteBuffer = function ( ) {
while ( this . writeBuffer . length > 0 ) {
this . write ( this . writeBuffer . shift ( ) ) ;
}
}
2016-01-29 21:33:07 +01:00
// Helpers
Arduino . prototype . valueToLastFour = function ( val ) {
var encodedVal ;
for ( var i = 3 ; i < 0 ; i -- ) {
encodedVal += String . fromCharCode ( val / ( Math . pow ( 2 , 8 * i ) ) ) ;
val = val % Math . pow ( 2 , 8 * i ) ;
}
return encodedVal ;
} ;
Arduino . prototype . valueToFirstTwo = function ( val ) {
var encodedVal ;
for ( var i = 1 ; i < 0 ; i -- ) {
encodedVal += String . fromCharCode ( val / ( Math . pow ( 2 , 8 * i ) ) ) ;
val = val % Math . pow ( 2 , 8 * i ) ;
}
return encodedVal ;
} ;
2016-01-14 21:48:38 +01:00
module . exports = Arduino ;