more dpt refactoring

This commit is contained in:
Thomas Kunze 2024-09-13 23:05:27 +02:00
parent 00b9c98786
commit 576ed31577
6 changed files with 42 additions and 94 deletions

View File

@ -330,4 +330,37 @@ namespace Knx
*/ */
virtual bool decode(uint8_t* data) = 0; virtual bool decode(uint8_t* data) = 0;
}; };
template<typename T> 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;
};
} }

View File

@ -2,13 +2,6 @@
#include "dptconvert.h" #include "dptconvert.h"
Knx::Dpt1::Dpt1() {}
Knx::Dpt1::Dpt1(bool value)
{
this->value(value);
}
Knx::Go_SizeCode Knx::Dpt1::size() const Knx::Go_SizeCode Knx::Dpt1::size() const
{ {
return Go_1_Bit; return Go_1_Bit;
@ -16,32 +9,11 @@ Knx::Go_SizeCode Knx::Dpt1::size() const
void Knx::Dpt1::encode(uint8_t* data) const void Knx::Dpt1::encode(uint8_t* data) const
{ {
bitToPayload(data, 7, _value); bitToPayload(data, 7, value());
} }
bool Knx::Dpt1::decode(uint8_t* data) bool Knx::Dpt1::decode(uint8_t* data)
{ {
_value = bitFromPayload(data, 7); value(bitFromPayload(data, 7));
return true; 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;
}

View File

@ -2,22 +2,12 @@
#include "dpt.h" #include "dpt.h"
namespace Knx namespace Knx
{ {
class Dpt1: public Dpt class Dpt1: public DPT<bool>
{ {
public: public:
Dpt1();
Dpt1(bool value);
Go_SizeCode size() const override; Go_SizeCode size() const override;
void encode(uint8_t* data) const override; void encode(uint8_t* data) const override;
bool decode(uint8_t* data) 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<typename T> class DPT1 : public Dpt1 template<typename T> class DPT1 : public Dpt1

View File

@ -9,7 +9,7 @@ namespace Knx
{ {
NoControl, Control NoControl, Control
}; };
template<typename T> class DPT2: public Dpt template<typename T> class DPT2: public DPT<T>
{ {
public: public:
Go_SizeCode size() const override Go_SizeCode size() const override
@ -26,7 +26,7 @@ namespace Knx
} }
bitToPayload(data, 6, true); bitToPayload(data, 6, true);
bitToPayload(data, 7, ((int)_value) == 1); bitToPayload(data, 7, ((int)DPT<T>::value()) == 1);
} }
bool decode(uint8_t* data) override bool decode(uint8_t* data) override
@ -41,7 +41,7 @@ namespace Knx
bool v = bitFromPayload(data, 7); bool v = bitFromPayload(data, 7);
_value = v ? (T)1 : (T)0; value(v ? (T)1 : (T)0);
return true; return true;
} }
@ -55,16 +55,6 @@ namespace Knx
return _control; return _control;
} }
void value(T value)
{
_value = value;
}
T value() const
{
return _value;
}
private: private:
ControlValue _control; ControlValue _control;
T _value; T _value;

View File

@ -2,13 +2,6 @@
#include "dptconvert.h" #include "dptconvert.h"
Knx::Dpt4::Dpt4() {}
Knx::Dpt4::Dpt4(char value)
{
_value = value;
}
Knx::Go_SizeCode Knx::Dpt4::size() const Knx::Go_SizeCode Knx::Dpt4::size() const
{ {
return Go_1_Octet; return Go_1_Octet;
@ -16,36 +9,15 @@ Knx::Go_SizeCode Knx::Dpt4::size() const
void Knx::Dpt4::encode(uint8_t* data) 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) bool Knx::Dpt4::decode(uint8_t* data)
{ {
_value = signed8FromPayload(data, 0); value(signed8FromPayload(data, 0));
return true; 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) bool Knx::DPT_Char_ASCII::decode(uint8_t* data)
{ {
Dpt4::value(signed8FromPayload(data, 0) & 0x7F); Dpt4::value(signed8FromPayload(data, 0) & 0x7F);

View File

@ -2,22 +2,13 @@
#include "dpt.h" #include "dpt.h"
namespace Knx namespace Knx
{ {
class Dpt4: public Dpt class Dpt4: public DPT<char>
{ {
public: public:
Dpt4();
Dpt4(char value);
Go_SizeCode size() const override; Go_SizeCode size() const override;
void encode(uint8_t* data) const override; void encode(uint8_t* data) const override;
bool decode(uint8_t* data) 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 class DPT_Char_ASCII: public Dpt4