Skip to content

Commit

Permalink
Add SoftwareSerial support for SBUS communication
Browse files Browse the repository at this point in the history
Related to bolderflight#69

Add support for `AltSoftSerial` for SBUS communication.

* **src/sbus.h**
  - Include the `AltSoftSerial` library.
  - Add constructors for `SbusRx` and `SbusTx` that accept `AltSoftSerial`.
  - Modify `SbusRx` and `SbusTx` classes to use `AltSoftSerial`.

* **src/sbus.cpp**
  - Include the `AltSoftSerial` library.
  - Modify the `Begin` method in `SbusRx` and `SbusTx` to initialize `AltSoftSerial`.
  - Modify the `Read` and `Write` methods in `SbusRx` and `SbusTx` to use `AltSoftSerial`.

* **examples/arduino/sbus_example/sbus_example.ino**
  - Include the `AltSoftSerial` library.
  - Modify the example to use `AltSoftSerial` for SBUS communication.

* **README.md**
  - Update instructions to include the use of `AltSoftSerial` for `SoftwareSerial` support.
  • Loading branch information
vishwamartur committed Dec 13, 2024
1 parent 003c5c3 commit 051821a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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++
Expand Down
8 changes: 6 additions & 2 deletions examples/arduino/sbus_example/sbus_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
* IN THE SOFTWARE.
*/

#include <AltSoftSerial.h>
#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;

Expand Down
35 changes: 28 additions & 7 deletions src/sbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "sbus.h" // NOLINT
#if defined(ARDUINO)
#include <Arduino.h>
#include <AltSoftSerial.h>
#else
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -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() {
Expand All @@ -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_))) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 15 additions & 2 deletions src/sbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#if defined(ARDUINO)
#include <Arduino.h>
#include <AltSoftSerial.h>
#else
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -59,14 +60,20 @@ 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();
inline SbusData data() const {return data_;}

private:
/* Communication */
HardwareSerial *uart_;
HardwareSerial *uart_ = nullptr;
AltSoftSerial *soft_uart_ = nullptr;
bool inv_ = true;
bool fast_ = false;
#if defined(ESP32)
Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand Down

0 comments on commit 051821a

Please sign in to comment.