mirror of
https://github.com/sandeepmistry/arduino-LoRa.git
synced 2025-06-06 23:01:00 +03:00
non blocking functions added (#62)
This commit is contained in:
parent
07bfead143
commit
9d2a8c9c82
@ -33,5 +33,6 @@ script:
|
|||||||
buildExampleSketch LoRaReceiverCallback;
|
buildExampleSketch LoRaReceiverCallback;
|
||||||
fi
|
fi
|
||||||
- buildExampleSketch LoRaSender
|
- buildExampleSketch LoRaSender
|
||||||
|
- buildExampleSketch LoRaSenderNonBlocking
|
||||||
- buildExampleSketch LoRaSetSpread
|
- buildExampleSketch LoRaSetSpread
|
||||||
- buildExampleSketch LoRaSetSyncWord
|
- buildExampleSketch LoRaSetSyncWord
|
||||||
|
7
API.md
7
API.md
@ -82,7 +82,7 @@ LoRa.beginPacket(implicitHeader);
|
|||||||
|
|
||||||
* `implicitHeader` - (optional) `true` enables implicit header mode, `false` enables explicit header mode (default)
|
* `implicitHeader` - (optional) `true` enables implicit header mode, `false` enables explicit header mode (default)
|
||||||
|
|
||||||
Returns `1` on success, `0` on failure.
|
Returns `1` if radio is ready to transmit, `0` if busy or on failure.
|
||||||
|
|
||||||
### Writing
|
### Writing
|
||||||
|
|
||||||
@ -109,8 +109,11 @@ Returns the number of bytes written.
|
|||||||
End the sequence of sending a packet.
|
End the sequence of sending a packet.
|
||||||
|
|
||||||
```arduino
|
```arduino
|
||||||
LoRa.endPacket()
|
LoRa.endPacket();
|
||||||
|
|
||||||
|
LoRa.endPacket(async);
|
||||||
```
|
```
|
||||||
|
* `async` - (optional) `true` enables non-blocking mode, `false` waits for transmission to be completed (default)
|
||||||
|
|
||||||
Returns `1` on success, `0` on failure.
|
Returns `1` on success, `0` on failure.
|
||||||
|
|
||||||
|
35
examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino
Normal file
35
examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include <SPI.h>
|
||||||
|
#include <LoRa.h>
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
while (!Serial);
|
||||||
|
|
||||||
|
Serial.println("LoRa Sender non-blocking");
|
||||||
|
|
||||||
|
if (!LoRa.begin(915E6)) {
|
||||||
|
Serial.println("Starting LoRa failed!");
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// wait until the radio is ready to send a packet
|
||||||
|
while (LoRa.beginPacket() == 0) {
|
||||||
|
Serial.print("waiting for radio ... ");
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print("Sending packet non-blocking: ");
|
||||||
|
Serial.println(counter);
|
||||||
|
|
||||||
|
// send in async / non-blocking mode
|
||||||
|
LoRa.beginPacket();
|
||||||
|
LoRa.print("hello ");
|
||||||
|
LoRa.print(counter);
|
||||||
|
LoRa.endPacket(true); // true = async / non-blocking mode
|
||||||
|
|
||||||
|
counter++;
|
||||||
|
}
|
26
src/LoRa.cpp
26
src/LoRa.cpp
@ -149,6 +149,10 @@ void LoRaClass::end()
|
|||||||
|
|
||||||
int LoRaClass::beginPacket(int implicitHeader)
|
int LoRaClass::beginPacket(int implicitHeader)
|
||||||
{
|
{
|
||||||
|
if (isTransmitting()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// put in standby mode
|
// put in standby mode
|
||||||
idle();
|
idle();
|
||||||
|
|
||||||
@ -165,22 +169,40 @@ int LoRaClass::beginPacket(int implicitHeader)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int LoRaClass::endPacket()
|
int LoRaClass::endPacket(bool async)
|
||||||
{
|
{
|
||||||
// put in TX mode
|
// put in TX mode
|
||||||
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
|
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
|
||||||
|
|
||||||
|
if (async) {
|
||||||
|
// grace time is required for the radio
|
||||||
|
delayMicroseconds(150);
|
||||||
|
} else {
|
||||||
// wait for TX done
|
// wait for TX done
|
||||||
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
|
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear IRQ's
|
// clear IRQ's
|
||||||
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
|
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool LoRaClass::isTransmitting()
|
||||||
|
{
|
||||||
|
if ((readRegister(REG_OP_MODE) & MODE_TX) == MODE_TX) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) {
|
||||||
|
// clear IRQ's
|
||||||
|
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int LoRaClass::parsePacket(int size)
|
int LoRaClass::parsePacket(int size)
|
||||||
{
|
{
|
||||||
int packetLength = 0;
|
int packetLength = 0;
|
||||||
|
@ -32,7 +32,7 @@ public:
|
|||||||
void end();
|
void end();
|
||||||
|
|
||||||
int beginPacket(int implicitHeader = false);
|
int beginPacket(int implicitHeader = false);
|
||||||
int endPacket();
|
int endPacket(bool async = false);
|
||||||
|
|
||||||
int parsePacket(int size = 0);
|
int parsePacket(int size = 0);
|
||||||
int packetRssi();
|
int packetRssi();
|
||||||
@ -88,6 +88,7 @@ private:
|
|||||||
void implicitHeaderMode();
|
void implicitHeaderMode();
|
||||||
|
|
||||||
void handleDio0Rise();
|
void handleDio0Rise();
|
||||||
|
bool isTransmitting();
|
||||||
|
|
||||||
int getSpreadingFactor();
|
int getSpreadingFactor();
|
||||||
long getSignalBandwidth();
|
long getSignalBandwidth();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user