Skip to content

non blocking functions added #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ script:
- buildExampleSketch LoRaReceiver
- buildExampleSketch LoRaReceiverCallback
- buildExampleSketch LoRaSender
- buildExampleSketch LoRaSenderNonBlocking
- buildExampleSketch LoRaSetSpread
- buildExampleSketch LoRaSetSyncWord
7 changes: 5 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ LoRa.beginPacket(implicitHeader);

* `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

Expand All @@ -98,8 +98,11 @@ Returns the number of bytes written.
End the sequence of sending a packet.

```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.

Expand Down
35 changes: 35 additions & 0 deletions examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino
Original file line number Diff line number Diff line change
@@ -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++;
}
36 changes: 29 additions & 7 deletions src/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ void LoRaClass::end()

int LoRaClass::beginPacket(int implicitHeader)
{
if (isTransmitting()) {
return 0;
}

// put in standby mode
idle();

Expand All @@ -140,22 +144,40 @@ int LoRaClass::beginPacket(int implicitHeader)
return 1;
}

int LoRaClass::endPacket()
int LoRaClass::endPacket(bool async)
{
// put in TX mode
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);

// wait for TX done
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
yield();
if (async) {
// grace time is required for the radio
delayMicroseconds(150);
} else {
// wait for TX done
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
yield();
}
// clear IRQ's
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
}

// clear IRQ's
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);

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 packetLength = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/LoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LoRaClass : public Stream {
void end();

int beginPacket(int implicitHeader = false);
int endPacket();
int endPacket(bool async = false);

int parsePacket(int size = 0);
int packetRssi();
Expand Down Expand Up @@ -70,6 +70,7 @@ class LoRaClass : public Stream {
void implicitHeaderMode();

void handleDio0Rise();
bool isTransmitting();

uint8_t readRegister(uint8_t address);
void writeRegister(uint8_t address, uint8_t value);
Expand Down