diff --git a/README.md b/README.md index 00200fa..5ccaf0d 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,20 @@ bfs::SbusRx sbus(&Serial1, false); **(ESP32 ONLY) SbusRx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin, const bool inv, const bool fast)** Same as the constructor above, but enables selecting the fast SBUS baudrate (200000) if *fast* is true. +**SbusRx(AltSoftSerial *bus)** Creates an *SbusRx* object. A pointer to the *AltSoftSerial* object corresponding to the software serial port used is passed. The RX pin of the software serial port will receive SBUS packets. + +```C++ +bfs::SbusRx sbus(&altSerial); +``` + +**SbusRx(AltSoftSerial *bus, const bool inv)** Creates an *SbusRx* object. A pointer to the *AltSoftSerial* object corresponding to the software serial port used is passed along with a second parameter, *inv*, which sets whether inverted serial is used. If *inv* is true, the signal is the standard inverted SBUS, otherwise it is non-inverted SBUS. + +```C++ +bfs::SbusRx sbus(&altSerial, false); +``` + +**SbusRx(AltSoftSerial *bus, const bool inv, const bool fast)** Same as the constructor above, but enables selecting the fast SBUS baudrate (200000) if *fast* is true. + **void Begin()** Initializes SBUS communication. ```C++ @@ -157,8 +171,24 @@ bfs::SbusTx sbus(&Serial1); bfs::SbusTx sbus(&Serial1, false); ``` +**SbusTx(HardwareSerial *bus, const bool inv, const bool fast)** Same as the constructor above, but enables selecting the fast SBUS baudrate (200000) if *fast* is true. + **(ESP32 ONLY) SbusTx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin, const bool inv)** Creates an *SbusTx* object. A pointer to the *Serial* object corresponding to the serial port used is passed along with the RX pin number (*rxpin*), TX pin number (*txpin*), and whether inverted serial is used (*inv*). If *inv* is true, the signal is the standard inverted SBUS, otherwise it is non-inverted SBUS. +**SbusTx(AltSoftSerial *bus)** Creates an *SbusTx* object. A pointer to the *AltSoftSerial* object corresponding to the software serial port used is passed. The TX pin of the software serial port will receive SBUS packets. + +```C++ +bfs::SbusTx sbus(&altSerial); +``` + +**SbusTx(AltSoftSerial *bus, const bool inv)** Creates an *SbusTx* object. A pointer to the *AltSoftSerial* object corresponding to the software serial port used is passed along with a second parameter, *inv*, which sets whether inverted serial is used. If *inv* is true, the signal is the standard inverted SBUS, otherwise it is non-inverted SBUS. + +```C++ +bfs::SbusTx sbus(&altSerial, false); +``` + +**SbusTx(AltSoftSerial *bus, const bool inv, const bool fast)** Same as the constructor above, but enables selecting the fast SBUS baudrate (200000) if *fast* is true. + **void Begin()** Initializes SBUS communication. ```C++ diff --git a/examples/arduino/sbus_example/sbus_example.ino b/examples/arduino/sbus_example/sbus_example.ino index e37b8e8..f6c9cc1 100644 --- a/examples/arduino/sbus_example/sbus_example.ino +++ b/examples/arduino/sbus_example/sbus_example.ino @@ -23,12 +23,16 @@ * IN THE SOFTWARE. */ +#include #include "sbus.h" +/* AltSoftSerial object for SBUS communication */ +AltSoftSerial altSerial; + /* SBUS object, reading SBUS */ -bfs::SbusRx sbus_rx(&Serial2); +bfs::SbusRx sbus_rx(&altSerial); /* SBUS object, writing SBUS */ -bfs::SbusTx sbus_tx(&Serial2); +bfs::SbusTx sbus_tx(&altSerial); /* SBUS data */ bfs::SbusData data; diff --git a/src/sbus.cpp b/src/sbus.cpp index b019402..711193c 100644 --- a/src/sbus.cpp +++ b/src/sbus.cpp @@ -26,6 +26,7 @@ #include "sbus.h" // NOLINT #if defined(ARDUINO) #include +#include #else #include #include @@ -74,10 +75,18 @@ void SbusRx::Begin() { uart_->begin(baud_, SERIAL_8E2, rxpin_, txpin_, inv_); /* Everything else, with a hardware inverter */ #else - uart_->begin(baud_, SERIAL_8E2); + if (uart_ != nullptr) { + uart_->begin(baud_, SERIAL_8E2); + } else if (soft_uart_ != nullptr) { + soft_uart_->begin(baud_); + } #endif /* flush the bus */ - uart_->flush(); + if (uart_ != nullptr) { + uart_->flush(); + } else if (soft_uart_ != nullptr) { + soft_uart_->flush(); + } } bool SbusRx::Read() { @@ -87,14 +96,18 @@ bool SbusRx::Read() { if (Parse()) { new_data_ = true; } - } while (uart_->available()); + } while ((uart_ != nullptr && uart_->available()) || (soft_uart_ != nullptr && soft_uart_->available())); return new_data_; } bool SbusRx::Parse() { /* Parse messages */ - while (uart_->available()) { - cur_byte_ = uart_->read(); + while ((uart_ != nullptr && uart_->available()) || (soft_uart_ != nullptr && soft_uart_->available())) { + if (uart_ != nullptr) { + cur_byte_ = uart_->read(); + } else if (soft_uart_ != nullptr) { + cur_byte_ = soft_uart_->read(); + } if (state_ == 0) { if ((cur_byte_ == HEADER_) && ((prev_byte_ == FOOTER_) || ((prev_byte_ & 0x0F) == FOOTER2_))) { @@ -225,7 +238,11 @@ void SbusTx::Begin() { uart_->begin(baud_, SERIAL_8E2, rxpin_, txpin_, inv_); /* Everything else, with a hardware inverter */ #else - uart_->begin(baud_, SERIAL_8E2); + if (uart_ != nullptr) { + uart_->begin(baud_, SERIAL_8E2); + } else if (soft_uart_ != nullptr) { + soft_uart_->begin(baud_); + } #endif } @@ -284,7 +301,11 @@ void SbusTx::Write() { serial_timer.priority(255); serial_timer.begin(SendByte, 130); #else - uart_->write(buf_, sizeof(buf_)); + if (uart_ != nullptr) { + uart_->write(buf_, sizeof(buf_)); + } else if (soft_uart_ != nullptr) { + soft_uart_->write(buf_, sizeof(buf_)); + } #endif } diff --git a/src/sbus.h b/src/sbus.h index 74a2d7a..67081ea 100644 --- a/src/sbus.h +++ b/src/sbus.h @@ -28,6 +28,7 @@ #if defined(ARDUINO) #include +#include #else #include #include @@ -59,6 +60,11 @@ class SbusRx { SbusRx(HardwareSerial *bus, const bool inv, const bool fast) : uart_(bus), inv_(inv), fast_(fast) {} + explicit SbusRx(AltSoftSerial *bus) : soft_uart_(bus) {} + SbusRx(AltSoftSerial *bus, const bool inv) : soft_uart_(bus), inv_(inv) {} + SbusRx(AltSoftSerial *bus, const bool inv, const bool fast) : soft_uart_(bus), + inv_(inv), + fast_(fast) {} #endif void Begin(); bool Read(); @@ -66,7 +72,8 @@ class SbusRx { private: /* Communication */ - HardwareSerial *uart_; + HardwareSerial *uart_ = nullptr; + AltSoftSerial *soft_uart_ = nullptr; bool inv_ = true; bool fast_ = false; #if defined(ESP32) @@ -113,6 +120,11 @@ class SbusTx { SbusTx(HardwareSerial *bus, const bool inv, const bool fast) : uart_(bus), inv_(inv), fast_(fast) {} + explicit SbusTx(AltSoftSerial *bus) : soft_uart_(bus) {} + SbusTx(AltSoftSerial *bus, const bool inv) : soft_uart_(bus), inv_(inv) {} + SbusTx(AltSoftSerial *bus, const bool inv, const bool fast) : soft_uart_(bus), + inv_(inv), + fast_(fast) {} #endif void Begin(); void Write(); @@ -121,7 +133,8 @@ class SbusTx { private: /* Communication */ - HardwareSerial *uart_; + HardwareSerial *uart_ = nullptr; + AltSoftSerial *soft_uart_ = nullptr; bool inv_ = true; bool fast_ = false; #if defined(ESP32)