diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..926d143 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "editor.tokenColorCustomizations": { + "comments": "", + "textMateRules": [] + } +} \ No newline at end of file diff --git a/src/cud_display.cpp b/src/cud_display.cpp new file mode 100644 index 0000000..041dc38 --- /dev/null +++ b/src/cud_display.cpp @@ -0,0 +1,142 @@ +#include "cud_display.hpp" + +CUDDisplay::CUDDisplay(cud_display_conf_t *conf) : ESPMegaDisplay(conf->uart, conf->communication_baudrate, conf->ota_baudrate, conf->tx, conf->rx) +{ + this->conf = conf; +} + +void CUDDisplay::begin() +{ + // Register callbacks + auto binded_input_callback = std::bind(&CUDDisplay::handle_input_change, this, std::placeholders::_1, std::placeholders::_2); + 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); + 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); + // Initialize the display + this->display_init(); +} + +void CUDDisplay::display_init() { + // Perform a reset on the display + this->reset(); + // Set the display to the main screen + this->jumpToPage(LCD_PAGE_ID_DASHBOARD); + // Send the initial states to the display +} + +bool CUDDisplay::get_lights_state() +{ + for (int i = 0; i < 4; i++) + { + if (this->conf->outputCard->getState(this->conf->light_pins[i])) + return true; + } + return false; +} + +bool CUDDisplay::get_fans_state() +{ + for (int i = 0; i < 3; i++) + { + if (this->conf->outputCard->getState(this->conf->fan_pins[i])) + return true; + } + return false; +} + +void CUDDisplay::handle_input_change(uint8_t pin, bool state) +{ + // This is not needed for now. We are only handling outputs + // Putting this here in case we need to handle inputs in the future +} + +void CUDDisplay::handle_output_change(uint8_t pin, bool state) +{ + // Check if it is a light or fan + // If it's a light, call set_display_light_state + // If it's a fan, call set_display_fan_state + // If it's the air purifier, call set_display_air_purifier_state + // If it's the mosquito zapper, call set_display_mosquito_zapper_state + + // Check if it's the air purifier + if (pin == this->conf->air_purifier_pin) + { + this->set_display_air_purifier_state(state); + return; + } + + // Check if it's the mosquito zapper + if (pin == this->conf->mosquito_zapper_pin) + { + this->set_display_mosquito_zapper_state(state); + return; + } + + // Loop through each to check if the pin is a light + for (int i = 0; i < 4; i++) + { + if (pin == this->conf->light_pins[i]) + { + this->set_display_light_state(i, state); + return; + } + } + // Loop through each to check if the pin is a fan + for (int i = 0; i < 3; i++) + { + if (pin == this->conf->fan_pins[i]) + { + this->set_display_fan_state(i, state); + return; + } + } + // The pin 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 + float aqi = atof(value); + // TODO - Update the AQI value on the display +} + +/** + * @brief Handle the payload from the display that is not handled by the parent class + * + * @param type Payload type + * @param payload The payload itself + * @param length The length of the payload excluding the type + */ +void CUDDisplay::handle_payload(uint8_t type, uint8_t *payload, uint8_t length) +{ + // If payload of type 0x92 is received + // Reset the display and reinitialize it + if (type == 0x92) + { + this->display_init(); + } +} + +void CUDDisplay::set_display_light_state(uint8_t row, bool state) +{ + // set_display_light_state implementation +} + +void CUDDisplay::set_display_fan_state(uint8_t row, bool state) +{ + // set_display_fan_state implementation +} + +void CUDDisplay::set_display_mosquito_zapper_state(bool state) +{ + // set_display_mosquito_zapper_state implementation +} + +void CUDDisplay::set_display_air_purifier_state(bool state) +{ + // set_display_air_purifier_state implementation +} \ No newline at end of file diff --git a/src/cud_display.hpp b/src/cud_display.hpp new file mode 100644 index 0000000..a9ecf19 --- /dev/null +++ b/src/cud_display.hpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include + +struct cud_display_conf_t +{ + RemoteVariable *aqi; + DigitalInputCard *inputCard; + DigitalOutputCard *outputCard; + ClimateCard *ac; + HardwareSerial *uart; + const int tx; + const int rx; + const uint32_t communication_baudrate; + const uint32_t ota_baudrate; + uint8_t light_pins[4]; + uint8_t fan_pins[3]; + uint8_t air_purifier_pin; + uint8_t mosquito_zapper_pin; +}; + +class CUDDisplay : public ESPMegaDisplay +{ + public: + CUDDisplay(cud_display_conf_t *conf); + void begin(); + void display_init(); + private: + // States Calculation + bool get_lights_state(); + bool get_fans_state(); + // Callbacks + void handle_input_change(uint8_t pin, bool state); + 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); + // Change Display Elements + void set_display_light_state(uint8_t row, bool state); + void set_display_fan_state(uint8_t row, bool state); + void set_display_mosquito_zapper_state(bool state); + void set_display_air_purifier_state(bool state); + // Local Variables + cud_display_conf_t *conf; +}; \ No newline at end of file diff --git a/src/ir_codes.cpp b/src/ir_codes.cpp index f934419..c704a8d 100644 --- a/src/ir_codes.cpp +++ b/src/ir_codes.cpp @@ -1,4 +1,4 @@ -#include +#include "ir_codes.hpp" const uint16_t ir_code_cool[4][17][407] = { // Fan Speed Auto diff --git a/src/lcd_elements.hpp b/src/lcd_elements.hpp index e69de29..90acee7 100644 --- a/src/lcd_elements.hpp +++ b/src/lcd_elements.hpp @@ -0,0 +1,2 @@ +#define LCD_PAGE_ID_BOOT 0 +#define LCD_PAGE_ID_DASHBOARD 1 \ No newline at end of file diff --git a/src/main.hpp b/src/main.hpp index 8e06b7b..a8e28f5 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -1,9 +1,9 @@ #include -#include -#include #include #include #include +#include "lcd_elements.hpp" +#include "ir_codes.hpp" /*********************************************** * Pin Definitions *