From 1b9163d468470d3784015ab44cb3c08cfabf33ff Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 13 Feb 2024 02:28:34 +0700 Subject: [PATCH 1/4] 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); From 531f7e05a746ee90b5476f4c4ae428ce8037ce81 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 14 Feb 2024 02:01:39 +0700 Subject: [PATCH 2/4] fix IoT Callback issue when the topic is base-1 --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp | 13 +++++++++---- ESPMegaPRO-OS-SDK/src/main.cpp | 12 +++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index 9362e10..b6f2a8f 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -41,16 +41,21 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) // Create a null terminated string from the payload memcpy(payload_buffer, payload, length); payload_buffer[length] = '\0'; + // If the topic is not appended with the base topic, call only the absolute callbacks + if (strncmp(topic, this->mqtt_config.base_topic, base_topic_length) != 0) + { + for (const auto &callback : mqtt_callbacks) + { + callback.second(topic, payload_buffer); + } + return; + } // Remove the base topic from the topic char *topic_without_base = topic + strlen(this->mqtt_config.base_topic) + 1; for (const auto &callback : mqtt_relative_callbacks) { callback.second(topic_without_base, payload_buffer); } - for (const auto &callback : mqtt_callbacks) - { - callback.second(topic, payload_buffer); - } // Call the respective card's mqtt callback // Note that after the base topic, there should be the card id // /base_topic/card_id/... diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index 957ec89..c154233 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -9,9 +9,9 @@ // #define FRAM_DEBUG // #define MQTT_DEBUG -#define WRITE_DEFAULT_NETCONF -#define CLIMATE_CARD_ENABLE -#define MQTT_CARD_REGISTER +// #define WRITE_DEFAULT_NETCONF +//#define CLIMATE_CARD_ENABLE +//#define MQTT_CARD_REGISTER #define DISPLAY_ENABLE #define WEB_SERVER_ENABLE #define LCD_OTA_ENABLE @@ -199,8 +199,10 @@ void setup() ESP_LOGI("Initializer", "Enabling internal display"); espmega.enableInternalDisplay(&Serial); ESP_LOGI("Initializer", "Binding climate card to internal display"); +#ifdef CLIMATE_CARD_ENABLE espmega.display->bindClimateCard(&climateCard); #endif +#endif #ifdef WEB_SERVER_ENABLE ESP_LOGI("Initializer", "Enabling web server"); espmega.enableWebServer(80); @@ -213,8 +215,8 @@ void setup() #endif #ifdef REMOTE_VARIABLE_ENABLE ESP_LOGI("Initializer", "Initializing testvar"); - testVar.begin(32, "/testvar", espmega.iot, true,"/testvar/request"); - testVar.enableSetValue("/testvar/set"); + testVar.begin(32, "/xm/fan_speed", espmega.iot, true,"/pm/request_fan_speed"); + testVar.enableSetValue("/pm/request_switch_state"); #endif #ifdef CT_ENABLE ESP_LOGI("Initializer", "Initializing analog card"); From 06b69da78fa1b8bcc332526d85fcae65e7262644 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 14 Feb 2024 02:08:09 +0700 Subject: [PATCH 3/4] expand mqtt callbacks from 2^8 to 2^16 --- .../lib/ESPMegaPRO/ESPMegaIoT.cpp | 12 +++++----- .../lib/ESPMegaPRO/ESPMegaIoT.hpp | 24 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index b6f2a8f..60a14b2 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -377,7 +377,7 @@ void ESPMegaIoT::publish(const char *topic, const char *payload) * @param callback The callback function * @return The handler for the callback */ -uint8_t ESPMegaIoT::registerMqttCallback(std::function callback) +uint16_t ESPMegaIoT::registerMqttCallback(std::function callback) { mqtt_callbacks[mqtt_callbacks_handler_index] = callback; return mqtt_callbacks_handler_index++; @@ -388,7 +388,7 @@ uint8_t ESPMegaIoT::registerMqttCallback(std::function cal * * @param handler The handler of the callback */ -void ESPMegaIoT::unregisterMqttCallback(uint8_t handler) +void ESPMegaIoT::unregisterMqttCallback(uint16_t handler) { mqtt_callbacks.erase(handler); } @@ -479,7 +479,7 @@ void ESPMegaIoT::sessionKeepAlive() * @param callback The callback function * @return The handler for the callback */ -uint8_t ESPMegaIoT::registerRelativeMqttCallback(std::function callback) +uint16_t ESPMegaIoT::registerRelativeMqttCallback(std::function callback) { mqtt_relative_callbacks[mqtt_relative_callbacks_handler_index] = callback; return mqtt_relative_callbacks_handler_index++; @@ -490,7 +490,7 @@ uint8_t ESPMegaIoT::registerRelativeMqttCallback(std::function callback) +uint16_t ESPMegaIoT::registerSubscribeCallback(std::function callback) { subscribe_callbacks[subscribe_callbacks_handler_index] = callback; return subscribe_callbacks_handler_index++; @@ -539,7 +539,7 @@ uint8_t ESPMegaIoT::registerSubscribeCallback(std::function callback * * @param handler The handler of the callback */ -void ESPMegaIoT::unregisterSubscribeCallback(uint8_t handler) +void ESPMegaIoT::unregisterSubscribeCallback(uint16_t handler) { subscribe_callbacks.erase(handler); } diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp index 74f5dd2..c5467b2 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -105,12 +105,12 @@ public: bool mqttConnected(); void disconnectFromMqtt(); void publish(const char *topic, const char *payload); - uint8_t registerMqttCallback(std::function callback); - void unregisterMqttCallback(uint8_t handler); - uint8_t registerRelativeMqttCallback(std::function callback); - void unregisterRelativeMqttCallback(uint8_t handler); - uint8_t registerSubscribeCallback(std::function callback); - void unregisterSubscribeCallback(uint8_t handler); + uint16_t registerMqttCallback(std::function callback); + void unregisterMqttCallback(uint16_t handler); + uint16_t registerRelativeMqttCallback(std::function callback); + void unregisterRelativeMqttCallback(uint16_t handler); + uint16_t registerSubscribeCallback(std::function callback); + void unregisterSubscribeCallback(uint16_t handler); void setBaseTopic(char *base_topic); void bindEthernetInterface(ETHClass *ethernetIface); bool networkConnected(); @@ -137,12 +137,12 @@ private: void wifiReconnect(); void mqttSubscribe(); void mqttCallback(char *topic, byte *payload, unsigned int length); - uint8_t mqtt_callbacks_handler_index; - uint8_t mqtt_relative_callbacks_handler_index; - uint8_t subscribe_callbacks_handler_index; - std::map> mqtt_callbacks; - std::map> mqtt_relative_callbacks; - std::map> subscribe_callbacks; + uint16_t mqtt_callbacks_handler_index; + uint16_t mqtt_relative_callbacks_handler_index; + uint16_t subscribe_callbacks_handler_index; + std::map> mqtt_callbacks; + std::map> mqtt_relative_callbacks; + std::map> subscribe_callbacks; void publishRelative(uint8_t card_id, char *topic, char *payload); bool active; PubSubClient mqtt; From 8988b59103604e7b5128345997e2438f1f529787 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 16 Feb 2024 01:23:59 +0700 Subject: [PATCH 4/4] fix internal display pwm adjustment bug --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp | 4 ++-- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp | 8 ++++---- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/InternalDisplay.cpp | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp index e80c924..21a4303 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.cpp @@ -507,7 +507,7 @@ void ESPMegaDisplay::reset() * @brief Constructor for the ESPMegaDisplay class. * @param displayAdapter The serial adapter connected to the display. */ -ESPMegaDisplay::ESPMegaDisplay(HardwareSerial *displayAdapter, uint16_t baudRate, uint16_t uploadBaudRate, uint8_t txPin, uint8_t rxPin) +ESPMegaDisplay::ESPMegaDisplay(HardwareSerial *displayAdapter, uint32_t baudRate, uint32_t uploadBaudRate, uint8_t txPin, uint8_t rxPin) { this->baudRate = baudRate; this->uploadBaudRate = uploadBaudRate; @@ -657,7 +657,7 @@ bool ESPMegaDisplay::beginUpdate(size_t size) * @note The baud rate that is used to transfer the data is defined by the uploadBaudRate parameter in the constructor. * @return True if the OTA update is started, false otherwise. */ -bool ESPMegaDisplay::beginUpdate(size_t size, uint16_t baudRate) +bool ESPMegaDisplay::beginUpdate(size_t size, uint32_t baudRate) { if (xSemaphoreTake(this->serialMutex, DISPLAY_MUTEX_TAKE_TIMEOUT) == pdFALSE) { diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp index 2337942..3a24707 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaDisplay.hpp @@ -17,7 +17,7 @@ class ESPMegaDisplay { public: - ESPMegaDisplay(HardwareSerial *displayAdapter, uint16_t baudRate, uint16_t uploadBaudRate, uint8_t txPin, uint8_t rxPin); + ESPMegaDisplay(HardwareSerial *displayAdapter, uint32_t baudRate, uint32_t uploadBaudRate, uint8_t txPin, uint8_t rxPin); void begin(); void loop(); void reset(); @@ -39,13 +39,13 @@ class ESPMegaDisplay void giveSerialMutex(); SemaphoreHandle_t serialMutex; bool beginUpdate(size_t size); - bool beginUpdate(size_t size, uint16_t baudRate); + bool beginUpdate(size_t size, uint32_t baudRate); bool writeUpdate(uint8_t* data, size_t size); void endUpdate(); size_t getUpdateBytesWritten(); protected: - uint16_t baudRate; - uint16_t uploadBaudRate; + uint32_t baudRate; + uint32_t uploadBaudRate; uint8_t txPin; uint8_t rxPin; size_t otaBytesWritten; diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/InternalDisplay.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/InternalDisplay.cpp index a847b63..2fc8efa 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/InternalDisplay.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/InternalDisplay.cpp @@ -608,6 +608,7 @@ void InternalDisplay::refreshPWMAdjustmentId() this->displayAdapter->print(pmwAdjustmentPin); this->displayAdapter->print("\""); this->sendStopBytes(); + this->giveSerialMutex(); } /**