From 1b9163d468470d3784015ab44cb3c08cfabf33ff Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 13 Feb 2024 02:28:34 +0700 Subject: [PATCH] type adaptive remote variable --- .../lib/ESPMegaPRO/RemoteVariable.cpp | 77 ++++++++++++++++++- .../lib/ESPMegaPRO/RemoteVariable.hpp | 6 ++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.cpp index e06b622..4976400 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.cpp @@ -135,10 +135,85 @@ void RemoteVariable::setValue(const char* value) { this->iot->publish(this->setValueTopic, value); } +/** + * @brief Register a callback for value change + * This method will be called when the value of the variable changes on the remote device + * @param callback The callback function + * @return uint8_t The handler for the callback +*/ uint8_t RemoteVariable::registerCallback(std::function callback) { this->valueChangeCallback[this->valueChangeCallbackCount] = callback; return valueChangeCallbackCount++; } + +/** + * @brief Unregister a callback + * This method is used to unregister a callback + * @param handler The handler of the callback +*/ void RemoteVariable::unregisterCallback(uint8_t handler) { this->valueChangeCallback.erase(handler); -} \ No newline at end of file +} + +/** + * @brief Get the value of the variable as an integer + * This method is used to get the value of the variable as an integer + * @return int The value of the variable as an integer + * @note If the value is not a valid integer, it will return 0 +*/ +int RemoteVariable::getValueAsInt() { + return atoi(this->value); +} + +/** + * @brief Get the value of the variable as a long + * This method is used to get the value of the variable as a long + * @return long The value of the variable as a long + * @note If the value is not a valid long, it will return 0 +*/ +long RemoteVariable::getValueAsLong() { + return atol(this->value); +} + +/** + * @brief Get the value of the variable as a double + * This method is used to get the value of the variable as a double + * @return double The value of the variable as a double + * @note If the value is not a valid double, it will return 0 +*/ +double RemoteVariable::getValueAsDouble() { + return atof(this->value); +} + +/** + * @brief Set the value of the variable as an integer + * This method is used to set the value of the variable as an integer + * @param value The value to set +*/ +void RemoteVariable::setIntValue(int value) { + char buffer[this->size]; + itoa(value, buffer, DEC); + this->setValue(buffer); +} + +/** + * @brief Set the value of the variable as a long + * This method is used to set the value of the variable as a long + * @param value The value to set +*/ +void RemoteVariable::setLongValue(long value) { + char buffer[this->size]; + ltoa(value, buffer, DEC); + this->setValue(buffer); +} + +/** + * @brief Set the value of the variable as a double + * This method is used to set the value of the variable as a double + * @param value The value to set +*/ +void RemoteVariable::setDoubleValue(double value) { + char buffer[this->size]; + snprintf(buffer, this->size, "%f", value); + this->setValue(buffer); +} diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.hpp index 56e38d1..3a8857f 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/RemoteVariable.hpp @@ -20,6 +20,12 @@ class RemoteVariable void subscribe(); void requestValue(); char* getValue(); + int getValueAsInt(); + long getValueAsLong(); + double getValueAsDouble(); + void setIntValue(int value); + void setLongValue(long value); + void setDoubleValue(double value); uint8_t registerCallback(std::function); void unregisterCallback(uint8_t handler);