template the BCD converters
This commit is contained in:
parent
f06a5f97e6
commit
ab17dc05ba
@ -23,8 +23,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "dmx.h"
|
#include "dmx.h"
|
||||||
#include <limits.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include "pro.h"
|
#include "pro.h"
|
||||||
|
|
||||||
namespace ENTTEC::Pro {
|
namespace ENTTEC::Pro {
|
||||||
@ -215,21 +213,13 @@ void MsgRecievedDMXChanged::oStream(std::shared_ptr<bufferstream> stream) const
|
|||||||
|
|
||||||
void MsgGetWidgetSerialReply::iStream(std::shared_ptr<bufferstream> stream)
|
void MsgGetWidgetSerialReply::iStream(std::shared_ptr<bufferstream> stream)
|
||||||
{
|
{
|
||||||
const uint8_t NIBBLE_BIT = CHAR_BIT / 2;
|
serial = BCDtoDecimal(stream->readType<uint32_t>());
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void MsgGetWidgetSerialReply::oStream(std::shared_ptr<bufferstream> stream) const
|
void MsgGetWidgetSerialReply::oStream(std::shared_ptr<bufferstream> stream) const
|
||||||
{
|
{
|
||||||
const uint8_t NIBBLE_BIT = CHAR_BIT / 2;
|
*stream << DecimalToBCD(serial);
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,8 +25,10 @@
|
|||||||
|
|
||||||
#include <bufferstream.h>
|
#include <bufferstream.h>
|
||||||
|
|
||||||
|
#include <climits>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <math.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -418,4 +420,39 @@ struct MsgSendRDMDiscovery
|
|||||||
uint8_t request[38]; //!< DISC_UNIQUE_BRANCH RDM request packet to send.
|
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
|
} // namespace ENTTEC::Pro
|
||||||
|
Loading…
Reference in New Issue
Block a user