From 73425cb438c69c6d421a1c99e102c3f6987dbaf9 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Mon, 18 Mar 2024 02:24:17 +0700 Subject: [PATCH] touch handling --- src/cud_display.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ src/cud_display.hpp | 1 + 2 files changed, 66 insertions(+) diff --git a/src/cud_display.cpp b/src/cud_display.cpp index 2fc851c..1489fb9 100644 --- a/src/cud_display.cpp +++ b/src/cud_display.cpp @@ -29,10 +29,12 @@ void CUDDisplay::begin() auto binded_output_callback = std::bind(&CUDDisplay::handle_output_change, this, std::placeholders::_1, std::placeholders::_2); auto binded_aqi_callback = std::bind(&CUDDisplay::handle_aqi_change, this, std::placeholders::_1); auto binded_payload_callback = std::bind(&CUDDisplay::handle_payload, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + auto binded_touch_callback = std::bind(&CUDDisplay::handle_touch, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); this->conf->inputCard->registerCallback(binded_input_callback); this->conf->outputCard->registerChangeCallback(binded_output_callback); this->conf->aqi->registerCallback(binded_aqi_callback); this->registerPayloadCallback(binded_payload_callback); + this->registerTouchCallback(binded_touch_callback); // Initialize the display this->display_init(); } @@ -99,6 +101,7 @@ void CUDDisplay::handle_output_change(uint8_t pin, bool state) if (pin == this->conf->light_pins[i]) { this->set_display_light_state(i, state); + this->set_display_light_all_state(); return; } } @@ -108,12 +111,74 @@ void CUDDisplay::handle_output_change(uint8_t pin, bool state) if (pin == this->conf->fan_pins[i]) { this->set_display_fan_state(i, state); + this->set_display_fan_all_state(); return; } } // The pin is not the one that our display is handling, so we can ignore it } +void CUDDisplay::handle_touch(uint8_t page_id, uint8_t element_id, uint8_t touch_type) +{ + // Check if element_id is the light all button + if (element_id == LCD_DASHBOARD_ELEMENT_LIGHT_ALL) + { + // If it is, toggle all the lights + bool light_on = this->get_lights_state(); + for (int i = 0; i < 4; i++) + { + this->conf->outputCard->setState(this->conf->light_pins[i], !light_on); + } + return; + } + // Check if element_id is the fan all button + else if (element_id == LCD_DASHBOARD_ELEMENT_FAN_ALL) + { + // If it is, toggle all the fans + bool fan_on = this->get_fans_state(); + for (int i = 0; i < 3; i++) + { + this->conf->outputCard->setState(this->conf->fan_pins[i], !fan_on); + } + return; + } + // Check if element_id is the air purifier button + else if (element_id == LCD_DASHBOARD_ELEMENT_AIR_PURIFIER) + { + // If it is, toggle the air purifier + this->conf->outputCard->setState(this->conf->air_purifier_pin, !this->conf->outputCard->getState(this->conf->air_purifier_pin)); + return; + } + // Check if element_id is the mosquito zapper button + else if (element_id == LCD_DASHBOARD_ELEMENT_MOSQUITO_ZAPPER) + { + // If it is, toggle the mosquito zapper + this->conf->outputCard->setState(this->conf->mosquito_zapper_pin, !this->conf->outputCard->getState(this->conf->mosquito_zapper_pin)); + return; + } + // Check if element_id is a light button + for (int i = 0; i < 4; i++) + { + if (element_id == this->light_group.element_id[i]) + { + // If it is, toggle the light + this->conf->outputCard->setState(this->conf->light_pins[i], !this->conf->outputCard->getState(this->conf->light_pins[i])); + return; + } + } + // Check if element_id is a fan button + for (int i = 0; i < 3; i++) + { + if (element_id == this->fan_group.element_id[i]) + { + // If it is, toggle the fan + this->conf->outputCard->setState(this->conf->fan_pins[i], !this->conf->outputCard->getState(this->conf->fan_pins[i])); + return; + } + } + // The element_id is not the one that our display is handling, so we can ignore it +} + void CUDDisplay::handle_aqi_change(char *value) { // Update the AQI value on the display diff --git a/src/cud_display.hpp b/src/cud_display.hpp index 3dbbe04..6abe390 100644 --- a/src/cud_display.hpp +++ b/src/cud_display.hpp @@ -57,6 +57,7 @@ class CUDDisplay : public ESPMegaDisplay void handle_output_change(uint8_t pin, bool state); void handle_aqi_change(char *value); void handle_payload(uint8_t type, uint8_t* payload, uint8_t length); + void handle_touch(uint8_t page_id, uint8_t element_id, uint8_t touch_type); // Change Display Elements void set_display_light_state(uint8_t row, bool state); void set_display_light_all_state();