diff --git a/extras/test/src/test_decode.cpp b/extras/test/src/test_decode.cpp index 7b66ef90e..c95d4afe9 100644 --- a/extras/test/src/test_decode.cpp +++ b/extras/test/src/test_decode.cpp @@ -382,6 +382,24 @@ SCENARIO("Arduino Cloud Properties are decoded", "[ArduinoCloudThing::decode]") /************************************************************************************/ + WHEN("A Time property is changed via CBOR message") + { + PropertyContainer property_container; + + CloudTime test; + test = 0; + addPropertyToContainer(property_container, test, "test", Permission::ReadWrite); + + /* [{0: "test", 2: 4294967295}] = 81 A2 00 64 74 65 73 74 02 1A FF FF FF FF */ + uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x1A, 0xFF, 0xFF, 0xFF, 0xFF}; + int const payload_length = sizeof(payload) / sizeof(uint8_t); + CBORDecoder::decode(property_container, payload, payload_length); + + REQUIRE(test == 4294967295); + } + + /************************************************************************************/ + WHEN("Multiple properties is changed via CBOR message") { WHEN("Multiple properties of different type are changed via CBOR message") diff --git a/extras/test/src/test_encode.cpp b/extras/test/src/test_encode.cpp index 985d1c2dc..8a4904fc6 100644 --- a/extras/test/src/test_encode.cpp +++ b/extras/test/src/test_encode.cpp @@ -65,7 +65,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]" CloudInt int_test = 123; addPropertyToContainer(property_container, int_test, "test", Permission::ReadWrite); - /* [{0: "test", 3: 123}] = 9F A2 00 64 74 65 73 74 02 18 7B FF */ + /* [{0: "test", 2: 123}] = 9F A2 00 64 74 65 73 74 02 18 7B FF */ std::vector const expected = {0x9F, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x18, 0x7B, 0xFF}; std::vector const actual = cbor::encode(property_container); REQUIRE(actual == expected); @@ -114,7 +114,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]" CloudLocation location_test = CloudLocation(2.0f, 3.0f); addPropertyToContainer(property_container, location_test, "test", Permission::ReadWrite); - /* [{0: "test:lat", 3: 2},{0: "test:lon", 3: 3}] = 9F A2 00 68 74 65 73 74 3A 6C 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 FA 40 40 00 00 FF*/ + /* [{0: "test:lat", 2: 2},{0: "test:lon", 2: 3}] = 9F A2 00 68 74 65 73 74 3A 6C 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 FA 40 40 00 00 FF*/ std::vector const expected = { 0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x61, 0x74, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6C, 0x6F, 0x6E, 0x02, 0xFA, 0x40, 0x40, 0x00, 0x00, 0xFF }; std::vector const actual = cbor::encode(property_container); REQUIRE(actual == expected); @@ -164,7 +164,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]" CloudColoredLight color_test = CloudColoredLight(true, 2.0, 2.0, 2.0); addPropertyToContainer(property_container, color_test, "test", Permission::ReadWrite); - /* [{0: "test:swi", 4: true},{0: "test:hue", 2: 2.0},{0: "test:sat", 2: 2.0},{0: "test:bri", 2: 2.0}] = 83 A2 00 68 74 65 73 74 3A 73 77 69 04 F5 //A2 00 68 74 65 73 74 3A 68 75 65 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/ + /* [{0: "test:swi", 4: true},{0: "test:hue", 2: 2.0},{0: "test:sat", 2: 2.0},{0: "test:bri", 2: 2.0}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 68 75 65 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 40 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/ std::vector const expected = {0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x77, 0x69, 0x04, 0xF5, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x68, 0x75, 0x65, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x61, 0x74, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x62, 0x72, 0x69, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xFF }; std::vector const actual = cbor::encode(property_container); REQUIRE(actual == expected); @@ -180,7 +180,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]" CloudTelevision tv_test = CloudTelevision(true, 50, false, PlaybackCommands::Play, InputValue::TV, 7); addPropertyToContainer(property_container, tv_test, "test", Permission::ReadWrite); - /* [{0: "test:swi", 4: true},{0: "test:vol", 2: 50},{0: "test:mut", 2: false},{0: "test:pbc", 2: 3},{0: "test:inp", 2: 55},{0: "test:cha", 2: 7}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 76 6F 6C 02 18 32 A2 00 68 74 65 73 74 3A 6D 75 74 04 F4 A2 00 68 74 65 73 74 3A 70 62 63 02 03 A2 00 68 74 65 73 74 3A 69 6E 70 02 18 37 A2 00 68 74 65 73 74 3A 63 68 61 02 07 FF */ + /* [{0: "test:swi", 4: true},{0: "test:vol", 2: 50},{0: "test:mut", 4: false},{0: "test:pbc", 2: 3},{0: "test:inp", 2: 55},{0: "test:cha", 2: 7}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 76 6F 6C 02 18 32 A2 00 68 74 65 73 74 3A 6D 75 74 04 F4 A2 00 68 74 65 73 74 3A 70 62 63 02 03 A2 00 68 74 65 73 74 3A 69 6E 70 02 18 37 A2 00 68 74 65 73 74 3A 63 68 61 02 07 FF */ std::vector const expected = {0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x77, 0x69, 0x04, 0xF5, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x76, 0x6F, 0x6C, 0x02, 0x18, 0x32, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x6D, 0x75, 0x74, 0x04, 0xF4, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x70, 0x62, 0x63, 0x02, 0x03, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x69, 0x6E, 0x70, 0x02, 0x18, 0x37, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x63, 0x68, 0x61, 0x02, 0x07, 0xFF}; std::vector const actual = cbor::encode(property_container); REQUIRE(actual == expected); @@ -196,7 +196,7 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]" CloudDimmedLight color_test = CloudDimmedLight(true, 2.0); addPropertyToContainer(property_container, color_test, "test", Permission::ReadWrite); - /* [{0: "test:swi", 4: true},{0: "test:hue", 2: 0.0},{0: "test:sat", 2: 0.0},{0: "test:bri", 2: 2.0}] = 83 A2 00 68 74 65 73 74 3A 73 77 69 04 F5 //A2 00 68 74 65 73 74 3A 68 75 65 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/ + /* [{0: "test:swi", 4: true},{0: "test:hue", 2: 0.0},{0: "test:sat", 2: 0.0},{0: "test:bri", 2: 2.0}] = 9F A2 00 68 74 65 73 74 3A 73 77 69 04 F5 A2 00 68 74 65 73 74 3A 68 75 65 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 73 61 74 02 FA 00 00 00 00 A2 00 68 74 65 73 74 3A 62 72 69 02 FA 40 00 00 00 FF*/ std::vector const expected = {0x9F, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x77, 0x69, 0x04, 0xF5, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x68, 0x75, 0x65, 0x02, 0xFA, 0x00, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x73, 0x61, 0x74, 0x02, 0xFA, 0x00, 0x00, 0x00, 0x00, 0xA2, 0x00, 0x68, 0x74, 0x65, 0x73, 0x74, 0x3A, 0x62, 0x72, 0x69, 0x02, 0xFA, 0x40, 0x00, 0x00, 0x00, 0xFF }; std::vector const actual = cbor::encode(property_container); REQUIRE(actual == expected); @@ -306,6 +306,22 @@ SCENARIO("Arduino Cloud Properties are encoded", "[ArduinoCloudThing::encode-1]" /************************************************************************************/ + WHEN("A time property is added") + { + PropertyContainer property_container; + cbor::encode(property_container); + + CloudTime test = 1633342784; + addPropertyToContainer(property_container, test, "test", Permission::ReadWrite); + + /* [{0: "test", 2: 1633342784}] = 9F A2 00 64 74 65 73 74 02 1A 61 5A D5 40 FF*/ + std::vector const expected = {0x9F, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x1A, 0x61, 0x5A, 0xD5, 0x40, 0xFF}; + std::vector const actual = cbor::encode(property_container); + REQUIRE(actual == expected); + } + + /************************************************************************************/ + WHEN("Multiple properties are added") { PropertyContainer property_container; diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index 0f117e993..5b5637eb3 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -146,6 +146,28 @@ Property& ArduinoIoTCloudClass::addPropertyReal(int& property, String name, int return addPropertyToContainer(_property_container, *p, name, permission, tag); } +void ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property)) +{ + addPropertyReal(property, name, -1, permission_type, seconds, fn, minDelta, synFn); +} + +void ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, int tag, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property)) +{ + Property* p = new CloudWrapperUnsignedInt(property); + addPropertyReal(*p, name, tag, permission_type, seconds, fn, minDelta, synFn); +} + +Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, Permission const permission) +{ + return addPropertyReal(property, name, -1, permission); +} + +Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, int tag, Permission const permission) +{ + Property* p = new CloudWrapperUnsignedInt(property); + return addPropertyToContainer(_property_container, *p, name, permission, tag); +} + void ArduinoIoTCloudClass::addPropertyReal(String& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property)) { addPropertyReal(property, name, -1, permission_type, seconds, fn, minDelta, synFn); diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 0130e3efc..7c1f9679c 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -39,6 +39,7 @@ #include "property/types/CloudWrapperBool.h" #include "property/types/CloudWrapperFloat.h" #include "property/types/CloudWrapperInt.h" +#include "property/types/CloudWrapperUnsignedInt.h" #include "property/types/CloudWrapperString.h" #include "utility/time/TimeService.h" @@ -110,12 +111,14 @@ class ArduinoIoTCloudClass void addPropertyReal(bool& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); void addPropertyReal(float& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); void addPropertyReal(int& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); + void addPropertyReal(unsigned int& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); void addPropertyReal(String& property, String name, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); Property& addPropertyReal(Property& property, String name, Permission const permission); Property& addPropertyReal(bool& property, String name, Permission const permission); Property& addPropertyReal(float& property, String name, Permission const permission); Property& addPropertyReal(int& property, String name, Permission const permission); + Property& addPropertyReal(unsigned int& property, String name, Permission const permission); Property& addPropertyReal(String& property, String name, Permission const permission); /* The following methods are for MKR WAN 1300/1310 LoRa boards since @@ -128,12 +131,14 @@ class ArduinoIoTCloudClass void addPropertyReal(bool& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); void addPropertyReal(float& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); void addPropertyReal(int& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); + void addPropertyReal(unsigned int& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); void addPropertyReal(String& property, String name, int tag, permissionType permission_type = READWRITE, long seconds = ON_CHANGE, void(*fn)(void) = NULL, float minDelta = 0.0f, void(*synFn)(Property & property) = CLOUD_WINS) __attribute__((deprecated("Use addProperty(property, Permission::ReadWrite) instead."))); Property& addPropertyReal(Property& property, String name, int tag, Permission const permission); Property& addPropertyReal(bool& property, String name, int tag, Permission const permission); Property& addPropertyReal(float& property, String name, int tag, Permission const permission); Property& addPropertyReal(int& property, String name, int tag, Permission const permission); + Property& addPropertyReal(unsigned int& property, String name, int tag, Permission const permission); Property& addPropertyReal(String& property, String name, int tag, Permission const permission); protected: diff --git a/src/property/Property.cpp b/src/property/Property.cpp index 20be6e35b..201998408 100644 --- a/src/property/Property.cpp +++ b/src/property/Property.cpp @@ -171,6 +171,15 @@ CborError Property::appendAttributeReal(int value, String attributeName, CborEnc }, encoder); } +CborError Property::appendAttributeReal(unsigned int value, String attributeName, CborEncoder *encoder) { + return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder) + { + CHECK_CBOR(cbor_encode_int(&mapEncoder, static_cast(CborIntegerMapKey::Value))); + CHECK_CBOR(cbor_encode_int(&mapEncoder, value)); + return CborNoError; + }, encoder); +} + CborError Property::appendAttributeReal(float value, String attributeName, CborEncoder *encoder) { return appendAttributeName(attributeName, [value](CborEncoder & mapEncoder) { @@ -264,6 +273,12 @@ void Property::setAttributeReal(int& value, String attributeName) { }); } +void Property::setAttributeReal(unsigned int& value, String attributeName) { + setAttributeReal(attributeName, [&value](CborMapData & md) { + value = md.val.get(); + }); +} + void Property::setAttributeReal(float& value, String attributeName) { setAttributeReal(attributeName, [&value](CborMapData & md) { value = md.val.get(); diff --git a/src/property/Property.h b/src/property/Property.h index 1e697609e..1ead6f265 100644 --- a/src/property/Property.h +++ b/src/property/Property.h @@ -186,6 +186,7 @@ class Property CborError append(CborEncoder * encoder, bool lightPayload); CborError appendAttributeReal(bool value, String attributeName = "", CborEncoder *encoder = nullptr); CborError appendAttributeReal(int value, String attributeName = "", CborEncoder *encoder = nullptr); + CborError appendAttributeReal(unsigned int value, String attributeName = "", CborEncoder *encoder = nullptr); CborError appendAttributeReal(float value, String attributeName = "", CborEncoder *encoder = nullptr); CborError appendAttributeReal(String value, String attributeName = "", CborEncoder *encoder = nullptr); #ifndef __AVR__ @@ -198,6 +199,7 @@ class Property void setAttributesFromCloud(std::list * map_data_list); void setAttributeReal(bool& value, String attributeName = ""); void setAttributeReal(int& value, String attributeName = ""); + void setAttributeReal(unsigned int& value, String attributeName = ""); void setAttributeReal(float& value, String attributeName = ""); void setAttributeReal(String& value, String attributeName = ""); String getAttributeName(String propertyName, char separator); diff --git a/src/property/PropertyContainer.h b/src/property/PropertyContainer.h index b1e83ec28..fc038f583 100644 --- a/src/property/PropertyContainer.h +++ b/src/property/PropertyContainer.h @@ -37,6 +37,7 @@ #include "types/CloudBool.h" #include "types/CloudFloat.h" #include "types/CloudInt.h" +#include "types/CloudUnsignedInt.h" #include "types/CloudString.h" #include "types/CloudLocation.h" #include "types/CloudColor.h" @@ -72,7 +73,7 @@ typedef CloudFloat CloudElectricCurrent; typedef CloudFloat CloudElectricPotential; typedef CloudFloat CloudElectricResistance; typedef CloudFloat CloudCapacitance; -typedef CloudFloat CloudTime; +typedef CloudUnsignedInt CloudTime; typedef CloudFloat CloudFrequency; typedef CloudFloat CloudDataRate; typedef CloudFloat CloudHeartRate; diff --git a/src/property/types/CloudUnsignedInt.h b/src/property/types/CloudUnsignedInt.h new file mode 100644 index 000000000..37ec2d1cb --- /dev/null +++ b/src/property/types/CloudUnsignedInt.h @@ -0,0 +1,222 @@ +// +// This file is part of ArduinoCloudThing +// +// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of ArduinoCloudThing. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to modify or +// otherwise use the software for commercial activities involving the Arduino +// software without disclosing the source code of your own applications. To purchase +// a commercial license, send an email to license@arduino.cc. +// + +#ifndef CLOUDUINT_H_ +#define CLOUDUINT_H_ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include +#include "../Property.h" + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + + + +class CloudUnsignedInt : public Property { + private: + unsigned int _value, + _cloud_value; + public: + CloudUnsignedInt() { + CloudUnsignedInt(0); + } + CloudUnsignedInt(unsigned int v) : _value(v), _cloud_value(v) {} + operator unsigned int() const { + return _value; + } + virtual bool isDifferentFromCloud() { + return _value != _cloud_value && ((std::max(_value , _cloud_value) - std::min(_value , _cloud_value)) >= Property::_min_delta_property); + } + virtual void fromCloudToLocal() { + _value = _cloud_value; + } + virtual void fromLocalToCloud() { + _cloud_value = _value; + } + virtual CborError appendAttributesToCloud() { + return appendAttribute(_value); + } + virtual void setAttributesFromCloud() { + setAttribute(_cloud_value); + } + //modifiers + CloudUnsignedInt& operator=(unsigned int v) { + _value = v; + updateLocalTimestamp(); + return *this; + } + CloudUnsignedInt& operator=(CloudUnsignedInt v) { + return operator=((unsigned int)v); + } + CloudUnsignedInt& operator+=(unsigned int v) { + return operator=(_value += v); + } + CloudUnsignedInt& operator-=(unsigned int v) { + return operator=(_value -= v); + } + CloudUnsignedInt& operator*=(unsigned int v) { + return operator=(_value *= v); + } + CloudUnsignedInt& operator/=(unsigned int v) { + return operator=(_value /= v); + } + CloudUnsignedInt& operator%=(unsigned int v) { + return operator=(_value %= v); + } + CloudUnsignedInt& operator++() { + return operator=(++_value); + } + CloudUnsignedInt& operator--() { + return operator=(--_value); + } + CloudUnsignedInt operator++(int) { + operator=(_value + 1); + return CloudUnsignedInt(_value); + } + CloudUnsignedInt operator--(int) { + operator=(_value - 1); + return CloudUnsignedInt(_value); + } + CloudUnsignedInt& operator&=(unsigned int v) { + return operator=(_value &= v); + } + CloudUnsignedInt& operator|=(unsigned int v) { + return operator=(_value |= v); + } + CloudUnsignedInt& operator^=(unsigned int v) { + return operator=(_value ^= v); + } + CloudUnsignedInt& operator<<=(unsigned int v) { + return operator=(_value <<= v); + } + CloudUnsignedInt& operator>>=(unsigned int v) { + return operator=(_value >>= v); + } + //accessors + CloudUnsignedInt operator+() const { + return CloudUnsignedInt(+_value); + } + CloudUnsignedInt operator-() const { + return CloudUnsignedInt(-_value); + } + CloudUnsignedInt operator!() const { + return CloudUnsignedInt(!_value); + } + CloudUnsignedInt operator~() const { + return CloudUnsignedInt(~_value); + } + //friends + friend CloudUnsignedInt operator+(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw += v; + } + friend CloudUnsignedInt operator+(CloudUnsignedInt iw, unsigned int v) { + return iw += v; + } + friend CloudUnsignedInt operator+(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) += iw; + } + friend CloudUnsignedInt operator-(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw -= v; + } + friend CloudUnsignedInt operator-(CloudUnsignedInt iw, unsigned int v) { + return iw -= v; + } + friend CloudUnsignedInt operator-(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) -= iw; + } + friend CloudUnsignedInt operator*(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw *= v; + } + friend CloudUnsignedInt operator*(CloudUnsignedInt iw, unsigned int v) { + return iw *= v; + } + friend CloudUnsignedInt operator*(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) *= iw; + } + friend CloudUnsignedInt operator/(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw /= v; + } + friend CloudUnsignedInt operator/(CloudUnsignedInt iw, unsigned int v) { + return iw /= v; + } + friend CloudUnsignedInt operator/(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) /= iw; + } + friend CloudUnsignedInt operator%(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw %= v; + } + friend CloudUnsignedInt operator%(CloudUnsignedInt iw, unsigned int v) { + return iw %= v; + } + friend CloudUnsignedInt operator%(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) %= iw; + } + friend CloudUnsignedInt operator&(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw &= v; + } + friend CloudUnsignedInt operator&(CloudUnsignedInt iw, unsigned int v) { + return iw &= v; + } + friend CloudUnsignedInt operator&(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) &= iw; + } + friend CloudUnsignedInt operator|(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw |= v; + } + friend CloudUnsignedInt operator|(CloudUnsignedInt iw, unsigned int v) { + return iw |= v; + } + friend CloudUnsignedInt operator|(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) |= iw; + } + friend CloudUnsignedInt operator^(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw ^= v; + } + friend CloudUnsignedInt operator^(CloudUnsignedInt iw, unsigned int v) { + return iw ^= v; + } + friend CloudUnsignedInt operator^(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) ^= iw; + } + friend CloudUnsignedInt operator<<(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw <<= v; + } + friend CloudUnsignedInt operator<<(CloudUnsignedInt iw, unsigned int v) { + return iw <<= v; + } + friend CloudUnsignedInt operator<<(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) <<= iw; + } + friend CloudUnsignedInt operator>>(CloudUnsignedInt iw, CloudUnsignedInt v) { + return iw >>= v; + } + friend CloudUnsignedInt operator>>(CloudUnsignedInt iw, unsigned int v) { + return iw >>= v; + } + friend CloudUnsignedInt operator>>(unsigned int v, CloudUnsignedInt iw) { + return CloudUnsignedInt(v) >>= iw; + } + +}; + + +#endif /* CLOUDUINT_H_ */ diff --git a/src/property/types/CloudWrapperUnsignedInt.h b/src/property/types/CloudWrapperUnsignedInt.h new file mode 100644 index 000000000..d722f585f --- /dev/null +++ b/src/property/types/CloudWrapperUnsignedInt.h @@ -0,0 +1,63 @@ +// +// This file is part of ArduinoCloudThing +// +// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of ArduinoCloudThing. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to modify or +// otherwise use the software for commercial activities involving the Arduino +// software without disclosing the source code of your own applications. To purchase +// a commercial license, send an email to license@arduino.cc. +// + +#ifndef CLOUDWRAPPERUINT_H_ +#define CLOUDWRAPPERUINT_H_ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include +#include "CloudWrapperBase.h" + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class CloudWrapperUnsignedInt : public CloudWrapperBase { + private: + unsigned int &_primitive_value, + _cloud_value, + _local_value; + public: + CloudWrapperUnsignedInt(unsigned int& v) : _primitive_value(v), _cloud_value(v), _local_value(v) {} + virtual bool isDifferentFromCloud() { + return _primitive_value != _cloud_value && ((std::max(_primitive_value , _cloud_value) - std::min(_primitive_value , _cloud_value)) >= Property::_min_delta_property); + } + virtual void fromCloudToLocal() { + _primitive_value = _cloud_value; + } + virtual void fromLocalToCloud() { + _cloud_value = _primitive_value; + } + virtual CborError appendAttributesToCloud() { + return appendAttribute(_primitive_value); + } + virtual void setAttributesFromCloud() { + setAttribute(_cloud_value); + } + virtual bool isPrimitive() { + return true; + } + virtual bool isChangedLocally() { + return _primitive_value != _local_value; + } +}; + + +#endif /* CLOUDWRAPPERINT_H_ */