-
Notifications
You must be signed in to change notification settings - Fork 516
/
SerialFirmata.cpp
394 lines (367 loc) · 11.4 KB
/
SerialFirmata.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
SerialFirmata.cpp
Copyright (C) 2016 Jeff Hoefs. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
This version of SerialFirmata.cpp differs from the ConfigurableFirmata
version in the following ways:
- handlePinMode calls Firmata::setPinMode
Last updated March 16th, 2020
*/
#include "SerialFirmata.h"
// The RX and TX hardware FIFOs of the ESP8266 hold 128 bytes that can be
// extended using interrupt handlers. The Arduino constants are not available
// for the ESP8266 platform.
#if !defined(SERIAL_RX_BUFFER_SIZE) && defined(UART_TX_FIFO_SIZE)
#define SERIAL_RX_BUFFER_SIZE UART_TX_FIFO_SIZE
#endif
SerialFirmata::SerialFirmata()
{
#if defined(SoftwareSerial_h)
swSerial0 = NULL;
swSerial1 = NULL;
swSerial2 = NULL;
swSerial3 = NULL;
#endif
serialIndex = -1;
#if defined(FIRMATA_SERIAL_RX_DELAY)
for (byte i = 0; i < SERIAL_READ_ARR_LEN; i++) {
maxRxDelay[i] = FIRMATA_SERIAL_RX_DELAY; // @todo provide setter
}
#endif
}
boolean SerialFirmata::handlePinMode(byte pin, int mode)
{
// used for both HW and SW serial
if (mode == PIN_MODE_SERIAL) {
Firmata.setPinMode(pin, PIN_MODE_SERIAL);
return true;
}
return false;
}
void SerialFirmata::handleCapability(byte pin)
{
if (IS_PIN_SERIAL(pin)) {
Firmata.write(PIN_MODE_SERIAL);
Firmata.write(getSerialPinType(pin));
}
}
boolean SerialFirmata::handleSysex(byte command, byte argc, byte *argv)
{
if (command == SERIAL_MESSAGE) {
Stream *serialPort;
byte mode = argv[0] & SERIAL_MODE_MASK;
byte portId = argv[0] & SERIAL_PORT_ID_MASK;
if (portId >= SERIAL_READ_ARR_LEN) return false;
switch (mode) {
case SERIAL_CONFIG:
{
long baud = (long)argv[1] | ((long)argv[2] << 7) | ((long)argv[3] << 14);
serial_pins pins;
#if defined(FIRMATA_SERIAL_RX_DELAY)
lastBytesAvailable[portId] = 0;
lastBytesReceived[portId] = 0;
#endif
if (portId < 8) {
serialPort = getPortFromId(portId);
if (serialPort != NULL) {
pins = getSerialPinNumbers(portId);
if (pins.rx != 0 && pins.tx != 0) {
Firmata.setPinMode(pins.rx, PIN_MODE_SERIAL);
Firmata.setPinMode(pins.tx, PIN_MODE_SERIAL);
// Fixes an issue where some serial devices would not work properly with Arduino Due
// because all Arduino pins are set to OUTPUT by default in StandardFirmata.
pinMode(pins.rx, INPUT);
}
((HardwareSerial*)serialPort)->begin(baud);
}
} else {
#if defined(SoftwareSerial_h)
byte swTxPin, swRxPin;
if (argc > 4) {
swRxPin = argv[4];
swTxPin = argv[5];
} else {
// RX and TX pins must be specified when using SW serial
Firmata.sendString("Specify serial RX and TX pins");
return false;
}
switch (portId) {
case SW_SERIAL0:
if (swSerial0 == NULL) {
swSerial0 = new SoftwareSerial(swRxPin, swTxPin);
}
break;
case SW_SERIAL1:
if (swSerial1 == NULL) {
swSerial1 = new SoftwareSerial(swRxPin, swTxPin);
}
break;
case SW_SERIAL2:
if (swSerial2 == NULL) {
swSerial2 = new SoftwareSerial(swRxPin, swTxPin);
}
break;
case SW_SERIAL3:
if (swSerial3 == NULL) {
swSerial3 = new SoftwareSerial(swRxPin, swTxPin);
}
break;
}
serialPort = getPortFromId(portId);
if (serialPort != NULL) {
Firmata.setPinMode(swRxPin, PIN_MODE_SERIAL);
Firmata.setPinMode(swTxPin, PIN_MODE_SERIAL);
((SoftwareSerial*)serialPort)->begin(baud);
}
#endif
}
break; // SERIAL_CONFIG
}
case SERIAL_WRITE:
{
byte data;
serialPort = getPortFromId(portId);
if (serialPort == NULL) {
break;
}
for (byte i = 1; i < argc; i += 2) {
data = argv[i] + (argv[i + 1] << 7);
serialPort->write(data);
}
break; // SERIAL_WRITE
}
case SERIAL_READ:
if (argv[1] == SERIAL_READ_CONTINUOUSLY) {
if (serialIndex + 1 >= MAX_SERIAL_PORTS) {
break;
}
if (argc > 2) {
// maximum number of bytes to read from buffer per iteration of loop()
serialBytesToRead[portId] = (int)argv[2] | ((int)argv[3] << 7);
} else {
// read all available bytes per iteration of loop()
serialBytesToRead[portId] = 0;
}
serialIndex++;
reportSerial[serialIndex] = portId;
} else if (argv[1] == SERIAL_STOP_READING) {
byte serialIndexToSkip = 0;
if (serialIndex <= 0) {
serialIndex = -1;
} else {
for (byte i = 0; i < serialIndex + 1; i++) {
if (reportSerial[i] == portId) {
serialIndexToSkip = i;
break;
}
}
// shift elements over to fill space left by removed element
for (byte i = serialIndexToSkip; i < serialIndex + 1; i++) {
if (i < MAX_SERIAL_PORTS) {
reportSerial[i] = reportSerial[i + 1];
}
}
serialIndex--;
}
}
break; // SERIAL_READ
case SERIAL_CLOSE:
serialPort = getPortFromId(portId);
if (serialPort != NULL) {
if (portId < 8) {
((HardwareSerial*)serialPort)->end();
} else {
#if defined(SoftwareSerial_h)
((SoftwareSerial*)serialPort)->end();
if (serialPort != NULL) {
free(serialPort);
serialPort = NULL;
}
#endif
}
}
break; // SERIAL_CLOSE
case SERIAL_FLUSH:
serialPort = getPortFromId(portId);
if (serialPort != NULL) {
getPortFromId(portId)->flush();
}
break; // SERIAL_FLUSH
#if defined(SoftwareSerial_h)
case SERIAL_LISTEN:
// can only call listen() on software serial ports
if (portId > 7) {
serialPort = getPortFromId(portId);
if (serialPort != NULL) {
((SoftwareSerial*)serialPort)->listen();
}
}
break; // SERIAL_LISTEN
#endif
} // end switch
return true;
}
return false;
}
void SerialFirmata::update()
{
checkSerial();
}
void SerialFirmata::reset()
{
#if defined(SoftwareSerial_h)
Stream *serialPort;
// free memory allocated for SoftwareSerial ports
for (byte i = SW_SERIAL0; i < SW_SERIAL3 + 1; i++) {
serialPort = getPortFromId(i);
if (serialPort != NULL) {
free(serialPort);
serialPort = NULL;
}
}
#endif
serialIndex = -1;
for (byte i = 0; i < SERIAL_READ_ARR_LEN; i++) {
serialBytesToRead[i] = 0;
#if defined(FIRMATA_SERIAL_RX_DELAY)
lastBytesAvailable[i] = 0;
lastBytesReceived[i] = 0;
#endif
}
}
// get a pointer to the serial port associated with the specified port id
Stream* SerialFirmata::getPortFromId(byte portId)
{
switch (portId) {
case HW_SERIAL0:
// block use of Serial (typically pins 0 and 1) until ability to reclaim Serial is implemented
//return &Serial;
return NULL;
#if defined(PIN_SERIAL1_RX)
case HW_SERIAL1:
return &Serial1;
#endif
#if defined(PIN_SERIAL2_RX)
case HW_SERIAL2:
return &Serial2;
#endif
#if defined(PIN_SERIAL3_RX)
case HW_SERIAL3:
return &Serial3;
#endif
#if defined(PIN_SERIAL4_RX)
case HW_SERIAL4:
return &Serial4;
#endif
#if defined(PIN_SERIAL5_RX)
case HW_SERIAL5:
return &Serial5;
#endif
#if defined(PIN_SERIAL6_RX)
case HW_SERIAL6:
return &Serial6;
#endif
#if defined(SoftwareSerial_h)
case SW_SERIAL0:
if (swSerial0 != NULL) {
// instances of SoftwareSerial are already pointers so simply return the instance
return swSerial0;
}
break;
case SW_SERIAL1:
if (swSerial1 != NULL) {
return swSerial1;
}
break;
case SW_SERIAL2:
if (swSerial2 != NULL) {
return swSerial2;
}
break;
case SW_SERIAL3:
if (swSerial3 != NULL) {
return swSerial3;
}
break;
#endif
}
return NULL;
}
// Check serial ports that have READ_CONTINUOUS mode set and relay any data
// for each port to the device attached to that port.
void SerialFirmata::checkSerial()
{
byte portId, serialData;
int bytesToRead = 0;
int numBytesToRead = 0;
Stream* serialPort;
if (serialIndex > -1) {
#if defined(FIRMATA_SERIAL_RX_DELAY)
unsigned long currentMillis = millis();
#endif
// loop through all reporting (READ_CONTINUOUS) serial ports
for (byte i = 0; i < serialIndex + 1; i++) {
portId = reportSerial[i];
bytesToRead = serialBytesToRead[portId];
serialPort = getPortFromId(portId);
if (serialPort == NULL) {
continue;
}
#if defined(SoftwareSerial_h)
// only the SoftwareSerial port that is "listening" can read data
if (portId > 7 && !((SoftwareSerial*)serialPort)->isListening()) {
continue;
}
#endif
int bytesAvailable = serialPort->available();
if (bytesAvailable > 0) {
#if defined(FIRMATA_SERIAL_RX_DELAY)
if (bytesAvailable > lastBytesAvailable[portId]) {
lastBytesReceived[portId] = currentMillis;
}
lastBytesAvailable[portId] = bytesAvailable;
#endif
if (bytesToRead <= 0 || (bytesAvailable <= bytesToRead)) {
numBytesToRead = bytesAvailable;
} else {
numBytesToRead = bytesToRead;
}
#if defined(FIRMATA_SERIAL_RX_DELAY)
if (maxRxDelay[portId] >= 0 && numBytesToRead > 0) {
// read and send immediately only if
// - expected bytes are unknown and the receive buffer has reached 50 %
// - expected bytes are available
// - maxRxDelay has expired since last receive (or time counter wrap)
if (!((bytesToRead <= 0 && bytesAvailable >= SERIAL_RX_BUFFER_SIZE/2)
|| (bytesToRead > 0 && bytesAvailable >= bytesToRead)
|| (maxRxDelay[portId] > 0 && (currentMillis < lastBytesReceived[portId] || (currentMillis - lastBytesReceived[portId]) >= maxRxDelay[portId])))) {
// delay
numBytesToRead = 0;
}
}
#endif
// relay serial data to the serial device
if (numBytesToRead > 0) {
#if defined(FIRMATA_SERIAL_RX_DELAY)
lastBytesAvailable[portId] -= numBytesToRead;
#endif
Firmata.write(START_SYSEX);
Firmata.write(SERIAL_MESSAGE);
Firmata.write(SERIAL_REPLY | portId);
// relay serial data to the serial device
while (numBytesToRead > 0) {
serialData = serialPort->read();
Firmata.write(serialData & 0x7F);
Firmata.write((serialData >> 7) & 0x7F);
numBytesToRead--;
}
Firmata.write(END_SYSEX);
}
}
}
}
}