From b9571112d68399348a632bd2cd052d86406b8e9c Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Thu, 12 Dec 2019 23:40:03 +0100 Subject: [PATCH] add some convienience methods to Property class --- src/knx/data_property.cpp | 24 ++--------- src/knx/ip_parameter_object.cpp | 12 ++---- src/knx/property.cpp | 70 +++++++++++++++++++++++++++++++++ src/knx/property.h | 6 +++ 4 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/knx/data_property.cpp b/src/knx/data_property.cpp index 88e6dbc..901ae65 100644 --- a/src/knx/data_property.cpp +++ b/src/knx/data_property.cpp @@ -86,39 +86,21 @@ DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType typ uint16_t maxElements, uint8_t access, uint16_t value) : Property(id, writeEnable, type, maxElements, access) { - uint8_t elementSize = ElementSize(); - if (elementSize == 2) - { - uint8_t data[elementSize]; - pushWord(value, data); - write(1, 1, data); - } + Property::write(value); } DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType type, uint16_t maxElements, uint8_t access, uint32_t value) : Property(id, writeEnable, type, maxElements, access) { - uint8_t elementSize = ElementSize(); - if (elementSize == 4) - { - uint8_t data[elementSize]; - pushInt(value, data); - write(1, 1, data); - } + Property::write(value); } DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType type, uint16_t maxElements, uint8_t access, uint8_t value) : Property(id, writeEnable, type, maxElements, access) { - uint8_t elementSize = ElementSize(); - if (elementSize == 1) - { - uint8_t data[elementSize]; - data[0] = value; - write(1, 1, data); - } + Property::write(value); } uint16_t DataProperty::saveSize() diff --git a/src/knx/ip_parameter_object.cpp b/src/knx/ip_parameter_object.cpp index eb20827..7d67f9f 100644 --- a/src/knx/ip_parameter_object.cpp +++ b/src/knx/ip_parameter_object.cpp @@ -100,12 +100,8 @@ uint32_t IpParameterObject::multicastAddress() const { const Property* prop = property(PID_ROUTING_MULTICAST_ADDRESS); - uint8_t data[4]; - uint8_t count = prop->read(1, 1, data); - uint32_t value = DEFAULT_MULTICAST_ADDR; - if (count == 1) - popInt(value, data); + prop->read(value); return value; } @@ -114,8 +110,8 @@ uint8_t IpParameterObject::ttl() const { const Property* prop = property(PID_TTL); - uint8_t data[1]; - prop->read(1, 1, data); - return data[0]; + uint8_t value = 0; + prop->read(value); + return value; } #endif \ No newline at end of file diff --git a/src/knx/property.cpp b/src/knx/property.cpp index 54141e6..93096ae 100644 --- a/src/knx/property.cpp +++ b/src/knx/property.cpp @@ -111,3 +111,73 @@ Property::Property(PropertyID id, bool writeEnable, PropertyDataType type, Property::~Property() {} + + +uint8_t Property::read(uint8_t& value) const +{ + if (ElementSize() != 1) + return 0; + + return read(1, 1, &value); +} + + +uint8_t Property::read(uint16_t& value) const +{ + if (ElementSize() != 2) + return 0; + + uint8_t data[2]; + uint8_t count = read(1, 1, data); + if (count > 0) + { + popWord(value, data); + } + return count; +} + + +uint8_t Property::read(uint32_t& value) const +{ + if (ElementSize() != 4) + return 0; + + uint8_t data[4]; + uint8_t count = read(1, 1, data); + if (count > 0) + { + popInt(value, data); + } + return count; +} + + +uint8_t Property::write(uint8_t& value) +{ + if (ElementSize() != 1) + return 0; + + return write(1, 1, &value); +} + + +uint8_t Property::write(uint16_t& value) +{ + if (ElementSize() != 2) + return 0; + + uint8_t data[2]; + pushWord(value, data); + return write(1, 1, data); +} + + +uint8_t Property::write(uint32_t& value) +{ + if (ElementSize() != 4) + return 0; + + uint8_t data[4]; + pushInt(value, data); + return write(1, 1, data); +} diff --git a/src/knx/property.h b/src/knx/property.h index c581ad5..1ed8541 100644 --- a/src/knx/property.h +++ b/src/knx/property.h @@ -238,6 +238,12 @@ class Property : public SaveRestore uint8_t ElementSize() const; virtual uint8_t read(uint16_t start, uint8_t count, uint8_t* data) const = 0; virtual uint8_t write(uint16_t start, uint8_t count, uint8_t* data) = 0; + uint8_t read(uint8_t& value) const; + uint8_t read(uint16_t& value) const; + uint8_t read(uint32_t& value) const; + uint8_t write(uint8_t& value); + uint8_t write(uint16_t& value); + uint8_t write(uint32_t& value); protected: PropertyID _id; bool _writeEnable;