This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayer1_LoRa.cpp
202 lines (172 loc) · 4.33 KB
/
Layer1_LoRa.cpp
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
#include "Layer1_LoRa.h"
Layer1Class::Layer1Class(SPIClass &spi)
: _csPin(18),
_resetPin(23),
_DIOPin(26),
_spreadingFactor(9),
_loraFrequency(915E6),
_txPower(17),
_loraInitialized(0),
_spiFrequency(100E3),
_spi(spi)
{
txBuffer = new packetBuffer();
rxBuffer = new packetBuffer();
};
bool _dioFlag = false;
bool _enableInterrupt = true;
int _packetSize = 0;
int Layer1Class::debug_printf(const char* format, ...) {
#ifdef DEBUG
int ret;
va_list args;
va_start(args, format);
ret = vfprintf(stderr, format, args);
va_end(args);
fflush(stderr);
return ret;
#else
return 0;
#endif
}
/* Public access to local variables
*/
int Layer1Class::getTime(){
return millis();
}
int Layer1Class::loraInitialized(){
return _loraInitialized;
}
int Layer1Class::loraCSPin(){
return _csPin;
}
int Layer1Class::resetPin(){
return _resetPin;
}
int Layer1Class::DIOPin(){
return _DIOPin;
}
int Layer1Class::spreadingFactor(){
return _spreadingFactor;
}
uint32_t Layer1Class::spiFrequency(){
return _spiFrequency;
}
uint32_t Layer1Class::loraFrequency(){
return _loraFrequency;
}
int Layer1Class::txPower(){
return _txPower;
}
/* User configurable settings
*/
void Layer1Class::setPins(int cs, int reset, int dio){
_csPin = cs;
_resetPin = reset;
_DIOPin = dio;
}
void Layer1Class::setSPIFrequency(uint32_t frequency){
_spiFrequency = frequency;
}
void Layer1Class::setLoRaFrequency(uint32_t frequency){
_loraFrequency = frequency;
}
void Layer1Class::setSpreadingFactor(uint8_t spreadingFactor){
_spreadingFactor = spreadingFactor;
LoRa.setSpreadingFactor(_spreadingFactor); // ranges from 6-12
}
void Layer1Class::setTxPower(int txPower){
_txPower = txPower;
LoRa.setTxPower(_txPower);
}
/* Private functions
*/
// Send packet function
int Layer1Class::sendPacket(char* data, size_t len){
int ret = 0;
#ifdef LL2_DEBUG
Serial.printf("Layer1::sendPacket(): data = ");
for(int i = 0; i < len; i++){
Serial.printf("%c", data[i]);
}
Serial.printf("\r\n");
#endif
if((ret = LoRa.beginPacket())){
for( int i = 0 ; i < len ; i++){
LoRa.write(data[i]);
}
LoRa.endPacket(1);
LoRa.receive();
}
return ret;
}
// Receive packet callback
void Layer1Class::setFlag(int packetSize) {
// check if the interrupt is enabled
if(!_enableInterrupt) {
return;
}
// we got a packet, set the flag
_packetSize = packetSize;
_dioFlag = true;
}
/*Main public functions
*/
// Initialization
int Layer1Class::init(){
LoRa.setSPI(_spi);
LoRa.setPins(_csPin, _resetPin, _DIOPin); // set CS, reset, DIO pin
LoRa.setSPIFrequency(_spiFrequency);
LoRa.setTxPower(_txPower);
if (!LoRa.begin(_loraFrequency)) { // defaults to 915MHz, can also be 433MHz or 868Mhz
return _loraInitialized;
}
LoRa.setSpreadingFactor(_spreadingFactor); // ranges from 6-12, default 9
LoRa.onReceive(setFlag);
LoRa.receive();
_loraInitialized = 1;
return _loraInitialized;
}
// Transmit polling function
int Layer1Class::transmit(){
BufferEntry entry = txBuffer->read();
if(entry.length != 0){
sendPacket(entry.data, entry.length);
}
return entry.length;
}
// Receive polling function
int Layer1Class::receive(){
int ret = 0;
if(_dioFlag) {
#ifdef LL2_DEBUG
Serial.printf("Layer1Class::receive(): _packetSize = %d\r\n", _packetSize);
#endif
_enableInterrupt = false;
_dioFlag = false;
if (_packetSize > 0){
char data[MAX_PACKET_SIZE];
int len = 0;
while (LoRa.available()) {
data[len] = (char)LoRa.read();
len++;
}
BufferEntry entry;
memcpy(&entry.data[0], &data[0], len);
entry.length = len;
rxBuffer->write(entry);
#ifdef LL2_DEBUG
Serial.printf("Layer1::receive(): data = ");
for(int i = 0; i < len; i++){
Serial.printf("%c", data[i]);
}
Serial.printf("\r\n");
#endif
ret = _packetSize;
}
// reset packetSize to zero and renable interrupt
_packetSize = 0;
_enableInterrupt = true;
}
return ret;
}