Skip to content

Commit

Permalink
Switched the SbusRx Begin method to a bool to indicate whether a pack…
Browse files Browse the repository at this point in the history
…et was received within the timeout.
  • Loading branch information
flybrianfly committed Mar 11, 2021
1 parent 004139f commit 95a1176
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v2.1.0
- Added a timeout to the SbusRx.Begin() method to indicate whether a packet was received

## v2.0.0
- Updated to namespace *bfs*
- Updated sensors::Sbus to SbusRx and actuators::Sbus to SbusTx
Expand Down
2 changes: 1 addition & 1 deletion 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 1.1.6
VERSION 2.0.1
DESCRIPTION "SBUS encoder and decoder"
LANGUAGES C CXX
)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ This library is within the namespace *bfs*
bfs::SbusRx sbus(&Serial1);
```
**void Begin()** Initializes SBUS communication.
**bool Begin()** Initializes SBUS communication, returns true if an SBUS packet is received within the timeout (5 seconds) otherwise returns false.
```C++
sbus.Begin();
Expand Down
9 changes: 6 additions & 3 deletions examples/sbus_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
#include "sbus/sbus.h"

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

int main() {
/* Serial to display data */
Serial.begin(115200);
while(!Serial) {}
/* Begin communicating on SBUS serial */
sbus_rx.Begin();
if (!sbus_rx.Begin()) {
Serial.println("Unable to establish communication with SBUS receiver");
while (1) {}
}
sbus_tx.Begin();
while(1) {
/* Check if SBUS packet received */
Expand Down
3 changes: 2 additions & 1 deletion include/sbus/sbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace bfs {
class SbusRx {
public:
explicit SbusRx(HardwareSerial *bus) : bus_(bus) {}
void Begin();
bool Begin();
bool Read();
inline std::array<uint16_t, 16> rx_channels() const {return ch_;}
inline bool failsafe() const {return failsafe_;}
Expand All @@ -46,6 +46,7 @@ class SbusRx {
/* Communication */
HardwareSerial *bus_;
static constexpr uint32_t BAUD_ = 100000;
static constexpr unsigned int TIMEOUT_MS_ = 5000;
/* Parsing */
static constexpr uint8_t HEADER_ = 0x0F;
static constexpr uint8_t FOOTER_ = 0x00;
Expand Down
10 changes: 9 additions & 1 deletion src/sbus/sbus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace bfs {

void SbusRx::Begin() {
bool SbusRx::Begin() {
state_ = 0;
prev_byte_ = FOOTER_;
#if defined(__MK20DX128__) || defined(__MK20DX256__)
Expand All @@ -37,6 +37,14 @@ void SbusRx::Begin() {
#endif
/* flush the bus */
bus_->flush();
/* check communication */
elapsedMillis timer_ms = 0;
while (timer_ms < TIMEOUT_MS_) {
if (Read()) {
return true;
}
}
return false;
}
bool SbusRx::Read() {
bool status = false;
Expand Down

0 comments on commit 95a1176

Please sign in to comment.