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
26 changes: 25 additions & 1 deletion src/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,31 @@ 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;
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);
//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