Skip to content
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

Improve on random() #190

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/github/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](https://www.semtech.com/apps/product.php?pn=SX1276) based boards including:
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=LoRa
version=0.8.0
author=Sandeep Mistry <sandeep.mistry@gmail.com>
maintainer=Sandeep Mistry <sandeep.mistry@gmail.com>
author=WereCatf <werecatf@runbox.com>
maintainer=WereCatf <werecatf@runbox.com>
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
38 changes: 37 additions & 1 deletion src/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,43 @@ void LoRaClass::setGain(uint8_t gain)

byte LoRaClass::random()
{
return readRegister(REG_RSSI_WIDEBAND);
uint8_t currMode = readRegister(REG_OP_MODE);
uint8_t retVal = 0, bits=7;
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm generally oppose to these kinds of checks, since this PR was originally submit, 'support' has been added for the 1310 board(sorry this hasn't got attention...) and doesn't even begin to handle many other boards out there. Can this check instead be performed against _onReceive itself?

_prevOnReceive = _onReceive;
onReceive(NULL);
receive();
#else
explicitHeaderMode();
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS);
#endif
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
if(_prevOnReceive) onReceive(_prevOnReceive);
#endif
return retVal;
}

void LoRaClass::setPins(int ss, int reset, int dio0)
Expand Down