From 2e771721a5325baa3d017b889917d06ee5fbee7b Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Fri, 13 Sep 2024 23:49:55 +0200 Subject: [PATCH] reimplement dpt 6 --- src/CMakeLists.txt | 2 + src/knx/group_object/dpt/dpt.h | 9 +- src/knx/group_object/dpt/dpt4.cpp | 4 +- src/knx/group_object/dpt/dpt6.cpp | 109 +++++++++++++++++ src/knx/group_object/dpt/dpt6.h | 42 +++++++ src/knx/group_object/dpt/dptconvert.cpp | 152 ------------------------ src/knx/group_object/dpt/dptconvert.h | 12 -- src/knx/group_object/dpt/dpts.h | 1 + 8 files changed, 157 insertions(+), 174 deletions(-) create mode 100644 src/knx/group_object/dpt/dpt6.cpp create mode 100644 src/knx/group_object/dpt/dpt6.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 99b1cc4..eb201ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,6 +48,8 @@ set(SOURCES ./knx/group_object/dpt/dpt4.h ./knx/group_object/dpt/dpt5.cpp ./knx/group_object/dpt/dpt5.h +./knx/group_object/dpt/dpt6.cpp +./knx/group_object/dpt/dpt6.h ./knx/group_object/dpt/dpt9.cpp ./knx/group_object/dpt/dpt9.h ./knx/group_object/dpt/dptconvert.cpp diff --git a/src/knx/group_object/dpt/dpt.h b/src/knx/group_object/dpt/dpt.h index 0e8fb5f..c7a8187 100644 --- a/src/knx/group_object/dpt/dpt.h +++ b/src/knx/group_object/dpt/dpt.h @@ -3,13 +3,6 @@ #include "../group_object.h" namespace Knx { - - - - -#define DPT_Percent_V8 Dpt(6, 1) -#define DPT_Value_1_Count Dpt(6, 10) -#define DPT_Status_Mode3 Dpt(6, 20) #define DPT_Value_2_Ucount Dpt(7, 1) #define DPT_TimePeriodMsec Dpt(7, 2) #define DPT_TimePeriod10MSec Dpt(7, 3) @@ -360,7 +353,7 @@ namespace Knx _value = value; return *this; } - private: + protected: T _value; }; } \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt4.cpp b/src/knx/group_object/dpt/dpt4.cpp index 8e94b50..c55b1a2 100644 --- a/src/knx/group_object/dpt/dpt4.cpp +++ b/src/knx/group_object/dpt/dpt4.cpp @@ -14,13 +14,13 @@ void Knx::Dpt4::encode(uint8_t* data) const bool Knx::Dpt4::decode(uint8_t* data) { - value(signed8FromPayload(data, 0)); + value(unsigned8FromPayload(data, 0)); return true; } bool Knx::DPT_Char_ASCII::decode(uint8_t* data) { - Dpt4::value(signed8FromPayload(data, 0) & 0x7F); + Dpt4::value(unsigned8FromPayload(data, 0) & 0x7F); return true; } diff --git a/src/knx/group_object/dpt/dpt6.cpp b/src/knx/group_object/dpt/dpt6.cpp new file mode 100644 index 0000000..b06913d --- /dev/null +++ b/src/knx/group_object/dpt/dpt6.cpp @@ -0,0 +1,109 @@ +#include "dpt6.h" + +#include "dptconvert.h" + +Knx::Go_SizeCode Knx::Dpt6::size() const +{ + return Go_1_Octet; +} + +void Knx::Dpt6::encode(uint8_t* data) const +{ + signed8ToPayload(data, 0, value(), 0xFF); +} + +bool Knx::Dpt6::decode(uint8_t* data) +{ + value(signed8FromPayload(data, 0)); + return true; +} + +bool Knx::DPT_Status_Mode3::decode(uint8_t* data) +{ + int8_t val = signed8FromPayload(data, 0); + + int8_t mode = val & 0x7; + + if ( mode != (1 << 0) && mode != (1 << 1) && mode != (1 << 2) ) + return false; + + value(val); + return true; +} + +Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::A() +{ + return ( _value & (1 << 7) ) > 0 ? Clear : Set; +} + +void Knx::DPT_Status_Mode3::A(SetClearValue value) +{ + if (value == Set) + _value &= ~ (1 << 7); + else + _value |= (1 << 7); +} + + +Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::B() +{ + return ( _value & (1 << 6) ) > 0 ? Clear : Set; +} + +void Knx::DPT_Status_Mode3::B(SetClearValue value) +{ + if (value == Set) + _value &= ~ (1 << 6); + else + _value |= (1 << 6); +} + +Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::C() +{ + return ( _value & (1 << 5) ) > 0 ? Clear : Set; +} + +void Knx::DPT_Status_Mode3::C(SetClearValue value) +{ + if (value == Set) + _value &= ~ (1 << 5); + else + _value |= (1 << 5); +} + +Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::D() +{ + return ( _value & (1 << 4) ) > 0 ? Clear : Set; +} + +void Knx::DPT_Status_Mode3::D(SetClearValue value) +{ + if (value == Set) + _value &= ~ (1 << 4); + else + _value |= (1 << 4); +} + +Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::E() +{ + return ( _value & (1 << 3) ) > 0 ? Clear : Set; +} + +void Knx::DPT_Status_Mode3::E(SetClearValue value) +{ + if (value == Set) + _value &= ~ (1 << 3); + else + _value |= (1 << 3); +} + +Knx::DPT_Status_Mode3::ActiveModeValue Knx::DPT_Status_Mode3::activeMode() +{ + return (ActiveModeValue) (_value & 0x7); +} + +void Knx::DPT_Status_Mode3::activeMode(ActiveModeValue value) +{ + _value &= ~ (0x7); + _value |= (int8_t)value; +} diff --git a/src/knx/group_object/dpt/dpt6.h b/src/knx/group_object/dpt/dpt6.h new file mode 100644 index 0000000..20f341e --- /dev/null +++ b/src/knx/group_object/dpt/dpt6.h @@ -0,0 +1,42 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt6: public DPT + { + public: + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + }; + + typedef Dpt6 DPT_Percent_V8; + typedef Dpt6 DPT_Value_1_Count; + + class DPT_Status_Mode3: public Dpt6 + { + public: + bool decode(uint8_t* data) override; + enum SetClearValue { Set = 0, Clear = 1}; + enum ActiveModeValue { Mode0Active = 0x1, Mode1Active = 0x2, Mode2Active = 0x4}; + + SetClearValue A(); + void A(SetClearValue value); + + SetClearValue B(); + void B(SetClearValue value); + + SetClearValue C(); + void C(SetClearValue value); + + SetClearValue D(); + void D(SetClearValue value); + + SetClearValue E(); + void E(SetClearValue value); + + ActiveModeValue activeMode(); + void activeMode(ActiveModeValue value); + }; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dptconvert.cpp b/src/knx/group_object/dpt/dptconvert.cpp index 9fab76f..b30e2b8 100644 --- a/src/knx/group_object/dpt/dptconvert.cpp +++ b/src/knx/group_object/dpt/dptconvert.cpp @@ -18,18 +18,6 @@ namespace Knx { if (payload_length > 0) { - // DPT 5.* - Unsigned 8 Bit Integer - if (datatype.mainGroup == 5 && ((datatype.subGroup >= 1 && datatype.subGroup <= 6 && datatype.subGroup != 2) || datatype.subGroup == 10) && !datatype.index) - return busValueToUnsigned8(payload, payload_length, datatype, value); - - // DPT 6.001/6.010 - Signed 8 Bit Integer - if (datatype.mainGroup == 6 && (datatype.subGroup == 1 || datatype.subGroup == 10) && !datatype.index) - return busValueToSigned8(payload, payload_length, datatype, value); - - // DPT 6.020 - Status with Mode - if (datatype.mainGroup == 6 && datatype.subGroup == 20 && datatype.index <= 5) - return busValueToStatusAndMode(payload, payload_length, datatype, value); - // DPT 7.001/7.010/7.011/7.012/7.013/7.600 - Unsigned 16 Bit Integer if (datatype.mainGroup == 7 && (datatype.subGroup == 1 || (datatype.subGroup >= 10 && datatype.subGroup <= 13) || (datatype.subGroup == 600)) && !datatype.index) return busValueToUnsigned16(payload, payload_length, datatype, value); @@ -160,19 +148,6 @@ namespace Knx int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { - - // DPT 5.* - Unsigned 8 Bit Integer - if (datatype.mainGroup == 5 && ((datatype.subGroup >= 1 && datatype.subGroup <= 6 && datatype.subGroup != 2) || datatype.subGroup == 10) && !datatype.index) - return valueToBusValueUnsigned8(value, payload, payload_length, datatype); - - // DPT 6.001/6.010 - Signed 8 Bit Integer - if (datatype.mainGroup == 6 && (datatype.subGroup == 1 || datatype.subGroup == 10) && !datatype.index) - return valueToBusValueSigned8(value, payload, payload_length, datatype); - - // DPT 6.020 - Status with Mode - if (datatype.mainGroup == 6 && datatype.subGroup == 20 && datatype.index <= 5) - return valueToBusValueStatusAndMode(value, payload, payload_length, datatype); - // DPT 7.001/7.010/7.011/7.012/7.013/7.600 - Unsigned 16 Bit Integer if (datatype.mainGroup == 7 && (datatype.subGroup == 1 || (datatype.subGroup >= 10 && datatype.subGroup <= 13) || datatype.subGroup == 600) && !datatype.index) return valueToBusValueUnsigned16(value, payload, payload_length, datatype); @@ -300,61 +275,6 @@ namespace Knx return false; } - int busValueToUnsigned8(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) - { - ASSERT_PAYLOAD(1); - - switch (datatype.subGroup) - { - case 1: - value = (uint8_t)round(unsigned8FromPayload(payload, 0) * 100.0 / 255.0); - return true; - - case 3: - value = (uint8_t)round(unsigned8FromPayload(payload, 0) * 360.0 / 255.0); - return true; - - case 6: - { - uint8_t numValue = unsigned8FromPayload(payload, 0); - - if (numValue == 0xFF) - return false; - - value = numValue; - return true; - } - } - - value = unsigned8FromPayload(payload, 0); - return true; - } - - int busValueToSigned8(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) - { - ASSERT_PAYLOAD(1); - value = (uint8_t)(unsigned8FromPayload(payload, 0)); - return true; - } - - int busValueToStatusAndMode(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) - { - ASSERT_PAYLOAD(1); - - if (datatype.index < 5) - { - value = bitFromPayload(payload, datatype.index); - return true; - } - else if (datatype.index == 5) - { - value = (uint8_t)(unsigned8FromPayload(payload, 0) & 0x07); - return true; - } - - return false; - } - int busValueToUnsigned16(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) { ASSERT_PAYLOAD(2); @@ -907,78 +827,6 @@ namespace Knx //------------------------------------------------------------------------------------------------------------------------------------- - int valueToBusValueUnsigned8(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - if ((int64_t)value < INT64_C(0)) - return false; - - switch (datatype.subGroup) - { - case 1: - { - if ((double)value > 100.0) - return false; - - unsigned8ToPayload(payload, 0, round((double)value * 255.0 / 100.0), 0xFF); - break; - } - - case 3: - { - if ((double)value > 360.0) - return false; - - unsigned8ToPayload(payload, 0, round((double)value * 255.0 / 360.0), 0xFF); - break; - } - - case 6: - { - if ((int64_t)value > INT64_C(254)) - return false; - - unsigned8ToPayload(payload, 0, (uint64_t)value, 0xFF); - break; - } - - default: - { - if ((int64_t)value > INT64_C(255)) - return false; - - unsigned8ToPayload(payload, 0, (uint64_t)value, 0xFF); - } - } - - return true; - } - - int valueToBusValueSigned8(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - if ((int64_t)value < INT64_C(-128) || (int64_t)value > INT64_C(127)) - return false; - - signed8ToPayload(payload, 0, (uint64_t)value, 0xFF); - return true; - } - - int valueToBusValueStatusAndMode(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - if (datatype.index < 5) - bitToPayload(payload, datatype.index, value); - else if (datatype.index == 5) - { - if ((int64_t)value < INT64_C(0) || (int64_t)value > INT64_C(7)) - return false; - - unsigned8ToPayload(payload, 0, (uint64_t)value, 0x07); - } - else - return false; - - return true; - } - int valueToBusValueUnsigned16(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { if ((int64_t)value < INT64_C(0) || (int64_t)value > INT64_C(65535)) diff --git a/src/knx/group_object/dpt/dptconvert.h b/src/knx/group_object/dpt/dptconvert.h index 2325909..2358fd6 100644 --- a/src/knx/group_object/dpt/dptconvert.h +++ b/src/knx/group_object/dpt/dptconvert.h @@ -47,12 +47,6 @@ namespace Knx int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); //KNX to internal - int busValueToBinaryControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); - int busValueToStepControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); - int busValueToCharacter(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); - int busValueToUnsigned8(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); - int busValueToSigned8(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); - int busValueToStatusAndMode(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToUnsigned16(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToTimePeriod(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToSigned16(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); @@ -84,12 +78,6 @@ namespace Knx int busValueToActiveEnergy(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); //Internal to KNX - int valueToBusValueBinaryControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); - int valueToBusValueStepControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); - int valueToBusValueCharacter(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); - int valueToBusValueUnsigned8(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); - int valueToBusValueSigned8(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); - int valueToBusValueStatusAndMode(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueUnsigned16(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueTimePeriod(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueSigned16(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); diff --git a/src/knx/group_object/dpt/dpts.h b/src/knx/group_object/dpt/dpts.h index 71cc597..e9ca5fb 100644 --- a/src/knx/group_object/dpt/dpts.h +++ b/src/knx/group_object/dpt/dpts.h @@ -5,4 +5,5 @@ #include "dpt3.h" #include "dpt4.h" #include "dpt5.h" +#include "dpt6.h" #include "dpt9.h" \ No newline at end of file