From e4431d66f4f27684f1a940bd7b00de301084574b Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Mon, 16 Sep 2024 23:47:04 +0200 Subject: [PATCH] reimplement dpt 11-13 --- src/CMakeLists.txt | 6 ++ src/knx/group_object/dpt/dpt.h | 11 ---- src/knx/group_object/dpt/dpt11.cpp | 73 +++++++++++++++++++++++++ src/knx/group_object/dpt/dpt11.h | 28 ++++++++++ src/knx/group_object/dpt/dpt12.cpp | 19 +++++++ src/knx/group_object/dpt/dpt12.h | 20 +++++++ src/knx/group_object/dpt/dpt13.cpp | 19 +++++++ src/knx/group_object/dpt/dpt13.h | 26 +++++++++ src/knx/group_object/dpt/dpt9.cpp | 2 +- src/knx/group_object/dpt/dpt9.h | 2 +- src/knx/group_object/dpt/dptconvert.cpp | 73 ------------------------- src/knx/group_object/dpt/dptconvert.h | 2 - src/knx/group_object/dpt/dpts.h | 6 +- 13 files changed, 198 insertions(+), 89 deletions(-) create mode 100644 src/knx/group_object/dpt/dpt11.cpp create mode 100644 src/knx/group_object/dpt/dpt11.h create mode 100644 src/knx/group_object/dpt/dpt12.cpp create mode 100644 src/knx/group_object/dpt/dpt12.h create mode 100644 src/knx/group_object/dpt/dpt13.cpp create mode 100644 src/knx/group_object/dpt/dpt13.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index eb69d23..9bce1a3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,6 +58,12 @@ set(SOURCES ./knx/group_object/dpt/dpt9.h ./knx/group_object/dpt/dpt10.cpp ./knx/group_object/dpt/dpt10.h +./knx/group_object/dpt/dpt11.cpp +./knx/group_object/dpt/dpt11.h +./knx/group_object/dpt/dpt12.cpp +./knx/group_object/dpt/dpt12.h +./knx/group_object/dpt/dpt13.cpp +./knx/group_object/dpt/dpt13.h ./knx/group_object/dpt/dptconvert.cpp ./knx/group_object/dpt/dptconvert.h ./knx/group_object/group_object.cpp diff --git a/src/knx/group_object/dpt/dpt.h b/src/knx/group_object/dpt/dpt.h index d222715..2eaa593 100644 --- a/src/knx/group_object/dpt/dpt.h +++ b/src/knx/group_object/dpt/dpt.h @@ -3,17 +3,6 @@ #include "../group_object.h" namespace Knx { -#define DPT_Date Dpt(11, 1) -#define DPT_Value_4_Ucount Dpt(12, 1) -#define DPT_Value_4_Count Dpt(13, 1) -#define DPT_FlowRate_m3 Dpt(13, 2) / h -#define DPT_ActiveEnergy Dpt(13, 10) -#define DPT_ApparantEnergy Dpt(13, 11) -#define DPT_ReactiveEnergy Dpt(13, 12) -#define DPT_ActiveEnergy_kWh Dpt(13, 13) -#define DPT_ApparantEnergy_kVAh Dpt(13, 14) -#define DPT_ReactiveEnergy_kVARh Dpt(13, 15) -#define DPT_LongDeltaTimeSec Dpt(13, 100) #define DPT_Value_Acceleration Dpt(14, 0) #define DPT_Value_Acceleration_Angular Dpt(14, 1) #define DPT_Value_Activation_Energy Dpt(14, 2) diff --git a/src/knx/group_object/dpt/dpt11.cpp b/src/knx/group_object/dpt/dpt11.cpp new file mode 100644 index 0000000..8082843 --- /dev/null +++ b/src/knx/group_object/dpt/dpt11.cpp @@ -0,0 +1,73 @@ +#include "dpt10.h" + +#include "dptconvert.h" +#include "dpt11.h" + +Knx::Go_SizeCode Knx::DPT_Date::size() const +{ + return Go_3_Octets; +} + +void Knx::DPT_Date::encode(uint8_t* data) const +{ + unsigned8ToPayload(data, 0, _day, 0x1F); + unsigned8ToPayload(data, 1, _month, 0x0F); + unsigned8ToPayload(data, 2, _year % 100, 0x7F); +} + +bool Knx::DPT_Date::decode(uint8_t* data) +{ + _year = unsigned8FromPayload(data, 2) & 0x7F; + _month = unsigned8FromPayload(data, 1) & 0x0F; + _day = unsigned8FromPayload(data, 0) & 0x1F; + + if (_year > 99 || _month < 1 || _month > 12 || _day < 1) + { + _year = 0; + _month = 0; + _day = 0; + return false; + } + + _year += _year >= 90 ? 1900 : 2000; + return true; +} + +uint8_t Knx::DPT_Date::day() const +{ + return _day; +} + +void Knx::DPT_Date::day(uint8_t value) +{ + if (value < 1 || value > 31) + return; + + _day = value; +} + +uint8_t Knx::DPT_Date::month() const +{ + return _month; +} + +void Knx::DPT_Date::month(uint8_t value) +{ + if (value < 1 || value > 12) + return; + + _month = value; +} + +uint16_t Knx::DPT_Date::year() const +{ + return _year; +} + +void Knx::DPT_Date::year(uint16_t value) +{ + if (value < 1990 || value > 2089) + return; + + _year = value; +} diff --git a/src/knx/group_object/dpt/dpt11.h b/src/knx/group_object/dpt/dpt11.h new file mode 100644 index 0000000..93c2942 --- /dev/null +++ b/src/knx/group_object/dpt/dpt11.h @@ -0,0 +1,28 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class DPT_Date: public Dpt + { + public: + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + + uint8_t day() const; + void day(uint8_t value); + + uint8_t month() const; + void month(uint8_t value); + + uint16_t year() const; + void year(uint16_t value); + private: + uint8_t _day; + uint8_t _month; + uint16_t _year; + }; + + +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt12.cpp b/src/knx/group_object/dpt/dpt12.cpp new file mode 100644 index 0000000..0ec4d82 --- /dev/null +++ b/src/knx/group_object/dpt/dpt12.cpp @@ -0,0 +1,19 @@ +#include "dpt12.h" + +#include "dptconvert.h" + +Knx::Go_SizeCode Knx::Dpt12::size() const +{ + return Go_4_Octets; +} + +void Knx::Dpt12::encode(uint8_t* data) const +{ + unsigned32ToPayload(data, 0, _value, 0xFFFFFFFF); +} + +bool Knx::Dpt12::decode(uint8_t* data) +{ + _value = unsigned32FromPayload(data, 0); + return true; +} diff --git a/src/knx/group_object/dpt/dpt12.h b/src/knx/group_object/dpt/dpt12.h new file mode 100644 index 0000000..049226f --- /dev/null +++ b/src/knx/group_object/dpt/dpt12.h @@ -0,0 +1,20 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt12: public ValueDpt + { + public: + Dpt12() {}; + Dpt12(uint32_t value) : ValueDpt(value) {} + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + }; + + typedef Dpt12 DPT_Value_4_Ucount; + typedef Dpt12 DPT_LongTimePeriod_Sec; + typedef Dpt12 DPT_LongTimePeriod_Min; + typedef Dpt12 DPT_LongTimePeriod_Hrs; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt13.cpp b/src/knx/group_object/dpt/dpt13.cpp new file mode 100644 index 0000000..dc59977 --- /dev/null +++ b/src/knx/group_object/dpt/dpt13.cpp @@ -0,0 +1,19 @@ +#include "dpt13.h" + +#include "dptconvert.h" + +Knx::Go_SizeCode Knx::Dpt13::size() const +{ + return Go_4_Octets; +} + +void Knx::Dpt13::encode(uint8_t* data) const +{ + signed32ToPayload(data, 0, _value, 0xFFFFFFFF); +} + +bool Knx::Dpt13::decode(uint8_t* data) +{ + _value = signed32FromPayload(data, 0); + return true; +} diff --git a/src/knx/group_object/dpt/dpt13.h b/src/knx/group_object/dpt/dpt13.h new file mode 100644 index 0000000..a908be3 --- /dev/null +++ b/src/knx/group_object/dpt/dpt13.h @@ -0,0 +1,26 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt13: public ValueDpt + { + public: + Dpt13() {}; + Dpt13(int32_t value) : ValueDpt(value) {} + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + }; + + typedef Dpt13 DPT_Value_4_Count; + typedef Dpt13 DPT_FlowRate_m3_h; + typedef Dpt13 DPT_ActiveEnergy; + typedef Dpt13 DPT_ApparantEnergy; + typedef Dpt13 DPT_ReactiveEnergy; + typedef Dpt13 DPT_ActiveEnergy_kWh; + typedef Dpt13 DPT_ApparantEnergy_kVAh; + typedef Dpt13 DPT_ReactiveEnergy_kVARh; + typedef Dpt13 DPT_ActiveEnergy_MWh; + typedef Dpt13 DPT_LongDeltaTimeSec; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt9.cpp b/src/knx/group_object/dpt/dpt9.cpp index d862be2..bfef0d2 100644 --- a/src/knx/group_object/dpt/dpt9.cpp +++ b/src/knx/group_object/dpt/dpt9.cpp @@ -4,7 +4,7 @@ Knx::Go_SizeCode Knx::Dpt9::size() const { - return Go_1_Bit; + return Go_2_Octets; } void Knx::Dpt9::encode(uint8_t* data) const diff --git a/src/knx/group_object/dpt/dpt9.h b/src/knx/group_object/dpt/dpt9.h index 5f85650..64e1b03 100644 --- a/src/knx/group_object/dpt/dpt9.h +++ b/src/knx/group_object/dpt/dpt9.h @@ -60,5 +60,5 @@ namespace Knx typedef Dpt9 DPT_Rain_Amount; typedef Dpt9 DPT_Value_Wsp_kmh; typedef Dpt9GeZero DPT_Value_Absolute_Humidity; - typedef Dpt9GeZero DPT_Concentration_µgm3; + typedef Dpt9GeZero DPT_Concentration_ugm3; } \ 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 a6c027d..1af55e0 100644 --- a/src/knx/group_object/dpt/dptconvert.cpp +++ b/src/knx/group_object/dpt/dptconvert.cpp @@ -18,22 +18,6 @@ namespace Knx { if (payload_length > 0) { - // DPT 11.* - Date - if (datatype.mainGroup == 11 && datatype.subGroup == 1 && !datatype.index) - return busValueToDate(payload, payload_length, datatype, value); - - // DPT 12.* - Unsigned 32 Bit Integer - if (datatype.mainGroup == 12 && datatype.subGroup == 1 && !datatype.index) - return busValueToUnsigned32(payload, payload_length, datatype, value); - - // DPT 13.001/13.002/13.010-13.015 - Signed 32 Bit Integer - if (datatype.mainGroup == 13 && (datatype.subGroup == 1 || datatype.subGroup == 2 || (datatype.subGroup >= 10 && datatype.subGroup <= 15)) && !datatype.index) - return busValueToSigned32(payload, payload_length, datatype, value); - - // DPT 13.100 - Long Time Period - if (datatype.mainGroup == 13 && datatype.subGroup == 100 && !datatype.index) - return busValueToLongTimePeriod(payload, payload_length, datatype, value); - // DPT 14.* - 32 Bit Float if (datatype.mainGroup == 14 && datatype.subGroup <= 79 && !datatype.index) return busValueToFloat32(payload, payload_length, datatype, value); @@ -128,22 +112,6 @@ namespace Knx int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { - // DPT 11.* - Date - if (datatype.mainGroup == 11 && datatype.subGroup == 1 && !datatype.index) - return valueToBusValueDate(value, payload, payload_length, datatype); - - // DPT 12.* - Unsigned 32 Bit Integer - if (datatype.mainGroup == 12 && datatype.subGroup == 1 && !datatype.index) - return valueToBusValueUnsigned32(value, payload, payload_length, datatype); - - // DPT 13.001/13.002/13.010-13.015 - Signed 32 Bit Integer - if (datatype.mainGroup == 13 && (datatype.subGroup == 1 || datatype.subGroup == 2 || (datatype.subGroup >= 10 && datatype.subGroup <= 15)) && !datatype.index) - return valueToBusValueSigned32(value, payload, payload_length, datatype); - - // DPT 13.100 - Long Time Period - if (datatype.mainGroup == 13 && datatype.subGroup == 100 && !datatype.index) - return valueToBusValueLongTimePeriod(value, payload, payload_length, datatype); - // DPT 14.* - 32 Bit Float if (datatype.mainGroup == 14 && datatype.subGroup <= 79 && !datatype.index) return valueToBusValueFloat32(value, payload, payload_length, datatype); @@ -235,25 +203,6 @@ namespace Knx return false; } - int busValueToDate(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) - { - ASSERT_PAYLOAD(3); - unsigned short year = unsigned8FromPayload(payload, 2) & 0x7F; - unsigned char month = unsigned8FromPayload(payload, 1) & 0x0F; - unsigned char day = unsigned8FromPayload(payload, 0) & 0x1F; - - if (year > 99 || month < 1 || month > 12 || day < 1) - return false; - - struct tm tmp = {0}; - year += year >= 90 ? 1900 : 2000; - tmp.tm_mday = day; - tmp.tm_year = year; - tmp.tm_mon = month; - value = tmp; - return true; - } - int busValueToUnsigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) { ASSERT_PAYLOAD(4); @@ -716,19 +665,6 @@ namespace Knx //------------------------------------------------------------------------------------------------------------------------------------- - int valueToBusValueDate(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - struct tm tmp = value; - - if (tmp.tm_year < 1990 || tmp.tm_year > 2089) - return false; - - unsigned8ToPayload(payload, 0, tmp.tm_mday, 0x1F); - unsigned8ToPayload(payload, 1, tmp.tm_mon, 0x0F); - unsigned8ToPayload(payload, 2, tmp.tm_year % 100, 0x7F); - return true; - } - int valueToBusValueUnsigned32(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(4294967295)) @@ -738,15 +674,6 @@ namespace Knx return true; } - int valueToBusValueSigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - if ((int64_t)value < INT64_C(-2147483648) || (int64_t)value > INT64_C(2147483647)) - return false; - - signed32ToPayload(payload, 0, (uint64_t)value, 0xFFFFFFFF); - return true; - } - int valueToBusValueLongTimePeriod(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { if ((int64_t)value < INT64_C(-2147483648) || (int64_t)value > INT64_C(2147483647)) diff --git a/src/knx/group_object/dpt/dptconvert.h b/src/knx/group_object/dpt/dptconvert.h index 92b8f02..a720b44 100644 --- a/src/knx/group_object/dpt/dptconvert.h +++ b/src/knx/group_object/dpt/dptconvert.h @@ -47,7 +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 busValueToDate(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToUnsigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToSigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToLongTimePeriod(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); @@ -73,7 +72,6 @@ namespace Knx int busValueToActiveEnergy(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); //Internal to KNX - int valueToBusValueDate(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueUnsigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueSigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueLongTimePeriod(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 014b979..909317a 100644 --- a/src/knx/group_object/dpt/dpts.h +++ b/src/knx/group_object/dpt/dpts.h @@ -8,4 +8,8 @@ #include "dpt6.h" #include "dpt7.h" #include "dpt8.h" -#include "dpt9.h" \ No newline at end of file +#include "dpt9.h" +#include "dpt10.h" +#include "dpt11.h" +#include "dpt12.h" +#include "dpt13.h" \ No newline at end of file