Skip to content

Commit

Permalink
Reimplement dpt2
Browse files Browse the repository at this point in the history
  • Loading branch information
thelsing committed Sep 12, 2024
1 parent c77490a commit ae60d24
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 81 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ set(SOURCES
./knx/group_object/dpt/dpt.h
./knx/group_object/dpt/dpt1.cpp
./knx/group_object/dpt/dpt1.h
./knx/group_object/dpt/dpt2.cpp
./knx/group_object/dpt/dpt2.h
./knx/group_object/dpt/dpt9.cpp
./knx/group_object/dpt/dpt9.h
./knx/group_object/dpt/dptconvert.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/knx/group_object/dpt/dpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ namespace Knx
: mainGroup(mainGroup), subGroup(subGroup), index(index)
{
if (subGroup == 0)
println("WARNING: You used and invalid Dpt *.0");
LOGGER.warning("You used and invalid Dpt *.0");
}
}
32 changes: 0 additions & 32 deletions src/knx/group_object/dpt/dpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,6 @@
#include "../group_object.h"
namespace Knx
{
#define DPT_Ramp Dpt(1, 4)
#define DPT_Alarm Dpt(1, 5)
#define DPT_BinaryValue Dpt(1, 6)
#define DPT_Step Dpt(1, 7)
#define DPT_UpDown Dpt(1, 8)
#define DPT_OpenClose Dpt(1, 9)
#define DPT_Start Dpt(1, 10)
#define DPT_State Dpt(1, 11)
#define DPT_Invert Dpt(1, 12)
#define DPT_DimSendStyle Dpt(1, 13)
#define DPT_InputSource Dpt(1, 14)
#define DPT_Reset Dpt(1, 15)
#define DPT_Ack Dpt(1, 16)
#define DPT_Trigger Dpt(1, 17)
#define DPT_Occupancy Dpt(1, 18)
#define DPT_Window_Door Dpt(1, 19)
#define DPT_LogicalFunction Dpt(1, 21)
#define DPT_Scene_AB Dpt(1, 22)
#define DPT_ShutterBlinds_Mode Dpt(1, 23)
#define DPT_Heat_Cool Dpt(1, 100)
#define DPT_Switch_Control Dpt(2, 1)
#define DPT_Bool_Control Dpt(2, 2)
#define DPT_Enable_Control Dpt(2, 3)
#define DPT_Ramp_Control Dpt(2, 4)
#define DPT_Alarm_Control Dpt(2, 5)
#define DPT_BinaryValue_Control Dpt(2, 6)
#define DPT_Step_Control Dpt(2, 7)
#define DPT_Direction1_Control Dpt(2, 8)
#define DPT_Direction2_Control Dpt(2, 9)
#define DPT_Start_Control Dpt(2, 10)
#define DPT_State_Control Dpt(2, 11)
#define DPT_Invert_Control Dpt(2, 12)
#define DPT_Control_Dimming Dpt(3, 7)
#define DPT_Control_Blinds Dpt(3, 8)
#define DPT_Char_ASCII Dpt(4, 1)
Expand Down
22 changes: 22 additions & 0 deletions src/knx/group_object/dpt/dpt1.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,26 @@ namespace Knx
operator EnableValue() const;
DPT_Enable& operator=(const EnableValue value);
};

// TODO:
#define DPT_Ramp Dpt1(4)
#define DPT_Alarm Dpt1(5)
#define DPT_BinaryValue Dpt1(6)
#define DPT_Step Dpt1(7)
#define DPT_UpDown Dpt1(8)
#define DPT_OpenClose Dpt1(9)
#define DPT_Start Dpt1(10)
#define DPT_State Dpt1(11)
#define DPT_Invert Dpt1(12)
#define DPT_DimSendStyle Dpt1(13)
#define DPT_InputSource Dpt1(14)
#define DPT_Reset Dpt1(15)
#define DPT_Ack Dpt1(16)
#define DPT_Trigger Dpt1(17)
#define DPT_Occupancy Dpt1(18)
#define DPT_Window_Door Dpt1(19)
#define DPT_LogicalFunction Dpt1(21)
#define DPT_Scene_AB Dpt1(22)
#define DPT_ShutterBlinds_Mode Dpt1(23)
#define DPT_DayNight Dpt1(24)
}
64 changes: 64 additions & 0 deletions src/knx/group_object/dpt/dpt2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "dpt2.h"

#include "dptconvert.h"

Knx::Dpt2::Dpt2(unsigned short subgroup /* = 0*/) : Dpt(2, subgroup) {}

Knx::Dpt2::Dpt2(Dpt2Value value) : Dpt2()
{
_value = value;
}

Knx::Go_SizeCode Knx::Dpt2::size() const
{
return Go_2_Bit;
}

void Knx::Dpt2::encode(uint8_t* data) const
{
if (_value == NoControl)
{
bitToPayload(data, 6, false);
return;
}

bitToPayload(data, 6, true);
bitToPayload(data, 7, _value == Control_Function1);
}

void Knx::Dpt2::decode(uint8_t* data)
{
bool c = bitFromPayload(data, 6);

if (!c)
{
_value = NoControl;
return;
}

bool v = bitFromPayload(data, 7);

_value = v ? Control_Function1 : Control_Function0;
}

void Knx::Dpt2::value(Dpt2Value value)
{
_value = value;
}

Knx::Dpt2::Dpt2Value Knx::Dpt2::value() const
{
return _value;
}

Knx::Dpt2::operator Dpt2Value() const
{
return _value;
}


Knx::Dpt2& Knx::Dpt2::operator=(const Dpt2Value value)
{
_value = value;
return *this;
}
40 changes: 40 additions & 0 deletions src/knx/group_object/dpt/dpt2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include "dpt.h"
namespace Knx
{
class Dpt2: public Dpt
{
public:
enum Dpt2Value
{
NoControl, Control_Function0, Control_Function1
};

Dpt2(unsigned short subgroup = 0);
Dpt2(Dpt2Value value);
Go_SizeCode size() const override;

virtual void encode(uint8_t* data) const override;
virtual void decode(uint8_t* data) override;

void value(Dpt2Value value);
Dpt2Value value() const;
operator Dpt2Value() const;
Dpt2& operator=(const Dpt2Value value);
private:
Dpt2Value _value;
};

#define DPT_Switch_Control Dpt2(1)
#define DPT_Bool_Control Dpt2(2)
#define DPT_Enable_Control Dpt2(3)
#define DPT_Ramp_Control Dpt2(4)
#define DPT_Alarm_Control Dpt2(5)
#define DPT_BinaryValue_Control Dpt2(6)
#define DPT_Step_Control Dpt2(7)
#define DPT_Direction1_Control Dpt2(8)
#define DPT_Direction2_Control Dpt2(9)
#define DPT_Start_Control Dpt2(10)
#define DPT_State_Control Dpt2(11)
#define DPT_Invert_Control Dpt2(12)
}
7 changes: 5 additions & 2 deletions src/knx/group_object/dpt/dpt9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

#include "dptconvert.h"

Knx::Dpt9::Dpt9() { mainGroup = 9; }
Knx::Dpt9::Dpt9(unsigned short subgroup /* = 0*/): Dpt(9, subgroup) {}

Knx::Dpt9::Dpt9(float value) : _value(value) { mainGroup = 9; }
Knx::Dpt9::Dpt9(float value) : Dpt9()
{
_value = value;
}

Knx::Go_SizeCode Knx::Dpt9::size() const
{
Expand Down
2 changes: 1 addition & 1 deletion src/knx/group_object/dpt/dpt9.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Knx
class Dpt9: public Dpt
{
public:
Dpt9();
Dpt9(unsigned short subgroup = 0);
Dpt9(float value);
Go_SizeCode size() const override;

Expand Down
45 changes: 0 additions & 45 deletions src/knx/group_object/dpt/dptconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ namespace Knx
{
if (payload_length > 0)
{
// DPT 2.* - Binary Control

if (datatype.mainGroup == 2 && datatype.subGroup >= 1 && datatype.subGroup <= 12 && datatype.index <= 1)
return busValueToBinaryControl(payload, payload_length, datatype, value);

// DPT 3.* - Step Control
if (datatype.mainGroup == 3 && datatype.subGroup >= 7 && datatype.subGroup <= 8 && datatype.index <= 1)
return busValueToStepControl(payload, payload_length, datatype, value);
Expand Down Expand Up @@ -173,11 +168,6 @@ namespace Knx

int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{

// DPT 2.* - Binary Control
if (datatype.mainGroup == 2 && datatype.subGroup >= 1 && datatype.subGroup <= 12 && datatype.index <= 1)
return valueToBusValueBinaryControl(value, payload, payload_length, datatype);

// DPT 3.* - Step Control
if (datatype.mainGroup == 3 && datatype.subGroup >= 7 && datatype.subGroup <= 8 && datatype.index <= 1)
return valueToBusValueStepControl(value, payload, payload_length, datatype);
Expand Down Expand Up @@ -325,23 +315,6 @@ namespace Knx
return false;
}

int busValueToBinaryControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(1);

switch (datatype.index)
{
case 0:
value = bitFromPayload(payload, 6);
return true;

case 1:
value = bitFromPayload(payload, 7);
return true;
}

return false;
}

int busValueToStepControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
Expand Down Expand Up @@ -988,24 +961,6 @@ namespace Knx

//-------------------------------------------------------------------------------------------------------------------------------------

int valueToBusValueBinaryControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
switch (datatype.index)
{
case 0:
bitToPayload(payload, 6, value);
break;

case 1:
bitToPayload(payload, 7, value);
break;

default:
return false;
}

return true;
}

int valueToBusValueStepControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
Expand Down
1 change: 1 addition & 0 deletions src/knx/group_object/dpt/dpts.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "dpt1.h"
#include "dpt2.h"
#include "dpt9.h"

0 comments on commit ae60d24

Please sign in to comment.