From a73831b9016380ec40fa978c512bd8fa2099fdd6 Mon Sep 17 00:00:00 2001 From: "Lang, Samuel" Date: Fri, 17 Nov 2017 11:42:54 +0100 Subject: [PATCH 1/4] non blocking functions added --- .../LoRaSenderNonBlocking.ino | 40 +++++++++++++++++++ src/LoRa.cpp | 20 ++++++++++ src/LoRa.h | 2 + 3 files changed, 62 insertions(+) create mode 100644 examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino diff --git a/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino b/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino new file mode 100644 index 0000000..4a2e83f --- /dev/null +++ b/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino @@ -0,0 +1,40 @@ +#include +#include + +int counter = 0; + +void setup() +{ + Serial.begin(9600); + while (!Serial) + ; + + Serial.println("LoRa Sender"); + + if (!LoRa.begin(915E6)) + { + Serial.println("Starting LoRa failed!"); + while (1) + ; + } +} + +void loop() +{ + if (LoRa.isTransmitting()) + { + delay(100); + Serial.print('w'); + return; + } + Serial.print("\nSending packet: "); + Serial.println(counter); + + // send packet + LoRa.beginPacket(); + LoRa.print("hello "); + LoRa.print(counter); + LoRa.endPacketasync(); + + counter++; +} \ No newline at end of file diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 94b9e3e..f25bab0 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -152,6 +152,26 @@ int LoRaClass::endPacket() return 1; } +void LoRaClass::endPacketasync() +{ + // put in TX mode + writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX); + // apparently this grace time is required for the radio + delayMicroseconds(150); +} + +bool LoRaClass::isTransmitting() +{ + if ((readRegister(REG_OP_MODE) & MODE_TX) == MODE_TX) + return true; + + if (!(readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) + // clear IRQ's + writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK); + + return false; +} + int LoRaClass::parsePacket(int size) { int packetLength = 0; diff --git a/src/LoRa.h b/src/LoRa.h index 7616ebd..a846c62 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -23,6 +23,8 @@ class LoRaClass : public Stream { int beginPacket(int implicitHeader = false); int endPacket(); + void endPacketasync(); + bool isTransmitting(); int parsePacket(int size = 0); int packetRssi(); From caa21c2d517d0a41a9b5ff03b150afca1386b673 Mon Sep 17 00:00:00 2001 From: "Lang, Samuel" Date: Fri, 17 Nov 2017 11:54:21 +0100 Subject: [PATCH 2/4] extendedTravis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 2dcac81..703d0c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,5 +28,6 @@ script: - buildExampleSketch LoRaReceiver - buildExampleSketch LoRaReceiverCallback - buildExampleSketch LoRaSender + - buildExampleSketch LoRaSenderNonBlocking - buildExampleSketch LoRaSetSpread - buildExampleSketch LoRaSetSyncWord From 12a91128d1724cd13074832d4189b0af83c83d51 Mon Sep 17 00:00:00 2001 From: "Lang, Samuel" Date: Wed, 22 Nov 2017 09:25:31 +0100 Subject: [PATCH 3/4] lastchanges --- .../LoRaSenderNonBlocking.ino | 8 +++-- src/LoRa.cpp | 34 ++++++++++--------- src/LoRa.h | 3 +- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino b/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino index 4a2e83f..aa16394 100644 --- a/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino +++ b/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino @@ -21,12 +21,12 @@ void setup() void loop() { - if (LoRa.isTransmitting()) + while (LoRa.beginPacket() == 0) { delay(100); Serial.print('w'); - return; } + Serial.print("\nSending packet: "); Serial.println(counter); @@ -34,7 +34,9 @@ void loop() LoRa.beginPacket(); LoRa.print("hello "); LoRa.print(counter); - LoRa.endPacketasync(); + + // send using async API + LoRa.endPacket(true); counter++; } \ No newline at end of file diff --git a/src/LoRa.cpp b/src/LoRa.cpp index f25bab0..a11dad4 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -122,6 +122,9 @@ void LoRaClass::end() int LoRaClass::beginPacket(int implicitHeader) { + if (isTransmitting) + return 0; + // put in standby mode idle(); @@ -138,26 +141,25 @@ 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); - - // clear IRQ's - writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK); - - return 1; -} - -void LoRaClass::endPacketasync() -{ - // put in TX mode - writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX); - // apparently this grace time is required for the radio - delayMicroseconds(150); + if (async) + { + // grace time is required for the radio + delayMicroseconds(150); + return 0; + } 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); + return 1; + } } bool LoRaClass::isTransmitting() diff --git a/src/LoRa.h b/src/LoRa.h index a846c62..e3e20c8 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -22,8 +22,7 @@ class LoRaClass : public Stream { void end(); int beginPacket(int implicitHeader = false); - int endPacket(); - void endPacketasync(); + int endPacket(bool async = false); bool isTransmitting(); int parsePacket(int size = 0); From 9fa2e4f2bff3beaf6c2782fa4ffa5ffa38d2bded Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Sun, 19 Aug 2018 10:29:23 -0400 Subject: [PATCH 4/4] Code style and doc + example edits --- API.md | 12 ++++--- .../LoRaSenderNonBlocking.ino | 31 +++++++------------ src/LoRa.cpp | 16 +++++----- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/API.md b/API.md index ecff644..7fb8c1a 100644 --- a/API.md +++ b/API.md @@ -71,7 +71,7 @@ LoRa.beginPacket(implicitHeader); * `implicitHeader` - (optional) `true` enables implicit header mode, `false` enables explicit header mode (default) -Returns `1` if radio is ready to transmit, `0` if busy or on failure. Consider your code responsible for testing first before actually writing to radio. +Returns `1` if radio is ready to transmit, `0` if busy or on failure. ### Writing @@ -95,13 +95,15 @@ Returns the number of bytes written. ### End packet -End the sequence of sending a packet. Set `async` to `true` in order to switch to non-blocking mode. +End the sequence of sending a packet. ```arduino -LoRa.endPacket(bool async) +LoRa.endPacket(); + +LoRa.endPacket(async); ``` - * `async` - (optional) `true` enables non-blocking mode, `false` returns after transmission (default) - + * `async` - (optional) `true` enables non-blocking mode, `false` waits for transmission to be completed (default) + Returns `1` on success, `0` on failure. ## Receiving data diff --git a/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino b/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino index 8d074a4..0f39077 100644 --- a/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino +++ b/examples/LoRaSenderNonBlocking/LoRaSenderNonBlocking.ino @@ -3,40 +3,33 @@ int counter = 0; -void setup() -{ +void setup() { Serial.begin(9600); - while (!Serial) - ; + while (!Serial); Serial.println("LoRa Sender non-blocking"); - if (!LoRa.begin(915E6)) - { + if (!LoRa.begin(915E6)) { Serial.println("Starting LoRa failed!"); - while (1) - ; + while (1); } } -void loop() -{ - while (LoRa.beginPacket() == 0) - { +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('w'); } - Serial.print("\nSending packet non-blocking: "); + Serial.print("Sending packet non-blocking: "); Serial.println(counter); - // send packet + // send in async / non-blocking mode LoRa.beginPacket(); LoRa.print("hello "); LoRa.print(counter); - - // send using async API - LoRa.endPacket(true); + LoRa.endPacket(true); // true = async / non-blocking mode counter++; -} \ No newline at end of file +} diff --git a/src/LoRa.cpp b/src/LoRa.cpp index ed99be3..f257887 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -124,8 +124,9 @@ void LoRaClass::end() int LoRaClass::beginPacket(int implicitHeader) { - if (isTransmitting()) + if (isTransmitting()) { return 0; + } // put in standby mode idle(); @@ -148,11 +149,9 @@ int LoRaClass::endPacket(bool async) // put in TX mode writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX); - if (async) - { + if (async) { // grace time is required for the radio delayMicroseconds(150); - return 0; } else { // wait for TX done while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) { @@ -160,18 +159,21 @@ int LoRaClass::endPacket(bool async) } // clear IRQ's writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK); - return 1; } + + return 1; } bool LoRaClass::isTransmitting() { - if ((readRegister(REG_OP_MODE) & MODE_TX) == MODE_TX) + if ((readRegister(REG_OP_MODE) & MODE_TX) == MODE_TX) { return true; + } - if (!(readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) + if (readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) { // clear IRQ's writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK); + } return false; }