Repo restyled using astyle (#683)

The rules are in .tools/astyle/config/style.cfg
and can also be reviewed at
https://www.mysensors.org/view/260#coding-guidelines-core-library
This commit is contained in:
Patrick Fallberg
2016-12-07 23:44:29 +01:00
committed by GitHub
parent d4c4ab82ea
commit 8e1ef13804
167 changed files with 16103 additions and 15168 deletions
+125 -98
View File
@@ -26,19 +26,24 @@
#if defined(LINUX_ARCH_RASPBERRYPI)
uint8_t spi_rxbuff[32 + 1]; //SPI receive buffer (payload max 32 bytes, MYS protocol limitation)
uint8_t spi_txbuff[32 + 1]; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command, MYS protocol limitation)
uint8_t spi_txbuff[32 +
1]; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command, MYS protocol limitation)
#endif
LOCAL void RFM95_csn(const bool level) {
LOCAL void RFM95_csn(const bool level)
{
hwDigitalWrite(MY_RFM95_SPI_CS, level);
}
LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t len, const bool aReadMode) {
LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_t len,
const bool aReadMode)
{
uint8_t status;
uint8_t* current = buf;
#if !defined(MY_SOFTSPI)
_SPI.beginTransaction(SPISettings(MY_RFM95_SPI_MAX_SPEED, MY_RFM95_SPI_DATA_ORDER, MY_RFM95_SPI_DATA_MODE));
#endif
#if !defined(MY_SOFTSPI)
_SPI.beginTransaction(SPISettings(MY_RFM95_SPI_MAX_SPEED, MY_RFM95_SPI_DATA_ORDER,
MY_RFM95_SPI_DATA_MODE));
#endif
RFM95_csn(LOW);
#if defined(LINUX_ARCH_RASPBERRYPI)
uint8_t * prx = spi_rxbuff;
@@ -49,8 +54,7 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
while (len--) {
if (aReadMode) {
*ptx++ = RF24_NOP;
}
else {
} else {
*ptx++ = *current++;
}
}
@@ -58,16 +62,14 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
if (aReadMode) {
if (size == 2) {
status = *++prx; // result is 2nd byte of receive buffer
}
else {
} else {
status = *prx++; // status is 1st byte of receive buffer
// decrement before to skip status byte
// decrement before to skip status byte
while (--size) {
*buf++ = *prx++;
}
}
}
else {
} else {
status = *prx; // status is 1st byte of receive buffer
}
#else
@@ -78,24 +80,27 @@ LOCAL uint8_t RFM95_spiMultiByteTransfer(const uint8_t cmd, uint8_t* buf, uint8_
if (buf != NULL) {
*current++ = status;
}
} else {
status = _SPI.transfer(*current++);
}
else status = _SPI.transfer(*current++);
}
#endif
RFM95_csn(HIGH);
#if !defined(MY_SOFTSPI)
_SPI.endTransaction();
#endif
#if !defined(MY_SOFTSPI)
_SPI.endTransaction();
#endif
return status;
}
// low level register access
LOCAL uint8_t RFM95_RAW_readByteRegister(const uint8_t address) {
LOCAL uint8_t RFM95_RAW_readByteRegister(const uint8_t address)
{
return RFM95_spiMultiByteTransfer(address, NULL, 1, true);
}
// low level register access
LOCAL uint8_t RFM95_RAW_writeByteRegister(const uint8_t address, uint8_t value) {
LOCAL uint8_t RFM95_RAW_writeByteRegister(const uint8_t address, uint8_t value)
{
return RFM95_spiMultiByteTransfer(address, &value, 1, false);
}
@@ -109,24 +114,25 @@ LOCAL uint8_t RFM95_RAW_writeByteRegister(const uint8_t address, uint8_t value)
// check RegPktRssiValue (0x1A) vs. RegRssiValue (0x1B)
LOCAL bool RFM95_initialise(const float frequency) {
LOCAL bool RFM95_initialise(const float frequency)
{
RFM95_DEBUG(PSTR("RFM95:INIT\n"));
// reset radio module if rst pin defined
#if defined(MY_RFM95_RST_PIN)
hwPinMode(MY_RFM95_RST_PIN, OUTPUT);
hwDigitalWrite(MY_RFM95_RST_PIN, HIGH);
// 100uS
delayMicroseconds(100);
hwDigitalWrite(MY_RFM95_RST_PIN, LOW);
// wait until chip ready
delay(5);
#endif
#if defined(MY_RFM95_RST_PIN)
hwPinMode(MY_RFM95_RST_PIN, OUTPUT);
hwDigitalWrite(MY_RFM95_RST_PIN, HIGH);
// 100uS
delayMicroseconds(100);
hwDigitalWrite(MY_RFM95_RST_PIN, LOW);
// wait until chip ready
delay(5);
#endif
// set variables
RFM95.address = RFM95_BROADCAST_ADDRESS;
RFM95.rxBufferValid = false;
RFM95.txSequenceNumber = 0; // initialise TX sequence counter
RFM95.powerLevel = 0;
RFM95.powerLevel = 0;
RFM95.ATCenabled = false;
RFM95.ATCtargetRSSI = RFM95_TARGET_RSSI + RFM95_RSSI_OFFSET;
@@ -158,23 +164,24 @@ LOCAL bool RFM95_initialise(const float frequency) {
// IRQ
hwPinMode(MY_RFM95_IRQ_PIN, INPUT);
#if defined (SPI_HAS_TRANSACTION) && !defined (ESP8266)
_SPI.usingInterrupt(digitalPinToInterrupt(MY_RFM95_IRQ_PIN));
#endif
#if defined (SPI_HAS_TRANSACTION) && !defined (ESP8266)
_SPI.usingInterrupt(digitalPinToInterrupt(MY_RFM95_IRQ_PIN));
#endif
attachInterrupt(digitalPinToInterrupt(MY_RFM95_IRQ_PIN), RFM95_interruptHandler, RISING);
return true;
}
// RxDone, TxDone, CADDone is mapped to DI0
LOCAL void RFM95_interruptHandler(void) {
LOCAL void RFM95_interruptHandler(void)
{
// Read the interrupt register
const uint8_t irqFlags = RFM95_readReg(RFM95_REG_12_IRQ_FLAGS);
if (RFM95.radioMode == RFM95_RADIO_MODE_RX && (irqFlags & (RFM95_RX_TIMEOUT | RFM95_PAYLOAD_CRC_ERROR)) ) {
const uint8_t irqFlags = RFM95_readReg(RFM95_REG_12_IRQ_FLAGS);
if (RFM95.radioMode == RFM95_RADIO_MODE_RX &&
(irqFlags & (RFM95_RX_TIMEOUT | RFM95_PAYLOAD_CRC_ERROR)) ) {
// CRC error or timeout
// RXcontinuous mode: radio stays in RX mode, clearing IRQ needed
}
else if (RFM95.radioMode == RFM95_RADIO_MODE_RX && (irqFlags & RFM95_RX_DONE)) {
} else if (RFM95.radioMode == RFM95_RADIO_MODE_RX && (irqFlags & RFM95_RX_DONE)) {
// set radio to STDBY (we are in RXcontinuous mode)
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
// Have received a packet
@@ -188,44 +195,46 @@ LOCAL void RFM95_interruptHandler(void) {
RFM95.currentPacket.SNR = static_cast<rfm95_SNR_t>(RFM95_readReg(RFM95_REG_19_PKT_SNR_VALUE));
RFM95.currentPacket.payloadLen = bufLen - RFM95_HEADER_LEN;
// Message for us
if ((RFM95.currentPacket.header.version >= RFM95_MIN_PACKET_HEADER_VERSION) &&
(RFM95_PROMISCUOUS || RFM95.currentPacket.header.recipient == RFM95.address || RFM95.currentPacket.header.recipient == RFM95_BROADCAST_ADDRESS)) {
if ((RFM95.currentPacket.header.version >= RFM95_MIN_PACKET_HEADER_VERSION) &&
(RFM95_PROMISCUOUS || RFM95.currentPacket.header.recipient == RFM95.address ||
RFM95.currentPacket.header.recipient == RFM95_BROADCAST_ADDRESS)) {
RFM95.rxBufferValid = true;
}
}
}
else if (RFM95.radioMode == RFM95_RADIO_MODE_TX && (irqFlags & RFM95_TX_DONE) ) {
} else if (RFM95.radioMode == RFM95_RADIO_MODE_TX && (irqFlags & RFM95_TX_DONE) ) {
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
}
else if (RFM95.radioMode == RFM95_RADIO_MODE_CAD && (irqFlags & RFM95_CAD_DONE) ) {
} else if (RFM95.radioMode == RFM95_RADIO_MODE_CAD && (irqFlags & RFM95_CAD_DONE) ) {
RFM95.cad = irqFlags & RFM95_CAD_DETECTED;
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
}
}
// Clear all IRQ flags
RFM95_writeReg(RFM95_REG_12_IRQ_FLAGS, 0xFF);
}
LOCAL bool RFM95_available(void) {
LOCAL bool RFM95_available(void)
{
if (RFM95.radioMode == RFM95_RADIO_MODE_TX) {
return false;
}
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_RX);
return RFM95.rxBufferValid;
return RFM95.rxBufferValid;
}
LOCAL void RFM95_clearRxBuffer(void) {
LOCAL void RFM95_clearRxBuffer(void)
{
noInterrupts();
RFM95.rxBufferValid = false;
interrupts();
}
LOCAL uint8_t RFM95_recv(uint8_t* buf) {
LOCAL uint8_t RFM95_recv(uint8_t* buf)
{
if (!RFM95_available()) {
return false;
}
// atomic
noInterrupts();
const uint8_t payloadLen = RFM95.currentPacket.payloadLen;
@@ -234,10 +243,10 @@ LOCAL uint8_t RFM95_recv(uint8_t* buf) {
const uint8_t controlFlags = RFM95.currentPacket.header.controlFlags;
const rfm95_RSSI_t RSSI = RFM95.currentPacket.RSSI; // of incoming packet
const rfm95_SNR_t SNR = RFM95.currentPacket.SNR;
if (buf != NULL) {
if (buf != NULL) {
memcpy((void*)buf, (void*)RFM95.currentPacket.payload, payloadLen);
RFM95.rxBufferValid = false;
}
}
interrupts();
// ACK handling
@@ -249,29 +258,32 @@ LOCAL uint8_t RFM95_recv(uint8_t* buf) {
return payloadLen;
}
LOCAL bool RFM95_send(rfm95_packet_t &packet) {
LOCAL bool RFM95_send(rfm95_packet_t &packet)
{
const uint8_t finalLen = packet.payloadLen + RFM95_HEADER_LEN;
// Make sure we dont interrupt an outgoing message
RFM95_waitPacketSent();
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_STDBY);
// Check channel activity
if (!RFM95_waitCAD()) {
return false;
return false;
}
packet.header.sequenceNumber = RFM95.txSequenceNumber++;
// Position at the beginning of the TX FIFO
packet.header.sequenceNumber = RFM95.txSequenceNumber++;
// Position at the beginning of the TX FIFO
RFM95_writeReg(RFM95_REG_0D_FIFO_ADDR_PTR, RFM95_TX_FIFO_ADDR);
// write packet
// write packet
RFM95_burstWriteReg(RFM95_REG_00_FIFO, packet.data, finalLen);
// total payload length
// total payload length
RFM95_writeReg(RFM95_REG_22_PAYLOAD_LENGTH, finalLen);
// send message, if sent, irq fires and radio returns to standby
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_TX);
return true;
}
LOCAL bool RFM95_sendFrame(const uint8_t recipient, uint8_t* data, const uint8_t len, const rfm95_flag_t flags) {
LOCAL bool RFM95_sendFrame(const uint8_t recipient, uint8_t* data, const uint8_t len,
const rfm95_flag_t flags)
{
rfm95_packet_t packet;
packet.header.version = RFM95_PACKET_HEADER_VERSION;
packet.header.sender = RFM95.address;
@@ -283,14 +295,16 @@ LOCAL bool RFM95_sendFrame(const uint8_t recipient, uint8_t* data, const uint8_t
return RFM95_send(packet);
}
LOCAL void RFM95_setFrequency(const float centre) {
const uint32_t freq = (centre * 1000000.0) / RFM95_FSTEP;
LOCAL void RFM95_setFrequency(const float centre)
{
const uint32_t freq = (centre * 1000000.0) / RFM95_FSTEP;
RFM95_writeReg(RFM95_REG_06_FRF_MSB, (freq >> 16) & 0xff);
RFM95_writeReg(RFM95_REG_07_FRF_MID, (freq >> 8) & 0xff);
RFM95_writeReg(RFM95_REG_08_FRF_LSB, freq & 0xff);
}
LOCAL bool RFM95_setTxPower(uint8_t powerLevel) {
LOCAL bool RFM95_setTxPower(uint8_t powerLevel)
{
// RFM95/96/97/98 does not have RFO pins connected to anything. Only PA_BOOST
powerLevel = max((uint8_t)RFM95_MIN_POWER_LEVEL_DBM, powerLevel);
powerLevel = min((uint8_t)RFM95_MAX_POWER_LEVEL_DBM, powerLevel);
@@ -304,8 +318,7 @@ LOCAL bool RFM95_setTxPower(uint8_t powerLevel) {
// My measurements show 20dBm is correct
RFM95_writeReg(RFM95_REG_4D_PA_DAC, RFM95_PA_DAC_ENABLE);
val = powerLevel - 8;
}
else {
} else {
RFM95_writeReg(RFM95_REG_4D_PA_DAC, RFM95_PA_DAC_DISABLE);
val = powerLevel - 5;
}
@@ -318,27 +331,32 @@ LOCAL bool RFM95_setTxPower(uint8_t powerLevel) {
}
// Sets registers from a canned modem configuration structure
LOCAL void RFM95_setModemRegisters(const rfm95_modemConfig_t config) {
LOCAL void RFM95_setModemRegisters(const rfm95_modemConfig_t config)
{
RFM95_writeReg(RFM95_REG_1D_MODEM_CONFIG1, config.reg_1d);
RFM95_writeReg(RFM95_REG_1E_MODEM_CONFIG2, config.reg_1e);
RFM95_writeReg(RFM95_REG_26_MODEM_CONFIG3, config.reg_26);
}
LOCAL void RFM95_setPreambleLength(const uint16_t preambleLength) {
LOCAL void RFM95_setPreambleLength(const uint16_t preambleLength)
{
RFM95_writeReg(RFM95_REG_20_PREAMBLE_MSB, preambleLength >> 8);
RFM95_writeReg(RFM95_REG_21_PREAMBLE_LSB, preambleLength & 0xff);
}
LOCAL void RFM95_setAddress(const uint8_t addr) {
LOCAL void RFM95_setAddress(const uint8_t addr)
{
RFM95.address = addr;
}
LOCAL uint8_t RFM95_getAddress(void) {
LOCAL uint8_t RFM95_getAddress(void)
{
return RFM95.address;
}
LOCAL bool RFM95_isChannelActive(void) {
LOCAL bool RFM95_isChannelActive(void)
{
(void)RFM95_setRadioMode(RFM95_RADIO_MODE_CAD);
while (RFM95.radioMode == RFM95_RADIO_MODE_CAD) {
doYield();
@@ -347,7 +365,8 @@ LOCAL bool RFM95_isChannelActive(void) {
return RFM95.cad;
}
LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode) {
LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode)
{
if (RFM95.radioMode == newRadioMode) {
return false;
}
@@ -355,35 +374,35 @@ LOCAL bool RFM95_setRadioMode(const rfm95_radioMode_t newRadioMode) {
if (newRadioMode == RFM95_RADIO_MODE_STDBY) {
regMode = RFM95_MODE_STDBY;
}
else if (newRadioMode == RFM95_RADIO_MODE_SLEEP) {
} else if (newRadioMode == RFM95_RADIO_MODE_SLEEP) {
regMode = RFM95_MODE_SLEEP;
}
else if (newRadioMode == RFM95_RADIO_MODE_CAD) {
} else if (newRadioMode == RFM95_RADIO_MODE_CAD) {
regMode = RFM95_MODE_CAD;
RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x80); // Interrupt on CadDone, DIO0
}
else if (newRadioMode == RFM95_RADIO_MODE_RX) {
} else if (newRadioMode == RFM95_RADIO_MODE_RX) {
regMode = RFM95_MODE_RXCONTINUOUS;
RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x00); // Interrupt on RxDone, DIO0
}
else if (newRadioMode == RFM95_RADIO_MODE_TX) {
} else if (newRadioMode == RFM95_RADIO_MODE_TX) {
regMode = RFM95_MODE_TX;
RFM95_writeReg(RFM95_REG_40_DIO_MAPPING1, 0x40); // Interrupt on TxDone, DIO0
} else {
return false;
}
else return false;
RFM95_writeReg(RFM95_REG_01_OP_MODE, regMode);
RFM95.radioMode = newRadioMode;
return true;
}
LOCAL bool RFM95_sleep(void) {
LOCAL bool RFM95_sleep(void)
{
return RFM95_setRadioMode(RFM95_RADIO_MODE_SLEEP);
}
// should be called immediately after reception in case sender wants ACK
LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t sequenceNumber, const rfm95_RSSI_t RSSI, const rfm95_RSSI_t SNR) {
LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t sequenceNumber,
const rfm95_RSSI_t RSSI, const rfm95_RSSI_t SNR)
{
RFM95_DEBUG(PSTR("RFM95:SAC:SEND ACK to=%d,RSSI=%d\n"),recipient,RSSI);
rfm95_ack_t ACK;
ACK.sequenceNumber = sequenceNumber;
@@ -395,25 +414,26 @@ LOCAL void RFM95_sendACK(const uint8_t recipient, const rfm95_sequenceNumber_t s
(void)RFM95_sendFrame(recipient, (uint8_t*)&ACK, sizeof(rfm95_ack_t), flags);
}
LOCAL bool RFM95_executeATC(const rfm95_RSSI_t currentRSSI, const rfm95_RSSI_t targetRSSI) {
LOCAL bool RFM95_executeATC(const rfm95_RSSI_t currentRSSI, const rfm95_RSSI_t targetRSSI)
{
// allow +-5%
if (currentRSSI < (targetRSSI*1.05) && RFM95.powerLevel < RFM95_MAX_POWER_LEVEL_DBM) {
// increase transmitter power
RFM95.powerLevel++;
}
else if (currentRSSI > (targetRSSI*0.95) && RFM95.powerLevel > RFM95_MIN_POWER_LEVEL_DBM) {
} else if (currentRSSI > (targetRSSI*0.95) && RFM95.powerLevel > RFM95_MIN_POWER_LEVEL_DBM) {
// decrease transmitter power
RFM95.powerLevel--;
}
else {
} else {
// nothing to adjust
return false;
}
RFM95_DEBUG(PSTR("RFM95:ATC:ADJ TXL,cR=%d,tR=%d,TXL=%d\n"), currentRSSI - RFM95_RSSI_OFFSET, targetRSSI - RFM95_RSSI_OFFSET, RFM95.powerLevel);
RFM95_DEBUG(PSTR("RFM95:ATC:ADJ TXL,cR=%d,tR=%d,TXL=%d\n"), currentRSSI - RFM95_RSSI_OFFSET,
targetRSSI - RFM95_RSSI_OFFSET, RFM95.powerLevel);
return RFM95_setTxPower(RFM95.powerLevel);;
}
LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer, const uint8_t bufferSize, const uint8_t retries, const uint32_t retryWaitTime)
LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer,
const uint8_t bufferSize, const uint8_t retries, const uint32_t retryWaitTime)
{
for (uint8_t retry = 0; retry < retries; retry++) {
RFM95_DEBUG(PSTR("RFM95:SWR:SEND TO=%d,RETRY=%d\n"), recipient, retry);
@@ -431,10 +451,12 @@ LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer, cons
const uint8_t sender = RFM95.currentPacket.header.sender;
const rfm95_sequenceNumber_t ACKsequenceNumber = RFM95.currentPacket.ACK.sequenceNumber;
const rfm95_flag_t flag = RFM95.currentPacket.header.controlFlags;
if (sender == recipient && RFM95_getACKReceived(flag) && (ACKsequenceNumber == RFM95.txSequenceNumber - 1)) {
RFM95_DEBUG(PSTR("RFM95:SWR:ACK FROM=%d,SEQ=%d,RSSI=%d,SNR=%d\n"),sender,ACKsequenceNumber, RFM95.currentPacket.ACK.RSSI-RFM95_RSSI_OFFSET, (int8_t)RFM95.currentPacket.ACK.SNR / 4);
if (sender == recipient && RFM95_getACKReceived(flag) &&
(ACKsequenceNumber == RFM95.txSequenceNumber - 1)) {
RFM95_DEBUG(PSTR("RFM95:SWR:ACK FROM=%d,SEQ=%d,RSSI=%d,SNR=%d\n"),sender,ACKsequenceNumber,
RFM95.currentPacket.ACK.RSSI-RFM95_RSSI_OFFSET, (int8_t)RFM95.currentPacket.ACK.SNR / 4);
RFM95_clearRxBuffer();
// ATC
if (RFM95.ATCenabled && RFM95_getACKRSSIReport(flag)) {
(void)RFM95_executeATC(RFM95.currentPacket.ACK.RSSI, RFM95.ATCtargetRSSI);
@@ -455,7 +477,8 @@ LOCAL bool RFM95_sendWithRetry(const uint8_t recipient, const void* buffer, cons
// Wait until no channel activity detected or timeout
LOCAL bool RFM95_waitCAD(void) {
LOCAL bool RFM95_waitCAD(void)
{
const uint32_t enterMS = hwMillis();
while (RFM95_isChannelActive()) {
if (hwMillis() - enterMS > RFM95_CAD_TIMEOUT_MS) {
@@ -467,23 +490,27 @@ LOCAL bool RFM95_waitCAD(void) {
}
// Wait for any previous transmission to finish
LOCAL bool RFM95_waitPacketSent(void) {
LOCAL bool RFM95_waitPacketSent(void)
{
while (RFM95.radioMode == RFM95_RADIO_MODE_TX) {
doYield();
}
return true;
}
LOCAL int16_t RFM95_getRSSI(void) {
LOCAL int16_t RFM95_getRSSI(void)
{
return (int16_t)(RFM95.currentPacket.RSSI - RFM95_RSSI_OFFSET);
}
LOCAL void RFM95_ATCmode(const bool OnOff, const int16_t targetRSSI) {
LOCAL void RFM95_ATCmode(const bool OnOff, const int16_t targetRSSI)
{
RFM95.ATCenabled = OnOff;
RFM95.ATCtargetRSSI = (uint8_t)(targetRSSI + RFM95_RSSI_OFFSET);
}
LOCAL bool RFM95_sanityCheck(void) {
LOCAL bool RFM95_sanityCheck(void)
{
// not implemented yet
return true;
}