From f5f45e2ce1dccdecd1a057a636fafae3752b8785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cornelius=20K=C3=B6pp?= Date: Sun, 13 Aug 2023 22:01:45 +0200 Subject: [PATCH] Allow Setting Value of GroupObject (KO) with Checking Change after Conversion by `valueNoSendCompare(..)` * Return if Value was Changed * Always Set the First Value * Copy on Changes Only * Make Comparison Independent of Sending * Extend Doc of `valueNoSendCompare(..)`; Add Note for using `valueNoSend(..)` --- src/knx/group_object.cpp | 24 ++++++++++++++++++++++++ src/knx/group_object.h | 13 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/knx/group_object.cpp b/src/knx/group_object.cpp index 30420e3..80865f4 100644 --- a/src/knx/group_object.cpp +++ b/src/knx/group_object.cpp @@ -287,3 +287,27 @@ void GroupObject::valueNoSend(const KNXValue& value, const Dpt& type) KNX_Encode_Value(value, _data, _dataLength, type); } + +bool GroupObject::valueNoSendCompare(const KNXValue& value, const Dpt& type) +{ + if (_commFlag == Uninitialized) + { + // always set first value + this->valueNoSend(value, type); + return true; + } + else + { + // convert new value to given dtp + uint8_t newData[_dataLength]; + memset(newData, 0, _dataLength); + KNX_Encode_Value(value, newData, _dataLength, type); + + // check for change in converted value / update value on change only + const bool dataChanged = memcmp(_data, newData, _dataLength); + if (dataChanged) + memcpy(_data, newData, _dataLength); + + return dataChanged; + } +} \ No newline at end of file diff --git a/src/knx/group_object.h b/src/knx/group_object.h index cf46307..38b8291 100644 --- a/src/knx/group_object.h +++ b/src/knx/group_object.h @@ -181,6 +181,19 @@ class GroupObject * The parameters must fit the group object. Otherwise it will stay unchanged. */ void valueNoSend(const KNXValue& value, const Dpt& type); + + /** + * Check if the value (after conversion to dpt) will differ from current value of the group object and update if necessary. + * Use this method only, when the value change is relevant, otherwise valueNoSend(const KNXValue&, const Dpt&) will do the same (without overhead for comparing) + * @param value the value the group object is set to + * @param type the datapoint type used for the conversion. + * + * The parameters must fit the group object. Otherwise it will stay unchanged. + * + * @returns true if the value of the group object has changed + */ + bool valueNoSendCompare(const KNXValue& value, const Dpt& type); + /** * set the current value of the group object. * @param value the value the group object is set to