Added custom min/max/abs macros

This commit is contained in:
jgromes 2023-05-16 17:13:43 +01:00
parent 1a55220020
commit efbec6b9d1
6 changed files with 14 additions and 17 deletions

View file

@ -316,13 +316,6 @@
#include <stdint.h> #include <stdint.h>
#if !defined(min)
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#if !defined(max)
#define max(a,b) ((a)>(b)?(a):(b))
#endif
#endif #endif
/* /*
@ -458,7 +451,6 @@
*/ */
#define RADIOLIB_ASSERT(STATEVAR) { if((STATEVAR) != RADIOLIB_ERR_NONE) { return(STATEVAR); } } #define RADIOLIB_ASSERT(STATEVAR) { if((STATEVAR) != RADIOLIB_ERR_NONE) { return(STATEVAR); } }
/*! /*!
\brief Macro to check variable is within constraints - this is commonly used to check parameter ranges. Requires RADIOLIB_CHECK_RANGE to be enabled \brief Macro to check variable is within constraints - this is commonly used to check parameter ranges. Requires RADIOLIB_CHECK_RANGE to be enabled
*/ */
@ -474,6 +466,11 @@
#define RADIOLIB_ERRATA_SX127X(...) {} #define RADIOLIB_ERRATA_SX127X(...) {}
#endif #endif
// these macros are usually defined by Arduino, but some platforms undef them, so its safer to use our own
#define RADIOLIB_MIN(a,b) ((a)<(b)?(a):(b))
#define RADIOLIB_MAX(a,b) ((a)>(b)?(a):(b))
#define RADIOLIB_ABS(x) ((x)>0?(x):-(x))
// version definitions // version definitions
#define RADIOLIB_VERSION_MAJOR (0x06) #define RADIOLIB_VERSION_MAJOR (0x06)
#define RADIOLIB_VERSION_MINOR (0x00) #define RADIOLIB_VERSION_MINOR (0x00)

View file

@ -302,11 +302,11 @@ int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
/*if(len > RADIOLIB_CC1101_MAX_PACKET_LENGTH) { /*if(len > RADIOLIB_CC1101_MAX_PACKET_LENGTH) {
SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, data, RADIOLIB_CC1101_FIFO_THRESH_TX); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, data, RADIOLIB_CC1101_FIFO_THRESH_TX);
} else { } else {
uint8_t initialWrite = min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); uint8_t initialWrite = RADIOLIB_MIN((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent));
SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, data, initialWrite); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, data, initialWrite);
dataSent += initialWrite; dataSent += initialWrite;
}*/ }*/
uint8_t initialWrite = min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); uint8_t initialWrite = RADIOLIB_MIN((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent));
SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, data, initialWrite); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, data, initialWrite);
dataSent += initialWrite; dataSent += initialWrite;
@ -327,7 +327,7 @@ int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
// if there's room then put other data // if there's room then put other data
if (bytesInFIFO < RADIOLIB_CC1101_FIFO_SIZE) { if (bytesInFIFO < RADIOLIB_CC1101_FIFO_SIZE) {
uint8_t bytesToWrite = min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - bytesInFIFO), (uint8_t)(len - dataSent)); uint8_t bytesToWrite = RADIOLIB_MIN((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - bytesInFIFO), (uint8_t)(len - dataSent));
SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, &data[dataSent], bytesToWrite); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, &data[dataSent], bytesToWrite);
dataSent += bytesToWrite; dataSent += bytesToWrite;
} else { } else {
@ -429,7 +429,7 @@ int16_t CC1101::readData(uint8_t* data, size_t len) {
} }
// read the minimum between "remaining length" and bytesInFifo // read the minimum between "remaining length" and bytesInFifo
uint8_t bytesToRead = min((uint8_t)(length - readBytes), bytesInFIFO); uint8_t bytesToRead = RADIOLIB_MIN((uint8_t)(length - readBytes), bytesInFIFO);
SPIreadRegisterBurst(RADIOLIB_CC1101_REG_FIFO, bytesToRead, &(data[readBytes])); SPIreadRegisterBurst(RADIOLIB_CC1101_REG_FIFO, bytesToRead, &(data[readBytes]));
readBytes += bytesToRead; readBytes += bytesToRead;
lastPop = this->mod->hal->millis(); lastPop = this->mod->hal->millis();

View file

@ -605,7 +605,7 @@ int16_t SX126x::startReceiveDutyCycleAuto(uint16_t senderPreambleLength, uint16_
// We need to ensure that the timeout is longer than senderPreambleLength. // We need to ensure that the timeout is longer than senderPreambleLength.
// So we must satisfy: wakePeriod > (preamblePeriod - (sleepPeriod - 1000)) / 2. (A) // So we must satisfy: wakePeriod > (preamblePeriod - (sleepPeriod - 1000)) / 2. (A)
// we also need to ensure the unit is awake to see at least minSymbols. (B) // we also need to ensure the unit is awake to see at least minSymbols. (B)
uint32_t wakePeriod = max( uint32_t wakePeriod = RADIOLIB_MAX(
(symbolLength * (senderPreambleLength + 1) - (sleepPeriod - 1000)) / 2, // (A) (symbolLength * (senderPreambleLength + 1) - (sleepPeriod - 1000)) / 2, // (A)
symbolLength * (minSymbols + 1)); //(B) symbolLength * (minSymbols + 1)); //(B)
RADIOLIB_DEBUG_PRINTLN("Auto wake period: ", wakePeriod); RADIOLIB_DEBUG_PRINTLN("Auto wake period: ", wakePeriod);

View file

@ -1156,7 +1156,7 @@ uint32_t SX127x::getTimeOnAir(size_t len) {
// Get number of bits preamble // Get number of bits preamble
float n_pre = (float) ((this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_MSB) << 8) | this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_LSB)); float n_pre = (float) ((this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_MSB) << 8) | this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_LSB));
// Get number of bits payload // Get number of bits payload
float n_pay = 8.0 + max(ceil((8.0 * (float) len - 4.0 * (float) this->spreadingFactor + 28.0 + 16.0 * crc - 20.0 * ih) / (4.0 * (float) this->spreadingFactor - 8.0 * de)) * (float) this->codingRate, 0.0); float n_pay = 8.0 + RADIOLIB_MAX(ceil((8.0 * (float) len - 4.0 * (float) this->spreadingFactor + 28.0 + 16.0 * crc - 20.0 * ih) / (4.0 * (float) this->spreadingFactor - 8.0 * de)) * (float) this->codingRate, 0.0);
// Get time-on-air in us // Get time-on-air in us
return ceil(symbolLength * (n_pre + n_pay + 4.25)) * 1000; return ceil(symbolLength * (n_pre + n_pay + 4.25)) * 1000;

View file

@ -1242,7 +1242,7 @@ uint32_t SX128x::getTimeOnAir(size_t len) {
uint32_t N_symbolPreamble = (this->preambleLengthLoRa & 0x0F) * (uint32_t(1) << ((this->preambleLengthLoRa & 0xF0) >> 4)); uint32_t N_symbolPreamble = (this->preambleLengthLoRa & 0x0F) * (uint32_t(1) << ((this->preambleLengthLoRa & 0xF0) >> 4));
// calculate the number of symbols // calculate the number of symbols
N_symbol = (float)N_symbolPreamble + coeff1 + 8.0 + ceil(max((int16_t)(8 * len + N_bitCRC - coeff2 + N_symbolHeader), (int16_t)0) / (float)coeff3) * (float)(this->codingRateLoRa + 4); N_symbol = (float)N_symbolPreamble + coeff1 + 8.0 + ceil(RADIOLIB_MAX((int16_t)(8 * len + N_bitCRC - coeff2 + N_symbolHeader), (int16_t)0) / (float)coeff3) * (float)(this->codingRateLoRa + 4);
} else { } else {
// long interleaving - abandon hope all ye who enter here // long interleaving - abandon hope all ye who enter here

View file

@ -100,7 +100,7 @@ int16_t APRSClient::sendMicE(float lat, float lon, uint16_t heading, uint16_t sp
// as discussed in https://github.com/jgromes/RadioLib/issues/430 // as discussed in https://github.com/jgromes/RadioLib/issues/430
// latitude first, because that is in the destination field // latitude first, because that is in the destination field
float lat_abs = abs(lat); float lat_abs = RADIOLIB_ABS(lat);
int lat_deg = (int)lat_abs; int lat_deg = (int)lat_abs;
int lat_min = (lat_abs - (float)lat_deg) * 60.0f; int lat_min = (lat_abs - (float)lat_deg) * 60.0f;
int lat_hun = (((lat_abs - (float)lat_deg) * 60.0f) - lat_min) * 100.0f; int lat_hun = (((lat_abs - (float)lat_deg) * 60.0f) - lat_min) * 100.0f;
@ -133,7 +133,7 @@ int16_t APRSClient::sendMicE(float lat, float lon, uint16_t heading, uint16_t sp
info[infoPos++] = RADIOLIB_APRS_MIC_E_GPS_DATA_CURRENT; info[infoPos++] = RADIOLIB_APRS_MIC_E_GPS_DATA_CURRENT;
// encode the longtitude // encode the longtitude
float lon_abs = abs(lon); float lon_abs = RADIOLIB_ABS(lon);
int32_t lon_deg = (int32_t)lon_abs; int32_t lon_deg = (int32_t)lon_abs;
int32_t lon_min = (lon_abs - (float)lon_deg) * 60.0f; int32_t lon_min = (lon_abs - (float)lon_deg) * 60.0f;
int32_t lon_hun = (((lon_abs - (float)lon_deg) * 60.0f) - lon_min) * 100.0f; int32_t lon_hun = (((lon_abs - (float)lon_deg) * 60.0f) - lon_min) * 100.0f;