Skip to content

Commit

Permalink
reimplement dpt 11-13
Browse files Browse the repository at this point in the history
  • Loading branch information
thelsing committed Sep 16, 2024
1 parent 02b0736 commit e4431d6
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 89 deletions.
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ set(SOURCES
./knx/group_object/dpt/dpt9.h
./knx/group_object/dpt/dpt10.cpp
./knx/group_object/dpt/dpt10.h
./knx/group_object/dpt/dpt11.cpp
./knx/group_object/dpt/dpt11.h
./knx/group_object/dpt/dpt12.cpp
./knx/group_object/dpt/dpt12.h
./knx/group_object/dpt/dpt13.cpp
./knx/group_object/dpt/dpt13.h
./knx/group_object/dpt/dptconvert.cpp
./knx/group_object/dpt/dptconvert.h
./knx/group_object/group_object.cpp
Expand Down
11 changes: 0 additions & 11 deletions src/knx/group_object/dpt/dpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@
#include "../group_object.h"
namespace Knx
{
#define DPT_Date Dpt(11, 1)
#define DPT_Value_4_Ucount Dpt(12, 1)
#define DPT_Value_4_Count Dpt(13, 1)
#define DPT_FlowRate_m3 Dpt(13, 2) / h
#define DPT_ActiveEnergy Dpt(13, 10)
#define DPT_ApparantEnergy Dpt(13, 11)
#define DPT_ReactiveEnergy Dpt(13, 12)
#define DPT_ActiveEnergy_kWh Dpt(13, 13)
#define DPT_ApparantEnergy_kVAh Dpt(13, 14)
#define DPT_ReactiveEnergy_kVARh Dpt(13, 15)
#define DPT_LongDeltaTimeSec Dpt(13, 100)
#define DPT_Value_Acceleration Dpt(14, 0)
#define DPT_Value_Acceleration_Angular Dpt(14, 1)
#define DPT_Value_Activation_Energy Dpt(14, 2)
Expand Down
73 changes: 73 additions & 0 deletions src/knx/group_object/dpt/dpt11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "dpt10.h"

#include "dptconvert.h"
#include "dpt11.h"

Knx::Go_SizeCode Knx::DPT_Date::size() const
{
return Go_3_Octets;
}

void Knx::DPT_Date::encode(uint8_t* data) const
{
unsigned8ToPayload(data, 0, _day, 0x1F);
unsigned8ToPayload(data, 1, _month, 0x0F);
unsigned8ToPayload(data, 2, _year % 100, 0x7F);
}

bool Knx::DPT_Date::decode(uint8_t* data)
{
_year = unsigned8FromPayload(data, 2) & 0x7F;
_month = unsigned8FromPayload(data, 1) & 0x0F;
_day = unsigned8FromPayload(data, 0) & 0x1F;

if (_year > 99 || _month < 1 || _month > 12 || _day < 1)
{
_year = 0;
_month = 0;
_day = 0;
return false;
}

_year += _year >= 90 ? 1900 : 2000;
return true;
}

uint8_t Knx::DPT_Date::day() const
{
return _day;
}

void Knx::DPT_Date::day(uint8_t value)
{
if (value < 1 || value > 31)
return;

_day = value;
}

uint8_t Knx::DPT_Date::month() const
{
return _month;
}

void Knx::DPT_Date::month(uint8_t value)
{
if (value < 1 || value > 12)
return;

_month = value;
}

uint16_t Knx::DPT_Date::year() const
{
return _year;
}

void Knx::DPT_Date::year(uint16_t value)
{
if (value < 1990 || value > 2089)
return;

_year = value;
}
28 changes: 28 additions & 0 deletions src/knx/group_object/dpt/dpt11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include "dpt.h"
namespace Knx
{
class DPT_Date: public Dpt
{
public:
Go_SizeCode size() const override;

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

uint8_t day() const;
void day(uint8_t value);

uint8_t month() const;
void month(uint8_t value);

uint16_t year() const;
void year(uint16_t value);
private:
uint8_t _day;
uint8_t _month;
uint16_t _year;
};


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

#include "dptconvert.h"

Knx::Go_SizeCode Knx::Dpt12::size() const
{
return Go_4_Octets;
}

void Knx::Dpt12::encode(uint8_t* data) const
{
unsigned32ToPayload(data, 0, _value, 0xFFFFFFFF);
}

bool Knx::Dpt12::decode(uint8_t* data)
{
_value = unsigned32FromPayload(data, 0);
return true;
}
20 changes: 20 additions & 0 deletions src/knx/group_object/dpt/dpt12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "dpt.h"
namespace Knx
{
class Dpt12: public ValueDpt<uint32_t>
{
public:
Dpt12() {};
Dpt12(uint32_t value) : ValueDpt(value) {}
Go_SizeCode size() const override;

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

typedef Dpt12 DPT_Value_4_Ucount;
typedef Dpt12 DPT_LongTimePeriod_Sec;
typedef Dpt12 DPT_LongTimePeriod_Min;
typedef Dpt12 DPT_LongTimePeriod_Hrs;
}
19 changes: 19 additions & 0 deletions src/knx/group_object/dpt/dpt13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "dpt13.h"

#include "dptconvert.h"

Knx::Go_SizeCode Knx::Dpt13::size() const
{
return Go_4_Octets;
}

void Knx::Dpt13::encode(uint8_t* data) const
{
signed32ToPayload(data, 0, _value, 0xFFFFFFFF);
}

bool Knx::Dpt13::decode(uint8_t* data)
{
_value = signed32FromPayload(data, 0);
return true;
}
26 changes: 26 additions & 0 deletions src/knx/group_object/dpt/dpt13.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "dpt.h"
namespace Knx
{
class Dpt13: public ValueDpt<int32_t>
{
public:
Dpt13() {};
Dpt13(int32_t value) : ValueDpt(value) {}
Go_SizeCode size() const override;

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

typedef Dpt13 DPT_Value_4_Count;
typedef Dpt13 DPT_FlowRate_m3_h;
typedef Dpt13 DPT_ActiveEnergy;
typedef Dpt13 DPT_ApparantEnergy;
typedef Dpt13 DPT_ReactiveEnergy;
typedef Dpt13 DPT_ActiveEnergy_kWh;
typedef Dpt13 DPT_ApparantEnergy_kVAh;
typedef Dpt13 DPT_ReactiveEnergy_kVARh;
typedef Dpt13 DPT_ActiveEnergy_MWh;
typedef Dpt13 DPT_LongDeltaTimeSec;
}
2 changes: 1 addition & 1 deletion src/knx/group_object/dpt/dpt9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Knx::Go_SizeCode Knx::Dpt9::size() const
{
return Go_1_Bit;
return Go_2_Octets;
}

void Knx::Dpt9::encode(uint8_t* data) 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 @@ -60,5 +60,5 @@ namespace Knx
typedef Dpt9 DPT_Rain_Amount;
typedef Dpt9 DPT_Value_Wsp_kmh;
typedef Dpt9GeZero DPT_Value_Absolute_Humidity;
typedef Dpt9GeZero DPT_Concentration_µgm3;
typedef Dpt9GeZero DPT_Concentration_ugm3;
}
73 changes: 0 additions & 73 deletions src/knx/group_object/dpt/dptconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@ namespace Knx
{
if (payload_length > 0)
{
// DPT 11.* - Date
if (datatype.mainGroup == 11 && datatype.subGroup == 1 && !datatype.index)
return busValueToDate(payload, payload_length, datatype, value);

// DPT 12.* - Unsigned 32 Bit Integer
if (datatype.mainGroup == 12 && datatype.subGroup == 1 && !datatype.index)
return busValueToUnsigned32(payload, payload_length, datatype, value);

// DPT 13.001/13.002/13.010-13.015 - Signed 32 Bit Integer
if (datatype.mainGroup == 13 && (datatype.subGroup == 1 || datatype.subGroup == 2 || (datatype.subGroup >= 10 && datatype.subGroup <= 15)) && !datatype.index)
return busValueToSigned32(payload, payload_length, datatype, value);

// DPT 13.100 - Long Time Period
if (datatype.mainGroup == 13 && datatype.subGroup == 100 && !datatype.index)
return busValueToLongTimePeriod(payload, payload_length, datatype, value);

// DPT 14.* - 32 Bit Float
if (datatype.mainGroup == 14 && datatype.subGroup <= 79 && !datatype.index)
return busValueToFloat32(payload, payload_length, datatype, value);
Expand Down Expand Up @@ -128,22 +112,6 @@ namespace Knx

int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
// DPT 11.* - Date
if (datatype.mainGroup == 11 && datatype.subGroup == 1 && !datatype.index)
return valueToBusValueDate(value, payload, payload_length, datatype);

// DPT 12.* - Unsigned 32 Bit Integer
if (datatype.mainGroup == 12 && datatype.subGroup == 1 && !datatype.index)
return valueToBusValueUnsigned32(value, payload, payload_length, datatype);

// DPT 13.001/13.002/13.010-13.015 - Signed 32 Bit Integer
if (datatype.mainGroup == 13 && (datatype.subGroup == 1 || datatype.subGroup == 2 || (datatype.subGroup >= 10 && datatype.subGroup <= 15)) && !datatype.index)
return valueToBusValueSigned32(value, payload, payload_length, datatype);

// DPT 13.100 - Long Time Period
if (datatype.mainGroup == 13 && datatype.subGroup == 100 && !datatype.index)
return valueToBusValueLongTimePeriod(value, payload, payload_length, datatype);

// DPT 14.* - 32 Bit Float
if (datatype.mainGroup == 14 && datatype.subGroup <= 79 && !datatype.index)
return valueToBusValueFloat32(value, payload, payload_length, datatype);
Expand Down Expand Up @@ -235,25 +203,6 @@ namespace Knx
return false;
}

int busValueToDate(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(3);
unsigned short year = unsigned8FromPayload(payload, 2) & 0x7F;
unsigned char month = unsigned8FromPayload(payload, 1) & 0x0F;
unsigned char day = unsigned8FromPayload(payload, 0) & 0x1F;

if (year > 99 || month < 1 || month > 12 || day < 1)
return false;

struct tm tmp = {0};
year += year >= 90 ? 1900 : 2000;
tmp.tm_mday = day;
tmp.tm_year = year;
tmp.tm_mon = month;
value = tmp;
return true;
}

int busValueToUnsigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(4);
Expand Down Expand Up @@ -716,19 +665,6 @@ namespace Knx

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

int valueToBusValueDate(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
struct tm tmp = value;

if (tmp.tm_year < 1990 || tmp.tm_year > 2089)
return false;

unsigned8ToPayload(payload, 0, tmp.tm_mday, 0x1F);
unsigned8ToPayload(payload, 1, tmp.tm_mon, 0x0F);
unsigned8ToPayload(payload, 2, tmp.tm_year % 100, 0x7F);
return true;
}

int valueToBusValueUnsigned32(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(4294967295))
Expand All @@ -738,15 +674,6 @@ namespace Knx
return true;
}

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

signed32ToPayload(payload, 0, (uint64_t)value, 0xFFFFFFFF);
return true;
}

int valueToBusValueLongTimePeriod(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype)
{
if ((int64_t)value < INT64_C(-2147483648) || (int64_t)value > INT64_C(2147483647))
Expand Down
2 changes: 0 additions & 2 deletions src/knx/group_object/dpt/dptconvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace Knx
int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);

//KNX to internal
int busValueToDate(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToUnsigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToSigned32(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
int busValueToLongTimePeriod(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);
Expand All @@ -73,7 +72,6 @@ namespace Knx
int busValueToActiveEnergy(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value);

//Internal to KNX
int valueToBusValueDate(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueUnsigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueSigned32(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
int valueToBusValueLongTimePeriod(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype);
Expand Down
6 changes: 5 additions & 1 deletion src/knx/group_object/dpt/dpts.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
#include "dpt6.h"
#include "dpt7.h"
#include "dpt8.h"
#include "dpt9.h"
#include "dpt9.h"
#include "dpt10.h"
#include "dpt11.h"
#include "dpt12.h"
#include "dpt13.h"

0 comments on commit e4431d6

Please sign in to comment.