File tree 5 files changed +72
-10
lines changed
examples/LoRaSenderNonBlocking
5 files changed +72
-10
lines changed Original file line number Diff line number Diff line change @@ -33,5 +33,6 @@ script:
33
33
buildExampleSketch LoRaReceiverCallback;
34
34
fi
35
35
- buildExampleSketch LoRaSender
36
+ - buildExampleSketch LoRaSenderNonBlocking
36
37
- buildExampleSketch LoRaSetSpread
37
38
- buildExampleSketch LoRaSetSyncWord
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ LoRa.beginPacket(implicitHeader);
82
82
83
83
* ` implicitHeader ` - (optional) ` true ` enables implicit header mode, ` false ` enables explicit header mode (default)
84
84
85
- Returns ` 1 ` on success , ` 0 ` on failure.
85
+ Returns ` 1 ` if radio is ready to transmit , ` 0 ` if busy or on failure.
86
86
87
87
### Writing
88
88
@@ -109,8 +109,11 @@ Returns the number of bytes written.
109
109
End the sequence of sending a packet.
110
110
111
111
``` arduino
112
- LoRa.endPacket()
112
+ LoRa.endPacket();
113
+
114
+ LoRa.endPacket(async);
113
115
```
116
+ * ` async ` - (optional) ` true ` enables non-blocking mode, ` false ` waits for transmission to be completed (default)
114
117
115
118
Returns ` 1 ` on success, ` 0 ` on failure.
116
119
Original file line number Diff line number Diff line change
1
+ #include < SPI.h>
2
+ #include < LoRa.h>
3
+
4
+ int counter = 0 ;
5
+
6
+ void setup () {
7
+ Serial.begin (9600 );
8
+ while (!Serial);
9
+
10
+ Serial.println (" LoRa Sender non-blocking" );
11
+
12
+ if (!LoRa.begin (915E6 )) {
13
+ Serial.println (" Starting LoRa failed!" );
14
+ while (1 );
15
+ }
16
+ }
17
+
18
+ void loop () {
19
+ // wait until the radio is ready to send a packet
20
+ while (LoRa.beginPacket () == 0 ) {
21
+ Serial.print (" waiting for radio ... " );
22
+ delay (100 );
23
+ }
24
+
25
+ Serial.print (" Sending packet non-blocking: " );
26
+ Serial.println (counter);
27
+
28
+ // send in async / non-blocking mode
29
+ LoRa.beginPacket ();
30
+ LoRa.print (" hello " );
31
+ LoRa.print (counter);
32
+ LoRa.endPacket (true ); // true = async / non-blocking mode
33
+
34
+ counter++;
35
+ }
Original file line number Diff line number Diff line change @@ -149,6 +149,10 @@ void LoRaClass::end()
149
149
150
150
int LoRaClass::beginPacket (int implicitHeader)
151
151
{
152
+ if (isTransmitting ()) {
153
+ return 0 ;
154
+ }
155
+
152
156
// put in standby mode
153
157
idle ();
154
158
@@ -165,22 +169,40 @@ int LoRaClass::beginPacket(int implicitHeader)
165
169
return 1 ;
166
170
}
167
171
168
- int LoRaClass::endPacket ()
172
+ int LoRaClass::endPacket (bool async )
169
173
{
170
174
// put in TX mode
171
175
writeRegister (REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
172
176
173
- // wait for TX done
174
- while ((readRegister (REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0 ) {
175
- yield ();
177
+ if (async) {
178
+ // grace time is required for the radio
179
+ delayMicroseconds (150 );
180
+ } else {
181
+ // wait for TX done
182
+ while ((readRegister (REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0 ) {
183
+ yield ();
184
+ }
185
+ // clear IRQ's
186
+ writeRegister (REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
176
187
}
177
188
178
- // clear IRQ's
179
- writeRegister (REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
180
-
181
189
return 1 ;
182
190
}
183
191
192
+ bool LoRaClass::isTransmitting ()
193
+ {
194
+ if ((readRegister (REG_OP_MODE) & MODE_TX) == MODE_TX) {
195
+ return true ;
196
+ }
197
+
198
+ if (readRegister (REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) {
199
+ // clear IRQ's
200
+ writeRegister (REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
201
+ }
202
+
203
+ return false ;
204
+ }
205
+
184
206
int LoRaClass::parsePacket (int size)
185
207
{
186
208
int packetLength = 0 ;
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ class LoRaClass : public Stream {
32
32
void end ();
33
33
34
34
int beginPacket (int implicitHeader = false );
35
- int endPacket ();
35
+ int endPacket (bool async = false );
36
36
37
37
int parsePacket (int size = 0 );
38
38
int packetRssi ();
@@ -88,6 +88,7 @@ class LoRaClass : public Stream {
88
88
void implicitHeaderMode ();
89
89
90
90
void handleDio0Rise ();
91
+ bool isTransmitting ();
91
92
92
93
int getSpreadingFactor ();
93
94
long getSignalBandwidth ();
You can’t perform that action at this time.
0 commit comments