@@ -14,12 +14,12 @@ const uint8_t SET_REPORT_MODE_CMD = 0x05;
1414const uint8_t ACTUATE_CMD = 0xF0 ;
1515
1616// Commands executor caller -- one vector position == one command
17- void (*executor[255 ])(const char *);
17+ int (*executor[255 ])(const char *);
1818
19- void not_implemented (const char * data)
19+ int not_implemented (const char * data)
2020{
2121 // nothing to do...
22- return ;
22+ return 0 ;
2323}
2424
2525typedef enum ReportModes {average, bulk, rt};
@@ -39,7 +39,7 @@ const char* ACK = "\xFF\x00";
3939/* *
4040 ANALOG_PRECISION: 0x01 0x01 [BITS]
4141*/
42- void set_analog_precision (const char * data)
42+ int set_analog_precision (const char * data)
4343{
4444 int i = byte (data[2 ]);
4545 // Tal vez conviene separar estas funciones ya que hay boards con resoluciones distintas...
@@ -48,55 +48,67 @@ void set_analog_precision(const char* data)
4848 analogReadResolution (i);
4949
5050 LOG (" Resolution changed to: " , i);
51+
52+ return 3 ; // Command of 3 bytes
5153}
5254
5355/* *
5456 PIN_MODE: 0x04 0x02 [PIN] [MODE]
5557*/
56- void set_pin_mode (const char * data)
58+ int set_pin_mode (const char * data)
5759{
5860 pinMode (data[2 ], data[3 ]);
5961 LOG (" Changed pin mode on pin " , uint8_t (data[2 ]));
6062 LOG (" Mode set to " , uint8_t (data[3 ]));
63+
64+ return 4 ; // Command of 4 bytes
6165}
6266
6367/* *
6468 REPORT_MODE: 0x05 0x03 [MODE] [READ_COUNT] [READ_DELAY]
6569*/
66- void set_report_mode (const char * data)
70+ int set_report_mode (const char * data)
6771{
6872 REPORT_MODE = (ReportModes)(data[2 ]);
6973 REPORT_READ_COUNT = byte (data[3 ]);
7074 REPORT_READ_DELAY = byte (data[4 ]);
7175 LOG (" Report mode changed on port " , byte (data[3 ]));
76+
77+ return 5 ; // Command of 5 bytes
7278}
7379
7480/* *
7581 ANALOG_INPUT: 0x02 0x01 [PORT]
7682*/
77- void set_analog_input (const char * data)
83+ int set_analog_input (const char * data)
7884{
7985 INPUT_PORTS[0 ] += 1 ;
8086 INPUT_PORTS[INPUT_PORTS[0 ]] = byte (data[2 ]);
8187 LOG (" New analog input: " , byte (data[2 ]));
88+
89+ return 3 ; // Command of 3 bytes
8290}
8391
8492/* *
8593 * ANALOG_OUTPUT: 0x03 0x01 [PORT]
8694 */
87- void set_analog_output (const char * data)
95+ int set_analog_output (const char * data)
8896{
8997 // No se si vale la pena guardar registro de pines de salida...
98+
99+ return 3 ; // Command of 3 bytes
90100}
91101
92102/* *
93- * ACTUATE: 0xF0 [PIN_COUNT ] [PIN_A] [VALUE_PIN_A] ... [PIN_N] [VALUE_PIN_N]
103+ * ACTUATE: 0xF0 [DATA_LEN ] [PIN_A] [VALUE_PIN_A] ... [PIN_N] [VALUE_PIN_N]
94104 */
95- void actuate (const char * data)
105+ int actuate (const char * data)
96106{
97107 int offset = 0 ;
98108 int byte_count = byte (data[1 ]); // Un byte puede llegar a limitar la cantidad de salidas... creo
99109
110+ LOG (" Actutating over payload of size: " , byte_count);
111+
100112 // ACTUATION ZONE
101113 while (offset < byte_count)
102114 {
@@ -130,7 +142,7 @@ void actuate(const char* data)
130142 {
131143 if ( INPUT_PORTS[i] >= A0 )
132144 {
133- int data = analogRead (INPUT_PORTS[i]);
145+ int data = analogRead (INPUT_PORTS[i]-A0 );
134146 response[len + 1 ] = byte ((data & 0xFF00 ) >> 8 ); // Se guarda el msb en el buffer
135147 response[len + 2 ] = byte (data & 0xFF ); // Se guarda el lsb en el buffer
136148
@@ -148,13 +160,15 @@ void actuate(const char* data)
148160
149161 response[1 ] = len - 1 ;
150162 SerialUSB.write (response, len + 2 ); // 2 bytes extras por el id de comando y la longitud
163+
164+ return offset + 2 ;
151165}
152166
153167void setup () {
154168 SerialUSB.begin (115200 );
155169
156170 #ifdef DEBUG
157- Serial.begin (57600 );
171+ Serial.begin (115200 );
158172 #endif
159173
160174 for (int i = 0 ; i < 255 ; i++)
@@ -172,29 +186,30 @@ void setup() {
172186 executor[SET_REPORT_MODE_CMD] = &set_report_mode;
173187 executor[ACTUATE_CMD] = &actuate;
174188
175- executor[ANALOG_PRECISION_CMD](" \x01\x01\x12 " );
176- executor[ADD_INPUT_PORT_CMD](" \x02\x01\x37 " ); // Configura a A1 como lectura
177- executor[ADD_INPUT_PORT_CMD](" \x02\x01\x38 " ); // Configura a A2 como lectura
178- executor[SET_PIN_MODE_CMD](" \x04\x02\x28\x01 " );
179- executor[SET_PIN_MODE_CMD](" \x04\x02\x55\x00 " );
180-
181189}
182190
183191void loop () {
184- LOG (" Escribiendo..." , " " );
185- executor[ACTUATE_CMD](" \xF0\x01\x28\x01 " );
192+ // executor[ACTUATE_CMD]("\xF0\x01\x28\x01");
186193
187194 if (SerialUSB.available () > 0 )
188195 {
196+ LOG (" USB serial data available " , SerialUSB.available ());
189197 char input[64 ]; // Esto se reserva en el stack, tal vez hacerlo global consume menos recursos...
190-
198+
199+ byte b_read = 0 ;
200+ byte b_pos = 0 ;
201+
191202 while (SerialUSB.available () > 0 )
192203 {
193- SerialUSB.readBytes (input, SerialUSB.available ());
204+ b_read += SerialUSB.readBytes (input, SerialUSB.available ());
205+ }
206+
207+ // Loop to process all commands received in the buffer
208+ while (b_pos < b_read)
209+ {
210+ LOG (" Executing command: " , int (input[b_pos]));
211+ b_pos += executor[input[b_pos]](&input[b_pos]); // Does the callback for the command
212+ LOG (" b_pos " , b_pos);
194213 }
195-
196- executor[input[0 ]](input); // Does the callback for the command
197214 }
198-
199- delay (5000 );
200215}
0 commit comments