Skip to content

Commit

Permalink
more dpt refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thelsing committed Sep 13, 2024
1 parent 00b9c98 commit 576ed31
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 94 deletions.
33 changes: 33 additions & 0 deletions src/knx/group_object/dpt/dpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,37 @@ namespace Knx
*/
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;
};
}
32 changes: 2 additions & 30 deletions src/knx/group_object/dpt/dpt1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,18 @@

#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;
}

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;
}
12 changes: 1 addition & 11 deletions src/knx/group_object/dpt/dpt1.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@
#include "dpt.h"
namespace Knx
{
class Dpt1: public Dpt
class Dpt1: public DPT<bool>
{
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<typename T> class DPT1 : public Dpt1
Expand Down
16 changes: 3 additions & 13 deletions src/knx/group_object/dpt/dpt2.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Knx
{
NoControl, Control
};
template<typename T> class DPT2: public Dpt
template<typename T> class DPT2: public DPT<T>
{
public:
Go_SizeCode size() const override
Expand All @@ -26,7 +26,7 @@ namespace Knx
}

bitToPayload(data, 6, true);
bitToPayload(data, 7, ((int)_value) == 1);
bitToPayload(data, 7, ((int)DPT<T>::value()) == 1);
}

bool decode(uint8_t* data) override
Expand All @@ -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;
}

Expand All @@ -55,16 +55,6 @@ namespace Knx
return _control;
}

void value(T value)
{
_value = value;
}

T value() const
{
return _value;
}

private:
ControlValue _control;
T _value;
Expand Down
32 changes: 2 additions & 30 deletions src/knx/group_object/dpt/dpt4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,22 @@

#include "dptconvert.h"

Knx::Dpt4::Dpt4() {}

Knx::Dpt4::Dpt4(char value)
{
_value = value;
}

Knx::Go_SizeCode Knx::Dpt4::size() const
{
return Go_1_Octet;
}

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);
Expand Down
11 changes: 1 addition & 10 deletions src/knx/group_object/dpt/dpt4.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@
#include "dpt.h"
namespace Knx
{
class Dpt4: public Dpt
class Dpt4: public DPT<char>
{
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
Expand Down

0 comments on commit 576ed31

Please sign in to comment.