Skip to content

Commit

Permalink
reimplement dpt 6
Browse files Browse the repository at this point in the history
  • Loading branch information
thelsing committed Sep 13, 2024
1 parent 576ed31 commit 2e77172
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 174 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ set(SOURCES
./knx/group_object/dpt/dpt4.h
./knx/group_object/dpt/dpt5.cpp
./knx/group_object/dpt/dpt5.h
./knx/group_object/dpt/dpt6.cpp
./knx/group_object/dpt/dpt6.h
./knx/group_object/dpt/dpt9.cpp
./knx/group_object/dpt/dpt9.h
./knx/group_object/dpt/dptconvert.cpp
Expand Down
9 changes: 1 addition & 8 deletions src/knx/group_object/dpt/dpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
#include "../group_object.h"
namespace Knx
{




#define DPT_Percent_V8 Dpt(6, 1)
#define DPT_Value_1_Count Dpt(6, 10)
#define DPT_Status_Mode3 Dpt(6, 20)
#define DPT_Value_2_Ucount Dpt(7, 1)
#define DPT_TimePeriodMsec Dpt(7, 2)
#define DPT_TimePeriod10MSec Dpt(7, 3)
Expand Down Expand Up @@ -360,7 +353,7 @@ namespace Knx
_value = value;
return *this;
}
private:
protected:
T _value;
};
}
4 changes: 2 additions & 2 deletions src/knx/group_object/dpt/dpt4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ void Knx::Dpt4::encode(uint8_t* data) const

bool Knx::Dpt4::decode(uint8_t* data)
{
value(signed8FromPayload(data, 0));
value(unsigned8FromPayload(data, 0));
return true;
}

bool Knx::DPT_Char_ASCII::decode(uint8_t* data)
{
Dpt4::value(signed8FromPayload(data, 0) & 0x7F);
Dpt4::value(unsigned8FromPayload(data, 0) & 0x7F);
return true;
}

Expand Down
109 changes: 109 additions & 0 deletions src/knx/group_object/dpt/dpt6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "dpt6.h"

#include "dptconvert.h"

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

void Knx::Dpt6::encode(uint8_t* data) const
{
signed8ToPayload(data, 0, value(), 0xFF);
}

bool Knx::Dpt6::decode(uint8_t* data)
{
value(signed8FromPayload(data, 0));
return true;
}

bool Knx::DPT_Status_Mode3::decode(uint8_t* data)
{
int8_t val = signed8FromPayload(data, 0);

int8_t mode = val & 0x7;

if ( mode != (1 << 0) && mode != (1 << 1) && mode != (1 << 2) )
return false;

value(val);
return true;
}

Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::A()
{
return ( _value & (1 << 7) ) > 0 ? Clear : Set;
}

void Knx::DPT_Status_Mode3::A(SetClearValue value)
{
if (value == Set)
_value &= ~ (1 << 7);
else
_value |= (1 << 7);
}


Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::B()
{
return ( _value & (1 << 6) ) > 0 ? Clear : Set;
}

void Knx::DPT_Status_Mode3::B(SetClearValue value)
{
if (value == Set)
_value &= ~ (1 << 6);
else
_value |= (1 << 6);
}

Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::C()
{
return ( _value & (1 << 5) ) > 0 ? Clear : Set;
}

void Knx::DPT_Status_Mode3::C(SetClearValue value)
{
if (value == Set)
_value &= ~ (1 << 5);
else
_value |= (1 << 5);
}

Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::D()
{
return ( _value & (1 << 4) ) > 0 ? Clear : Set;
}

void Knx::DPT_Status_Mode3::D(SetClearValue value)
{
if (value == Set)
_value &= ~ (1 << 4);
else
_value |= (1 << 4);
}

Knx::DPT_Status_Mode3::SetClearValue Knx::DPT_Status_Mode3::E()
{
return ( _value & (1 << 3) ) > 0 ? Clear : Set;
}

void Knx::DPT_Status_Mode3::E(SetClearValue value)
{
if (value == Set)
_value &= ~ (1 << 3);
else
_value |= (1 << 3);
}

Knx::DPT_Status_Mode3::ActiveModeValue Knx::DPT_Status_Mode3::activeMode()
{
return (ActiveModeValue) (_value & 0x7);
}

void Knx::DPT_Status_Mode3::activeMode(ActiveModeValue value)
{
_value &= ~ (0x7);
_value |= (int8_t)value;
}
42 changes: 42 additions & 0 deletions src/knx/group_object/dpt/dpt6.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include "dpt.h"
namespace Knx
{
class Dpt6: public DPT<int8_t>
{
public:
Go_SizeCode size() const override;

void encode(uint8_t* data) const override;
bool decode(uint8_t* data) override;
};

typedef Dpt6 DPT_Percent_V8;
typedef Dpt6 DPT_Value_1_Count;

class DPT_Status_Mode3: public Dpt6
{
public:
bool decode(uint8_t* data) override;
enum SetClearValue { Set = 0, Clear = 1};
enum ActiveModeValue { Mode0Active = 0x1, Mode1Active = 0x2, Mode2Active = 0x4};

SetClearValue A();
void A(SetClearValue value);

SetClearValue B();
void B(SetClearValue value);

SetClearValue C();
void C(SetClearValue value);

SetClearValue D();
void D(SetClearValue value);

SetClearValue E();
void E(SetClearValue value);

ActiveModeValue activeMode();
void activeMode(ActiveModeValue value);
};
}
152 changes: 0 additions & 152 deletions src/knx/group_object/dpt/dptconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ namespace Knx
{
if (payload_length > 0)
{
// DPT 5.* - Unsigned 8 Bit Integer
if (datatype.mainGroup == 5 && ((datatype.subGroup >= 1 && datatype.subGroup <= 6 && datatype.subGroup != 2) || datatype.subGroup == 10) && !datatype.index)
return busValueToUnsigned8(payload, payload_length, datatype, value);

// DPT 6.001/6.010 - Signed 8 Bit Integer
if (datatype.mainGroup == 6 && (datatype.subGroup == 1 || datatype.subGroup == 10) && !datatype.index)
return busValueToSigned8(payload, payload_length, datatype, value);

// DPT 6.020 - Status with Mode
if (datatype.mainGroup == 6 && datatype.subGroup == 20 && datatype.index <= 5)
return busValueToStatusAndMode(payload, payload_length, datatype, value);

// DPT 7.001/7.010/7.011/7.012/7.013/7.600 - Unsigned 16 Bit Integer
if (datatype.mainGroup == 7 && (datatype.subGroup == 1 || (datatype.subGroup >= 10 && datatype.subGroup <= 13) || (datatype.subGroup == 600)) && !datatype.index)
return busValueToUnsigned16(payload, payload_length, datatype, value);
Expand Down Expand Up @@ -160,19 +148,6 @@ namespace Knx

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

// DPT 5.* - Unsigned 8 Bit Integer
if (datatype.mainGroup == 5 && ((datatype.subGroup >= 1 && datatype.subGroup <= 6 && datatype.subGroup != 2) || datatype.subGroup == 10) && !datatype.index)
return valueToBusValueUnsigned8(value, payload, payload_length, datatype);

// DPT 6.001/6.010 - Signed 8 Bit Integer
if (datatype.mainGroup == 6 && (datatype.subGroup == 1 || datatype.subGroup == 10) && !datatype.index)
return valueToBusValueSigned8(value, payload, payload_length, datatype);

// DPT 6.020 - Status with Mode
if (datatype.mainGroup == 6 && datatype.subGroup == 20 && datatype.index <= 5)
return valueToBusValueStatusAndMode(value, payload, payload_length, datatype);

// DPT 7.001/7.010/7.011/7.012/7.013/7.600 - Unsigned 16 Bit Integer
if (datatype.mainGroup == 7 && (datatype.subGroup == 1 || (datatype.subGroup >= 10 && datatype.subGroup <= 13) || datatype.subGroup == 600) && !datatype.index)
return valueToBusValueUnsigned16(value, payload, payload_length, datatype);
Expand Down Expand Up @@ -300,61 +275,6 @@ namespace Knx
return false;
}

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

switch (datatype.subGroup)
{
case 1:
value = (uint8_t)round(unsigned8FromPayload(payload, 0) * 100.0 / 255.0);
return true;

case 3:
value = (uint8_t)round(unsigned8FromPayload(payload, 0) * 360.0 / 255.0);
return true;

case 6:
{
uint8_t numValue = unsigned8FromPayload(payload, 0);

if (numValue == 0xFF)
return false;

value = numValue;
return true;
}
}

value = unsigned8FromPayload(payload, 0);
return true;
}

int busValueToSigned8(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(1);
value = (uint8_t)(unsigned8FromPayload(payload, 0));
return true;
}

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

if (datatype.index < 5)
{
value = bitFromPayload(payload, datatype.index);
return true;
}
else if (datatype.index == 5)
{
value = (uint8_t)(unsigned8FromPayload(payload, 0) & 0x07);
return true;
}

return false;
}

int busValueToUnsigned16(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(2);
Expand Down Expand Up @@ -907,78 +827,6 @@ namespace Knx

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

int valueToBusValueUnsigned8(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
if ((int64_t)value < INT64_C(0))
return false;

switch (datatype.subGroup)
{
case 1:
{
if ((double)value > 100.0)
return false;

unsigned8ToPayload(payload, 0, round((double)value * 255.0 / 100.0), 0xFF);
break;
}

case 3:
{
if ((double)value > 360.0)
return false;

unsigned8ToPayload(payload, 0, round((double)value * 255.0 / 360.0), 0xFF);
break;
}

case 6:
{
if ((int64_t)value > INT64_C(254))
return false;

unsigned8ToPayload(payload, 0, (uint64_t)value, 0xFF);
break;
}

default:
{
if ((int64_t)value > INT64_C(255))
return false;

unsigned8ToPayload(payload, 0, (uint64_t)value, 0xFF);
}
}

return true;
}

int valueToBusValueSigned8(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
if ((int64_t)value < INT64_C(-128) || (int64_t)value > INT64_C(127))
return false;

signed8ToPayload(payload, 0, (uint64_t)value, 0xFF);
return true;
}

int valueToBusValueStatusAndMode(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
if (datatype.index < 5)
bitToPayload(payload, datatype.index, value);
else if (datatype.index == 5)
{
if ((int64_t)value < INT64_C(0) || (int64_t)value > INT64_C(7))
return false;

unsigned8ToPayload(payload, 0, (uint64_t)value, 0x07);
}
else
return false;

return true;
}

int valueToBusValueUnsigned16(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
if ((int64_t)value < INT64_C(0) || (int64_t)value > INT64_C(65535))
Expand Down
Loading

0 comments on commit 2e77172

Please sign in to comment.