From 00b9c98786cf73be1bd9d74c92f90acd0ca87f16 Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Fri, 13 Sep 2024 22:35:52 +0200 Subject: [PATCH] refactor dpts --- examples/knx-cc1310/CMakeLists.txt | 1 - src/CMakeLists.txt | 7 +- src/knx/group_object/dpt/dpt.cpp | 19 --- src/knx/group_object/dpt/dpt.h | 51 +++--- src/knx/group_object/dpt/dpt1.cpp | 82 +--------- src/knx/group_object/dpt/dpt1.h | 206 ++++++++++++++++++------ src/knx/group_object/dpt/dpt2.cpp | 64 -------- src/knx/group_object/dpt/dpt2.h | 97 ++++++++--- src/knx/group_object/dpt/dpt3.h | 68 ++++++++ src/knx/group_object/dpt/dpt4.cpp | 58 +++++++ src/knx/group_object/dpt/dpt4.h | 30 ++++ src/knx/group_object/dpt/dpt5.cpp | 105 ++++++++++++ src/knx/group_object/dpt/dpt5.h | 68 ++++++++ src/knx/group_object/dpt/dpt9.cpp | 9 +- src/knx/group_object/dpt/dpt9.h | 26 ++- src/knx/group_object/dpt/dptconvert.cpp | 88 ---------- src/knx/group_object/dpt/dpts.h | 3 + src/knx/group_object/group_object.h | 3 +- 18 files changed, 614 insertions(+), 371 deletions(-) delete mode 100644 src/knx/group_object/dpt/dpt.cpp delete mode 100644 src/knx/group_object/dpt/dpt2.cpp create mode 100644 src/knx/group_object/dpt/dpt3.h create mode 100644 src/knx/group_object/dpt/dpt4.cpp create mode 100644 src/knx/group_object/dpt/dpt4.h create mode 100644 src/knx/group_object/dpt/dpt5.cpp create mode 100644 src/knx/group_object/dpt/dpt5.h diff --git a/examples/knx-cc1310/CMakeLists.txt b/examples/knx-cc1310/CMakeLists.txt index b5623e7c..42dbfca1 100644 --- a/examples/knx-cc1310/CMakeLists.txt +++ b/examples/knx-cc1310/CMakeLists.txt @@ -71,7 +71,6 @@ set(${PROJECT_NAME}_SOURCES ../../src/knx/interface_object/data_property.h ../../src/knx/interface_object/device_object.cpp ../../src/knx/interface_object/device_object.h - ../../src/knx/group_object/dpt/dpt.cpp ../../src/knx/group_object/dpt/dpt.h ../../src/knx/group_object/dpt/dptconvert.cpp ../../src/knx/group_object/dpt/dptconvert.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5ab9bbaa..99b1cc44 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,12 +39,15 @@ set(SOURCES ./knx/data_secure/security_interface_object.cpp ./knx/data_secure/security_interface_object.h ./knx/group_object/dpt/dpts.h -./knx/group_object/dpt/dpt.cpp ./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/dpt3.h +./knx/group_object/dpt/dpt4.cpp +./knx/group_object/dpt/dpt4.h +./knx/group_object/dpt/dpt5.cpp +./knx/group_object/dpt/dpt5.h ./knx/group_object/dpt/dpt9.cpp ./knx/group_object/dpt/dpt9.h ./knx/group_object/dpt/dptconvert.cpp diff --git a/src/knx/group_object/dpt/dpt.cpp b/src/knx/group_object/dpt/dpt.cpp deleted file mode 100644 index b7b57113..00000000 --- a/src/knx/group_object/dpt/dpt.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "dpt.h" - -#include "../../util/logger.h" -#include "../../bits.h" - -#define LOGGER Logger::logger("Dpt") - -namespace Knx -{ - Dpt::Dpt() - {} - - Dpt::Dpt(short mainGroup, short subGroup, short index /* = 0 */) - : mainGroup(mainGroup), subGroup(subGroup), index(index) - { - if (subGroup == 0) - LOGGER.warning("You used and invalid Dpt *.0"); - } -} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt.h b/src/knx/group_object/dpt/dpt.h index ba1c9e6a..c87fb496 100644 --- a/src/knx/group_object/dpt/dpt.h +++ b/src/knx/group_object/dpt/dpt.h @@ -3,16 +3,10 @@ #include "../group_object.h" namespace Knx { -#define DPT_Control_Dimming Dpt(3, 7) -#define DPT_Control_Blinds Dpt(3, 8) -#define DPT_Char_ASCII Dpt(4, 1) -#define DPT_Char_8859_1 Dpt(4, 2) -#define DPT_Scaling Dpt(5, 1) -#define DPT_Angle Dpt(5, 3) -#define DPT_Percent_U8 Dpt(5, 4) -#define DPT_DecimalFactor Dpt(5, 5) -#define DPT_Tariff Dpt(5, 6) -#define DPT_Value_1_Ucount Dpt(5, 10) + + + + #define DPT_Percent_V8 Dpt(6, 1) #define DPT_Value_1_Count Dpt(6, 10) #define DPT_Status_Mode3 Dpt(6, 20) @@ -36,25 +30,6 @@ namespace Knx #define DPT_DeltaTimeHrs Dpt(8, 7) #define DPT_Percent_V16 Dpt(8, 10) #define DPT_Rotation_Angle Dpt(8, 11) -#define DPT_Value_Temp Dpt(9, 1) -#define DPT_Value_Tempd Dpt(9, 2) -#define DPT_Value_Tempa Dpt(9, 3) -#define DPT_Value_Lux Dpt(9, 4) -#define DPT_Value_Wsp Dpt(9, 5) -#define DPT_Value_Pres Dpt(9, 6) -#define DPT_Value_Humidity Dpt(9, 7) -#define DPT_Value_AirQuality Dpt(9, 8) -#define DPT_Value_Time1 Dpt(9, 10) -#define DPT_Value_Time2 Dpt(9, 11) -#define DPT_Value_Volt Dpt(9, 20) -#define DPT_Value_Curr Dpt(9, 21) -#define DPT_PowerDensity Dpt(9, 22) -#define DPT_KelvinPerPercent Dpt(9, 23) -#define DPT_Power Dpt(9, 24) -#define DPT_Value_Volume_Flow Dpt(9, 25) -#define DPT_Rain_Amount Dpt(9, 26) -#define DPT_Value_Temp_F Dpt(9, 27) -#define DPT_Value_Wsp_kmh Dpt(9, 28) #define DPT_TimeOfDay Dpt(10, 1, 1) #define DPT_Date Dpt(11, 1) #define DPT_Value_4_Ucount Dpt(12, 1) @@ -328,19 +303,31 @@ namespace Knx #define DPT_StatusSAB Dpt(241, 800) #define DPT_Colour_RGBW Dpt(251, 600) + /** + * This class represents a value to sent to or receive from the bus. The stored data is always a valid representation of the Dpt. + */ class Dpt { public: - Dpt(); - Dpt(short mainGroup, short subGroup, short index = 0); virtual ~Dpt() {} unsigned short mainGroup; unsigned short subGroup; unsigned short index; + /** + * Size of the Dpt. It can only assigned to or received from group objects with a matching size code. + */ virtual Go_SizeCode size() const = 0; + /** + * Encode the data to a byte array. The size of the array is implict decided by the size of the Dpt. + */ virtual void encode(uint8_t* data) const = 0; - virtual void decode(uint8_t* data) = 0; + + /** + * Decodes the data from a byte array. @return true if the data can be decoded without error and false otherwise. + * This method will also return false, if the date could fit the datatype but is invalid for this Dpt. + */ + virtual bool decode(uint8_t* data) = 0; }; } \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt1.cpp b/src/knx/group_object/dpt/dpt1.cpp index 63d8143d..24d21b53 100644 --- a/src/knx/group_object/dpt/dpt1.cpp +++ b/src/knx/group_object/dpt/dpt1.cpp @@ -2,11 +2,11 @@ #include "dptconvert.h" -Knx::Dpt1::Dpt1(unsigned short subgroup /*= 0*/) : Dpt(1, subgroup) {} +Knx::Dpt1::Dpt1() {} -Knx::Dpt1::Dpt1(bool value) : Dpt1() +Knx::Dpt1::Dpt1(bool value) { - _value = value; + this->value(value); } Knx::Go_SizeCode Knx::Dpt1::size() const @@ -19,9 +19,10 @@ void Knx::Dpt1::encode(uint8_t* data) const bitToPayload(data, 7, _value); } -void Knx::Dpt1::decode(uint8_t* data) +bool Knx::Dpt1::decode(uint8_t* data) { _value = bitFromPayload(data, 7); + return true; } void Knx::Dpt1::value(bool value) @@ -39,81 +40,8 @@ Knx::Dpt1::operator bool() const return _value; } - Knx::Dpt1& Knx::Dpt1::operator=(const bool value) { _value = value; return *this; } - -Knx::DPT_Switch::DPT_Switch() : Dpt1() -{ - subGroup = 1; -} - -Knx::DPT_Switch::DPT_Switch(SwitchValue value) : Dpt1(value == On) -{ - subGroup = 1; -} - -void Knx::DPT_Switch::value(SwitchValue value) -{ - Dpt1::value(value == On); -} - -Knx::DPT_Switch::SwitchValue Knx::DPT_Switch::value() const -{ - return Dpt1::value() ? On : Off; -} - -Knx::DPT_Switch::operator Knx::DPT_Switch::SwitchValue() const -{ - return value(); -} - -Knx::DPT_Switch& Knx::DPT_Switch::operator=(const Knx::DPT_Switch::SwitchValue value) -{ - Dpt1::value(value == On); - return *this; -} - -Knx::DPT_Bool::DPT_Bool() : Dpt1() -{ - subGroup = 2; -} - -Knx::DPT_Bool::DPT_Bool(bool value) : DPT_Bool() -{ - Dpt1::value(value); -} - -Knx::DPT_Enable::DPT_Enable() : Dpt1() -{ - subGroup = 3; -} - -Knx::DPT_Enable::DPT_Enable(EnableValue value) : Dpt1(value == Enable) -{ - subGroup = 3; -} - -void Knx::DPT_Enable::value(EnableValue value) -{ - Dpt1::value(value == Enable); -} - -Knx::DPT_Enable::EnableValue Knx::DPT_Enable::value() const -{ - return Dpt1::value() ? Enable : Disable; -} - -Knx::DPT_Enable::operator Knx::DPT_Enable::EnableValue() const -{ - return value(); -} - -Knx::DPT_Enable& Knx::DPT_Enable::operator=(const Knx::DPT_Enable::EnableValue value) -{ - Dpt1::value(value == Enable); - return *this; -} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt1.h b/src/knx/group_object/dpt/dpt1.h index f53e8525..817b7f17 100644 --- a/src/knx/group_object/dpt/dpt1.h +++ b/src/knx/group_object/dpt/dpt1.h @@ -5,12 +5,12 @@ namespace Knx class Dpt1: public Dpt { public: - Dpt1(unsigned short subgroup = 0); + Dpt1(); Dpt1(bool value); Go_SizeCode size() const override; - virtual void encode(uint8_t* data) const override; - virtual void decode(uint8_t* data) override; + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; void value(bool value); bool value() const; @@ -20,64 +20,164 @@ namespace Knx bool _value; }; - class DPT_Switch : public Dpt1 + template class DPT1 : public Dpt1 { public: - enum SwitchValue + DPT1() {}; + DPT1(T value) : Dpt1(((int)value) == 1) {} + void value(T value) { - Off, On - }; + Dpt1::value(((int)value) == 1); + } + T value() const + { + return Dpt1::value() ? (T)1 : (T)0; + } + operator T() const + { + return value(); + } + DPT1& operator=(const T value) + { + this->value(value); + return *this; + } + }; - DPT_Switch(); - DPT_Switch(SwitchValue value); - void value(SwitchValue value); - SwitchValue value() const; - operator SwitchValue() const; - DPT_Switch& operator=(const SwitchValue value); + enum SwitchValue + { + Off = 0, On = 1 }; + typedef DPT1 DPT_Switch; - class DPT_Bool : public Dpt1 + typedef Dpt1 DPT_Bool; + + enum EnableValue { - public: - DPT_Bool(); - DPT_Bool(bool value); + Disable = 0, Enable = 1 }; + typedef DPT1 DPT_Enable; - class DPT_Enable : public Dpt1 + enum RampValue { - public: - enum EnableValue - { - Disable, Enable - }; - - DPT_Enable(); - DPT_Enable(EnableValue value); - void value(EnableValue value); - EnableValue value() const; - 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) + NoRamp = 0, Ramp = 1 + }; + typedef DPT1 DPT_Ramp; + + enum AlarmValue + { + NoAlarm = 0, Alarm = 1 + }; + typedef DPT1 DPT_Alarm; + + enum BinaryValue + { + Low = 0, High = 1 + }; + typedef DPT1 DPT_BinaryValue; + + enum StepValue + { + Decrease = 0, Increase = 1 + }; + typedef DPT1 DPT_Step; + + enum UpDownValue + { + Up = 0, Down = 1 + }; + typedef DPT1 DPT_UpDown; + + enum OpenCloseValue + { + OpenNormallyOpen = 0, ClosedNormallyOpen = 1 + }; + typedef DPT1 DPT_OpenClose; + + enum StartValue + { + Stop = 0, Start = 1 + }; + typedef DPT1 DPT_Start; + + enum StateValue + { + Inactive = 0, Active = 1 + }; + typedef DPT1 DPT_State; + + enum InvertValue + { + NotInverted = 0, Inverted = 1 + }; + typedef DPT1 DPT_Invert; + + enum DimSendStyleValue + { + StartStop = 0, Cyclically = 1 + }; + typedef DPT1 DPT_DimSendStyle; + + enum InputSourceValue + { + Fixed = 0, Calculated = 1 + }; + typedef DPT1 DPT_InputSource; + + enum ResetValue + { + NoActionReset = 0, ResetCommand = 1 + }; + typedef DPT1 DPT_Reset; + + enum AckValue + { + NoActionAck = 0, AcknowledgeCommand = 1 + }; + typedef DPT1 DPT_Ack; + + typedef Dpt1 DPT_Trigger; + + enum OccupancyValue + { + NotOccupied = 0, Occupied = 1 + }; + typedef DPT1 DPT_Occupancy; + + enum WindowDoorValue + { + ClosedNormallyClosed = 0, OpenNormallyClosed = 1 + }; + typedef DPT1 DPT_Window_Door; + + enum LogicalFunctionValue + { + OR = 0, AND = 1 + }; + typedef DPT1 DPT_LogicalFunction; + + enum SceneABValue + { + SceneA = 0, SceneB = 1 + }; + typedef DPT1 DPT_Scene_AB; + + enum ShutterBlindsMode + { + /** + * Only move up/down + */ + Shutter = 0, + /** + * move up/down and step/stop + */ + Blinds = 1 + }; + typedef DPT1 DPT_ShutterBlinds_Mode; + + enum DayNightValue + { + Day = 0, Night = 1 + }; + typedef DPT1 DPT_DayNight; } \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt2.cpp b/src/knx/group_object/dpt/dpt2.cpp deleted file mode 100644 index 28554e7d..00000000 --- a/src/knx/group_object/dpt/dpt2.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#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; -} diff --git a/src/knx/group_object/dpt/dpt2.h b/src/knx/group_object/dpt/dpt2.h index 85976b7c..a9a6f28f 100644 --- a/src/knx/group_object/dpt/dpt2.h +++ b/src/knx/group_object/dpt/dpt2.h @@ -1,40 +1,85 @@ #pragma once #include "dpt.h" +#include "dpt1.h" +#include "dptconvert.h" + namespace Knx { - class Dpt2: public Dpt + enum ControlValue + { + NoControl, Control + }; + template class DPT2: public Dpt { public: - enum Dpt2Value + Go_SizeCode size() const override + { + return Go_2_Bit; + } + + void encode(uint8_t* data) const override { - NoControl, Control_Function0, Control_Function1 - }; + if (_control == NoControl) + { + bitToPayload(data, 6, false); + return; + } + + bitToPayload(data, 6, true); + bitToPayload(data, 7, ((int)_value) == 1); + } + + bool decode(uint8_t* data) override + { + bool c = bitFromPayload(data, 6); + + if (!c) + { + _control = NoControl; + return true; + } - Dpt2(unsigned short subgroup = 0); - Dpt2(Dpt2Value value); - Go_SizeCode size() const override; + bool v = bitFromPayload(data, 7); - virtual void encode(uint8_t* data) const override; - virtual void decode(uint8_t* data) override; + _value = v ? (T)1 : (T)0; + return true; + } + + void control(ControlValue control) + { + _control = control; + } + + ControlValue control() const + { + return _control; + } + + void value(T value) + { + _value = value; + } + + T value() const + { + return _value; + } - void value(Dpt2Value value); - Dpt2Value value() const; - operator Dpt2Value() const; - Dpt2& operator=(const Dpt2Value value); private: - Dpt2Value _value; + ControlValue _control; + T _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) + typedef DPT2 DPT_Switch_Control; + typedef DPT2 DPT_Bool_Control; + typedef DPT2 DPT_Enable_Control; + typedef DPT2 DPT_Ramp_Control; + typedef DPT2 DPT_Alarm_Control; + typedef DPT2 DPT_BinaryValue_Control; + typedef DPT2 DPT_Step_Control; + typedef DPT2 DPT_Direction1_Control; + typedef DPT2 DPT_Direction2_Control; + typedef DPT2 DPT_Start_Control; + typedef DPT2 DPT_State_Control; + typedef DPT2 DPT_Invert_Control; } \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt3.h b/src/knx/group_object/dpt/dpt3.h new file mode 100644 index 00000000..ac13a946 --- /dev/null +++ b/src/knx/group_object/dpt/dpt3.h @@ -0,0 +1,68 @@ +#pragma once +#include "dpt.h" +#include "dpt1.h" +#include "dptconvert.h" +namespace Knx +{ + template class DPT3: public Dpt + { + public: + Go_SizeCode size() const override + { + return Go_4_Bit; + } + + void encode(uint8_t* data) const override + { + bitToPayload(data, 4, ((int)_direction) == 1); + unsigned8ToPayload(data, 0, _stepCode, 0x07); + } + + bool decode(uint8_t* data) override + { + bool c = bitFromPayload(data, 4); + _direction = c ? (T)1 : (T)0; + _stepCode = unsigned8FromPayload(data, 0) & 0x07; + return true; + } + + T direction() const + { + return _direction; + } + + void direction(T value) + { + _direction = value; + } + + float stepPercent() const + { + if (_stepCode == 0) + return 0.0f; + + return 100.0f / (0x1 << (_stepCode - 1)); + } + + uint8_t stepCode() const + { + return _stepCode; + } + + void stepCode(uint8_t value) + { + _stepCode = value & 0x7; + } + + bool stop() const + { + return _stepCode == 0; + } + private: + T _direction; + uint8_t _stepCode; + }; + + typedef DPT3 DPT_Control_Dimming; + typedef DPT3 DPT_Control_Blinds; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt4.cpp b/src/knx/group_object/dpt/dpt4.cpp new file mode 100644 index 00000000..c7bc861f --- /dev/null +++ b/src/knx/group_object/dpt/dpt4.cpp @@ -0,0 +1,58 @@ +#include "dpt4.h" + +#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); +} + +bool Knx::Dpt4::decode(uint8_t* data) +{ + _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); + return true; +} + +void Knx::DPT_Char_ASCII::value(char value) +{ + Dpt4::value(value & 0x7F); +} diff --git a/src/knx/group_object/dpt/dpt4.h b/src/knx/group_object/dpt/dpt4.h new file mode 100644 index 00000000..739fe369 --- /dev/null +++ b/src/knx/group_object/dpt/dpt4.h @@ -0,0 +1,30 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt4: public Dpt + { + 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 + { + bool decode(uint8_t* data) override; + void value(char value) override; + }; + + typedef Dpt4 DPT_Char_8859_1; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt5.cpp b/src/knx/group_object/dpt/dpt5.cpp new file mode 100644 index 00000000..d9edbe89 --- /dev/null +++ b/src/knx/group_object/dpt/dpt5.cpp @@ -0,0 +1,105 @@ +#include "dpt5.h" + +#include "dptconvert.h" + +Knx::Dpt5::Dpt5() {} + +Knx::Dpt5::Dpt5(float value) +{ + this->value(value); +} + +Knx::Go_SizeCode Knx::Dpt5::size() const +{ + return Go_1_Octet; +} + +void Knx::Dpt5::encode(uint8_t* data) const +{ + unsigned8ToPayload(data, 0, _rawValue, 0xFF); +} + +bool Knx::Dpt5::decode(uint8_t* data) +{ + _rawValue = signed8FromPayload(data, 0); + return true; +} + +void Knx::Dpt5::value(float value) +{ + _rawValue = (uint8_t)(value / scale() * 0xFF); +} + +float Knx::Dpt5::value() const +{ + return _rawValue / 0xFF * scale(); +} + +Knx::Dpt5::operator float() const +{ + return value(); +} + +Knx::Dpt5& Knx::Dpt5::operator=(const float value) +{ + this->value(value); + return *this; +} + +Knx::DPT_Value_1_Ucount::DPT_Value_1_Ucount() {} + +Knx::DPT_Value_1_Ucount::DPT_Value_1_Ucount(uint8_t value) +{ + this->value(value); +} + +void Knx::DPT_Value_1_Ucount::value(uint8_t value) +{ + Dpt5::_rawValue = value; +} + +uint8_t Knx::DPT_Value_1_Ucount::value() const +{ + return Dpt5::_rawValue; +} + +Knx::DPT_Value_1_Ucount::operator uint8_t() const +{ + return value(); +} + +Knx::DPT_Value_1_Ucount& Knx::DPT_Value_1_Ucount::operator=(const uint8_t value) +{ + this->value(value); + return *this; +} + +Knx::DPT_Tariff::DPT_Tariff() {} + +Knx::DPT_Tariff::DPT_Tariff(uint8_t value) +{ + this->value(value); +} + +bool Knx::DPT_Tariff::tariffAvailable() +{ + return _rawValue == 0; +} + +void Knx::DPT_Tariff::value(uint8_t value) +{ + if ( value == 0xFF) + return; +} + + +bool Knx::DPT_Tariff::decode(uint8_t* data) +{ + uint8_t value = signed8FromPayload(data, 0); + + if (value == 0xFF) + return false; + + _rawValue = value; + return true; +} diff --git a/src/knx/group_object/dpt/dpt5.h b/src/knx/group_object/dpt/dpt5.h new file mode 100644 index 00000000..3dafb67d --- /dev/null +++ b/src/knx/group_object/dpt/dpt5.h @@ -0,0 +1,68 @@ +#pragma once +#include "dpt.h" +namespace Knx +{ + class Dpt5: public Dpt + { + public: + Dpt5(); + Dpt5(float value); + Go_SizeCode size() const override; + + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; + + void value(float value); + float value() const; + operator float() const; + Dpt5& operator=(const float value); + protected: + virtual float scale() const + { + return 1.0; + }; + uint8_t _rawValue; + }; + + class DPT_Scaling : public Dpt5 + { + protected: + float scale() const override + { + return 100.0f; + } + }; + + class DPT_Angle : public Dpt5 + { + protected: + float scale() const override + { + return 360.0f; + } + }; + + class DPT_Value_1_Ucount : protected Dpt5 + { + public: + DPT_Value_1_Ucount(); + DPT_Value_1_Ucount(uint8_t value); + virtual void value(uint8_t value); + uint8_t value() const; + operator uint8_t() const; + DPT_Value_1_Ucount& operator=(const uint8_t value); + }; + + class DPT_Tariff: public DPT_Value_1_Ucount + { + public: + DPT_Tariff(); + DPT_Tariff(uint8_t value); + bool tariffAvailable(); + void value(uint8_t value) override; + bool decode(uint8_t* data) override; + }; + + typedef Dpt5 DPT_Percent_U8; + typedef Dpt5 DPT_DecimalFactor; +} \ No newline at end of file diff --git a/src/knx/group_object/dpt/dpt9.cpp b/src/knx/group_object/dpt/dpt9.cpp index 60602928..e2d6b727 100644 --- a/src/knx/group_object/dpt/dpt9.cpp +++ b/src/knx/group_object/dpt/dpt9.cpp @@ -2,9 +2,9 @@ #include "dptconvert.h" -Knx::Dpt9::Dpt9(unsigned short subgroup /* = 0*/): Dpt(9, subgroup) {} +Knx::Dpt9::Dpt9() {} -Knx::Dpt9::Dpt9(float value) : Dpt9() +Knx::Dpt9::Dpt9(float value) { _value = value; } @@ -19,12 +19,13 @@ void Knx::Dpt9::encode(uint8_t* data) const float16ToPayload(data, 0, _value, 0xFFFF); } -void Knx::Dpt9::decode(uint8_t* data) +bool Knx::Dpt9::decode(uint8_t* data) { if (unsigned16FromPayload(data, 0) == 0x7FFF) - return; + return false; _value = float16FromPayload(data, 0); + return true; } void Knx::Dpt9::value(float value) diff --git a/src/knx/group_object/dpt/dpt9.h b/src/knx/group_object/dpt/dpt9.h index b4d4f452..71e55900 100644 --- a/src/knx/group_object/dpt/dpt9.h +++ b/src/knx/group_object/dpt/dpt9.h @@ -5,12 +5,12 @@ namespace Knx class Dpt9: public Dpt { public: - Dpt9(unsigned short subgroup = 0); + Dpt9(); Dpt9(float value); Go_SizeCode size() const override; - virtual void encode(uint8_t* data) const override; - virtual void decode(uint8_t* data) override; + void encode(uint8_t* data) const override; + bool decode(uint8_t* data) override; void value(float value); float value() const; @@ -19,4 +19,24 @@ namespace Knx private: float _value; }; + +#define DPT_Value_Temp Dpt(9, 1) +#define DPT_Value_Tempd Dpt(9, 2) +#define DPT_Value_Tempa Dpt(9, 3) +#define DPT_Value_Lux Dpt(9, 4) +#define DPT_Value_Wsp Dpt(9, 5) +#define DPT_Value_Pres Dpt(9, 6) +#define DPT_Value_Humidity Dpt(9, 7) +#define DPT_Value_AirQuality Dpt(9, 8) +#define DPT_Value_Time1 Dpt(9, 10) +#define DPT_Value_Time2 Dpt(9, 11) +#define DPT_Value_Volt Dpt(9, 20) +#define DPT_Value_Curr Dpt(9, 21) +#define DPT_PowerDensity Dpt(9, 22) +#define DPT_KelvinPerPercent Dpt(9, 23) +#define DPT_Power Dpt(9, 24) +#define DPT_Value_Volume_Flow Dpt(9, 25) +#define DPT_Rain_Amount Dpt(9, 26) +#define DPT_Value_Temp_F Dpt(9, 27) +#define DPT_Value_Wsp_kmh Dpt(9, 28) } \ 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 e2804b78..9fab76f0 100644 --- a/src/knx/group_object/dpt/dptconvert.cpp +++ b/src/knx/group_object/dpt/dptconvert.cpp @@ -18,14 +18,6 @@ namespace Knx { if (payload_length > 0) { - // DPT 3.* - Step Control - if (datatype.mainGroup == 3 && datatype.subGroup >= 7 && datatype.subGroup <= 8 && datatype.index <= 1) - return busValueToStepControl(payload, payload_length, datatype, value); - - // DPT 4.* - Character - if (datatype.mainGroup == 4 && datatype.subGroup >= 1 && datatype.subGroup <= 2 && !datatype.index) - return busValueToCharacter(payload, payload_length, datatype, value); - // 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); @@ -168,13 +160,6 @@ namespace Knx int KNX_Encode_Value(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& 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); - - // DPT 4.* - Character - if (datatype.mainGroup == 4 && datatype.subGroup >= 1 && datatype.subGroup <= 2 && !datatype.index) - return valueToBusValueCharacter(value, payload, payload_length, 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) @@ -315,45 +300,6 @@ namespace Knx return false; } - - int busValueToStepControl(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) - { - ASSERT_PAYLOAD(1); - - switch (datatype.index) - { - case 0: - value = bitFromPayload(payload, 4); - return true; - - case 1: - { - const unsigned char stepCode = unsigned8FromPayload(payload, 0) & 0x07; - value = stepCode; - return true; - } - } - - return false; - } - int busValueToCharacter(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) - { - ASSERT_PAYLOAD(1); - int8_t charValue = signed8FromPayload(payload, 0); - - if (datatype.subGroup == 1 && (charValue & 0x80)) - return false; - - if (datatype.subGroup == 2) - { - value = (uint8_t)charValue; - return true; - } - - value = charValue; - return true; - } - int busValueToUnsigned8(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value) { ASSERT_PAYLOAD(1); @@ -961,40 +907,6 @@ namespace Knx //------------------------------------------------------------------------------------------------------------------------------------- - - int valueToBusValueStepControl(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - switch (datatype.index) - { - case 0: - bitToPayload(payload, 4, value); - break; - - case 1: - { - if ((int64_t)value < INT64_C(0) || (int64_t)value > INT64_C(7)) - return false; - - unsigned8ToPayload(payload, 0, (uint64_t)value, 0x07); - } - break; - - default: - return false; - } - - return true; - } - - int valueToBusValueCharacter(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) - { - if ((uint64_t)value > INT64_C(255) || (datatype.subGroup == 1 && (uint64_t)value > INT64_C(127))) - return false; - - unsigned8ToPayload(payload, 0, (uint64_t)value, 0xFF); - return true; - } - int valueToBusValueUnsigned8(const KNXValue& value, uint8_t* payload, size_t payload_length, const Dpt& datatype) { if ((int64_t)value < INT64_C(0)) diff --git a/src/knx/group_object/dpt/dpts.h b/src/knx/group_object/dpt/dpts.h index 68e28687..71cc5970 100644 --- a/src/knx/group_object/dpt/dpts.h +++ b/src/knx/group_object/dpt/dpts.h @@ -2,4 +2,7 @@ #include "dpt1.h" #include "dpt2.h" +#include "dpt3.h" +#include "dpt4.h" +#include "dpt5.h" #include "dpt9.h" \ No newline at end of file diff --git a/src/knx/group_object/group_object.h b/src/knx/group_object/group_object.h index 13732324..93918b4e 100644 --- a/src/knx/group_object/group_object.h +++ b/src/knx/group_object/group_object.h @@ -504,8 +504,7 @@ namespace Knx if (value.size() != sizeCode()) return false; - value.decode(_data); - return true; + return value.decode(_data); } template void GroupObject::valueNoSend(const DPT& value)