From 03d2a9bf265e70e06c8b43af53cd02a626207161 Mon Sep 17 00:00:00 2001 From: jgromes Date: Fri, 12 May 2023 20:55:51 +0200 Subject: [PATCH] [MOD] Added common reflect method --- src/Module.cpp | 19 ++++++------------- src/Module.h | 10 ++++------ src/protocols/AX25/AX25.cpp | 2 +- src/protocols/Pager/Pager.cpp | 10 +++++----- src/protocols/PhysicalLayer/PhysicalLayer.cpp | 2 +- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/Module.cpp b/src/Module.cpp index 8346d3fc..c4d72df8 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -372,19 +372,12 @@ void Module::waitForMicroseconds(uint32_t start, uint32_t len) { #endif } -uint8_t Module::flipBits(uint8_t b) { - b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; - b = (b & 0xCC) >> 2 | (b & 0x33) << 2; - b = (b & 0xAA) >> 1 | (b & 0x55) << 1; - return b; -} - -uint16_t Module::flipBits16(uint16_t i) { - i = (i & 0xFF00) >> 8 | (i & 0x00FF) << 8; - i = (i & 0xF0F0) >> 4 | (i & 0x0F0F) << 4; - i = (i & 0xCCCC) >> 2 | (i & 0x3333) << 2; - i = (i & 0xAAAA) >> 1 | (i & 0x5555) << 1; - return i; +uint32_t Module::reflect(uint32_t in, uint8_t bits) { + uint32_t res = 0; + for(uint8_t i = 0; i < bits; i++) { + res |= (((in & ((uint32_t)1 << i)) >> i) << (bits - i - 1)); + } + return(res); } void Module::hexdump(uint8_t* data, size_t len, uint32_t offset, uint8_t width, bool be) { diff --git a/src/Module.h b/src/Module.h index b240653e..7c5216d5 100644 --- a/src/Module.h +++ b/src/Module.h @@ -462,13 +462,11 @@ class Module { /*! \brief Function to reflect bits within a byte. + \param in The input to reflect. + \param bits Number of bits to reflect. + \return The reflected input. */ - static uint8_t flipBits(uint8_t b); - - /*! - \brief Function to reflect bits within an integer. - */ - static uint16_t flipBits16(uint16_t i); + static uint32_t reflect(uint32_t in, uint8_t bits); /*! \brief Function to dump data as hex into the debug port. diff --git a/src/protocols/AX25/AX25.cpp b/src/protocols/AX25/AX25.cpp index 53b4018c..62db834a 100644 --- a/src/protocols/AX25/AX25.cpp +++ b/src/protocols/AX25/AX25.cpp @@ -303,7 +303,7 @@ int16_t AX25Client::sendFrame(AX25Frame* frame) { // flip bit order for(size_t i = 0; i < frameBuffLen; i++) { - frameBuff[i] = Module::flipBits(frameBuff[i]); + frameBuff[i] = Module::reflect(frameBuff[i], 8); } // calculate FCS diff --git a/src/protocols/Pager/Pager.cpp b/src/protocols/Pager/Pager.cpp index 04b906ab..006f5e81 100644 --- a/src/protocols/Pager/Pager.cpp +++ b/src/protocols/Pager/Pager.cpp @@ -160,7 +160,7 @@ int16_t PagerClient::transmit(uint8_t* data, size_t len, uint32_t addr, uint8_t // first insert the remainder from previous code word (if any) if(remBits > 0) { // this doesn't apply to BCD messages, so no need to check that here - uint8_t prev = Module::flipBits(data[dataPos - 1]); + uint8_t prev = Module::reflect(data[dataPos - 1], 8); prev >>= 1; msg[blockPos] |= (uint32_t)prev << (RADIOLIB_PAGER_CODE_WORD_LEN - 1 - remBits); } @@ -174,7 +174,7 @@ int16_t PagerClient::transmit(uint8_t* data, size_t len, uint32_t addr, uint8_t if(encoding == RADIOLIB_PAGER_BCD) { symbol = encodeBCD(symbol); } - symbol = Module::flipBits(symbol); + symbol = Module::reflect(symbol, 8); symbol >>= (8 - symbolLength); // insert the next message symbol @@ -188,7 +188,7 @@ int16_t PagerClient::transmit(uint8_t* data, size_t len, uint32_t addr, uint8_t uint8_t numSteps = (symbolPos - RADIOLIB_PAGER_FUNC_BITS_POS + symbolLength)/symbolLength; for(uint8_t i = 0; i < numSteps; i++) { symbol = encodeBCD(' '); - symbol = Module::flipBits(symbol); + symbol = Module::reflect(symbol, 8); symbol >>= (8 - symbolLength); msg[blockPos] |= (uint32_t)symbol << symbolPos; symbolPos -= symbolLength; @@ -397,7 +397,7 @@ int16_t PagerClient::readData(uint8_t* data, size_t* len, uint32_t* addr) { uint32_t symbol = prevSymbol << (symbolLength - ovfBits) | currSymbol; // finally, we can flip the bits - symbol = Module::flipBits((uint8_t)symbol); + symbol = Module::reflect((uint8_t)symbol, 8); symbol >>= (8 - symbolLength); // decode BCD and we're done @@ -415,7 +415,7 @@ int16_t PagerClient::readData(uint8_t* data, size_t* len, uint32_t* addr) { while(bitPos >= RADIOLIB_PAGER_MESSAGE_END_POS) { // get the message symbol from the code word and reverse bits uint32_t symbol = (cw & (0x7FUL << bitPos)) >> bitPos; - symbol = Module::flipBits((uint8_t)symbol); + symbol = Module::reflect((uint8_t)symbol, 8); symbol >>= (8 - symbolLength); // decode BCD if needed diff --git a/src/protocols/PhysicalLayer/PhysicalLayer.cpp b/src/protocols/PhysicalLayer/PhysicalLayer.cpp index dce1e133..eee43ca6 100644 --- a/src/protocols/PhysicalLayer/PhysicalLayer.cpp +++ b/src/protocols/PhysicalLayer/PhysicalLayer.cpp @@ -367,7 +367,7 @@ void PhysicalLayer::updateDirectBuffer(uint8_t bit) { // check complete byte if(this->bufferBitPos == 8) { - this->buffer[this->bufferWritePos] = Module::flipBits(this->buffer[this->bufferWritePos]); + this->buffer[this->bufferWritePos] = Module::reflect(this->buffer[this->bufferWritePos], 8); RADIOLIB_VERBOSE_PRINTLN("R\t%X", this->buffer[this->bufferWritePos]); this->bufferWritePos++;