11#include " Arduino.h"
22#include " GenericArduinoController.h"
33
4+ #define DEBUG 1
5+
46#if DEBUG
5- #define LOG (x,y ) Serial.print(x); Serial.println(y);
7+ #include < string.h>
8+ #define __FILENAME__ (strrchr(__FILE__, ' /' ) ? strrchr(__FILE__, ' /' ) + 1 : __FILE__)
9+ #define LOG (x,y ) Serial.print(__FILENAME__); Serial.print(" :" ); Serial.print(__LINE__);\
10+ Serial.print(" -- [DEBUG] -- " );Serial.print(x); Serial.println(y);
611#else
712#define LOG (x,y )
8- #endif
913
14+ void inline dump_digital_buffer (const int &digital_pins_count, const int &report_read_count, const uint8_t ** digital_input_buffer)
15+ {
16+ for (int i = 0 ; i < digital_pins_count; i++)
17+ {
18+ for (int j = 0 ; j < report_read_count / 8 + 1 ; j++)
19+ {
20+ Serial.print (digital_input_buffer[i][j], HEX);
21+ }
22+ Serial.println (" " );
23+ }
24+ }
25+
26+ #endif
27+
1028#define VERSION " 0.1"
1129
1230const char * ACK = " \xFF\x00 " ;
1331
1432GenericArduinoController::GenericArduinoController (Stream &stream): stream_(stream)
1533{
1634
17- for (int i = 0 ; i < 255 ; i++)
35+ for (int i = 0 ; i <= 255 ; i++)
1836 {
1937 executor[i] = &GenericArduinoController::not_implemented;
2038 }
2139
2240 REPORT_READ_COUNT = 0 ;
2341 REPORT_READ_DELAY = 0 ;
2442 REPORT_MODE = average;
25- INPUT_PORTS[129 ]; // Port count in first position
43+ INPUT_PORTS[0 ] = 0 ;
2644 ANALOG_PINS_COUNT = 0 ;
2745 DIGITAL_PINS_COUNT = 0 ;
2846
29- INPUT_PORTS[0 ] = 0 ;
30-
3147 executor[ANALOG_PRECISION_CMD] = &GenericArduinoController::set_analog_precision;
3248 executor[ADD_INPUT_PIN_CMD] = &GenericArduinoController::set_analog_input;
3349 executor[ADD_OUTPUT_PIN_CMD] = &GenericArduinoController::set_analog_output;
@@ -53,14 +69,19 @@ void GenericArduinoController::handle_commands()
5369 b_read += stream_.readBytes (input, stream_.available ());
5470 }
5571
72+ LOG (" Bytes obtained: " , b_read);
5673 // Loop to process all commands received in the buffer
5774 while (b_pos < b_read)
5875 {
59- LOG (" Executing command: " , int (input[b_pos]));
76+ LOG (" command: " , int (input[b_pos]));
6077 b_pos += executor[input[b_pos]](this , &input[b_pos]); // Does the callback for the command
6178 LOG (" b_pos " , b_pos);
6279 }
6380 }
81+
82+ LOG (" No data" , " " );
83+
84+ delay (1000 );
6485}
6586
6687void GenericArduinoController::add_handler (uint8_t handler_id, int (*handler)(GenericArduinoController* this_, const char *))
@@ -71,7 +92,7 @@ void GenericArduinoController::add_handler(uint8_t handler_id, int (*handler)(Ge
7192// NULL operation
7293int GenericArduinoController::not_implemented (GenericArduinoController* this_, const char * data)
7394{
74- return 0 ;
95+ return 1 ; // Breaks the loop
7596}
7697
7798/* *
@@ -132,7 +153,10 @@ int GenericArduinoController::set_report_mode(GenericArduinoController* this_, c
132153 this_->REPORT_MODE = (ReportModes)(data[2 ]);
133154 this_->REPORT_READ_COUNT = byte (data[3 ]);
134155 this_->REPORT_READ_DELAY = byte (data[4 ]);
135- LOG (" Report mode changed on port " , byte (data[3 ]));
156+ LOG (" Report mode changed" , " " )
157+ LOG (" Report mode: " , this_->REPORT_MODE );
158+ LOG (" New Read count: " , byte (data[3 ]));
159+ LOG (" New Read delay: " , byte (data[4 ]));
136160
137161 return 5 ; // Command of 5 bytes
138162}
@@ -170,6 +194,7 @@ int GenericArduinoController::reset(GenericArduinoController* this_, const char*
170194
171195 this_->INPUT_PORTS [0 ] = 0 ;
172196 this_->ANALOG_PINS_COUNT = 0 ;
197+ this_->REPORT_READ_COUNT = 0 ;
173198 this_->DIGITAL_PINS_COUNT = 0 ;
174199
175200 LOG (" System reset executed" , " " );
@@ -233,21 +258,13 @@ int GenericArduinoController::actuate(GenericArduinoController* this_, const cha
233258 // Resets of digital buffers (required due the buffering strategy for digital pins)
234259 memset (digital_input_buffer, 0 , this_->DIGITAL_PINS_COUNT * (this_->REPORT_READ_COUNT / 8 + 2 ));
235260
236- for (int i = 0 ; i < this_->DIGITAL_PINS_COUNT ; i++)
237- {
238- for (int j = 0 ; j < this_->REPORT_READ_COUNT / 8 + 1 ; j++)
239- {
240- Serial.print (digital_input_buffer[i][j], HEX);
241- }
242- Serial.println (" " );
243- }
244-
245261 // Tracks count of digital and analog ports
246262 LOG (" Number of lectures to report: " , this_->REPORT_READ_COUNT );
247263 for (int lecture = 0 ; lecture <= this_->REPORT_READ_COUNT ; lecture++)
248264 {
249265 uint8_t current_digital = 0 ;
250266 uint8_t current_analog = 0 ;
267+ LOG (" Input ports count: " , this_->INPUT_PORTS [0 ]);
251268 for (int i = 1 ; i <= byte (this_->INPUT_PORTS [0 ]); i++)
252269 {
253270 // response[len + 1] = INPUT_PORTS[i];
@@ -287,6 +304,8 @@ int GenericArduinoController::actuate(GenericArduinoController* this_, const cha
287304 };
288305 }
289306
307+ LOG (" Finish" , " " );
308+
290309 // Every analog output will be in the buffer as
291310 if (this_->ANALOG_PINS_COUNT > 0 )
292311 {
@@ -299,6 +318,7 @@ int GenericArduinoController::actuate(GenericArduinoController* this_, const cha
299318 }
300319
301320 LOG (" Reporting actuate results " , len - 1 );
321+ LOG (" No idea..." , " " );
302322 response[1 ] = len;
303323 this_->stream_ .write (response, 2 ); // 2 bytes extras por el id de comando y la longitud y -1 por el padding
304324
0 commit comments