From 576ed315773a10acdc24f6f3c8d4b356b6c856d9 Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Fri, 13 Sep 2024 23:05:27 +0200 Subject: [PATCH] more dpt refactoring --- src/knx/group_object/dpt/dpt.h | 33 +++++++++++++++++++++++++++++++ src/knx/group_object/dpt/dpt1.cpp | 32 ++---------------------------- src/knx/group_object/dpt/dpt1.h | 12 +---------- src/knx/group_object/dpt/dpt2.h | 16 +++------------ src/knx/group_object/dpt/dpt4.cpp | 32 ++---------------------------- src/knx/group_object/dpt/dpt4.h | 11 +---------- 6 files changed, 42 insertions(+), 94 deletions(-) diff --git a/src/knx/group_object/dpt/dpt.h b/src/knx/group_object/dpt/dpt.h index c87fb496..0e8fb5fd 100644 --- a/src/knx/group_object/dpt/dpt.h +++ b/src/knx/group_object/dpt/dpt.h @@ -330,4 +330,37 @@ namespace Knx */ virtual bool decode(uint8_t* data) = 0; }; + + template class DPT: public Dpt + { + public: + DPT() {}; + DPT(T value) + { + _value = value; + } + + virtual void value(T value) + { + _value = value; + } + + T value() const + { + return _value; + } + + operator T() const + { + return _value; + } + + DPT& operator=(const T value) + { + _value = value; + return *this; + } + private: + T _value; + }; } \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt1.cpp b/src/knx/group_object/dpt/dpt1.cpp index 24d21b53..cc4e8e47 100644 --- a/src/knx/group_object/dpt/dpt1.cpp +++ b/src/knx/group_object/dpt/dpt1.cpp @@ -2,13 +2,6 @@ #include "dptconvert.h" -Knx::Dpt1::Dpt1() {} - -Knx::Dpt1::Dpt1(bool value) -{ - this->value(value); -} - Knx::Go_SizeCode Knx::Dpt1::size() const { return Go_1_Bit; @@ -16,32 +9,11 @@ Knx::Go_SizeCode Knx::Dpt1::size() const void Knx::Dpt1::encode(uint8_t* data) const { - bitToPayload(data, 7, _value); + bitToPayload(data, 7, value()); } bool Knx::Dpt1::decode(uint8_t* data) { - _value = bitFromPayload(data, 7); + value(bitFromPayload(data, 7)); return true; } - -void Knx::Dpt1::value(bool value) -{ - _value = value; -} - -bool Knx::Dpt1::value() const -{ - return _value; -} - -Knx::Dpt1::operator bool() const -{ - return _value; -} - -Knx::Dpt1& Knx::Dpt1::operator=(const bool value) -{ - _value = value; - return *this; -} diff --git a/src/knx/group_object/dpt/dpt1.h b/src/knx/group_object/dpt/dpt1.h index 817b7f17..e15f7bf6 100644 --- a/src/knx/group_object/dpt/dpt1.h +++ b/src/knx/group_object/dpt/dpt1.h @@ -2,22 +2,12 @@ #include "dpt.h" namespace Knx { - class Dpt1: public Dpt + class Dpt1: public DPT { public: - Dpt1(); - Dpt1(bool value); Go_SizeCode size() const override; - void encode(uint8_t* data) const override; bool decode(uint8_t* data) override; - - void value(bool value); - bool value() const; - operator bool() const; - Dpt1& operator=(const bool value); - private: - bool _value; }; template class DPT1 : public Dpt1 diff --git a/src/knx/group_object/dpt/dpt2.h b/src/knx/group_object/dpt/dpt2.h index a9a6f28f..74bf41bc 100644 --- a/src/knx/group_object/dpt/dpt2.h +++ b/src/knx/group_object/dpt/dpt2.h @@ -9,7 +9,7 @@ namespace Knx { NoControl, Control }; - template class DPT2: public Dpt + template class DPT2: public DPT { public: Go_SizeCode size() const override @@ -26,7 +26,7 @@ namespace Knx } bitToPayload(data, 6, true); - bitToPayload(data, 7, ((int)_value) == 1); + bitToPayload(data, 7, ((int)DPT::value()) == 1); } bool decode(uint8_t* data) override @@ -41,7 +41,7 @@ namespace Knx bool v = bitFromPayload(data, 7); - _value = v ? (T)1 : (T)0; + value(v ? (T)1 : (T)0); return true; } @@ -55,16 +55,6 @@ namespace Knx return _control; } - void value(T value) - { - _value = value; - } - - T value() const - { - return _value; - } - private: ControlValue _control; T _value; diff --git a/src/knx/group_object/dpt/dpt4.cpp b/src/knx/group_object/dpt/dpt4.cpp index c7bc861f..8e94b50a 100644 --- a/src/knx/group_object/dpt/dpt4.cpp +++ b/src/knx/group_object/dpt/dpt4.cpp @@ -2,13 +2,6 @@ #include "dptconvert.h" -Knx::Dpt4::Dpt4() {} - -Knx::Dpt4::Dpt4(char value) -{ - _value = value; -} - Knx::Go_SizeCode Knx::Dpt4::size() const { return Go_1_Octet; @@ -16,36 +9,15 @@ Knx::Go_SizeCode Knx::Dpt4::size() const void Knx::Dpt4::encode(uint8_t* data) const { - unsigned8ToPayload(data, 0, _value, 0xFF); + unsigned8ToPayload(data, 0, value(), 0xFF); } bool Knx::Dpt4::decode(uint8_t* data) { - _value = signed8FromPayload(data, 0); + value(signed8FromPayload(data, 0)); return true; } -void Knx::Dpt4::value(char value) -{ - _value = value; -} - -char Knx::Dpt4::value() const -{ - return _value; -} - -Knx::Dpt4::operator char() const -{ - return _value; -} - -Knx::Dpt4& Knx::Dpt4::operator=(const char value) -{ - _value = value; - return *this; -} - bool Knx::DPT_Char_ASCII::decode(uint8_t* data) { Dpt4::value(signed8FromPayload(data, 0) & 0x7F); diff --git a/src/knx/group_object/dpt/dpt4.h b/src/knx/group_object/dpt/dpt4.h index 739fe369..b46a6e1f 100644 --- a/src/knx/group_object/dpt/dpt4.h +++ b/src/knx/group_object/dpt/dpt4.h @@ -2,22 +2,13 @@ #include "dpt.h" namespace Knx { - class Dpt4: public Dpt + class Dpt4: public DPT { public: - Dpt4(); - Dpt4(char value); Go_SizeCode size() const override; void encode(uint8_t* data) const override; bool decode(uint8_t* data) override; - - virtual void value(char value); - char value() const; - operator char() const; - Dpt4& operator=(const char value); - private: - char _value; }; class DPT_Char_ASCII: public Dpt4