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);