1
0
Fork 0

template the BCD converters

This commit is contained in:
Kevin Matz 2023-04-28 20:16:41 -04:00
parent f06a5f97e6
commit ab17dc05ba
2 changed files with 39 additions and 12 deletions

View File

@ -23,8 +23,6 @@
*/
#include "dmx.h"
#include <limits.h>
#include <math.h>
#include "pro.h"
namespace ENTTEC::Pro {
@ -215,21 +213,13 @@ void MsgRecievedDMXChanged::oStream(std::shared_ptr<bufferstream> stream) const
void MsgGetWidgetSerialReply::iStream(std::shared_ptr<bufferstream> stream)
{
const uint8_t NIBBLE_BIT = CHAR_BIT / 2;
auto bcd = stream->readType<uint32_t>();
serial = bcd & 0xf;
for(size_t i = 1; i * NIBBLE_BIT < sizeof(bcd) * CHAR_BIT; i++)
serial += ((bcd >> (i * NIBBLE_BIT)) & 0xf) * pow(10,i);
serial = BCDtoDecimal(stream->readType<uint32_t>());
};
void MsgGetWidgetSerialReply::oStream(std::shared_ptr<bufferstream> stream) const
{
const uint8_t NIBBLE_BIT = CHAR_BIT / 2;
uint32_t bcd = serial & 0xf;
for(size_t i = 1; i * NIBBLE_BIT < sizeof(bcd) * CHAR_BIT; i++)
bcd += ((uint32_t)(serial / pow(10,i)) & 0xf) << (i * NIBBLE_BIT);
*stream << bcd;
*stream << DecimalToBCD(serial);
};

View File

@ -25,8 +25,10 @@
#include <bufferstream.h>
#include <climits>
#include <cstdint>
#include <cstring>
#include <math.h>
#include <memory>
#include <vector>
@ -418,4 +420,39 @@ struct MsgSendRDMDiscovery
uint8_t request[38]; //!< DISC_UNIQUE_BRANCH RDM request packet to send.
};
/**
* @brief BCDtoDecimal
* @param bcd
* @return
*/
template <typename T>
T BCDtoDecimal(const T bcd)
{
static_assert(std::is_integral<T>::value, "Integer type required.");
const uint_fast8_t NIBBLE_BIT = CHAR_BIT / 2;
T decimal = bcd & 0xf;
for(size_t i = 1; i * NIBBLE_BIT < sizeof(bcd) * CHAR_BIT; i++)
decimal += ((bcd >> (i * NIBBLE_BIT)) & 0xf) * pow(10,i);
return decimal;
}
/**
* @brief DecimalToBCD
* @param decimal
* @return
*/
template <typename T>
T DecimalToBCD(const T decimal)
{
static_assert(std::is_integral<T>::value, "Integer type required.");
const uint_fast8_t NIBBLE_BIT = CHAR_BIT / 2;
T bcd = decimal & 0xf;
for(size_t i = 1; i * NIBBLE_BIT < sizeof(bcd) * CHAR_BIT; i++)
bcd |= ((decimal / (uint)pow(10,i)) % 10) << (i * NIBBLE_BIT);
return bcd;
}
} // namespace ENTTEC::Pro