-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathsbus.h
149 lines (139 loc) · 4.9 KB
/
sbus.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Brian R Taylor
*
* Copyright (c) 2022 Bolder Flight Systems Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef SRC_SBUS_H_
#define SRC_SBUS_H_
#if defined(ARDUINO)
#include <Arduino.h>
#else
#include <cstddef>
#include <cstdint>
#include "core/core.h"
#endif
namespace bfs {
struct SbusData {
bool lost_frame;
bool failsafe;
bool ch17, ch18;
static constexpr int8_t NUM_CH = 16;
int16_t ch[NUM_CH];
};
class SbusRx {
public:
#if defined(ESP32)
SbusRx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
const bool inv) : uart_(bus), inv_(inv), rxpin_(rxpin), txpin_(txpin)
{}
SbusRx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
const bool inv, const bool fast) : uart_(bus), inv_(inv), fast_(fast),
rxpin_(rxpin), txpin_(txpin) {}
#else
explicit SbusRx(HardwareSerial *bus) : uart_(bus) {}
SbusRx(HardwareSerial *bus, const bool inv) : uart_(bus), inv_(inv) {}
SbusRx(HardwareSerial *bus, const bool inv, const bool fast) : uart_(bus),
inv_(inv),
fast_(fast) {}
#endif
void Begin();
bool Read();
inline SbusData data() const {return data_;}
private:
/* Communication */
HardwareSerial *uart_;
bool inv_ = true;
bool fast_ = false;
#if defined(ESP32)
int8_t rxpin_, txpin_;
#endif
int32_t baud_ = 100000;
/* Message len */
static constexpr int8_t PAYLOAD_LEN_ = 23;
static constexpr int8_t HEADER_LEN_ = 1;
static constexpr int8_t FOOTER_LEN_ = 1;
/* SBUS message defs */
static constexpr int8_t NUM_SBUS_CH_ = 16;
static constexpr uint8_t HEADER_ = 0x0F;
static constexpr uint8_t FOOTER_ = 0x00;
static constexpr uint8_t FOOTER2_ = 0x04;
static constexpr uint8_t CH17_MASK_ = 0x01;
static constexpr uint8_t CH18_MASK_ = 0x02;
static constexpr uint8_t LOST_FRAME_MASK_ = 0x04;
static constexpr uint8_t FAILSAFE_MASK_ = 0x08;
/* Parsing state tracking */
int8_t state_ = 0;
uint8_t prev_byte_ = FOOTER_;
uint8_t cur_byte_;
/* Buffer for storing messages */
uint8_t buf_[25];
/* Data */
bool new_data_;
SbusData data_;
bool Parse();
};
class SbusTx {
public:
#if defined(ESP32)
SbusTx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
const bool inv) : uart_(bus), inv_(inv), rxpin_(rxpin), txpin_(txpin)
{}
SbusTx(HardwareSerial *bus, const int8_t rxpin, const int8_t txpin,
const bool inv, const bool fast) : uart_(bus), inv_(inv), fast_(fast),
rxpin_(rxpin), txpin_(txpin) {}
#else
explicit SbusTx(HardwareSerial *bus) : uart_(bus) {}
SbusTx(HardwareSerial *bus, const bool inv) : uart_(bus), inv_(inv) {}
SbusTx(HardwareSerial *bus, const bool inv, const bool fast) : uart_(bus),
inv_(inv),
fast_(fast) {}
#endif
void Begin();
void Write();
inline void data(const SbusData &data) {data_ = data;}
inline SbusData data() const {return data_;}
private:
/* Communication */
HardwareSerial *uart_;
bool inv_ = true;
bool fast_ = false;
#if defined(ESP32)
int8_t rxpin_, txpin_;
#endif
int32_t baud_ = 100000;
/* Message len */
static constexpr int8_t BUF_LEN_ = 25;
/* SBUS message defs */
static constexpr int8_t NUM_SBUS_CH_ = 16;
static constexpr uint8_t HEADER_ = 0x0F;
static constexpr uint8_t FOOTER_ = 0x00;
static constexpr uint8_t FOOTER2_ = 0x04;
static constexpr uint8_t CH17_MASK_ = 0x01;
static constexpr uint8_t CH18_MASK_ = 0x02;
static constexpr uint8_t LOST_FRAME_MASK_ = 0x04;
static constexpr uint8_t FAILSAFE_MASK_ = 0x08;
/* Data */
uint8_t buf_[BUF_LEN_];
SbusData data_;
};
} // namespace bfs
#endif // SRC_SBUS_H_