-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlySkyIBus.h
executable file
·83 lines (69 loc) · 1.65 KB
/
FlySkyIBus.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
/*
* Simple interface to the Fly Sky IBus RC system.
*/
#include <inttypes.h>
enum Channels
{
ROLL=1,
PITCH,
THROTTLE,
YAW,
AUX1,
AUX2,
AUX3,
AUX4,
AUX5,
AUX6,
AUX7,
AUX8,
AUX9,
AUX10,
};
class HardwareSerial;
class Stream;
class FlySkyIBus
{
public:
void begin(HardwareSerial& serial, HardwareSerial& serialr);
void begin(Stream& stream, Stream& streamr);
void writeLoop(void);
void readLoop(void);
uint16_t readChannel(uint8_t channelNr);
void writeToChannel(uint8_t channelNr, uint16_t value);
private:
// Used as a state machine
enum State
{
GET_LENGTH,
GET_DATA,
GET_CHKSUML,
GET_CHKSUMH,
DISCARD,
};
static const uint8_t PROTOCOL_MAX_CHANNELS = 14;
static const uint8_t PROTOCOL_CHANNELS = 10;
static const uint8_t PROTOCOL_LENGTH = 0x20; // 32 bytes
static const uint8_t PROTOCOL_OVERHEAD = 3; // <len><cmd><data....><chkl><chkh>
static const uint8_t PROTOCOL_TIME = 7; // Packets are received very ~7ms
static const uint8_t PROTOCOL_TIMEGAP = 3; // Packets are received very ~7ms so use ~half that for the gap
static const uint8_t PROTOCOL_COMMAND40 = 0x40; // Command is always 0x40
static const uint16_t LOW_VALUE = 0x3E8;
static const uint16_t HIGH_VALUE = 0x7D0;
static const uint16_t HALF_VALUE = 0x5DC;
Stream* stream;
Stream* streamr;
uint8_t state;
uint8_t w_state;
uint32_t last;
uint32_t last_r;
uint8_t ptr;
uint8_t len;
uint8_t buffer[PROTOCOL_LENGTH];
uint8_t w_buffer[PROTOCOL_LENGTH];
uint16_t chksum;
uint16_t w_chksum;
uint8_t lchksum;
uint16_t channel[PROTOCOL_CHANNELS];
void initWriteBuffer();
};
extern FlySkyIBus IBus;