Skip to content

Commit

Permalink
Moved the HardwareSerial bus to the begin method
Browse files Browse the repository at this point in the history
  • Loading branch information
flybrianfly committed May 1, 2021
1 parent 3c729ae commit 09b2602
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v3.0.0
- Moved the HardwareSerial bus to the *Begin* method

## v2.1.1
- Added a check for lost-link / failsafe in SbusRx.Begin()

Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (DEFINED MCU)
endif()
# Project information
project(Sbus
VERSION 2.1.1
VERSION 3.0.0
DESCRIPTION "SBUS encoder and decoder"
LANGUAGES C CXX
)
Expand All @@ -18,7 +18,7 @@ if (DEFINED MCU)
FetchContent_Declare(
core
GIT_REPOSITORY https://github.com/bolderflight/core.git
GIT_TAG v2.0.4
GIT_TAG v2.0.5
)
FetchContent_MakeAvailable(core)
# Add the library target
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ This library is within the namespace *bfs*

# SbusRx

**SbusRx(HardwareSerial *bus)** Creates an *SbusRx* object. A pointer to the Serial object corresponding to the serial port used is passed. The RX pin of the serial port will receive SBUS packets.
**SbusRx()** Creates an *SbusRx* object.

```C++
bfs::SbusRx sbus(&Serial1);
bfs::SbusRx sbus;
```

**bool Begin()** Initializes SBUS communication, returns true if an SBUS packet is received within the timeout (5 seconds) otherwise returns false.
**bool Begin(HardwareSerial *bus)** Initializes SBUS communication. A pointer to the Serial object corresponding to the serial port used is passed. The RX pin of the serial port will receive SBUS packets. Returns true if an SBUS packet is received within the timeout (5 seconds) otherwise returns false.

```C++
sbus.Begin();
sbus.Begin(&Serial1);
```

**bool Read()** Parses SBUS packets, returns true on successfully receiving an SBUS packet.
Expand Down Expand Up @@ -109,16 +109,16 @@ bool failsafe = sbus.failsafe();

## SbusTx

**SbusTx(HardwareSerial *bus)** Creates an *SbusTx* object. A pointer to the Serial object corresponding to the serial port used is passed. The TX pin of the serial port will transmit SBUS packets.

**SbusTx()** Creates an *SbusTx* object.
```C++
bfs::SbusTx sbus(&Serial1);
bfs::SbusTx sbus();
```

**void Begin()** Initializes SBUS communication.
**void Begin(HardwareSerial *bus)** Initializes SBUS communication. A pointer to the Serial object corresponding to the serial port used is passed. The TX pin of the serial port will transmit SBUS packets.


```C++
sbus.Begin();
sbus.Begin(&Serial1);
```

**void Write()** Writes an SBUS packet. The packet is written immediately, you should regulate timing of sending packets to servos to maintain a frequency of approximately 100 Hz.
Expand Down
23 changes: 15 additions & 8 deletions cmake/config_mcu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ macro (configMcu MCU)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=c11>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-felide-constructors>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=narrowing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
-g
-Os
-Wno-psabi
Expand Down Expand Up @@ -68,11 +69,12 @@ macro (configMcu MCU)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=c11>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-felide-constructors>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=narrowing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
-g
-Os
-Wno-psabi
Expand Down Expand Up @@ -117,11 +119,12 @@ macro (configMcu MCU)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=c11>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-felide-constructors>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=narrowing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
-g
-Os
-Wno-psabi
Expand Down Expand Up @@ -170,13 +173,14 @@ macro (configMcu MCU)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=c11>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-felide-constructors>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=narrowing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
-g
-Os
-Os
-Wno-psabi
-mthumb
-ffunction-sections
Expand Down Expand Up @@ -223,11 +227,12 @@ macro (configMcu MCU)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=c11>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-felide-constructors>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=narrowing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
-g
-Os
-Wno-psabi
Expand Down Expand Up @@ -273,11 +278,12 @@ macro (configMcu MCU)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=gnu11>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-felide-constructors>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=narrowing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
-g
-Os
-Wno-psabi
Expand Down Expand Up @@ -327,11 +333,12 @@ macro (configMcu MCU)
# Compile options
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=gnu11>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++14>
$<$<COMPILE_LANGUAGE:CXX>:-std=c++20>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-felide-constructors>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-error=narrowing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
-g
-Os
-Wno-psabi
Expand Down
8 changes: 4 additions & 4 deletions examples/sbus_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
#include "sbus/sbus.h"

/* SBUS object, reading SBUS */
bfs::SbusRx sbus_rx(&Serial2);
bfs::SbusRx sbus_rx;
/* SBUS object, writing SBUS */
bfs::SbusTx sbus_tx(&Serial2);
bfs::SbusTx sbus_tx;

int main() {
/* Serial to display data */
Serial.begin(115200);
while(!Serial) {}
/* Begin communicating on SBUS serial */
if (!sbus_rx.Begin()) {
if (!sbus_rx.Begin(&Serial2)) {
Serial.println("Unable to establish communication with SBUS receiver");
while (1) {}
}
sbus_tx.Begin();
sbus_tx.Begin(&Serial2);
while(1) {
/* Check if SBUS packet received */
if (sbus_rx.Read()) {
Expand Down
6 changes: 2 additions & 4 deletions include/sbus/sbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ namespace bfs {

class SbusRx {
public:
explicit SbusRx(HardwareSerial *bus) : bus_(bus) {}
bool Begin();
bool Begin(HardwareSerial *bus);
bool Read();
inline std::array<uint16_t, 16> rx_channels() const {return ch_;}
inline bool failsafe() const {return failsafe_;}
Expand Down Expand Up @@ -67,8 +66,7 @@ class SbusRx {

class SbusTx {
public:
explicit SbusTx(HardwareSerial *bus) : bus_(bus) {}
void Begin();
void Begin(HardwareSerial *bus);
void Write();
inline void failsafe(const bool val) {failsafe_ = val;}
inline void lost_frame(const bool val) {lost_frame_ = val;}
Expand Down
6 changes: 4 additions & 2 deletions src/sbus/sbus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

namespace bfs {

bool SbusRx::Begin() {
bool SbusRx::Begin(HardwareSerial *bus) {
bus_ = bus;
state_ = 0;
prev_byte_ = FOOTER_;
#if defined(__MK20DX128__) || defined(__MK20DX256__)
Expand Down Expand Up @@ -137,7 +138,8 @@ namespace {
} // namespace
#endif

void SbusTx::Begin() {
void SbusTx::Begin(HardwareSerial *bus) {
bus_ = bus;
#if defined(__MK20DX128__) || defined(__MK20DX256__)
bus_->begin(BAUD_, SERIAL_8E1_RXINV_TXINV);
#else
Expand Down

0 comments on commit 09b2602

Please sign in to comment.