From 55317f7513494012cac3666511fffadd4dce1689 Mon Sep 17 00:00:00 2001 From: Kevin Matz Date: Sun, 9 Apr 2023 19:26:41 -0400 Subject: [PATCH] BCD encode/decode the serial number --- protocol/enttec/dmx-usb-pro/pro.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/protocol/enttec/dmx-usb-pro/pro.cpp b/protocol/enttec/dmx-usb-pro/pro.cpp index e5cd5d8..2cf7ac5 100644 --- a/protocol/enttec/dmx-usb-pro/pro.cpp +++ b/protocol/enttec/dmx-usb-pro/pro.cpp @@ -23,6 +23,8 @@ */ #include "dmx.h" +#include +#include #include "pro.h" namespace ENTTEC::Pro { @@ -158,7 +160,7 @@ void MsgOutputOnlySendDMX::oStream(std::shared_ptr stream) const { stream->write(data.data(), std::min(data.size(), DMX::E111_LAST_SLOT + 1)); for (size_t i = data.size(); i <= DMX_LAST_SLOT_MIN; i++) - *stream << (uint8_t) 0; + *stream << (uint8_t)0; }; @@ -212,13 +214,21 @@ void MsgRecievedDMXChanged::oStream(std::shared_ptr stream) const void MsgGetWidgetSerialReply::iStream(std::shared_ptr stream) { - *stream >> serial; + const uint8_t NIBBLE_BIT = CHAR_BIT / 2; + auto bcd = stream->readType(); + 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 stream) const { - *stream << serial; + 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; };