Added {} to control structures for clarity (#600)

This commit is contained in:
Thomas Mørch
2016-10-16 12:46:42 +02:00
committed by Olivier
parent 80659ddb00
commit 54776a4520
34 changed files with 740 additions and 406 deletions
+96 -71
View File
@@ -6,23 +6,23 @@
// **********************************************************************************
// License
// **********************************************************************************
// This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General
// This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General
// Public License along with this program.
// If not, see <http://www.gnu.org/licenses/>.
//
// Licence can be viewed at
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
@@ -93,17 +93,18 @@ bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID)
unsigned long start = millis();
uint8_t timeout = 50;
do {
writeReg(REG_SYNCVALUE1, 0xAA);
writeReg(REG_SYNCVALUE1, 0xAA);
yield();
} while (readReg(REG_SYNCVALUE1) != 0xaa && millis()-start < timeout);
start = millis();
do {
writeReg(REG_SYNCVALUE1, 0x55);
writeReg(REG_SYNCVALUE1, 0x55);
yield();
} while (readReg(REG_SYNCVALUE1) != 0x55 && millis()-start < timeout);
for (uint8_t i = 0; CONFIG[i][0] != 255; i++)
for (uint8_t i = 0; CONFIG[i][0] != 255; i++) {
writeReg(CONFIG[i][0], CONFIG[i][1]);
}
// Encryption is persistent between resets and can trip you up during debugging.
// Disable it during initialization so we always start from a known state.
@@ -113,10 +114,11 @@ bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID)
setMode(RF69_MODE_STANDBY);
start = millis();
while (((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) && millis()-start < timeout) {
yield();
yield();
} // wait for ModeReady
if (millis()-start >= timeout)
if (millis()-start >= timeout) {
return false;
}
attachInterrupt(_interruptNum, RFM69::isr0, RISING);
selfPointer = this;
@@ -149,17 +151,22 @@ void RFM69::setFrequency(uint32_t freqHz)
void RFM69::setMode(uint8_t newMode)
{
if (newMode == _mode)
if (newMode == _mode) {
return;
}
switch (newMode) {
case RF69_MODE_TX:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_TRANSMITTER);
if (_isRFM69HW) setHighPowerRegs(true);
if (_isRFM69HW) {
setHighPowerRegs(true);
}
break;
case RF69_MODE_RX:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_RECEIVER);
if (_isRFM69HW) setHighPowerRegs(false);
if (_isRFM69HW) {
setHighPowerRegs(false);
}
break;
case RF69_MODE_SYNTH:
writeReg(REG_OPMODE, (readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SYNTHESIZER);
@@ -209,7 +216,9 @@ void RFM69::setNetwork(uint8_t networkID)
void RFM69::setPowerLevel(uint8_t powerLevel)
{
_powerLevel = (powerLevel > 31 ? 31 : powerLevel);
if (_isRFM69HW) _powerLevel /= 2;
if (_isRFM69HW) {
_powerLevel /= 2;
}
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0xE0) | _powerLevel);
}
@@ -227,7 +236,9 @@ void RFM69::send(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool
{
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
uint32_t now = millis();
while (!canSend() && millis() - now < RF69_CSMA_LIMIT_MS) receiveDone();
while (!canSend() && millis() - now < RF69_CSMA_LIMIT_MS) {
receiveDone();
}
sendFrame(toAddress, buffer, bufferSize, requestACK, false);
}
@@ -258,8 +269,9 @@ bool RFM69::sendWithRetry(uint8_t toAddress, const void* buffer, uint8_t bufferS
// should be polled immediately after sending a packet with ACK request
bool RFM69::ACKReceived(uint8_t fromNodeID) {
if (receiveDone())
if (receiveDone()) {
return (SENDERID == fromNodeID || fromNodeID == RF69_BROADCAST_ADDR) && ACK_RECEIVED;
}
return false;
}
@@ -287,21 +299,25 @@ void RFM69::sendACK(const void* buffer, uint8_t bufferSize) {
void RFM69::interruptHook(uint8_t CTLbyte) {
(void)CTLbyte;
};
// internal function
void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK, bool sendACK)
{
setMode(RF69_MODE_STANDBY); // turn off receiver to prevent reception while filling fifo
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // wait for ModeReady
while ((readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00) {} // wait for ModeReady
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent"
if (bufferSize > RF69_MAX_DATA_LEN) bufferSize = RF69_MAX_DATA_LEN;
if (bufferSize > RF69_MAX_DATA_LEN) {
bufferSize = RF69_MAX_DATA_LEN;
}
// control byte
uint8_t CTLbyte = 0x00;
if (sendACK)
if (sendACK) {
CTLbyte = RFM69_CTL_SENDACK;
else if (requestACK)
}
else if (requestACK) {
CTLbyte = RFM69_CTL_REQACK;
}
// write to FIFO
select();
@@ -311,14 +327,15 @@ void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
SPI.transfer(_address);
SPI.transfer(CTLbyte);
for (uint8_t i = 0; i < bufferSize; i++)
for (uint8_t i = 0; i < bufferSize; i++) {
SPI.transfer(((uint8_t*) buffer)[i]);
}
unselect();
// no need to wait for transmit mode to be ready since its handled by the radio
setMode(RF69_MODE_TX);
uint32_t txStart = millis();
while (hwDigitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS); // wait for DIO0 to turn HIGH signalling transmission finish
while (hwDigitalRead(_interruptPin) == 0 && millis() - txStart < RF69_TX_LIMIT_MS) {} // wait for DIO0 to turn HIGH signalling transmission finish
//while (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT == 0x00); // wait for ModeReady
setMode(RF69_MODE_STANDBY);
}
@@ -352,14 +369,16 @@ void RFM69::interruptHandler() {
ACK_RECEIVED = CTLbyte & RFM69_CTL_SENDACK; // extract ACK-received flag
ACK_REQUESTED = CTLbyte & RFM69_CTL_REQACK; // extract ACK-requested flag
interruptHook(CTLbyte); // TWS: hook to derived class interrupt function
for (uint8_t i = 0; i < DATALEN; i++)
{
DATA[i] = SPI.transfer(0);
}
if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string
if (DATALEN < RF69_MAX_DATA_LEN) {
DATA[DATALEN] = 0; // add null at end of string
}
unselect();
setMode(RF69_MODE_RX);
}
@@ -379,8 +398,9 @@ void RFM69::receiveBegin() {
ACK_REQUESTED = 0;
ACK_RECEIVED = 0;
RSSI = 0;
if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)
if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) {
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
}
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); // set DIO0 to "PAYLOADREADY" in receive mode
setMode(RF69_MODE_RX);
}
@@ -414,8 +434,9 @@ void RFM69::encrypt(const char* key) {
{
select();
SPI.transfer(REG_AESKEY1 | 0x80);
for (uint8_t i = 0; i < 16; i++)
for (uint8_t i = 0; i < 16; i++) {
SPI.transfer(key[i]);
}
unselect();
}
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFE) | (key ? 1 : 0));
@@ -428,7 +449,7 @@ int16_t RFM69::readRSSI(bool forceTrigger) {
{
// RSSI trigger not needed if DAGC is in continuous mode
writeReg(REG_RSSICONFIG, RF_RSSI_START);
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00); // wait for RSSI_Ready
while ((readReg(REG_RSSICONFIG) & RF_RSSI_DONE) == 0x00) {} // wait for RSSI_Ready
}
rssi = -readReg(REG_RSSIVALUE);
rssi >>= 1;
@@ -489,10 +510,12 @@ void RFM69::promiscuous(bool onOff) {
void RFM69::setHighPower(bool onOff) {
_isRFM69HW = onOff;
writeReg(REG_OCP, _isRFM69HW ? RF_OCP_OFF : RF_OCP_ON);
if (_isRFM69HW) // turning ON
if (_isRFM69HW) { // turning ON
writeReg(REG_PALEVEL, (readReg(REG_PALEVEL) & 0x1F) | RF_PALEVEL_PA1_ON | RF_PALEVEL_PA2_ON); // enable P1 & P2 amplifier stages
else
}
else {
writeReg(REG_PALEVEL, RF_PALEVEL_PA0_ON | RF_PALEVEL_PA1_OFF | RF_PALEVEL_PA2_OFF | _powerLevel); // enable P0 only
}
}
// internal function
@@ -501,7 +524,7 @@ void RFM69::setHighPowerRegs(bool onOff) {
writeReg(REG_TESTPA2, onOff ? 0x7C : 0x70);
}
// set the slave select (CS) pin
// set the slave select (CS) pin
void RFM69::setCS(uint8_t newSPISlaveSelect) {
_slaveSelectPin = newSPISlaveSelect;
hwDigitalWrite(_slaveSelectPin, HIGH);
@@ -519,7 +542,9 @@ void SerialWrite ( uint8_t c ) {
}
void SerialPrint_P(PGM_P str, void (*f)(uint8_t) = SerialWrite ) {
for (uint8_t c; (c = pgm_read_byte(str)); str++) (*f)(c);
for (uint8_t c; (c = pgm_read_byte(str)); str++) {
(*f)(c);
}
}
#endif
@@ -527,7 +552,7 @@ void RFM69::readAllRegs()
{
uint8_t regVal;
#if REGISTER_DETAIL
#if REGISTER_DETAIL
int capVal;
//... State Variables for intelligent decoding
@@ -536,7 +561,7 @@ void RFM69::readAllRegs()
int freqDev = 0;
long freqCenter = 0;
#endif
Serial.println("Address - HEX - BIN");
for (uint8_t regAddr = 1; regAddr <= 0x4F; regAddr++)
{
@@ -551,8 +576,8 @@ void RFM69::readAllRegs()
Serial.print(" - ");
Serial.println(regVal,BIN);
#if REGISTER_DETAIL
switch ( regAddr )
#if REGISTER_DETAIL
switch ( regAddr )
{
case 0x1 : {
SerialPrint ( "Controls the automatic Sequencer ( see section 4.2 )\nSequencerOff : " );
@@ -561,19 +586,19 @@ void RFM69::readAllRegs()
} else {
SerialPrint ( "0 -> Operating mode as selected with Mode bits in RegOpMode is automatically reached with the Sequencer\n" );
}
SerialPrint( "\nEnables Listen mode, should be enabled whilst in Standby mode:\nListenOn : " );
if ( 0x40 & regVal ) {
SerialPrint ( "1 -> On\n" );
} else {
SerialPrint ( "0 -> Off ( see section 4.3)\n" );
}
SerialPrint( "\nAborts Listen mode when set together with ListenOn=0 See section 4.3.4 for details (Always reads 0.)\n" );
if ( 0x20 & regVal ) {
SerialPrint ( "ERROR - ListenAbort should NEVER return 1 this is a write only register\n" );
}
SerialPrint("\nTransceiver's operating modes:\nMode : ");
capVal = (regVal >> 2) & 0x7;
if ( capVal == 0b000 ) {
@@ -593,9 +618,9 @@ void RFM69::readAllRegs()
SerialPrint ( "\n" );
break;
}
case 0x2 : {
SerialPrint("Data Processing mode:\nDataMode : ");
capVal = (regVal >> 5) & 0x3;
if ( capVal == 0b00 ) {
@@ -607,7 +632,7 @@ void RFM69::readAllRegs()
} else if ( capVal == 0b11 ) {
SerialPrint ( "11 -> Continuous mode without bit synchronizer\n" );
}
SerialPrint("\nModulation scheme:\nModulation Type : ");
capVal = (regVal >> 3) & 0x3;
if ( capVal == 0b00 ) {
@@ -620,7 +645,7 @@ void RFM69::readAllRegs()
} else if ( capVal == 0b11 ) {
SerialPrint ( "11 -> reserved\n" );
}
SerialPrint("\nData shaping: ");
if ( modeFSK ) {
SerialPrint( "in FSK:\n" );
@@ -650,16 +675,16 @@ void RFM69::readAllRegs()
SerialPrint ( "ERROR - 11 is reserved\n" );
}
}
SerialPrint ( "\n" );
break;
}
case 0x3 : {
bitRate = (regVal << 8);
break;
}
case 0x4 : {
bitRate |= regVal;
SerialPrint ( "Bit Rate (Chip Rate when Manchester encoding is enabled)\nBitRate : ");
@@ -668,12 +693,12 @@ void RFM69::readAllRegs()
SerialPrint( "\n" );
break;
}
case 0x5 : {
freqDev = ( (regVal & 0x3f) << 8 );
break;
}
case 0x6 : {
freqDev |= regVal;
SerialPrint( "Frequency deviation\nFdev : " );
@@ -682,20 +707,20 @@ void RFM69::readAllRegs()
SerialPrint ( "\n" );
break;
}
case 0x7 : {
unsigned long tempVal = regVal;
freqCenter = ( tempVal << 16 );
break;
}
case 0x8 : {
unsigned long tempVal = regVal;
freqCenter = freqCenter | ( tempVal << 8 );
break;
}
case 0x9 : {
case 0x9 : {
freqCenter = freqCenter | regVal;
SerialPrint ( "RF Carrier frequency\nFRF : " );
unsigned long val = 61UL * freqCenter;
@@ -711,7 +736,7 @@ void RFM69::readAllRegs()
} else {
SerialPrint ( "0 -> RC calibration is in progress\n" );
}
SerialPrint ( "\n" );
break;
}
@@ -726,7 +751,7 @@ void RFM69::readAllRegs()
SerialPrint ( "\n" );
break;
}
case 0xc : {
SerialPrint ( "Reserved\n\n" );
break;
@@ -745,7 +770,7 @@ void RFM69::readAllRegs()
} else if ( val == 0b11 ) {
SerialPrint ( "11 -> 262 ms\n" );
}
SerialPrint ( "\nResolution of Listen mode Rx time (calibrated RC osc):\nListenResolRx : " );
val = (regVal >> 4) & 0x3;
if ( val == 0b00 ) {
@@ -764,7 +789,7 @@ void RFM69::readAllRegs()
} else {
SerialPrint ( "0 -> signal strength is above RssiThreshold\n" );
}
SerialPrint ( "\nAction taken after acceptance of a packet in Listen mode:\nListenEnd : " );
val = (regVal >> 1 ) & 0x3;
if ( val == 0b00 ) {
@@ -776,12 +801,12 @@ void RFM69::readAllRegs()
} else if ( val == 0b11 ) {
SerialPrint ( "11 -> Reserved\n" );
}
SerialPrint ( "\n" );
break;
}
default : {
}
}
@@ -794,12 +819,12 @@ uint8_t RFM69::readTemperature(uint8_t calFactor) // returns centigrade
{
setMode(RF69_MODE_STANDBY);
writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING));
while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING)) {}
return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; // 'complement' corrects the slope, rising temp = rising val
} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction
void RFM69::rcCalibration()
{
writeReg(REG_OSC1, RF_OSC1_RCCAL_START);
while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00);
}
while ((readReg(REG_OSC1) & RF_OSC1_RCCAL_DONE) == 0x00) {}
}