This commit is contained in:
Siwat Sirichai 2024-03-17 16:12:43 +07:00
parent 202e49f6cf
commit baf808bf26
6 changed files with 200 additions and 3 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"editor.tokenColorCustomizations": {
"comments": "",
"textMateRules": []
}
}

142
src/cud_display.cpp Normal file
View File

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

47
src/cud_display.hpp Normal file
View File

@ -0,0 +1,47 @@
#include <InternalDisplay.hpp>
#include <lcd_elements.hpp>
#include <ClimateCard.hpp>
#include <DigitalInputCard.hpp>
#include <DigitalOutputCard.hpp>
#include <RemoteVariable.hpp>
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;
};

View File

@ -1,4 +1,4 @@
#include <ir_codes.hpp>
#include "ir_codes.hpp"
const uint16_t ir_code_cool[4][17][407] = {
// Fan Speed Auto

View File

@ -0,0 +1,2 @@
#define LCD_PAGE_ID_BOOT 0
#define LCD_PAGE_ID_DASHBOARD 1

View File

@ -1,9 +1,9 @@
#include <ESPMegaProOS.hpp>
#include <lcd_elements.hpp>
#include <ir_codes.hpp>
#include <ClimateCard.hpp>
#include <ETH.h>
#include <RemoteVariable.hpp>
#include "lcd_elements.hpp"
#include "ir_codes.hpp"
/***********************************************
* Pin Definitions *