diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b90e4a7..73d89ed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -68,6 +68,8 @@ set(SOURCES ./knx/group_object/dpt/dpt14.h ./knx/group_object/dpt/dpt15.cpp ./knx/group_object/dpt/dpt15.h +./knx/group_object/dpt/dpt16.cpp +./knx/group_object/dpt/dpt16.h ./knx/group_object/dpt/dptconvert.cpp ./knx/group_object/dpt/dptconvert.h ./knx/group_object/group_object.cpp diff --git a/src/knx/group_object/dpt/dpt.h b/src/knx/group_object/dpt/dpt.h index b804dc5..e59e2b2 100644 --- a/src/knx/group_object/dpt/dpt.h +++ b/src/knx/group_object/dpt/dpt.h @@ -3,8 +3,6 @@ #include "../group_object.h" namespace Knx { -#define DPT_String_ASCII Dpt(16, 0) -#define DPT_String_8859_1 Dpt(16, 1) #define DPT_SceneNumber Dpt(17, 1) #define DPT_SceneControl Dpt(18, 1) #define DPT_DateTime Dpt(19, 1) diff --git a/src/knx/group_object/dpt/dpt16.cpp b/src/knx/group_object/dpt/dpt16.cpp new file mode 100644 index 0000000..58e6960 --- /dev/null +++ b/src/knx/group_object/dpt/dpt16.cpp @@ -0,0 +1,78 @@ +#include "dpt16.h" + +#include "dptconvert.h" +#include + +Knx::Dpt16::Dpt16() +{ + memset(_value, 0, 15); +} + +Knx::Dpt16::Dpt16(const char* value) : Dpt16() +{ + this->value(value); +} + +Knx::Go_SizeCode Knx::Dpt16::size() const +{ + return Go_14_Octets; +} + +void Knx::Dpt16::encode(uint8_t* data) const +{ + uint8_t val = _value[0]; + + for (int n = 0; n < 14; n++) + { + if (val) + val = _value[n]; //string terminator 0x00 will stop further assignments and init the remainig payload with zero + + unsigned8ToPayload(data, n, val, 0xff); + } +} + +bool Knx::Dpt16::decode(uint8_t* data) +{ + + _value[14] = '\0'; + + for (int n = 0; n < 14; ++n) + { + _value[n] = signed8FromPayload(data, n); + } + + return true; +} + +const char* Knx::Dpt16::value() const +{ + return _value; +} + +void Knx::Dpt16::value(const char* value) +{ + strncpy(_value, value, 14); + _value[14] = 0; +} + +Knx::DPT_String_ASCII::DPT_String_ASCII() : Dpt16() {} + +Knx::DPT_String_ASCII::DPT_String_ASCII(const char* value) : Dpt16(value) {} + +bool Knx::DPT_String_ASCII::decode(uint8_t* data) +{ + _value[14] = '\0'; + + for (int n = 0; n < 14; ++n) + { + _value[n] = signed8FromPayload(data, n); + + if ((_value[n] & 0x80)) + { + _value[0] = 0; + return false; + } + } + + return true; +} diff --git a/src/knx/group_object/dpt/dpt16.h b/src/knx/group_object/dpt/dpt16.h new file mode 100644 index 0000000..01b1b4a --- /dev/null +++ b/src/knx/group_object/dpt/dpt16.h @@ -0,0 +1,32 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt16: public Dpt + { + enum ReadDirectionValue { LeftToRight = 0, RightToLeft = 1}; + public: + Dpt16(); + Dpt16(const char* value); + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + + const char* value() const; + void value(const char* value); + protected: + // one character more than the dpt to store \0 + char _value[15]; + }; + + typedef Dpt16 DPT_String_8859_1; + + class DPT_String_ASCII: public Dpt16 + { + public: + DPT_String_ASCII(); + DPT_String_ASCII(const char* value); + bool decode(uint8_t* data) override; + }; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dptconvert.cpp b/src/knx/group_object/dpt/dptconvert.cpp index cacf6a2..4d53756 100644 --- a/src/knx/group_object/dpt/dptconvert.cpp +++ b/src/knx/group_object/dpt/dptconvert.cpp @@ -18,10 +18,6 @@ namespace Knx { if (payload_length > 0) { - // DPT 16.* - String - if (datatype.mainGroup == 16 && datatype.subGroup <= 1 && !datatype.index) - return busValueToString(payload, payload_length, datatype, value); - // DPT 17.* - Scene Number if (datatype.mainGroup == 17 && datatype.subGroup == 1 && !datatype.index) return busValueToScene(payload, payload_length, datatype, value); @@ -104,10 +100,6 @@ namespace Knx int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { - // DPT 16.* - String - if (datatype.mainGroup == 16 && datatype.subGroup <= 1 && !datatype.index) - return valueToBusValueString(value, payload, payload_length, datatype); - // DPT 17.* - Scene Number if (datatype.mainGroup == 17 && datatype.subGroup == 1 && !datatype.index) return valueToBusValueScene(value, payload, payload_length, datatype); @@ -598,22 +590,6 @@ namespace Knx return true; } - int valueToBusValueString(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - const char* strValue = value; - uint8_t val = strValue[0]; - - for (int n = 0; n < 14; n++) - { - if (val) - val = strValue[n]; //string terminator 0x00 will stop further assignments and init the remainig payload with zero - - unsigned8ToPayload(payload, n, val, 0xff); - } - - return true; - } - int valueToBusValueScene(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(63)) diff --git a/src/knx/group_object/dpt/dptconvert.h b/src/knx/group_object/dpt/dptconvert.h index 3d7b31b..6861ad1 100644 --- a/src/knx/group_object/dpt/dptconvert.h +++ b/src/knx/group_object/dpt/dptconvert.h @@ -49,7 +49,6 @@ namespace Knx //KNX to internal 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 busValueToString(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToScene(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToSceneControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); int busValueToSceneInfo(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value); @@ -71,7 +70,6 @@ namespace Knx //Internal to KNX 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 valueToBusValueString(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueScene(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueSceneControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); int valueToBusValueSceneInfo(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype); diff --git a/src/knx/group_object/dpt/dpts.h b/src/knx/group_object/dpt/dpts.h index 3f783d9..3b69f8a 100644 --- a/src/knx/group_object/dpt/dpts.h +++ b/src/knx/group_object/dpt/dpts.h @@ -14,4 +14,5 @@ #include "dpt12.h" #include "dpt13.h" #include "dpt14.h" -#include "dpt15.h" \ No newline at end of file +#include "dpt15.h" +#include "dpt16.h" \ No newline at end of file