From 27473ffae43dea597b70bb2d2506429eab7d6655 Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Sat, 14 Sep 2024 20:46:30 +0200 Subject: [PATCH] more dpt refactoring --- src/CMakeLists.txt | 2 + src/knx/group_object/dpt/dpt.h | 17 ++----- src/knx/group_object/dpt/dpt1.h | 4 +- src/knx/group_object/dpt/dpt2.h | 6 ++- src/knx/group_object/dpt/dpt4.h | 7 ++- src/knx/group_object/dpt/dpt6.h | 4 +- src/knx/group_object/dpt/dpt7.h | 26 +++++----- src/knx/group_object/dpt/dpt8.cpp | 19 ++++++++ src/knx/group_object/dpt/dpt8.h | 25 ++++++++++ src/knx/group_object/dpt/dpt9.cpp | 68 +++++++++++++++++++------- src/knx/group_object/dpt/dpt9.h | 80 ++++++++++++++++++++----------- src/knx/group_object/dpt/dpts.h | 1 + 12 files changed, 183 insertions(+), 76 deletions(-) create mode 100644 src/knx/group_object/dpt/dpt8.cpp create mode 100644 src/knx/group_object/dpt/dpt8.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 59fb2ec..53ae657 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,6 +52,8 @@ set(SOURCES ./knx/group_object/dpt/dpt6.h ./knx/group_object/dpt/dpt7.cpp ./knx/group_object/dpt/dpt7.h +./knx/group_object/dpt/dpt8.cpp +./knx/group_object/dpt/dpt8.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 f3c2671..9aa84ce 100644 --- a/src/knx/group_object/dpt/dpt.h +++ b/src/knx/group_object/dpt/dpt.h @@ -3,15 +3,6 @@ #include "../group_object.h" namespace Knx { -#define DPT_Value_2_Count Dpt(8, 1) -#define DPT_DeltaTimeMsec Dpt(8, 2) -#define DPT_DeltaTime10MSec Dpt(8, 3) -#define DPT_DeltaTime100MSec Dpt(8, 4) -#define DPT_DeltaTimeSec Dpt(8, 5) -#define DPT_DeltaTimeMin Dpt(8, 6) -#define DPT_DeltaTimeHrs Dpt(8, 7) -#define DPT_Percent_V16 Dpt(8, 10) -#define DPT_Rotation_Angle Dpt(8, 11) #define DPT_TimeOfDay Dpt(10, 1, 1) #define DPT_Date Dpt(11, 1) #define DPT_Value_4_Ucount Dpt(12, 1) @@ -313,11 +304,11 @@ namespace Knx virtual bool decode(uint8_t* data) = 0; }; - template class DPT: public Dpt + template class ValueDpt: public Dpt { public: - DPT() {}; - DPT(T value) + ValueDpt() {}; + ValueDpt(T value) { _value = value; } @@ -337,7 +328,7 @@ namespace Knx return _value; } - DPT& operator=(const T value) + ValueDpt& operator=(const T value) { _value = value; return *this; diff --git a/src/knx/group_object/dpt/dpt1.h b/src/knx/group_object/dpt/dpt1.h index e15f7bf..2023fc1 100644 --- a/src/knx/group_object/dpt/dpt1.h +++ b/src/knx/group_object/dpt/dpt1.h @@ -2,9 +2,11 @@ #include "dpt.h" namespace Knx { - class Dpt1: public DPT + class Dpt1: public ValueDpt { public: + Dpt1() {} + Dpt1(bool value) : ValueDpt(value) {} Go_SizeCode size() const override; void encode(uint8_t* data) const override; bool decode(uint8_t* data) override; diff --git a/src/knx/group_object/dpt/dpt2.h b/src/knx/group_object/dpt/dpt2.h index 74bf41b..0138012 100644 --- a/src/knx/group_object/dpt/dpt2.h +++ b/src/knx/group_object/dpt/dpt2.h @@ -9,9 +9,11 @@ namespace Knx { NoControl, Control }; - template class DPT2: public DPT + template class DPT2: public ValueDpt { public: + DPT2() {}; + DPT2(bool value) : ValueDpt(value) {} Go_SizeCode size() const override { return Go_2_Bit; @@ -26,7 +28,7 @@ namespace Knx } bitToPayload(data, 6, true); - bitToPayload(data, 7, ((int)DPT::value()) == 1); + bitToPayload(data, 7, ((int)ValueDpt::value()) == 1); } bool decode(uint8_t* data) override diff --git a/src/knx/group_object/dpt/dpt4.h b/src/knx/group_object/dpt/dpt4.h index 6840fc9..76bf1ae 100644 --- a/src/knx/group_object/dpt/dpt4.h +++ b/src/knx/group_object/dpt/dpt4.h @@ -2,9 +2,11 @@ #include "dpt.h" namespace Knx { - class Dpt4: public DPT + class Dpt4: public ValueDpt { public: + Dpt4() {}; + Dpt4(char value) : ValueDpt(value) {} Go_SizeCode size() const override; void encode(uint8_t* data) const override; @@ -13,6 +15,9 @@ namespace Knx class DPT_Char_ASCII: public Dpt4 { + public: + DPT_Char_ASCII() {}; + DPT_Char_ASCII(char value) : Dpt4(value) {} bool decode(uint8_t* data) override; void value(char value) override; }; diff --git a/src/knx/group_object/dpt/dpt6.h b/src/knx/group_object/dpt/dpt6.h index 20f341e..c5b5fe9 100644 --- a/src/knx/group_object/dpt/dpt6.h +++ b/src/knx/group_object/dpt/dpt6.h @@ -2,9 +2,11 @@ #include "dpt.h" namespace Knx { - class Dpt6: public DPT + class Dpt6: public ValueDpt { public: + Dpt6() {}; + Dpt6(int8_t value) : ValueDpt(value) {} Go_SizeCode size() const override; void encode(uint8_t* data) const override; diff --git a/src/knx/group_object/dpt/dpt7.h b/src/knx/group_object/dpt/dpt7.h index 08a70d0..e37bd89 100644 --- a/src/knx/group_object/dpt/dpt7.h +++ b/src/knx/group_object/dpt/dpt7.h @@ -2,24 +2,26 @@ #include "dpt.h" namespace Knx { - class Dpt7: public DPT + class Dpt7: public ValueDpt { public: + Dpt7() {}; + Dpt7(uint16_t value) : ValueDpt(value) {} Go_SizeCode size() const override; void encode(uint8_t* data) const override; bool decode(uint8_t* data) override; }; -typedef Dpt7 DPT_Value_2_Ucount; -typedef Dpt7 DPT_TimePeriodMsec; -typedef Dpt7 DPT_TimePeriod10MSec; -typedef Dpt7 DPT_TimePeriod100MSec; -typedef Dpt7 DPT_TimePeriodSec; -typedef Dpt7 DPT_TimePeriodMin; -typedef Dpt7 DPT_TimePeriodHrs; -typedef Dpt7 DPT_PropDataType; -typedef Dpt7 DPT_Length_mm; -typedef Dpt7 DPT_UElCurrentmA; -typedef Dpt7 DPT_Brightness; + typedef Dpt7 DPT_Value_2_Ucount; + typedef Dpt7 DPT_TimePeriodMsec; + typedef Dpt7 DPT_TimePeriod10MSec; + typedef Dpt7 DPT_TimePeriod100MSec; + typedef Dpt7 DPT_TimePeriodSec; + typedef Dpt7 DPT_TimePeriodMin; + typedef Dpt7 DPT_TimePeriodHrs; + typedef Dpt7 DPT_PropDataType; + typedef Dpt7 DPT_Length_mm; + typedef Dpt7 DPT_UElCurrentmA; + typedef Dpt7 DPT_Brightness; } \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt8.cpp b/src/knx/group_object/dpt/dpt8.cpp new file mode 100644 index 0000000..7808443 --- /dev/null +++ b/src/knx/group_object/dpt/dpt8.cpp @@ -0,0 +1,19 @@ +#include "dpt8.h" + +#include "dptconvert.h" + +Knx::Go_SizeCode Knx::Dpt8::size() const +{ + return Go_2_Octets; +} + +void Knx::Dpt8::encode(uint8_t* data) const +{ + signed16ToPayload(data, 0, _value, 0xFFFF); +} + +bool Knx::Dpt8::decode(uint8_t* data) +{ + _value = signed16FromPayload(data, 0); + return true; +} diff --git a/src/knx/group_object/dpt/dpt8.h b/src/knx/group_object/dpt/dpt8.h new file mode 100644 index 0000000..7fd3f4b --- /dev/null +++ b/src/knx/group_object/dpt/dpt8.h @@ -0,0 +1,25 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt8: public ValueDpt + { + public: + Dpt8() {}; + Dpt8(int16_t value) : ValueDpt(value) {} + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + }; + + typedef Dpt8 DPT_Value_2_Count; + typedef Dpt8 DPT_DeltaTimeMsec; + typedef Dpt8 DPT_DeltaTime10MSec; + typedef Dpt8 DPT_DeltaTime100MSec; + typedef Dpt8 DPT_DeltaTimeSec; + typedef Dpt8 DPT_DeltaTimeMin; + typedef Dpt8 DPT_DeltaTimeHrs; + typedef Dpt8 DPT_Percent_V16; + typedef Dpt8 DPT_Rotation_Angle; +} \ 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 e2d6b72..d862be2 100644 --- a/src/knx/group_object/dpt/dpt9.cpp +++ b/src/knx/group_object/dpt/dpt9.cpp @@ -2,13 +2,6 @@ #include "dptconvert.h" -Knx::Dpt9::Dpt9() {} - -Knx::Dpt9::Dpt9(float value) -{ - _value = value; -} - Knx::Go_SizeCode Knx::Dpt9::size() const { return Go_1_Bit; @@ -28,24 +21,65 @@ bool Knx::Dpt9::decode(uint8_t* data) return true; } -void Knx::Dpt9::value(float value) +bool Knx::DPT_Value_Temp::decode(uint8_t* data) { - _value = value; + Dpt9::decode(data); + + if (_value < -273.0f) + { + _value = 0; + return false; + } + + return true; } -float Knx::Dpt9::value() const +void Knx::DPT_Value_Temp::value(float value) { - return _value; + if (value < -273.0f) + return; + + Dpt9::value(value); } -Knx::Dpt9::operator float() const +bool Knx::DPT_Value_Temp_F::decode(uint8_t* data) { - return _value; + Dpt9::decode(data); + + if (_value < -459.6f) + { + _value = 0; + return false; + } + + return true; } - -Knx::Dpt9& Knx::Dpt9::operator=(const float value) +void Knx::DPT_Value_Temp_F::value(float value) { - _value = value; - return *this; + if (value < -459.6f) + return; + + Dpt9::value(value); +} + +bool Knx::Dpt9GeZero::decode(uint8_t* data) +{ + Dpt9::decode(data); + + if (_value < 0) + { + _value = 0; + return false; + } + + return true; +} + +void Knx::Dpt9GeZero::value(float value) +{ + if (value < 0) + return; + + Dpt9::value(value); } diff --git a/src/knx/group_object/dpt/dpt9.h b/src/knx/group_object/dpt/dpt9.h index 71e5590..5f85650 100644 --- a/src/knx/group_object/dpt/dpt9.h +++ b/src/knx/group_object/dpt/dpt9.h @@ -2,41 +2,63 @@ #include "dpt.h" namespace Knx { - class Dpt9: public Dpt + class Dpt9: public ValueDpt { public: - Dpt9(); - Dpt9(float value); + Dpt9() {}; + Dpt9(float value) : ValueDpt(value) {} Go_SizeCode size() const override; void encode(uint8_t* data) const override; bool decode(uint8_t* data) override; - - void value(float value); - float value() const; - operator float() const; - Dpt9& operator=(const float value); - private: - float _value; }; -#define DPT_Value_Temp Dpt(9, 1) -#define DPT_Value_Tempd Dpt(9, 2) -#define DPT_Value_Tempa Dpt(9, 3) -#define DPT_Value_Lux Dpt(9, 4) -#define DPT_Value_Wsp Dpt(9, 5) -#define DPT_Value_Pres Dpt(9, 6) -#define DPT_Value_Humidity Dpt(9, 7) -#define DPT_Value_AirQuality Dpt(9, 8) -#define DPT_Value_Time1 Dpt(9, 10) -#define DPT_Value_Time2 Dpt(9, 11) -#define DPT_Value_Volt Dpt(9, 20) -#define DPT_Value_Curr Dpt(9, 21) -#define DPT_PowerDensity Dpt(9, 22) -#define DPT_KelvinPerPercent Dpt(9, 23) -#define DPT_Power Dpt(9, 24) -#define DPT_Value_Volume_Flow Dpt(9, 25) -#define DPT_Rain_Amount Dpt(9, 26) -#define DPT_Value_Temp_F Dpt(9, 27) -#define DPT_Value_Wsp_kmh Dpt(9, 28) + class DPT_Value_Temp : public Dpt9 + { + public: + DPT_Value_Temp() {}; + DPT_Value_Temp(float value) : Dpt9(value) {} + bool decode(uint8_t* data) override; + void value(float value) override; + + }; + + class DPT_Value_Temp_F : public Dpt9 + { + public: + DPT_Value_Temp_F() {}; + DPT_Value_Temp_F(float value) : Dpt9(value) {} + bool decode(uint8_t* data) override; + void value(float value) override; + + }; + + class Dpt9GeZero : public Dpt9 + { + public: + Dpt9GeZero() {}; + Dpt9GeZero(float value) : Dpt9(value) {} + bool decode(uint8_t* data) override; + void value(float value) override; + }; + + typedef Dpt9 DPT_Value_Tempd; + typedef Dpt9 DPT_Value_Tempa; + typedef Dpt9GeZero DPT_Value_Lux; + typedef Dpt9GeZero DPT_Value_Wsp; + typedef Dpt9GeZero DPT_Value_Pres; + typedef Dpt9GeZero DPT_Value_Humidity; + typedef Dpt9GeZero DPT_Value_AirQuality; + typedef Dpt9 DPT_Value_Time1; + typedef Dpt9 DPT_Value_Time2; + typedef Dpt9 DPT_Value_Volt; + typedef Dpt9 DPT_Value_Curr; + typedef Dpt9 DPT_PowerDensity; + typedef Dpt9 DPT_KelvinPerPercent; + typedef Dpt9 DPT_Power; + typedef Dpt9 DPT_Value_Volume_Flow; + typedef Dpt9 DPT_Rain_Amount; + typedef Dpt9 DPT_Value_Wsp_kmh; + typedef Dpt9GeZero DPT_Value_Absolute_Humidity; + typedef Dpt9GeZero DPT_Concentration_µgm3; } \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpts.h b/src/knx/group_object/dpt/dpts.h index 2f6a900..014b979 100644 --- a/src/knx/group_object/dpt/dpts.h +++ b/src/knx/group_object/dpt/dpts.h @@ -7,4 +7,5 @@ #include "dpt5.h" #include "dpt6.h" #include "dpt7.h" +#include "dpt8.h" #include "dpt9.h" \ No newline at end of file