Skip to content

Commit 3ed2cfb

Browse files
committed
More debug messages in protocol.
1 parent fd650ee commit 3ed2cfb

File tree

5 files changed

+83
-34
lines changed

5 files changed

+83
-34
lines changed

MLC/arduino/Firmware/Firmware.ino

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
21
#include "GenericArduinoController.h"
32

3+
#define DEBUG 1
4+
45
GenericArduinoController controller(SerialUSB);
56

67
void setup() {
@@ -15,7 +16,14 @@ void setup() {
1516
void loop() {
1617
controller.handle_commands();
1718

19+
Serial.println("Waiting for commands...");
20+
21+
if (SerialUSB.available() > 0)
22+
Serial.println("Hay datos para leer...");
23+
1824
/**
1925
* HERE the user can insert any command
2026
*/
27+
28+
delay(1000);
2129
}

MLC/arduino/Firmware/GenericArduinoController.cpp

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
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

1230
const char* ACK = "\xFF\x00";
1331

1432
GenericArduinoController::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

6687
void 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
7293
int 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

MLC/arduino/protocol.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ def __init__(self, connection, board):
2727
self._read_delay = 0
2828
self._board = board
2929

30+
def get_analog_inputs(self):
31+
return self._anlg_inputs()
32+
33+
def get_digital_inputs(self):
34+
return self._digital_inputs
35+
36+
def get_analog_precition(self):
37+
return self._analog_precition
38+
39+
def get_report_modes(self):
40+
return self._report_mode
41+
42+
def get_read_count(self):
43+
return self._read_count
44+
45+
def get_board(self):
46+
return self._board
47+
3048
def get_version(self):
3149
self._connection.send(_PROTOCOL_CMDS["PROT_VERSION"])
3250
response = self._connection.recv(1)
@@ -56,7 +74,7 @@ def set_report_mode(self, mode, read_count=1, read_delay=0):
5674
raise Exception("Report mode error. Unknown value: %s" % mode)
5775

5876
if read_count <= 0:
59-
raise Exception("Read count value must be >= 0")
77+
raise Exception("Read count value must be > 0")
6078

6179
self._report_mode = mode
6280
self._read_count = read_count - 1

tests/pocs/performance_test.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@ def actuate(terminal):
99
connection = SerialConnection(port=terminal)
1010
arduinoDue = ArduinoInterface(connection, boards.Due)
1111

12+
arduinoDue.reset() #Just in case
13+
arduinoDue.set_report_mode("AVERAGE", read_count=1, read_delay=200)
14+
1215
arduinoDue.add_output(40)
1316
arduinoDue.add_output(66)
14-
arduinoDue.add_input(65)
15-
arduinoDue.add_input(64)
16-
arduinoDue.add_input(63)
17-
arduinoDue.add_input(62)
18-
arduinoDue.add_input(61)
19-
arduinoDue.add_input(60)
20-
arduinoDue.add_input(59)
21-
arduinoDue.add_input(58)
22-
arduinoDue.add_input(57)
23-
arduinoDue.add_input(56)
24-
arduinoDue.add_input(55)
17+
# arduinoDue.add_input(65)
18+
# arduinoDue.add_input(64)
19+
# arduinoDue.add_input(63)
20+
# arduinoDue.add_input(62)
21+
# arduinoDue.add_input(61)
22+
# arduinoDue.add_input(60)
23+
# arduinoDue.add_input(59)
24+
# arduinoDue.add_input(58)
25+
# arduinoDue.add_input(57)
26+
# arduinoDue.add_input(56)
27+
# arduinoDue.add_input(55)
2528
arduinoDue.add_input(54)
2629

2730
last_time = time.time()
2831
start_time = last_time
2932
read_c = 0
30-
31-
for i in range(0, 10000):
33+
34+
for i in xrange(0, 2):
3235
output = arduinoDue.actuate([(40,1),(66,255)])
3336
#print output
3437
read_c = read_c + 1

tests/pocs/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def actuate(terminal):
1010
arduinoDue = ArduinoInterface(connection, boards.Due)
1111

1212
arduinoDue.reset()
13-
arduinoDue.set_report_mode("BULK", read_count=5, read_delay=200)
13+
# arduinoDue.set_report_mode("BULK", read_count=5, read_delay=200)
1414
arduinoDue.add_output(40)
1515
arduinoDue.add_input(64)
1616
arduinoDue.add_input(63)

0 commit comments

Comments
 (0)