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

Fix Random function #395

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ script:
- buildExampleSketch LoRaSenderNonBlocking
- buildExampleSketch LoRaSetSpread
- buildExampleSketch LoRaSetSyncWord
- buildExampleSketch LoRandom
8 changes: 8 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ LoRa.disableInvertIQ();

## Other functions

### beginRandom

Initializes the random number generator.

```
LoRa.beginRandom();
```

### Random

Generate a random byte, based on the Wideband RSSI measurement.
Expand Down
66 changes: 66 additions & 0 deletions examples/LoRandom/LoRandom.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/********************************************************************
LoRandomSeed

Example demonstrating how to generate random numbers using LoRa.

created 7 Sep 2020
by Kongduino
adapted by Andres Sabas
***********************************************************************/

#include <SPI.h>
#include <LoRa.h>

void setup() {
Serial.begin(9600);
while(!Serial);
Serial.print(F("\n\n\n[SX1276] Initializing ... "));
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.print("Setting up LoRa ");

LoRa.setTxPower(20, PA_OUTPUT_PA_BOOST_PIN);
LoRa.setPreambleLength(8);
LoRa.beginRandom();
Serial.println("End of setup\n\n");
}

void loop() {
byte randomBytes[256];
// We'll build a stock of random bytes for use in code
int randomIndex = 0;
unsigned int i;
for (i = 0; i < 256; i++) {
int x = LoRa.random();
randomBytes[i] = x;
}
randomIndex = 0;
hexDump(randomBytes, 256);
delay(2000);
}

void hexDump(unsigned char *buf, unsigned int len) {
String s = "|", t = "| |";
Serial.println(F(" |.0 .1 .2 .3 .4 .5 .6 .7 .8 .9 .a .b .c .d .e .f |"));
Serial.println(F(" +------------------------------------------------+ +----------------+"));
for ( unsigned int i = 0; i < len; i += 16) {
for (int j = 0; j < 16; j++) {
if (i + j >= len) {
s = s + " "; t = t + " ";
} else {
char c = buf[i + j];
if (c < 16) s = s + "0";
s = s + String(c, HEX) + " ";
if (c < 32 || c > 127) t = t + ".";
else t = t + (String(c));
}
}
int index = i / 16;
Serial.print(index, HEX); Serial.write('.');
Serial.println(s + t + "|");
s = "|"; t = "| |";
}
Serial.println(F(" +------------------------------------------------+ +----------------+"));
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ disableCrc KEYWORD2
enableInvertIQ KEYWORD2
disableInvertIQ KEYWORD2

beginRandom KEYWORD2
random KEYWORD2
setPins KEYWORD2
setSPIFrequency KEYWORD2
Expand Down
19 changes: 18 additions & 1 deletion src/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ void LoRaClass::sleep()
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_SLEEP);
}

void LoRaClass::continuosMode()
sabas1080 marked this conversation as resolved.
Show resolved Hide resolved
{
writeRegister(REG_OP_MODE, 0x72);
}

void LoRaClass::setTxPower(int level, int outputPin)
{
if (PA_OUTPUT_RFO_PIN == outputPin) {
Expand Down Expand Up @@ -607,9 +612,21 @@ void LoRaClass::setOCP(uint8_t mA)
writeRegister(REG_OCP, 0x20 | (0x1F & ocpTrim));
}

void LoRaClass::beginRandom() {
continuosMode();
setSignalBandwidth(125E3);
setCodingRate4(5);
setSpreadingFactor(7);
}

byte LoRaClass::random()
{
return readRegister(REG_RSSI_WIDEBAND);
uint8_t x = 0;
for (uint8_t j = 0; j < 8; j++) {
x += (readRegister(REG_RSSI_WIDEBAND) & 0x01);
x = x << 1;
}
return x;
}

void LoRaClass::setPins(int ss, int reset, int dio0)
Expand Down
4 changes: 3 additions & 1 deletion src/LoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class LoRaClass : public Stream {
#endif
void idle();
void sleep();
void continuosMode();

void setTxPower(int level, int outputPin = PA_OUTPUT_PA_BOOST_PIN);
void setFrequency(long frequency);
Expand All @@ -81,7 +82,8 @@ class LoRaClass : public Stream {
// deprecated
void crc() { enableCrc(); }
void noCrc() { disableCrc(); }


void beginRandom();
byte random();

void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
Expand Down