Skip to content

Commit 9d2a8c9

Browse files
universam1sandeepmistry
authored andcommitted
non blocking functions added (#62)
1 parent 07bfead commit 9d2a8c9

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ script:
3333
buildExampleSketch LoRaReceiverCallback;
3434
fi
3535
- buildExampleSketch LoRaSender
36+
- buildExampleSketch LoRaSenderNonBlocking
3637
- buildExampleSketch LoRaSetSpread
3738
- buildExampleSketch LoRaSetSyncWord

API.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ LoRa.beginPacket(implicitHeader);
8282

8383
* `implicitHeader` - (optional) `true` enables implicit header mode, `false` enables explicit header mode (default)
8484

85-
Returns `1` on success, `0` on failure.
85+
Returns `1` if radio is ready to transmit, `0` if busy or on failure.
8686

8787
### Writing
8888

@@ -109,8 +109,11 @@ Returns the number of bytes written.
109109
End the sequence of sending a packet.
110110

111111
```arduino
112-
LoRa.endPacket()
112+
LoRa.endPacket();
113+
114+
LoRa.endPacket(async);
113115
```
116+
* `async` - (optional) `true` enables non-blocking mode, `false` waits for transmission to be completed (default)
114117

115118
Returns `1` on success, `0` on failure.
116119

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

src/LoRa.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ void LoRaClass::end()
149149

150150
int LoRaClass::beginPacket(int implicitHeader)
151151
{
152+
if (isTransmitting()) {
153+
return 0;
154+
}
155+
152156
// put in standby mode
153157
idle();
154158

@@ -165,22 +169,40 @@ int LoRaClass::beginPacket(int implicitHeader)
165169
return 1;
166170
}
167171

168-
int LoRaClass::endPacket()
172+
int LoRaClass::endPacket(bool async)
169173
{
170174
// put in TX mode
171175
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
172176

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);
176187
}
177188

178-
// clear IRQ's
179-
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
180-
181189
return 1;
182190
}
183191

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+
184206
int LoRaClass::parsePacket(int size)
185207
{
186208
int packetLength = 0;

src/LoRa.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class LoRaClass : public Stream {
3232
void end();
3333

3434
int beginPacket(int implicitHeader = false);
35-
int endPacket();
35+
int endPacket(bool async = false);
3636

3737
int parsePacket(int size = 0);
3838
int packetRssi();
@@ -88,6 +88,7 @@ class LoRaClass : public Stream {
8888
void implicitHeaderMode();
8989

9090
void handleDio0Rise();
91+
bool isTransmitting();
9192

9293
int getSpreadingFactor();
9394
long getSignalBandwidth();

0 commit comments

Comments
 (0)