Skip to content

Commit e2d39d2

Browse files
facchinmpolldo
authored andcommitted
Experimental: USBSerial: allow multiple callbacks on RX
1 parent 5443b47 commit e2d39d2

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

cores/arduino/USB/PluggableUSBSerial.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
* @endcode
4545
*/
4646

47+
#define MAX_CALLBACKS_ON_IRQ 4
48+
4749
namespace arduino {
4850

4951

@@ -155,7 +157,7 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
155157
USBCDC::lock();
156158

157159
if ((mptr != NULL) && (tptr != NULL)) {
158-
rx = ::mbed::Callback<void()>(mptr, tptr);
160+
rx[howManyCallbacks++] = ::mbed::Callback<void()>(mptr, tptr);
159161
}
160162

161163
USBCDC::unlock();
@@ -171,7 +173,7 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
171173
USBCDC::lock();
172174

173175
if (fptr != NULL) {
174-
rx = ::mbed::Callback<void()>(fptr);
176+
rx[howManyCallbacks++] = ::mbed::Callback<void()>(fptr);
175177
}
176178

177179
USBCDC::unlock();
@@ -186,7 +188,7 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
186188
{
187189
USBCDC::lock();
188190

189-
rx = cb;
191+
rx[howManyCallbacks++] = cb;
190192

191193
USBCDC::unlock();
192194
}
@@ -270,7 +272,8 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
270272
}
271273

272274
private:
273-
::mbed::Callback<void()> rx;
275+
::mbed::Callback<void()> rx[MAX_CALLBACKS_ON_IRQ];
276+
int howManyCallbacks = 0;
274277
void (*_settings_changed_callback)(int baud, int bits, int parity, int stop);
275278
};
276279
}

cores/arduino/USB/USBSerial.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ void USBSerial::data_rx()
9494
USBCDC::assert_locked();
9595

9696
//call a potential handler
97-
if (rx) {
98-
rx.call();
97+
int i = 0;
98+
while (i < howManyCallbacks) {
99+
if (rx[i]) {
100+
rx[i].call();
101+
} else {
102+
break;
103+
}
104+
i++;
99105
}
100106
}
101107

0 commit comments

Comments
 (0)