Skip to content

Commit 755b1db

Browse files
authored
Merge pull request #240 from facchinm/serial_dual_constructor
Allow UART constructors in both PinName and pin number
2 parents e5ac501 + dc43790 commit 755b1db

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cores/arduino/Serial.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ struct _mbed_serial {
3838
mbed::UnbufferedSerial* obj;
3939
};
4040

41+
UART::UART(int tx, int rx, int rts, int cts) {
42+
_tx = digitalPinToPinName(tx);
43+
_rx = digitalPinToPinName(rx);
44+
if (rts >= 0) {
45+
_rts = digitalPinToPinName(rts);
46+
} else {
47+
_rts = NC;
48+
}
49+
if (cts >= 0) {
50+
_cts = digitalPinToPinName(cts);
51+
} else {
52+
_cts = NC;
53+
}
54+
}
55+
4156
void UART::begin(unsigned long baudrate, uint16_t config) {
4257

4358
#if defined(SERIAL_CDC)

cores/arduino/Serial.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ namespace arduino {
3838

3939
class UART : public HardwareSerial {
4040
public:
41-
UART(int tx, int rx, int rts, int cts) : _tx((PinName)tx), _rx((PinName)rx), _rts((PinName)rts), _cts((PinName)cts) {}
41+
UART(int tx, int rx, int rts = -1, int cts = -1);
42+
UART(PinName tx, PinName rx, PinName rts = NC, PinName cts = NC) : _tx(tx), _rx(rx), _rts(rts), _cts(cts) {}
4243
UART() {
4344
is_usb = true;
4445
}

0 commit comments

Comments
 (0)