This commit is contained in:
Siwat Sirichai 2024-02-16 23:52:07 +07:00
commit f4af513d65
8 changed files with 111 additions and 34 deletions

View File

@ -507,7 +507,7 @@ void ESPMegaDisplay::reset()
* @brief Constructor for the ESPMegaDisplay class. * @brief Constructor for the ESPMegaDisplay class.
* @param displayAdapter The serial adapter connected to the display. * @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->baudRate = baudRate;
this->uploadBaudRate = uploadBaudRate; 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. * @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. * @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) if (xSemaphoreTake(this->serialMutex, DISPLAY_MUTEX_TAKE_TIMEOUT) == pdFALSE)
{ {

View File

@ -17,7 +17,7 @@
class ESPMegaDisplay class ESPMegaDisplay
{ {
public: 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 begin();
void loop(); void loop();
void reset(); void reset();
@ -39,13 +39,13 @@ class ESPMegaDisplay
void giveSerialMutex(); void giveSerialMutex();
SemaphoreHandle_t serialMutex; SemaphoreHandle_t serialMutex;
bool beginUpdate(size_t size); 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); bool writeUpdate(uint8_t* data, size_t size);
void endUpdate(); void endUpdate();
size_t getUpdateBytesWritten(); size_t getUpdateBytesWritten();
protected: protected:
uint16_t baudRate; uint32_t baudRate;
uint16_t uploadBaudRate; uint32_t uploadBaudRate;
uint8_t txPin; uint8_t txPin;
uint8_t rxPin; uint8_t rxPin;
size_t otaBytesWritten; size_t otaBytesWritten;

View File

@ -41,16 +41,21 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length)
// Create a null terminated string from the payload // Create a null terminated string from the payload
memcpy(payload_buffer, payload, length); memcpy(payload_buffer, payload, length);
payload_buffer[length] = '\0'; 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 // Remove the base topic from the topic
char *topic_without_base = topic + strlen(this->mqtt_config.base_topic) + 1; char *topic_without_base = topic + strlen(this->mqtt_config.base_topic) + 1;
for (const auto &callback : mqtt_relative_callbacks) for (const auto &callback : mqtt_relative_callbacks)
{ {
callback.second(topic_without_base, payload_buffer); 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 // Call the respective card's mqtt callback
// Note that after the base topic, there should be the card id // Note that after the base topic, there should be the card id
// /base_topic/card_id/... // /base_topic/card_id/...
@ -372,7 +377,7 @@ void ESPMegaIoT::publish(const char *topic, const char *payload)
* @param callback The callback function * @param callback The callback function
* @return The handler for the callback * @return The handler for the callback
*/ */
uint8_t ESPMegaIoT::registerMqttCallback(std::function<void(char *, char *)> callback) uint16_t ESPMegaIoT::registerMqttCallback(std::function<void(char *, char *)> callback)
{ {
mqtt_callbacks[mqtt_callbacks_handler_index] = callback; mqtt_callbacks[mqtt_callbacks_handler_index] = callback;
return mqtt_callbacks_handler_index++; return mqtt_callbacks_handler_index++;
@ -383,7 +388,7 @@ uint8_t ESPMegaIoT::registerMqttCallback(std::function<void(char *, char *)> cal
* *
* @param handler The handler of the callback * @param handler The handler of the callback
*/ */
void ESPMegaIoT::unregisterMqttCallback(uint8_t handler) void ESPMegaIoT::unregisterMqttCallback(uint16_t handler)
{ {
mqtt_callbacks.erase(handler); mqtt_callbacks.erase(handler);
} }
@ -474,7 +479,7 @@ void ESPMegaIoT::sessionKeepAlive()
* @param callback The callback function * @param callback The callback function
* @return The handler for the callback * @return The handler for the callback
*/ */
uint8_t ESPMegaIoT::registerRelativeMqttCallback(std::function<void(char *, char *)> callback) uint16_t ESPMegaIoT::registerRelativeMqttCallback(std::function<void(char *, char *)> callback)
{ {
mqtt_relative_callbacks[mqtt_relative_callbacks_handler_index] = callback; mqtt_relative_callbacks[mqtt_relative_callbacks_handler_index] = callback;
return mqtt_relative_callbacks_handler_index++; return mqtt_relative_callbacks_handler_index++;
@ -485,7 +490,7 @@ uint8_t ESPMegaIoT::registerRelativeMqttCallback(std::function<void(char *, char
* *
* @param handler The handler of the callback * @param handler The handler of the callback
*/ */
void ESPMegaIoT::unregisterRelativeMqttCallback(uint8_t handler) void ESPMegaIoT::unregisterRelativeMqttCallback(uint16_t handler)
{ {
mqtt_relative_callbacks.erase(handler); mqtt_relative_callbacks.erase(handler);
} }
@ -523,7 +528,7 @@ void ESPMegaIoT::subscribeRelative(const char *topic)
* @param callback The callback function * @param callback The callback function
* @return The handler for the callback * @return The handler for the callback
*/ */
uint8_t ESPMegaIoT::registerSubscribeCallback(std::function<void(void)> callback) uint16_t ESPMegaIoT::registerSubscribeCallback(std::function<void(void)> callback)
{ {
subscribe_callbacks[subscribe_callbacks_handler_index] = callback; subscribe_callbacks[subscribe_callbacks_handler_index] = callback;
return subscribe_callbacks_handler_index++; return subscribe_callbacks_handler_index++;
@ -534,7 +539,7 @@ uint8_t ESPMegaIoT::registerSubscribeCallback(std::function<void(void)> callback
* *
* @param handler The handler of the callback * @param handler The handler of the callback
*/ */
void ESPMegaIoT::unregisterSubscribeCallback(uint8_t handler) void ESPMegaIoT::unregisterSubscribeCallback(uint16_t handler)
{ {
subscribe_callbacks.erase(handler); subscribe_callbacks.erase(handler);
} }

View File

@ -105,12 +105,12 @@ public:
bool mqttConnected(); bool mqttConnected();
void disconnectFromMqtt(); void disconnectFromMqtt();
void publish(const char *topic, const char *payload); void publish(const char *topic, const char *payload);
uint8_t registerMqttCallback(std::function<void(char *, char *)> callback); uint16_t registerMqttCallback(std::function<void(char *, char *)> callback);
void unregisterMqttCallback(uint8_t handler); void unregisterMqttCallback(uint16_t handler);
uint8_t registerRelativeMqttCallback(std::function<void(char *, char *)> callback); uint16_t registerRelativeMqttCallback(std::function<void(char *, char *)> callback);
void unregisterRelativeMqttCallback(uint8_t handler); void unregisterRelativeMqttCallback(uint16_t handler);
uint8_t registerSubscribeCallback(std::function<void(void)> callback); uint16_t registerSubscribeCallback(std::function<void(void)> callback);
void unregisterSubscribeCallback(uint8_t handler); void unregisterSubscribeCallback(uint16_t handler);
void setBaseTopic(char *base_topic); void setBaseTopic(char *base_topic);
void bindEthernetInterface(ETHClass *ethernetIface); void bindEthernetInterface(ETHClass *ethernetIface);
bool networkConnected(); bool networkConnected();
@ -137,12 +137,12 @@ private:
void wifiReconnect(); void wifiReconnect();
void mqttSubscribe(); void mqttSubscribe();
void mqttCallback(char *topic, byte *payload, unsigned int length); void mqttCallback(char *topic, byte *payload, unsigned int length);
uint8_t mqtt_callbacks_handler_index; uint16_t mqtt_callbacks_handler_index;
uint8_t mqtt_relative_callbacks_handler_index; uint16_t mqtt_relative_callbacks_handler_index;
uint8_t subscribe_callbacks_handler_index; uint16_t subscribe_callbacks_handler_index;
std::map<uint8_t, std::function<void(char*, char*)>> mqtt_callbacks; std::map<uint16_t, std::function<void(char*, char*)>> mqtt_callbacks;
std::map<uint8_t, std::function<void(char*, char*)>> mqtt_relative_callbacks; std::map<uint16_t, std::function<void(char*, char*)>> mqtt_relative_callbacks;
std::map<uint8_t, std::function<void(void)>> subscribe_callbacks; std::map<uint16_t, std::function<void(void)>> subscribe_callbacks;
void publishRelative(uint8_t card_id, char *topic, char *payload); void publishRelative(uint8_t card_id, char *topic, char *payload);
bool active; bool active;
PubSubClient mqtt; PubSubClient mqtt;

View File

@ -608,6 +608,7 @@ void InternalDisplay::refreshPWMAdjustmentId()
this->displayAdapter->print(pmwAdjustmentPin); this->displayAdapter->print(pmwAdjustmentPin);
this->displayAdapter->print("\""); this->displayAdapter->print("\"");
this->sendStopBytes(); this->sendStopBytes();
this->giveSerialMutex();
} }
/** /**

View File

@ -153,4 +153,67 @@ uint8_t RemoteVariable::registerCallback(std::function<void(char*)> callback) {
*/ */
void RemoteVariable::unregisterCallback(uint8_t handler) { void RemoteVariable::unregisterCallback(uint8_t handler) {
this->valueChangeCallback.erase(handler); this->valueChangeCallback.erase(handler);
} }
/**
* @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);
}

View File

@ -20,6 +20,12 @@ class RemoteVariable
void subscribe(); void subscribe();
void requestValue(); void requestValue();
char* getValue(); 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(char*)>); uint8_t registerCallback(std::function<void(char*)>);
void unregisterCallback(uint8_t handler); void unregisterCallback(uint8_t handler);

View File

@ -9,9 +9,9 @@
// #define FRAM_DEBUG // #define FRAM_DEBUG
// #define MQTT_DEBUG // #define MQTT_DEBUG
#define WRITE_DEFAULT_NETCONF // #define WRITE_DEFAULT_NETCONF
#define CLIMATE_CARD_ENABLE //#define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER //#define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE #define DISPLAY_ENABLE
#define WEB_SERVER_ENABLE #define WEB_SERVER_ENABLE
#define LCD_OTA_ENABLE #define LCD_OTA_ENABLE
@ -199,8 +199,10 @@ void setup()
ESP_LOGI("Initializer", "Enabling internal display"); ESP_LOGI("Initializer", "Enabling internal display");
espmega.enableInternalDisplay(&Serial); espmega.enableInternalDisplay(&Serial);
ESP_LOGI("Initializer", "Binding climate card to internal display"); ESP_LOGI("Initializer", "Binding climate card to internal display");
#ifdef CLIMATE_CARD_ENABLE
espmega.display->bindClimateCard(&climateCard); espmega.display->bindClimateCard(&climateCard);
#endif #endif
#endif
#ifdef WEB_SERVER_ENABLE #ifdef WEB_SERVER_ENABLE
ESP_LOGI("Initializer", "Enabling web server"); ESP_LOGI("Initializer", "Enabling web server");
espmega.enableWebServer(80); espmega.enableWebServer(80);
@ -213,8 +215,8 @@ void setup()
#endif #endif
#ifdef REMOTE_VARIABLE_ENABLE #ifdef REMOTE_VARIABLE_ENABLE
ESP_LOGI("Initializer", "Initializing testvar"); ESP_LOGI("Initializer", "Initializing testvar");
testVar.begin(32, "/testvar", espmega.iot, true,"/testvar/request"); testVar.begin(32, "/xm/fan_speed", espmega.iot, true,"/pm/request_fan_speed");
testVar.enableSetValue("/testvar/set"); testVar.enableSetValue("/pm/request_switch_state");
#endif #endif
#ifdef CT_ENABLE #ifdef CT_ENABLE
ESP_LOGI("Initializer", "Initializing analog card"); ESP_LOGI("Initializer", "Initializing analog card");