forked from xreef/EByte_LoRa_E22_Series_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoRa_E22.h
404 lines (332 loc) · 11.5 KB
/
LoRa_E22.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* EBYTE LoRa E22 Series
*
* AUTHOR: Renzo Mischianti
* VERSION: 1.0.9
*
* https://www.mischianti.org
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
* You may copy, alter and reuse this code in any way you like, but please leave
* reference to www.mischianti.org in your comments if you redistribute this code.
*
* 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 LoRa_E22_h
#define LoRa_E22_h
#if !defined(ARDUINO_ARCH_STM32) && !defined(ESP32) && !defined(ARDUINO_ARCH_SAMD) && !defined(ARDUINO_ARCH_MBED) && !defined(__STM32F1__) && !defined(__STM32F4__)
#define ACTIVATE_SOFTWARE_SERIAL
#endif
#if defined(ESP32)
#define HARDWARE_SERIAL_SELECTABLE_PIN
#endif
#ifdef ACTIVATE_SOFTWARE_SERIAL
#include <SoftwareSerial.h>
#endif
#include <includes/statesNaming.h>
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define MAX_SIZE_TX_PACKET 240
// Uncomment to enable printing out nice debug messages.
//#define LoRa_E22_DEBUG
// Define where debug output will be printed.
#define DEBUG_PRINTER Serial
// Setup debug printing macros.
#ifdef LoRa_E22_DEBUG
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif
enum MODE_TYPE {
MODE_0_NORMAL = 0,
MODE_1_WOR = 1,
MODE_2_CONFIGURATION = 2,
MODE_2_PROGRAM = 2,
MODE_3_SLEEP = 3,
MODE_INIT = 0xFF
};
enum PROGRAM_COMMAND {
WRITE_CFG_PWR_DWN_SAVE = 0xC0,
READ_CONFIGURATION = 0xC1,
WRITE_CFG_PWR_DWN_LOSE = 0xC2,
WRONG_FORMAT = 0xFF,
RETURNED_COMMAND = 0xC1,
SPECIAL_WIFI_CONF_COMMAND = 0xCF
};
enum REGISTER_ADDRESS {
REG_ADDRESS_CFG = 0x00,
REG_ADDRESS_SPED = 0x03,
REG_ADDRESS_TRANS_MODE = 0x04,
REG_ADDRESS_CHANNEL = 0x05,
REG_ADDRESS_OPTION = 0x06,
REG_ADDRESS_CRYPT = 0x07,
REG_ADDRESS_PID = 0x80
};
enum PACKET_LENGHT {
PL_CONFIGURATION = 0x09,
PL_SPED = 0x01,
PL_OPTION = 0x01,
PL_TRANSMISSION_MODE= 0x01,
PL_CHANNEL = 0x01,
PL_CRYPT = 0x02,
PL_PID = 7
};
#pragma pack(push, 1)
struct Speed {
uint8_t airDataRate :3; //bit 0-2
String getAirDataRateDescription() {
return getAirDataRateDescriptionByParams(this->airDataRate);
}
uint8_t uartParity :2; //bit 3-4
String getUARTParityDescription() {
return getUARTParityDescriptionByParams(this->uartParity);
}
uint8_t uartBaudRate :3; //bit 5-7
String getUARTBaudRateDescription() {
return getUARTBaudRateDescriptionByParams(this->uartBaudRate);
}
};
struct TransmissionMode {
byte WORPeriod :3; //bit 2,1,0
String getWORPeriodByParamsDescription() {
return getWORPeriodByParams(this->WORPeriod);
}
byte WORTransceiverControl :1; //bit 3
String getWORTransceiverControlDescription() {
return getWORTransceiverControlByParams(this->WORTransceiverControl);
}
byte enableLBT :1; //bit 4
String getLBTEnableByteDescription() {
return getLBTEnableByteByParams(this->enableLBT);
}
byte enableRepeater :1; //bit 5
String getRepeaterModeEnableByteDescription() {
return getRepeaterModeEnableByteByParams(this->enableRepeater);
}
byte fixedTransmission :1; //bit 6
String getFixedTransmissionDescription() {
return getFixedTransmissionDescriptionByParams(this->fixedTransmission);
}
byte enableRSSI :1; //bit 7
String getRSSIEnableByteDescription() {
return getRSSIEnableByteByParams(this->enableRSSI);
}
};
struct Option {
uint8_t transmissionPower :2; //bit 0-1
String getTransmissionPowerDescription() {
return getTransmissionPowerDescriptionByParams(this->transmissionPower);
}
uint8_t reserve :3; //bit 2-3
// String getAirDataRate() {
// return getAirDataRateDescriptionByParams(this->airDataRate);
// }
uint8_t RSSIAmbientNoise :1; //bit 5
String getRSSIAmbientNoiseEnable() {
return getRSSIAmbientNoiseEnableByParams(this->RSSIAmbientNoise);
}
uint8_t subPacketSetting :2; //bit 6-7
String getSubPacketSetting() {
return getSubPacketSettingByParams(this->subPacketSetting);
}
};
struct Crypt {
byte CRYPT_H = 0;
byte CRYPT_L = 0;
};
struct Configuration {
byte COMMAND = 0;
byte STARTING_ADDRESS = 0;
byte LENGHT = 0;
byte ADDH = 0;
byte ADDL = 0;
byte NETID = 0;
struct Speed SPED;
struct Option OPTION;
byte CHAN = 0;
String getChannelDescription() {
return String(this->CHAN + OPERATING_FREQUENCY) + F("MHz");
}
struct TransmissionMode TRANSMISSION_MODE;
struct Crypt CRYPT;
};
struct ModuleInformation {
byte COMMAND = 0;
byte STARTING_ADDRESS = 0;
byte LENGHT = 0;
byte reserved[3] = {0,0,0};
byte model = 0;
byte version = 0;
byte features = 0;
byte end = 0;
};
struct ResponseStatus {
Status code;
String getResponseDescription() {
return getResponseDescriptionByParams(this->code);
}
};
struct ResponseStructContainer {
void *data;
byte rssi;
ResponseStatus status;
void close() {
free(this->data);
}
};
struct ResponseContainer {
String data;
byte rssi;
ResponseStatus status;
};
struct ConfigurationMessage
{
byte specialCommand1 = 0xCF;
byte specialCommand2 = 0xCF;
unsigned char message[];
};
//struct FixedStransmission {
// byte ADDL = 0;
// byte ADDH = 0;
// byte CHAN = 0;
// void *message;
//};
#pragma pack(pop)
class LoRa_E22 {
public:
#ifdef ACTIVATE_SOFTWARE_SERIAL
LoRa_E22(byte txE32pin, byte rxE32pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E22(byte txE32pin, byte rxE32pin, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E22(byte txE32pin, byte rxE32pin, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
#endif
LoRa_E22(HardwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E22(HardwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E22(HardwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
#ifdef HARDWARE_SERIAL_SELECTABLE_PIN
LoRa_E22(byte txE32pin, byte rxE32pin, HardwareSerial* serial, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
LoRa_E22(byte txE32pin, byte rxE32pin, HardwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
LoRa_E22(byte txE32pin, byte rxE32pin, HardwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
#endif
#ifdef ACTIVATE_SOFTWARE_SERIAL
LoRa_E22(SoftwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E22(SoftwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E22(SoftwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
#endif
// LoRa_E22(byte txE32pin, byte rxE32pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600, MODE_TYPE mode = MODE_0_NORMAL);
// LoRa_E22(HardwareSerial* serial = &Serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600, MODE_TYPE mode = MODE_0_NORMAL);
// LoRa_E22(SoftwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600, MODE_TYPE mode = MODE_0_NORMAL);
bool begin();
Status setMode(MODE_TYPE mode);
MODE_TYPE getMode();
ResponseStructContainer getConfiguration();
ResponseStatus setConfiguration(Configuration configuration, PROGRAM_COMMAND saveType = WRITE_CFG_PWR_DWN_LOSE);
ResponseStructContainer getModuleInformation();
ResponseStatus resetModule();
ResponseStatus sendMessage(const void *message, const uint8_t size);
ResponseContainer receiveMessageUntil(char delimiter = '\0');
ResponseStructContainer receiveMessage(const uint8_t size);
ResponseStructContainer receiveMessageRSSI(const uint8_t size);
ResponseStructContainer receiveMessageComplete(const uint8_t size, bool enableRSSI);
ResponseStatus sendMessage(const String message);
ResponseContainer receiveMessage();
ResponseContainer receiveMessageRSSI();
ResponseContainer receiveMessageComplete(bool enableRSSI);
ResponseStatus sendFixedMessage(byte ADDH, byte ADDL, byte CHAN, const String message);
ResponseStatus sendConfigurationMessage( byte ADDH,byte ADDL, byte CHAN, Configuration *configuration, PROGRAM_COMMAND programCommand = WRITE_CFG_PWR_DWN_SAVE);
ResponseStatus sendFixedMessage(byte ADDH,byte ADDL, byte CHAN, const void *message, const uint8_t size);
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const void *message, const uint8_t size );
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const String message);
ResponseContainer receiveInitialMessage(const uint8_t size);
int available();
private:
HardwareSerial* hs;
#ifdef ACTIVATE_SOFTWARE_SERIAL
SoftwareSerial* ss;
#endif
bool isSoftwareSerial = true;
int8_t txE32pin = -1;
int8_t rxE32pin = -1;
int8_t auxPin = -1;
#ifdef HARDWARE_SERIAL_SELECTABLE_PIN
uint32_t serialConfig = SERIAL_8N1;
#endif
int8_t m0Pin = -1;
int8_t m1Pin = -1;
unsigned long halfKeyloqKey = 0x06660708;
unsigned long encrypt(unsigned long data);
unsigned long decrypt(unsigned long data);
UART_BPS_RATE bpsRate = UART_BPS_RATE_9600;
struct NeedsStream {
template<typename T>
void begin(T &t, uint32_t baud) {
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud);
stream = &t;
}
#ifdef HARDWARE_SERIAL_SELECTABLE_PIN
// template< typename T >
// void begin( T &t, int baud, SerialConfig config ){
// DEBUG_PRINTLN("Begin ");
// t.setTimeout(500);
// t.begin(baud, config);
// stream = &t;
// }
//
template< typename T >
void begin( T &t, uint32_t baud, uint32_t config ) {
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud, config);
stream = &t;
}
template< typename T >
void begin( T &t, uint32_t baud, uint32_t config, int8_t txE32pin, int8_t rxE32pin ) {
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud, config, txE32pin, rxE32pin);
stream = &t;
}
#endif
void listen() {}
Stream *stream;
};
NeedsStream serialDef;
MODE_TYPE mode = MODE_0_NORMAL;
void managedDelay(unsigned long timeout);
Status waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100);
void flush();
void cleanUARTBuffer();
Status sendStruct(void *structureManaged, uint16_t size_);
Status receiveStruct(void *structureManaged, uint16_t size_);
bool writeProgramCommand(PROGRAM_COMMAND cmd, REGISTER_ADDRESS addr, PACKET_LENGHT pl);
RESPONSE_STATUS checkUARTConfiguration(MODE_TYPE mode);
#ifdef LoRa_E22_DEBUG
void printParameters(struct Configuration *configuration);
#endif
};
#endif