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.
* @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)
{

View File

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

View File

@ -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/...
@ -372,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<void(char *, char *)> callback)
uint16_t ESPMegaIoT::registerMqttCallback(std::function<void(char *, char *)> callback)
{
mqtt_callbacks[mqtt_callbacks_handler_index] = callback;
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
*/
void ESPMegaIoT::unregisterMqttCallback(uint8_t handler)
void ESPMegaIoT::unregisterMqttCallback(uint16_t handler)
{
mqtt_callbacks.erase(handler);
}
@ -474,7 +479,7 @@ void ESPMegaIoT::sessionKeepAlive()
* @param callback The callback function
* @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;
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
*/
void ESPMegaIoT::unregisterRelativeMqttCallback(uint8_t handler)
void ESPMegaIoT::unregisterRelativeMqttCallback(uint16_t handler)
{
mqtt_relative_callbacks.erase(handler);
}
@ -523,7 +528,7 @@ void ESPMegaIoT::subscribeRelative(const char *topic)
* @param callback The callback function
* @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;
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
*/
void ESPMegaIoT::unregisterSubscribeCallback(uint8_t handler)
void ESPMegaIoT::unregisterSubscribeCallback(uint16_t handler)
{
subscribe_callbacks.erase(handler);
}

View File

@ -105,12 +105,12 @@ public:
bool mqttConnected();
void disconnectFromMqtt();
void publish(const char *topic, const char *payload);
uint8_t registerMqttCallback(std::function<void(char *, char *)> callback);
void unregisterMqttCallback(uint8_t handler);
uint8_t registerRelativeMqttCallback(std::function<void(char *, char *)> callback);
void unregisterRelativeMqttCallback(uint8_t handler);
uint8_t registerSubscribeCallback(std::function<void(void)> callback);
void unregisterSubscribeCallback(uint8_t handler);
uint16_t registerMqttCallback(std::function<void(char *, char *)> callback);
void unregisterMqttCallback(uint16_t handler);
uint16_t registerRelativeMqttCallback(std::function<void(char *, char *)> callback);
void unregisterRelativeMqttCallback(uint16_t handler);
uint16_t registerSubscribeCallback(std::function<void(void)> 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<uint8_t, std::function<void(char*, char*)>> mqtt_callbacks;
std::map<uint8_t, std::function<void(char*, char*)>> mqtt_relative_callbacks;
std::map<uint8_t, std::function<void(void)>> subscribe_callbacks;
uint16_t mqtt_callbacks_handler_index;
uint16_t mqtt_relative_callbacks_handler_index;
uint16_t subscribe_callbacks_handler_index;
std::map<uint16_t, std::function<void(char*, char*)>> mqtt_callbacks;
std::map<uint16_t, std::function<void(char*, char*)>> mqtt_relative_callbacks;
std::map<uint16_t, std::function<void(void)>> subscribe_callbacks;
void publishRelative(uint8_t card_id, char *topic, char *payload);
bool active;
PubSubClient mqtt;

View File

@ -608,6 +608,7 @@ void InternalDisplay::refreshPWMAdjustmentId()
this->displayAdapter->print(pmwAdjustmentPin);
this->displayAdapter->print("\"");
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) {
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 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(char*)>);
void unregisterCallback(uint8_t handler);

View File

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