forked from KrisKasprzak/EBYTE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEBYTE.h
254 lines (212 loc) · 7.81 KB
/
EBYTE.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
Original code by: Kris Kasprzak [email protected]
Modified by: Max-Felix Mueller [email protected]
This library is intended to be used with EBYTE transcievers, small wireless units for MCU's such as
Teensy and Arduino. This library let's users program the operating parameters and both send and recieve data.
This company makes several modules with different capabilities, but most #defines here should be compatible with them
All constants were extracted from several data sheets and listed in binary as that's how the data sheet represented each setting
Hopefully, any changes or additions to constants can be a matter of copying the data sheet constants directly into these #defines
Usage of this library consumes around 970 bytes
Module connection
Module MCU Description
MO any digital pin Pin to control working/program modes
M1 any digital pin Pin to control working/program modes
Rx Tx of Serial Interface Serial communication via UART
Tx Rx of Serial Interface Serial communication via UART
AUX any digital pin Pin to indicate when an operation is complete (low is busy, high is done)
Vcc +3v3 Use 3v3 to be save. Current draw can be high during transmission
Gnd Ground Ground must be common to module and MCU
Code usage
1. Create a serial object
2. Create EBYTE object that uses the serial object
3. Begin the serial object
4. Init the EBYTE object
5. Set parameters (optional but required if sender and reciever are different)
6. Send or listen to sent data
*/
#include <Arduino.h>
// delays in milliseconds to allow module to save and change states
#define PIN_RECOVER 20
#define AUX_PIN_RECOVER 20
// operating modes of module
#define MODE_NORMAL 0 // can send and recieve
#define MODE_WAKEUP 1 // sends a preamble to waken receiver
#define MODE_POWERDOWN 2 // can't transmit but receive works only in wake up mode
#define MODE_PROGRAM 3 // for reading and changeing programming
// save parameters during power loss or not
#define PERMANENT 0xC0
#define TEMPORARY 0xC2
// parity bit options
// (must be the same for transmitter and reveiver)
#define PB_8N1 0b00 // default
#define PB_8O1 0b01
#define PB_8E1 0b11
// UART data rates
// (can be different for transmitter and reveiver)
#define UDR_1200 0b000 // 1200 baud
#define UDR_2400 0b001 // 2400 baud
#define UDR_4800 0b010 // 4800 baud
#define UDR_9600 0b011 // 9600 baud default
#define UDR_19200 0b100 // 19200 baud
#define UDR_38400 0b101 // 34800 baud
#define UDR_57600 0b110 // 57600 baud
#define UDR_115200 0b111 // 115200 baud
// air data rates (certian types of modules)
// (must be the same for transmitter and reveiver)
#define ADR_300 0b000 // 300 baud
#define ADR_1200 0b001 // 1200 baud
#define ADR_2400 0b010 // 2400 baud
#define ADR_4800 0b011 // 4800 baud
#define ADR_9600 0b100 // 9600 baud
#define ADR_19200 0b101 // 19200 baud
// air data rates (other types of modules)
#define ADR_1K 0b000 // 1k baud
#define ADR_2K 0b001 // 2K baud
#define ADR_5K 0b010 // 4K baud
#define ADR_8K 0b011 // 8K baud
#define ADR_10K 0b100 // 10K baud
#define ADR_15K 0b101 // 15K baud
#define ADR_20K 0b110 // 20K baud
#define ADR_25K 0b111 // 25K baud
// various options
// (can be different for transmitter and reveiver)
#define OPT_FMDISABLE 0b0 //default
#define OPT_FMENABLE 0b1
#define OPT_IOOPENDRAIN 0b0
#define OPT_IOPUSHPULL 0b1
#define OPT_WAKEUP250 0b000
#define OPT_WAKEUP500 0b001
#define OPT_WAKEUP750 0b010
#define OPT_WAKEUP1000 0b011
#define OPT_WAKEUP1250 0b100
#define OPT_WAKEUP1500 0b101
#define OPT_WAKEUP1750 0b110
#define OPT_WAKEUP2000 0b111
#define OPT_FECDISABLE 0b0
#define OPT_FECENABLE 0b1
// transmitter output power
// check local regulations on legal transmit power
// refer to the data sheet as not all modules support these power levels
// constants for 1W units
// (can be different for transmitter and reveiver)
//#define OPT_TP30 0b00 // 30 db
//#define OPT_TP27 0b01 // 27 db
//#define OPT_TP24 0b10 // 24 db
//#define OPT_TP21 0b11 // 21 db
// constants or 500 mW units
//#define OPT_TP27 0b00 // 27 db
//#define OPT_TP24 0b01 // 24 db
//#define OPT_TP21 0b10 // 21 db
//#define OPT_TP18 0b11 // 17 db
//#define OPT_TP17 0b11 // 17 db
// constants or 100 mW units
#define OPT_TP20 0b00 // 20 db
#define OPT_TP17 0b01 // 17 db
#define OPT_TP14 0b10 // 14 db
#define OPT_TP11 0b11 // 10 db
#define OPT_TP10 0b11 // 10 db
class Stream;
class EBYTE {
public:
// constructor
EBYTE(Stream *s, uint8_t PIN_M0 = 4, uint8_t PIN_M1 = 5, uint8_t PIN_AUX = 6);
// initialization
bool init();
// methods to set modules working parameters
// NOTHING WILL BE APPLIED UNLESS SaveParameters() is called
void SetMode(uint8_t mode = MODE_NORMAL);
void SetAddress(uint16_t val = 0);
void SetAddressH(uint8_t val = 0);
void SetAddressL(uint8_t val = 0);
void SetAirDataRate(uint8_t val);
void SetUARTBaudRate(uint8_t val);
void SetSpeed(uint8_t val);
void SetOptions(uint8_t val);
void SetChannel(uint8_t val);
void SetParityBit(uint8_t val);
void SetTransmissionMode(uint8_t val);
void SetPullupMode(uint8_t val);
void SetWORTIming(uint8_t val);
void SetFECMode(uint8_t val);
void SetTransmitPower(uint8_t val);
// serial interface pass through
bool available();
void flush();
// methods to get modules working parameters
uint16_t GetAddress();
uint8_t GetModel();
uint8_t GetVersion();
uint8_t GetFeatures();
uint8_t GetAddressH();
uint8_t GetAddressL();
uint8_t GetAirDataRate();
uint8_t GetUARTBaudRate();
uint8_t GetChannel();
uint8_t GetParityBit();
uint8_t GetTransmissionMode();
uint8_t GetPullupMode();
uint8_t GetWORTIming();
uint8_t GetFECMode();
uint8_t GetTransmitPower();
uint8_t GetOptions();
uint8_t GetSpeed();
// methods to get data from sending unit
uint8_t GetByte();
bool GetStruct(const void *TheStructure, uint16_t size_);
// method to send to data to receiving unit
void SendByte(uint8_t TheByte);
bool SendStruct(const void *TheStructure, uint16_t size_);
// debug method to print parameters
void PrintParameters();
// apply changes after setting parameters
void SaveParameters(uint8_t retention = TEMPORARY);
protected:
// not working...
void Reset();
// function to read modules parameters
bool ReadParameters();
// wait for module to finish last operation
void CompleteTask(unsigned long timeout = 0);
// utility funciton to build the collection of a few different parameters
void BuildSpeedByte();
// utility funciton to build the collection of a few different parameters
void BuildOptionByte();
private:
// read model information
bool ReadModelData();
// discard serial interface input buffer
void ClearBuffer();
// variable for the serial stream
Stream* _s;
// pins
int8_t _M0;
int8_t _M1;
int8_t _AUX;
// variable for the 6 bytes that are sent to the module to program it
// or bytes received to indicate modules programmed settings
uint8_t _Params[6];
// indicidual variables for each of the 6 bytes
// _Params could be used as the main variable storage, but since some bytes
// are a collection of several options, let's just make storage consistent
// also Param[1] is different data depending on the _Save variable
uint8_t _Save;
uint8_t _AddressHigh;
uint8_t _AddressLow;
uint8_t _Speed;
uint8_t _Channel;
uint8_t _Options;
// individual variables for all the options
uint8_t _ParityBit;
uint8_t _UARTDataRate;
uint8_t _AirDataRate;
uint8_t _OptionTrans;
uint8_t _OptionPullup;
uint8_t _OptionWakeup;
uint8_t _OptionFEC;
uint8_t _OptionPower;
uint16_t _Address;
uint8_t _Model;
uint8_t _Version;
uint8_t _Features;
uint8_t _buf;
};