Skip to content

Commit d7b473c

Browse files
committed
Merging CMake and Arduino SBUS libraries. Version 5 is the next available version number common between the two.
1 parent b9899dd commit d7b473c

30 files changed

+1692
-208
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
.vscode/*
1+
# ignore the build directory
2+
build/
3+
# ignore vs code
4+
*.vscode

.gitlab-ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
stages:
2+
- lint
3+
4+
Lint:
5+
stage: lint
6+
tags:
7+
- bfs
8+
script:
9+
- cpplint --verbose=0 src/sbus.cpp
10+
- cpplint --verbose=0 src/sbus.h
11+

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v5.0.0
4+
- Merging CMake and Arduino SBUS libraries. Version 5 is the next available version number common between the two.
5+
36
## v2.1.2
47
- Added Embedded Template Library support for AVR boards.
58

CMakeLists.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
if (DEFINED MCU)
3+
# Setting up the toolchain
4+
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/cmake/cortex.cmake")
5+
endif()
6+
# Project information
7+
project(Sbus
8+
VERSION 5.0.0
9+
DESCRIPTION "SBUS encoder and decoder"
10+
LANGUAGES C CXX
11+
)
12+
if (DEFINED MCU)
13+
# Grab the processor and set up definitions and compile options
14+
include(${CMAKE_SOURCE_DIR}/cmake/config_mcu.cmake)
15+
configMcu(${MCU})
16+
include(FetchContent)
17+
# Fetch core
18+
FetchContent_Declare(
19+
core
20+
GIT_REPOSITORY https://github.com/bolderflight/core.git
21+
GIT_TAG v3.0.0
22+
)
23+
FetchContent_MakeAvailable(core)
24+
# Add the library target
25+
add_library(sbus
26+
src/sbus.cpp
27+
src/sbus.h
28+
)
29+
# Link libraries
30+
target_link_libraries(sbus
31+
PUBLIC
32+
core
33+
)
34+
# Setup include directories
35+
target_include_directories(sbus PUBLIC
36+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
37+
$<INSTALL_INTERFACE:include>
38+
)
39+
40+
endif()
41+
# Example and test if this project is built separately
42+
if (PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
43+
if (DEFINED MCU)
44+
# Add the example target
45+
add_executable(sbus_example examples/cmake/sbus_example.cc)
46+
# Add the includes
47+
target_include_directories(sbus_example PUBLIC
48+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
49+
$<INSTALL_INTERFACE:include>
50+
)
51+
# Link libraries to the example target
52+
target_link_libraries(sbus_example
53+
PRIVATE
54+
sbus
55+
)
56+
# Add hex and upload targets
57+
include(${CMAKE_SOURCE_DIR}/cmake/flash_mcu.cmake)
58+
FlashMcu(sbus_example ${MCU})
59+
endif()
60+
endif()

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Contributing
2+
3+
Please see our [Contributing Guide](https://github.com/bolderflight/contributing) for tips on how to effectively make contributions to our project.

README.md

Lines changed: 88 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
# sbus-arduino
2-
This library communicates with SBUS receivers and servos and is built for use with the Arduino IDE.
1+
[![Pipeline](https://gitlab.com/bolderflight/software/sbus/badges/main/pipeline.svg)](https://gitlab.com/bolderflight/software/sbus/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
2+
3+
![Bolder Flight Systems Logo](img/logo-words_75.png) &nbsp; &nbsp; ![Arduino Logo](img/arduino_logo_75.png)
4+
5+
# Sbus
6+
This library communicates with SBUS receivers and servos and is compatible with Arduino and CMake build systems.
37
* [License](LICENSE.md)
48
* [Changelog](CHANGELOG.md)
59

@@ -16,7 +20,7 @@ The SBUS protocol uses an inverted serial logic with a baud rate of 100000, 8 da
1620
* Bit 4: failsafe activated (0x10)
1721
* Byte[24]: SBUS footer
1822

19-
Note that lost frame is indicated when a frame is lost between the transmitter and receiver. Failsafe activation typically requires that several frames are lost in a row and indicates that the receiver has moved into failsafe mode. Packets are sent approximately every 10 ms or 20 ms.
23+
Note that lost frame is indicated when a frame is lost between the transmitter and receiver. Failsafe activation typically requires that many frames are lost in a row and indicates that the receiver has moved into failsafe mode. Packets are sent approximately every 10 ms or 20 ms.
2024

2125
**Note on CH17 and CH18:** Channel 17 and channel 18 are digital on/off channels. These are not universally available on all SBUS receivers and servos.
2226

@@ -33,21 +37,49 @@ SBUS uses an inverted serial protocol, which is not commonly supported in Arduin
3337
* STM32L476xx
3438
* STM32L433xx
3539
* STM32L432xx
40+
* ESP32
3641

3742
For all other microcontrollers, you **must** use a serial inverter.
3843

3944
# Installation
45+
46+
## Arduino
4047
Simply clone or download and extract the zipped library into your Arduino/libraries folder. The library is added as:
4148

4249
```C++
4350
#include "sbus.h"
4451
```
4552

46-
**AVR Boards:** you will also need to clone or download the [Embedded Template Library](https://github.com/ETLCPP/etl-arduino) into your Arduino/libraries folder. This provides *etl::array* functionality as an alternative to *std::array*.
53+
## CMake
54+
CMake is used to build this library, which is exported as a library target called *sbus*. The header is added as:
55+
56+
```C++
57+
#include "sbus.h"
58+
```
59+
60+
The library can be also be compiled stand-alone using the CMake idiom of creating a *build* directory and then, from within that directory issuing:
4761

48-
# Methods
62+
```
63+
cmake .. -DMCU=MK66FX1M0
64+
make
65+
```
4966

50-
## Receiving SBUS
67+
This will build the library and example executable called *sbus_example*. The example executable source file is located at *examples/cmake/sbus_example.cc*. Notice that the *cmake* command includes a define specifying the microcontroller the code is being compiled for. This is required to correctly configure the code, CPU frequency, and compile/linker options. The available MCUs are:
68+
* MK64FX512
69+
* MK66FX1M0
70+
* MKL26Z64
71+
* IMXRT1062_T40
72+
* IMXRT1062_T41
73+
74+
These are known to work with the same packages used in Teensy products. Also switching packages is known to work well, as long as it's only a package change.
75+
76+
The *sbus_example* target creates an executable for communicating with sbus receivers and servos. This target also has a *_hex* for creating the hex file and an *_upload* for using the [Teensy CLI Uploader](https://www.pjrc.com/teensy/loader_cli.html) to flash the Teensy. Please note that the CMake build tooling is expected to be run under Linux or WSL, instructions for setting up your build environment can be found in our [build-tools repo](https://github.com/bolderflight/build-tools).
77+
78+
# Namespace
79+
This library is within the namespace *bfs*.
80+
81+
# SbusRx
82+
This class is used for receiving SBUS data from an SBUS capable receiver.
5183

5284
**SbusRx(HardwareSerial &ast;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.
5385

@@ -61,6 +93,8 @@ SbusRx sbus(&Serial1);
6193
sbus.Begin();
6294
```
6395

96+
**(ESP-32 ONLY) void Begin(const int8_t rxpin, const int8_t txpin)** Initialized SBUS communication, given the Serial RX and TX pins.
97+
6498
**bool Read()** Parses SBUS packets, returns true on successfully receiving an SBUS packet.
6599

66100
```C++
@@ -69,10 +103,25 @@ if (sbus.Read()) {
69103
}
70104
```
71105

72-
**std::array<uint16_t, 16> rx_channels()** Returns the array of received channel data.
106+
**static constexpr int8_t NUM_CH()** A constant defining the number of SBUS channels (i.e. 16), useful for defining arrays to read the data into.
107+
108+
**(Non-AVR ONLY) std::array<uint16_t, 16> ch()** Returns the array of received channel data.
109+
110+
```C++
111+
std::array<uint16_t, 16> sbus_data = sbus.ch();
112+
```
113+
114+
**int8_t ch(int16_t * data, const int8_t len)** Copys the array of received channel data given a pointer to a destination, *data*, and length of the destination array *len*. Returns the number of channels copied on success or -1 on failure. Note that the maximum number of channels is the smaller of the *len* or *NUM_CH* (i.e. 16).
115+
116+
```C++
117+
int16_t rx_ch[bfs::SbusRx::NUM_CH];
118+
sbus.ch(rx_ch, bfs::SbusRx::NUM_CH);
119+
```
120+
121+
**int16_t ch(const int8_t idx)** Returns received channel data given the channel index.
73122

74123
```C++
75-
std::array<uint16_t, 16> sbus_data = sbus.rx_channels();
124+
int16_t ch3_data = sbus.ch(3);
76125
```
77126

78127
**bool ch17()** Returns the value of channel 17.
@@ -99,7 +148,8 @@ bool lost_frame = sbus.lost_frame();
99148
bool failsafe = sbus.failsafe();
100149
```
101150

102-
## Writing SBUS
151+
# SbusTx
152+
This class is used for transmitting SBUS data to SBUS capable servos.
103153

104154
**SbusTx(HardwareSerial &ast;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.
105155

@@ -113,12 +163,16 @@ SbusTx sbus(&Serial1);
113163
sbus.Begin();
114164
```
115165

166+
**(ESP-32 ONLY) void Begin(const int8_t rxpin, const int8_t txpin)** Initialized SBUS communication, given the Serial RX and TX pins.
167+
116168
**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 or 50 Hz, depending on the setup of the SBUS system.
117169

118170
```C++
119171
sbus.Write();
120172
```
121173

174+
**static constexpr int8_t NUM_CH()** A constant defining the number of SBUS channels (i.e. 16), useful for defining arrays to write the data from.
175+
122176
**void ch17(bool val)** Sets the value of channel 17 to be transmitted.
123177

124178
```C++
@@ -143,10 +197,24 @@ sbus.lost_frame(true);
143197
sbus.failsafe(true);
144198
```
145199

146-
**void tx_channels(const std::array<uint16_t, 16> &val)** Sets the channel data to be transmitted.
200+
**(Non-AVR ONLY) void ch(const std::array<uint16_t, 16> &val)** Sets the channel data to be transmitted.
147201

148202
```C++
149-
sbus.tx_channels(sbus_tx_data);
203+
sbus.ch(sbus_tx_data);
204+
```
205+
206+
**bool ch(const int8_t idx, const int16_t val)** Sets the channel data to be transmitted, given a channel index and corresponding value. Returns true on success and false on failure.
207+
208+
```C++
209+
/* Set channel 3 to a value of 1200 */
210+
sbus.ch(3, 1200);
211+
```
212+
213+
**int8_t ch(int16_t const * const data, const int8_t len)** Sets the channel data to be transmitted given a pointer to an array of commands, *data*, and the array length, *len*. Returns the number of channels copied on success or -1 on failure. Note that the maximum number of channels is the smaller of the *len* or *NUM_CH* (i.e. 16).
214+
215+
```C++
216+
int16_t cmd[bfs::SbusTx::NUM_CH()];
217+
sbus.ch(cmd, bfs::SbusTx::NUM_CH());
150218
```
151219

152220
**bool ch17()** Returns the value of channel 17 to be transmitted.
@@ -173,8 +241,15 @@ bool lost_frame = sbus.lost_frame();
173241
bool failsafe = sbus.failsafe();
174242
```
175243

176-
**std::array<uint16_t, 16> tx_channels()** Returns the array of channel data to be transmitted.
244+
**(Non-AVR ONLY) std::array<uint16_t, 16> ch()** Returns the array of channel data to be transmitted.
245+
246+
```C++
247+
std::array<uint16_t, 16> sbus_tx_data = sbus.ch();
248+
```
249+
250+
**int16_t ch(const int8_t idx)** Returns the channel data to be transmitted, given an index.
177251

178252
```C++
179-
std::array<uint16_t, 16> sbus_tx_data = sbus.tx_channels();
253+
/* Get the command for channel 3 */
254+
int16_t data = sbus.ch(3);
180255
```

0 commit comments

Comments
 (0)