add some convienience methods to Property class

This commit is contained in:
Thomas Kunze 2019-12-12 23:40:03 +01:00
parent 398ba7408b
commit b9571112d6
4 changed files with 83 additions and 29 deletions

View File

@ -86,39 +86,21 @@ DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType typ
uint16_t maxElements, uint8_t access, uint16_t value) uint16_t maxElements, uint8_t access, uint16_t value)
: Property(id, writeEnable, type, maxElements, access) : Property(id, writeEnable, type, maxElements, access)
{ {
uint8_t elementSize = ElementSize(); Property::write(value);
if (elementSize == 2)
{
uint8_t data[elementSize];
pushWord(value, data);
write(1, 1, data);
}
} }
DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType type, DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType type,
uint16_t maxElements, uint8_t access, uint32_t value) uint16_t maxElements, uint8_t access, uint32_t value)
: Property(id, writeEnable, type, maxElements, access) : Property(id, writeEnable, type, maxElements, access)
{ {
uint8_t elementSize = ElementSize(); Property::write(value);
if (elementSize == 4)
{
uint8_t data[elementSize];
pushInt(value, data);
write(1, 1, data);
}
} }
DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType type, DataProperty::DataProperty(PropertyID id, bool writeEnable, PropertyDataType type,
uint16_t maxElements, uint8_t access, uint8_t value) uint16_t maxElements, uint8_t access, uint8_t value)
: Property(id, writeEnable, type, maxElements, access) : Property(id, writeEnable, type, maxElements, access)
{ {
uint8_t elementSize = ElementSize(); Property::write(value);
if (elementSize == 1)
{
uint8_t data[elementSize];
data[0] = value;
write(1, 1, data);
}
} }
uint16_t DataProperty::saveSize() uint16_t DataProperty::saveSize()

View File

@ -100,12 +100,8 @@ uint32_t IpParameterObject::multicastAddress() const
{ {
const Property* prop = property(PID_ROUTING_MULTICAST_ADDRESS); 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; uint32_t value = DEFAULT_MULTICAST_ADDR;
if (count == 1) prop->read(value);
popInt(value, data);
return value; return value;
} }
@ -114,8 +110,8 @@ uint8_t IpParameterObject::ttl() const
{ {
const Property* prop = property(PID_TTL); const Property* prop = property(PID_TTL);
uint8_t data[1]; uint8_t value = 0;
prop->read(1, 1, data); prop->read(value);
return data[0]; return value;
} }
#endif #endif

View File

@ -111,3 +111,73 @@ Property::Property(PropertyID id, bool writeEnable, PropertyDataType type,
Property::~Property() 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);
}

View File

@ -238,6 +238,12 @@ class Property : public SaveRestore
uint8_t ElementSize() const; uint8_t ElementSize() const;
virtual uint8_t read(uint16_t start, uint8_t count, uint8_t* data) const = 0; 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; 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: protected:
PropertyID _id; PropertyID _id;
bool _writeEnable; bool _writeEnable;