From b5c684253b62f2cae54dd2c96473b5c30b49eb8d Mon Sep 17 00:00:00 2001 From: WereCatf Date: Wed, 26 Sep 2018 01:08:20 +0300 Subject: [PATCH 1/8] Improve on random() The original code doesn't check for what mode the radio is in and returns 0, if the radio isn't in listening-mode; this code-change waits until any queued packets have been transmitted, checks the current mode, changes it to listening-mode, gets the random value and then proceeds to change the mode back to whatever it was. --- src/LoRa.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 94841b2..6c062e9 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -582,7 +582,18 @@ void LoRaClass::setOCP(uint8_t mA) byte LoRaClass::random() { - return readRegister(REG_RSSI_WIDEBAND); + uint8_t currMode = readRegister(REG_OP_MODE); + uint8_t retVal = 0; + + while(isTransmitting()); + + //We need to be listening to radio-traffic in order to generate random numbers + if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)){ receive(); delay(1); } + retVal = readRegister(REG_RSSI_WIDEBAND); + //Put the radio in the same mode as it was + if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)) writeRegister(REG_OP_MODE, currMode); + + return retVal; } void LoRaClass::setPins(int ss, int reset, int dio0) From e9bec969661d7b87b049d48ef1763d022c092836 Mon Sep 17 00:00:00 2001 From: WereCatf Date: Wed, 26 Sep 2018 01:37:13 +0300 Subject: [PATCH 2/8] Attempt to fix compilation for mkrwan1300 --- src/LoRa.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 6c062e9..22758db 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -588,7 +588,7 @@ byte LoRaClass::random() while(isTransmitting()); //We need to be listening to radio-traffic in order to generate random numbers - if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)){ receive(); delay(1); } + if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)){ this->receive(); delay(1); } retVal = readRegister(REG_RSSI_WIDEBAND); //Put the radio in the same mode as it was if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)) writeRegister(REG_OP_MODE, currMode); From 32a8d16b53693765f0c49c89d87133b543f3ceae Mon Sep 17 00:00:00 2001 From: WereCatf Date: Wed, 26 Sep 2018 02:50:18 +0300 Subject: [PATCH 3/8] Actually try to fix mkrwan1300 --- src/LoRa.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 22758db..8e6ab85 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -584,11 +584,19 @@ byte LoRaClass::random() { uint8_t currMode = readRegister(REG_OP_MODE); uint8_t retVal = 0; - + while(isTransmitting()); //We need to be listening to radio-traffic in order to generate random numbers - if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)){ this->receive(); delay(1); } + if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)){ +#ifndef ARDUINO_SAMD_MKRWAN1300 + receive(); +#else + explicitHeaderMode(); + writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS); +#endif + delay(1); + } retVal = readRegister(REG_RSSI_WIDEBAND); //Put the radio in the same mode as it was if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)) writeRegister(REG_OP_MODE, currMode); From f36fe47395000918097dc0a8f6262cc5dd31ea03 Mon Sep 17 00:00:00 2001 From: WereCatf Date: Wed, 26 Sep 2018 15:35:38 +0300 Subject: [PATCH 4/8] Yield()ing for ESP WDT, just in case --- src/LoRa.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 8e6ab85..d60998e 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -585,7 +585,7 @@ byte LoRaClass::random() uint8_t currMode = readRegister(REG_OP_MODE); uint8_t retVal = 0; - while(isTransmitting()); + while(isTransmitting()) yield(); //We need to be listening to radio-traffic in order to generate random numbers if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)){ From eb98ea9fc77559f8aebdc7fbc9a0b382f03bb576 Mon Sep 17 00:00:00 2001 From: WereCatf Date: Fri, 5 Oct 2018 19:03:59 +0300 Subject: [PATCH 5/8] Take onReceive() into account in random() --- src/LoRa.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index d60998e..5051886 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -584,23 +584,28 @@ byte LoRaClass::random() { uint8_t currMode = readRegister(REG_OP_MODE); uint8_t retVal = 0; - + void (*_prevOnReceive)(int) = NULL; + while(isTransmitting()) yield(); //We need to be listening to radio-traffic in order to generate random numbers if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)){ #ifndef ARDUINO_SAMD_MKRWAN1300 + _prevOnReceive = _onReceive; + onReceive(NULL); receive(); #else explicitHeaderMode(); writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS); #endif - delay(1); + delay(1); } retVal = readRegister(REG_RSSI_WIDEBAND); //Put the radio in the same mode as it was if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)) writeRegister(REG_OP_MODE, currMode); - +#ifndef ARDUINO_SAMD_MKRWAN1300 + if(_prevOnReceive) onReceive(_prevOnReceive); +#endif return retVal; } From 0a4e6bae480a7773b23146474836774c92ca5a42 Mon Sep 17 00:00:00 2001 From: WereCatf Date: Tue, 3 Nov 2020 22:59:50 +0200 Subject: [PATCH 6/8] Update info about this fork --- README.md | 8 +++++--- library.properties | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c89956a..7bdc8e7 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # Arduino LoRa -[![Build Status](https://travis-ci.org/sandeepmistry/arduino-LoRa.svg?branch=master)](https://travis-ci.org/sandeepmistry/arduino-LoRa) +[![Build Status](https://travis-ci.org/WereCatf/arduino-LoRa.svg?branch=master)](https://travis-ci.org/WereCatf/arduino-LoRa) An [Arduino](https://arduino.cc/) library for sending and receiving data using [LoRa](https://www.lora-alliance.org/) radios. +**This is a fork of [Sandeep Mistry's awesome library](https://github.com/sandeepmistry/arduino-LoRa) with the only meaningful change being a more useful and easier-to-use random() - function.** + ## Compatible Hardware * [Semtech SX1276/77/78/79](http://www.semtech.com/apps/product.php?pn=SX1276) based boards including: @@ -12,7 +14,7 @@ An [Arduino](https://arduino.cc/) library for sending and receiving data using [ * [Modtronix](http://modtronix.com/) [inAir4](http://modtronix.com/inair4.html), [inAir9](http://modtronix.com/inair9.html), and [inAir9B](http://modtronix.com/inair9b.html) * [Arduino MKR WAN 1300](https://store.arduino.cc/usa/mkr-wan-1300) * **NOTE:** Requires firmware v1.1.6 or later on the on-board Murata module. Please use the [MKRWANFWUpdate_standalone example](https://github.com/arduino-libraries/MKRWAN/blob/master/examples/MKRWANFWUpdate_standalone/MKRWANFWUpdate_standalone.ino) from latest [MKRWAN library](https://github.com/arduino-libraries/MKRWAN) release to update the firmware. - * **WARNING**: [LoRa.onReceive(...)](https://github.com/sandeepmistry/arduino-LoRa/blob/master/API.md#register-callback) and [LoRa.recieve()](https://github.com/sandeepmistry/arduino-LoRa/blob/master/API.md#receive-mode) is not compatible with this board! + * **WARNING**: [LoRa.onReceive(...)](https://github.com/WereCatf/arduino-LoRa/blob/master/API.md#register-callback) and [LoRa.recieve()](https://github.com/WereCatf/arduino-LoRa/blob/master/API.md#receive-mode) is not compatible with this board! ### Semtech SX1276/77/78/79 wiring @@ -47,7 +49,7 @@ An [Arduino](https://arduino.cc/) library for sending and receiving data using [ ```sh cd ~/Documents/Arduino/libraries/ -git clone https://github.com/sandeepmistry/arduino-LoRa LoRa +git clone https://github.com/WereCatf/arduino-LoRa LoRa ``` ## API diff --git a/library.properties b/library.properties index ea93ffc..7a14c37 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=LoRa version=0.7.2 -author=Sandeep Mistry -maintainer=Sandeep Mistry +author=WereCatf +maintainer=WereCatf sentence=An Arduino library for sending and receiving data using LoRa radios. paragraph=Supports Semtech SX1276/77/78/79 based boards/shields. category=Communication -url=https://github.com/sandeepmistry/arduino-LoRa +url=https://github.com/WereCatf/arduino-LoRa architectures=* includes=LoRa.h From 558d35b293dd76e98cbf0ef5e6abc5343235b500 Mon Sep 17 00:00:00 2001 From: WereCatf Date: Tue, 3 Nov 2020 23:06:30 +0200 Subject: [PATCH 7/8] Fix README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7bdc8e7..53b1c59 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Arduino LoRa -[![Build Status](https://travis-ci.org/WereCatf/arduino-LoRa.svg?branch=master)](https://travis-ci.org/WereCatf/arduino-LoRa) +[![Build Status](https://travis-ci.org/WereCatf/arduino-LoRa.svg?branch=master)](https://travis-ci.org/github/WereCatf/arduino-LoRa) An [Arduino](https://arduino.cc/) library for sending and receiving data using [LoRa](https://www.lora-alliance.org/) radios. From 14a1ca8c46c8cd1f6ce348d78631ff370e939cae Mon Sep 17 00:00:00 2001 From: WereCatf Date: Tue, 18 May 2021 13:08:12 +0300 Subject: [PATCH 8/8] More random random() --- src/LoRa.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 53d58e8..a298658 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -646,7 +646,7 @@ void LoRaClass::setGain(uint8_t gain) byte LoRaClass::random() { uint8_t currMode = readRegister(REG_OP_MODE); - uint8_t retVal = 0; + uint8_t retVal = 0, bits=7; void (*_prevOnReceive)(int) = NULL; while(isTransmitting()) yield(); @@ -664,6 +664,18 @@ byte LoRaClass::random() delay(1); } retVal = readRegister(REG_RSSI_WIDEBAND); + while(bits--) { + retVal<<=1; + while(1){ + // implement a basic von Neumann Extractor + uint8_t a=(readRegister(REG_RSSI_WIDEBAND) & 1); + if(a != (readRegister(REG_RSSI_WIDEBAND) & 1)){ + // put random, whitened bit in n + retVal |= a; + break; + } + } + } //Put the radio in the same mode as it was if(currMode != (MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS)) writeRegister(REG_OP_MODE, currMode); #ifndef ARDUINO_SAMD_MKRWAN1300