@@ -63,9 +63,9 @@ void GenericArduinoController::handle_commands()
6363 if (stream_.available () > 0 )
6464 {
6565 LOG (" Data available " , stream_.available ());
66- char input[128 ]; // Esto se reserva en el stack, tal vez hacerlo global consume menos recursos...
66+ uint8_t input[128 ]; // Esto se reserva en el stack, tal vez hacerlo global consume menos recursos...
6767
68- byte b_read = 0 ;
68+ uint8_t b_read = 0 ;
6969
7070 if (stream_.available () >= 5 )
7171 {
@@ -76,9 +76,9 @@ void GenericArduinoController::handle_commands()
7676
7777 uint32_t data_len = (uint32_t (input[1 ]) << 24 ) + (uint32_t (input[2 ]) << 16 ) + (uint32_t (input[3 ]) << 8 ) + uint32_t (input[4 ]);
7878
79- LOG (" Command: " , int (input[0 ]));
80- LOG (" Command of length: " , data_len);
81-
79+ LOG (" Command: " , uint8_t (input[0 ]));
80+ LOG (" Command of length: " , static_cast < unsigned long >( data_len) );
81+
8282 while (b_read - 5 < data_len)
8383 {
8484 b_read += stream_.readBytes (&input[b_read], data_len + 5 - b_read);
@@ -93,13 +93,13 @@ void GenericArduinoController::handle_commands()
9393
9494}
9595
96- void GenericArduinoController::add_handler (uint8_t handler_id, int (*handler)(GenericArduinoController* this_, uint32_t &buff_len, const char *))
96+ void GenericArduinoController::add_handler (uint8_t handler_id, int (*handler)(GenericArduinoController* this_, uint32_t &buff_len, const uint8_t *))
9797{
9898 executor[handler_id] = handler;
9999}
100100
101101// NULL operation
102- int GenericArduinoController::not_implemented (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
102+ int GenericArduinoController::not_implemented (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
103103{
104104 LOG (" Unknown command received!! Operation: " , data[0 ]);
105105 return 1 + buff_len; // Breaks the loop
@@ -108,7 +108,7 @@ int GenericArduinoController::not_implemented(GenericArduinoController* this_, u
108108/* *
109109 PROTOCOL_VERSION 0x07 0x00 0x00 0x00 0x03 X . Y
110110*/
111- int GenericArduinoController::protocol_version (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
111+ int GenericArduinoController::protocol_version (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
112112{
113113 char response[] = { char (VERSION_RESPONSE), char (0 ), char (0 ), char (0 ), char (0x03 )};
114114 this_->stream_ .write (response, 5 );
@@ -120,7 +120,7 @@ int GenericArduinoController::protocol_version(GenericArduinoController* this_,
120120/* *
121121 ANALOG_WRITE: 0x06 0x00 0x00 0x00 0x03 [PIN] [H_VALUE][L_VALUE]
122122*/
123- int GenericArduinoController::analog_write (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
123+ int GenericArduinoController::analog_write (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
124124{
125125 uint16_t value = (data[7 ] << 8 ) + data[8 ];
126126 analogWrite (data[6 ], value);
@@ -130,7 +130,7 @@ int GenericArduinoController::analog_write(GenericArduinoController* this_, uint
130130/* *
131131 ANALOG_PRECISION: 0x01 0x00 0x00 0x00 0x01 [BITS]
132132*/
133- int GenericArduinoController::set_analog_precision (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
133+ int GenericArduinoController::set_analog_precision (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
134134{
135135 int i = byte (data[5 ]);
136136 // Tal vez conviene separar estas funciones ya que hay boards con resoluciones distintas...
@@ -148,7 +148,7 @@ int GenericArduinoController::set_analog_precision(GenericArduinoController* thi
148148/* *
149149 PIN_MODE: 0x04 0x00 0x00 0x00 0x02 [PIN] [MODE]
150150*/
151- int GenericArduinoController::set_pin_mode (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
151+ int GenericArduinoController::set_pin_mode (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
152152{
153153 pinMode (data[5 ], data[6 ]);
154154 LOG (" Changed pin mode on pin " , uint8_t (data[5 ]));
@@ -160,11 +160,11 @@ int GenericArduinoController::set_pin_mode(GenericArduinoController* this_, uint
160160/* *
161161 REPORT_MODE: 0x05 0x00 0x00 0x00 0x03 [MODE] [READ_COUNT] [READ_DELAY]
162162*/
163- int GenericArduinoController::set_report_mode (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
163+ int GenericArduinoController::set_report_mode (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
164164{
165165 this_->REPORT_MODE = (ReportModes)(data[5 ]);
166- this_->REPORT_READ_COUNT = byte (data[6 ]);
167- this_->REPORT_READ_DELAY = byte (data[7 ]);
166+ this_->REPORT_READ_COUNT = uint8_t (data[6 ]);
167+ this_->REPORT_READ_DELAY = uint8_t (data[7 ]);
168168 LOG (" Report mode changed" , " " );
169169 LOG (" Report mode: " , this_->REPORT_MODE );
170170 LOG (" New Read count: " , this_->REPORT_READ_COUNT );
@@ -176,7 +176,7 @@ int GenericArduinoController::set_report_mode(GenericArduinoController* this_, u
176176/* *
177177 ANALOG_INPUT: 0x02 0x00 0x00 0x00 0x01 [PORT]
178178*/
179- int GenericArduinoController::set_analog_input (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
179+ int GenericArduinoController::set_analog_input (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
180180{
181181 this_->INPUT_PORTS [0 ] += 1 ;
182182 this_->INPUT_PORTS [this_->INPUT_PORTS [0 ]] = byte (data[5 ]);
@@ -197,7 +197,7 @@ int GenericArduinoController::set_analog_input(GenericArduinoController* this_,
197197/* *
198198 RESET: 0xFF 0x00 0x00 0x00 0x00
199199*/
200- int GenericArduinoController::reset (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
200+ int GenericArduinoController::reset (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
201201{
202202 for ( int i = 1 ; i <= byte (this_->INPUT_PORTS [0 ]); i++)
203203 {
@@ -217,7 +217,7 @@ int GenericArduinoController::reset(GenericArduinoController* this_, uint32_t &b
217217/* *
218218 ANALOG_OUTPUT: 0x03 0x00 0x00 0x00 0x01 [PORT]
219219*/
220- int GenericArduinoController::set_analog_output (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
220+ int GenericArduinoController::set_analog_output (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
221221{
222222 // No se si vale la pena guardar registro de pines de salida...
223223 LOG (" Added as output the pin" , data[5 ]);
@@ -227,32 +227,32 @@ int GenericArduinoController::set_analog_output(GenericArduinoController* this_,
227227/* *
228228 ACTUATE: 0xF0 [DATA_LEN] [PIN_A] [VALUE_PIN_A] ... [PIN_N] [VALUE_PIN_N]
229229*/
230- int GenericArduinoController::actuate (GenericArduinoController* this_, uint32_t &buff_len, const char * data)
230+ int GenericArduinoController::actuate (GenericArduinoController* this_, uint32_t &buff_len, const uint8_t * data)
231231{
232232 uint32_t offset = 0 ;
233233
234234 uint8_t digital_input_buffer[this_->DIGITAL_PINS_COUNT ][this_->REPORT_READ_COUNT / 8 + 2 ]; // 255 lectures of 1 bit for every digital pin -- 1 extra byte for the port address
235235 uint8_t analog_input_buffer[this_->ANALOG_PINS_COUNT ][(2 * this_->REPORT_READ_COUNT ) + 3 ]; // 255 lectures of 2 bytes for every analog pin -- 1 extra byte for the port address
236236
237- LOG (" Actutating over payload of size: " , buff_len);
237+ LOG (" Actuating over payload of size: " , buff_len);
238238
239239 // ACTUATION ZONE
240240 while (offset < buff_len)
241241 {
242242 // Se aplica la acción a cada puerto indicado
243- int port = byte (data[5 + offset]);
243+ uint8_t port = byte (data[5 + offset]);
244244
245245 // Detects an analog port
246246 if ( port >= A0 )
247247 {
248- int value = (data[6 + offset] << 8 ) + data[7 + offset];
248+ uint8_t value = (data[6 + offset] << 8 ) + data[7 + offset];
249249 analogWrite (port, value);
250250 offset += 3 ;
251251
252252 LOG (" Analog pin written" , port);
253253 } else
254254 {
255- int value = data[6 + offset] > 0 ? HIGH : LOW; // Creo que da igual si pongo el entero directamente
255+ uint8_t value = data[6 + offset] > 0 ? HIGH : LOW; // Creo que da igual si pongo el entero directamente
256256 digitalWrite (port, value);
257257 offset += 2 ;
258258
@@ -262,7 +262,7 @@ int GenericArduinoController::actuate(GenericArduinoController* this_, uint32_t
262262
263263 delayMicroseconds (this_->REPORT_READ_DELAY ); // FIXME: Usamos variable de 8 bits cuando la precisión de esta función llega a 16 bits.
264264
265- char response[130 ];
265+ uint8_t response[130 ];
266266 response[0 ] = ' \xF1 ' ;
267267 uint32_t len = 0 ; // Inicia en 1 para evitar pisar el id de comando
268268
@@ -271,23 +271,23 @@ int GenericArduinoController::actuate(GenericArduinoController* this_, uint32_t
271271
272272 // Tracks count of digital and analog ports
273273 LOG (" Number of lectures to report: " , this_->REPORT_READ_COUNT );
274- for (int lecture = 0 ; lecture <= this_->REPORT_READ_COUNT ; lecture++)
274+ for (uint8_t lecture = 0 ; lecture <= this_->REPORT_READ_COUNT ; lecture++)
275275 {
276276 uint8_t current_digital = 0 ;
277277 uint8_t current_analog = 0 ;
278278 LOG (" Input ports count: " , this_->INPUT_PORTS [0 ]);
279- for (int i = 1 ; i <= byte (this_->INPUT_PORTS [0 ]); i++)
279+ for (uint8_t i = 1 ; i <= uint8_t (this_->INPUT_PORTS [0 ]); i++)
280280 {
281281 // response[len + 1] = INPUT_PORTS[i];
282282 if ( this_->INPUT_PORTS [i] >= A0 )
283283 {
284284 LOG (" Reading analog port A" , this_->INPUT_PORTS [i] - A0);
285- int data = analogRead (this_->INPUT_PORTS [i] - A0);
285+ uint16_t data = analogRead (this_->INPUT_PORTS [i] - A0);
286286 // response[len + 2] = byte((data & 0xFF00) >> 8); // Se guarda el msb en el buffer
287287 // response[len + 3] = byte(data & 0xFF); // Se guarda el lsb en el buffer
288288 analog_input_buffer[current_analog][0 ] = this_->INPUT_PORTS [i];
289- analog_input_buffer[current_analog][(lecture * 2 ) + 1 ] = byte ((data & 0xFF00 ) >> 8 );
290- analog_input_buffer[current_analog][(lecture * 2 ) + 2 ] = byte (data & 0xFF );
289+ analog_input_buffer[current_analog][(lecture * 2 ) + 1 ] = uint8_t ((data & 0xFF00 ) >> 8 );
290+ analog_input_buffer[current_analog][(lecture * 2 ) + 2 ] = uint8_t (data & 0xFF );
291291
292292 // len += 3; // Cada lectura de un recurso analógico ocupa dos bytes. FIXME: se puede optimizar con bajas resoluciones
293293 LOG (" =====================================" , " " );
@@ -297,19 +297,19 @@ int GenericArduinoController::actuate(GenericArduinoController* this_, uint32_t
297297 current_analog++;
298298 } else
299299 {
300- int data = digitalRead (this_->INPUT_PORTS [i]);
300+ uint8_t data = digitalRead (this_->INPUT_PORTS [i]);
301301 // response[len + 2] = byte(data);
302302 digital_input_buffer[current_digital][0 ] = this_->INPUT_PORTS [i];
303- digital_input_buffer[current_digital][(lecture / 8 ) + 1 ] += (byte ( data) & 0x01 ) << (lecture % 8 ); // Just keep first bit
303+ digital_input_buffer[current_digital][(lecture / 8 ) + 1 ] += (data & 0x01 ) << (lecture % 8 ); // Just keep first bit
304304 current_digital++;
305305 // len += 2;
306306 LOG (" =====================================" , " " );
307307 LOG (" Digital pin read: " , this_->INPUT_PORTS [i]);
308308 LOG (" Raw read: " , data);
309309 LOG (" Lecture: " , lecture);
310- LOG (" Filtered read: " , (byte ( data) & 0x01 ));
310+ LOG (" Filtered read: " , (data & 0x01 ));
311311 LOG (" Left padding: " , (lecture % 8 ));
312- LOG (" Added: " , (byte ( data) & 0x01 ) << (lecture % 8 ));
312+ LOG (" Added: " , (data & 0x01 ) << (lecture % 8 ));
313313 LOG (" Digital read value: " , digital_input_buffer[current_digital - 1 ][(lecture / 8 ) + 1 ]);
314314 }
315315 };
0 commit comments