Skip to content

Commit

Permalink
Moved ESP32 pin specification from constructor to Begin()
Browse files Browse the repository at this point in the history
  • Loading branch information
simondlevy committed Dec 3, 2021
1 parent 3d3736e commit e81a8e2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/SBUS_example/SBUS_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void setup() {
void loop() {
if (sbus_rx.Read()) {
/* Display the received data */
for (int i = 0; i < sbus_rx.rx_channels().size(); i++) {
for (uint8_t i = 0; i < sbus_rx.rx_channels().size(); i++) {
Serial.print(sbus_rx.rx_channels()[i]);
Serial.print("\t");
}
Expand Down
28 changes: 10 additions & 18 deletions src/sbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@
#include "Arduino.h"
#include "sbus.h"

#ifdef ESP32
SbusRx::SbusRx(HardwareSerial *bus, uint8_t rxpin, uint8_t txpin) {
bus_ = bus;
rxpin_ = rxpin;
txpin_ = txpin;
}
#else
SbusRx::SbusRx(HardwareSerial *bus) {
bus_ = bus;
}
#endif

#ifdef ESP32
void SbusRx::Begin(uint8_t rxpin, uint8_t txpin) {
#else
void SbusRx::Begin() {
#endif
parser_state_ = 0;
previous_byte_ = SBUS_FOOTER_;
/* Teensy 3.0 || Teensy 3.1/3.2 */
Expand All @@ -55,7 +51,7 @@ void SbusRx::Begin() {
bus_->begin(BAUD_, SERIAL_SBUS);
/* ESP32 */
#elif defined(ESP32)
bus_->begin(BAUD_, SERIAL_8E2, rxpin_, txpin_, true);
bus_->begin(BAUD_, SERIAL_8E2, rxpin, txpin, true);
/* Everything else, with a hardware inverter */
#else
bus_->begin(BAUD_, SERIAL_8E2);
Expand Down Expand Up @@ -149,19 +145,15 @@ namespace {
} // namespace
#endif

#ifdef ESP32
SbusTx::SbusTx(HardwareSerial *bus, uint8_t rxpin, uint8_t txpin) {
bus_ = bus;
rxpin_ = rxpin;
txpin_ = txpin;
}
#else
SbusTx::SbusTx(HardwareSerial *bus) {
bus_ = bus;
}
#endif

#ifdef ESP32
void SbusTx::Begin(uint8_t rxpin, uint8_t txpin) {
#else
void SbusTx::Begin() {
#endif
/* Teensy 3.0 || Teensy 3.1/3.2 */
#if defined(__MK20DX128__) || defined(__MK20DX256__)
bus_->begin(BAUD_, SERIAL_8E1_RXINV_TXINV);
Expand All @@ -176,7 +168,7 @@ void SbusTx::Begin() {
bus_->begin(BAUD_, SERIAL_SBUS);
/* ESP32 */
#elif defined(ESP32)
bus_->begin(BAUD_, SERIAL_8E2, rxpin_, txpin_, true);
bus_->begin(BAUD_, SERIAL_8E2, rxpin, txpin, true);
/* Everything else, with a hardware inverter */
#else
bus_->begin(BAUD_, SERIAL_8E2);
Expand Down
24 changes: 8 additions & 16 deletions src/sbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ namespace sbus = std;
class SbusRx {
public:

explicit SbusRx(HardwareSerial *bus);

#ifdef ESP32
explicit SbusRx(HardwareSerial *bus, uint8_t rxpin, uint8_t txpin); // supports ESP32
void Begin(uint8_t rxpin, uint8_t txpin);
#else
explicit SbusRx(HardwareSerial *bus);
void Begin();
#endif

void Begin();
bool Read();
sbus::array<uint16_t, 16> rx_channels();
inline bool failsafe() const {return failsafe_;}
Expand All @@ -61,11 +62,6 @@ class SbusRx {
/* Communication */
HardwareSerial *bus_;

#ifdef ESP32
uint8_t rxpin_ = 0;
uint8_t txpin_ = 0;
#endif

static constexpr uint32_t BAUD_ = 100000;
/* Parsing */
static constexpr uint8_t SBUS_HEADER_ = 0x0F;
Expand All @@ -88,13 +84,14 @@ class SbusRx {
class SbusTx {
public:

explicit SbusTx(HardwareSerial *bus);

#ifdef ESP32
explicit SbusTx(HardwareSerial *bus, uint8_t rxpin, uint8_t txpin);
void Begin(uint8_t rxpin, uint8_t txpin);
#else
explicit SbusTx(HardwareSerial *bus);
void Begin();
#endif

void Begin();
void Write();
void failsafe(bool val) {failsafe_ = val;}
void lost_frame(bool val) {lost_frame_ = val;}
Expand All @@ -111,11 +108,6 @@ class SbusTx {
/* Communication */
HardwareSerial *bus_;

#ifdef ESP32
uint8_t rxpin_ = 0;
uint8_t txpin_ = 0;
#endif

static constexpr uint32_t BAUD_ = 100000;
/* Parsing */
static constexpr uint8_t SBUS_HEADER_ = 0x0F;
Expand Down

0 comments on commit e81a8e2

Please sign in to comment.