-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpektrum.h
114 lines (84 loc) · 2.52 KB
/
Spektrum.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
/*
Spektrum.h
Spektrum serial receiver mbed library
Dennis Evangelista, 2018
*/
#ifndef SPEKTRUM_H
#define SPEKTRUM_H
#define SPEKTRUM_VERSION "1.0.1"
#include "mbed.h"
#include "rtos.h"
// bind modes
#define SPEKTRUM_INT_DSMX_22MS 7
#define SPEKTRUM_EXT_DSMX_22MS 8
#define SPEKTRUM_INT_DSMX_11MS 9
#define SPEKTRUM_EXT_DSMX_11MS 10
// DSM2 bind modes not recommended, not implemented
// EXT(ernal) bind modes don't really work on solitary satellite receiver
// field definitions
// #define SPEKTRUM_MASK_1024_CHANID 0xfc00
// #define SPEKTRUM_MASK_1024_SXPOS 0x03ff
// first two only used in DSM2 which are not implemented
#define SPEKTRUM_MASK_2048_CHANID 0x7800
#define SPEKTRUM_MASK_2048_CHANID_MSB 0x78
#define SPEKTRUM_MASK_2048_SXPOS 0x07ff
// allowable system field values
// #define SPEKTRUM_22MS_1024_DSM2 0x01
// #define SPEKTRUM_11MS_2048_DSM2 0x12
#define SPEKTRUM_22MS_2048_DSMX 0xa2
#define SPEKTRUM_11MS_2048_DSMX 0xb2
#define SPEKTRUM_BAUD 115200
// Spektrum baud is supposed to be 125000, but the LPC1768 seems not
// to support nonstandard baud rates.
#define SPEKTRUM_SERVOS 7
#define SPEKTRUM_PACKET_SIZE 16
#define SPEKTRUM_CHANNELS 16
#define SPEKTRUM_COUNT2US(x) (x*600/1024+900)
/** Spektrum receiver object for connecting to eg SPM9745 receiver
*/
class Spektrum{
public:
/** Number of fades (failed packets) from receiver */
unsigned int fades;
/** Tells if system is in DSMX 11ms or 22ms for example */
unsigned int system;
/** Contains 0-2048 values for all channels 0-15 */
unsigned int channel[SPEKTRUM_CHANNELS];
/** Contains approx 900-2100us pulsewidths corresponding to chan 0-15 */
unsigned int pulsewidth[SPEKTRUM_CHANNELS];
/** If true, data is value */
bool valid; // TODO switch to EventFlags?
/** 11 or 22 ms */
unsigned int period_ms;
Spektrum(PinName tx, PinName rx); // constructor
~Spektrum(); // destructor
private:
UARTSerial _rx;
unsigned char _buf[SPEKTRUM_PACKET_SIZE];
Thread _packet_thread;
void _packet_callback(void);
};
/** For binding a Spektrum receiver to transmitter */
class BindPlug{
public:
/** e.g. SPEKTRUM_INT_DSMX_11MS, SPEKTRUM_INT_DSMX_22MS */
int mode;
BindPlug(PinName tx, PinName rx, int mode = SPEKTRUM_INT_DSMX_11MS);
~BindPlug();
void bind();
private:
DigitalOut _3Vpin;
DigitalOut _datapin;
};
/* LATER
class SpektrumTestDevice{
public:
unsigned int fades;
unsigned int servo[7];
SpektrumTestDevice(PinName tx, PinName rx);
~SpektrumTestDevice();
private:
Serial _receiver;
};
*/
#endif