address some TODO and modify standby behavior

This commit is contained in:
reaw55 2024-02-04 05:19:26 +07:00
parent ee230bb3be
commit ab759ef08e
5 changed files with 466 additions and 303 deletions

View file

@ -1,9 +1,20 @@
#include <ise_display.hpp>
ISEDisplay::ISEDisplay(HardwareSerial *adapter) : ESPMegaDisplay(adapter)
{
}
// sat->sun
// TODO : Implement
// debug to complie
// standby
// dimable
// sun -> mon
// debug to work
// appdeamon send data / receive data
// sound
// mon -> tue
// slider
void ISEDisplay::begin(DigitalInputCard *inputCard, DigitalOutputCard *outputCard, ClimateCard *climateCard)
{
this->inputCard = inputCard;
@ -14,16 +25,35 @@ void ISEDisplay::begin(DigitalInputCard *inputCard, DigitalOutputCard *outputCar
auto bindedHandleTouch = std::bind(&ISEDisplay::handleTouch, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
this->outputCallbackHandle = this->outputCard->registerChangeCallback(bindedHandlePWMChange);
this->climateCallbackHandle = this->climateCard->registerChangeCallback(bindedHandleACChange);
this->user_mode = 1; // set to cool by default
this->user_mode = 1; // initialized to cool by default
this->time_since_last_screen_update = 0;
this->registerTouchCallback(bindedHandleTouch);
this->reset();
delay(1000);
//TODO : Will the light be on or off when the system is started?, You need to jump to its respective page
this->jumpToPage(1);
// TODO : Will the light be on or off when the system is started?, You need to jump to its respective page
// first jump to main then if no activity jump to standby
this->jumpToPage(2);
delay(100);
this->updateLightGroupState();
this->updateAirPurifierState();
this->updateACState();
this->updateLightGroupStatePageDashboard();
this->updateLightGroupStatePageStandby();
}
void ISEDisplay::loop()
{
// Check if there is data in the serial buffer
// If there is data, process the data
recieveSerialCommand();
// Update the time since the last screen update using millis()
u_int32_t current_time = millis();
if (current_time - this->time_since_last_screen_update > 120000)
{
// jump to standby page if there is no activity for 2 minutes
this->jumpToPage(1);
}
}
void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type)
@ -35,29 +65,31 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type
case COMPONENT_STANDBY_OPEN_ALL_TOGGLE:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
//TODO : Should you really jump to page 2 here? should't page jumping be handled reactivly?
// EX. if atleast one light is on, then jump to active page, else jump to standby page
// This will allow page to change correctly when the system is started and when controlled remotely which won't call handleTouch
// TODO : Should you really jump to page 2 here? should't page jumping be handled reactivly?
// EX. if atleast one light is on, then jump to active page, else jump to standby page
// This will allow page to change correctly when the system is started and when controlled remotely which won't call handleTouch
this->jumpToPage(2);
// the function of the button is to open the dashboard from standby
break;
case COMPONENT_STANDBY_LIGHT_TOGGLE:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
//TODO : So this does nothing? Shouldn't it turn on the lights?
updateLightGroupStatePageStandby();
// TODO : So this does nothing? Shouldn't it turn on the lights?
// should turn it on now
toggleLightGroupState();
break;
case COMPONENT_STANDBY_AC_TOGGLE:
//TODO : What's the expexted behavior of standby? Does it enter standby when the lights are all off and ignore the AC
// or does it only enter standby when ac is also off and purifier is off, or is it a timed thing?
// TODO : What's the expexted behavior of standby? Does it enter standby when the lights are all off and ignore the AC
// or does it only enter standby when ac is also off and purifier is off, or is it a timed thing?
if (touch_type != TOUCH_TYPE_RELEASE)
break;
this->climateCard->setMode(this->climateCard->getMode() == 0 ? 1 : 0);
// need fix
toggleAC();
break;
case COMPONENT_STANDBY_PM_TOGGLE:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
this->outputCard->setState(5, !this->outputCard->getState(5));
togglePM();
break;
default:
break;
@ -66,15 +98,18 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type
else if (page == PAGE_DASHBOARD)
{
u_int8_t pm_fan_speed = 0;
u_int8_t fan_speed = 0;
switch (component)
{
case COMPONENT_LIGHT_MASTER_BUTTON:
//TODO : this only update the display to match the light, it doesn't toggle the light.
// TODO : this only update the display to match the light, it doesn't toggle the light.
if (touch_type != TOUCH_TYPE_RELEASE)
break;
updateLightGroupStatePageDashboard();
toggleLightGroupState();
break;
//TODO : can't this be done better with array lookup?
// TODOlater : can't this be done better with array lookup?
case COMPONENT_LIGHT_ROW1_LEVEL0_TOUCHPOINT:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
@ -159,8 +194,7 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type
case COMPONENT_AC_TOGGLE_BUTTON:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
this->climateCard->setMode(this->climateCard->getMode() == 0 ? 1 : 0);
// need fix
toggleAC();
break;
case COMPONENT_AC_MODE:
if (touch_type != TOUCH_TYPE_RELEASE)
@ -171,9 +205,10 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type
case COMPONENT_AC_FAN_SPEED:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
uint8_t fan_speed = this->climateCard->getFanSpeed();
fan_speed = this->climateCard->getFanSpeed();
// TODO : We have auto, low, mid, high right?, that's 0,1,2,3 a modulo operation of 3 only gives 0,1,2
this->climateCard->setFanSpeed((fan_speed + 1) % 3);
// mod 4 should fixed it
this->climateCard->setFanSpeed((fan_speed + 1) % 4);
break;
case COMPONENT_AC_TEMP_DOWN_BUTTON:
if (touch_type != TOUCH_TYPE_RELEASE)
@ -193,16 +228,16 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type
case COMPONENT_PM_FAN_SPEED_DECREASE:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
uint8_t pm_fan_speed = this->outputCard->getState(6);
pm_fan_speed = this->outputCard->getValue(6);
if (pm_fan_speed >= 0 && pm_fan_speed <= 20)
this->outputCard->setState(5, (pm_fan_speed - 1));
this->outputCard->setValue(5, (pm_fan_speed - 1));
break;
case COMPONENT_PM_FAN_SPEED_INCREASE:
if (touch_type != TOUCH_TYPE_RELEASE)
break;
uint8_t pm_fan_speed = this->outputCard->getState(6);
pm_fan_speed = this->outputCard->getValue(6);
if (pm_fan_speed >= 0 && pm_fan_speed <= 20)
this->outputCard->setState(5, (pm_fan_speed + 1));
this->outputCard->setValue(5, (pm_fan_speed + 1));
break;
default:
break;
@ -217,16 +252,23 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type
void ISEDisplay::handlePWMChange(uint8_t pin, bool state, uint16_t value)
{
// NEED to switch case for different page
// Question: can add another input and placeholder for page differentation
// or that would mean changing the data structure of the callback
if (pin >= 1 && pin <= 4)
{
// Light
updateLightGroupStatePageDashboard();
updateLightGroupStatePageStandby();
time_since_last_screen_update = millis(); // update time since last activity
}
else if (pin == 0)
else if (pin == 4 || pin == 5)
{
// Air Purifier
updateAirPurifierState();
time_since_last_screen_update = millis(); // update time since last activity
}
}
void ISEDisplay::updateDateTimeText(rtctime_t time)
@ -249,6 +291,8 @@ void ISEDisplay::updateDateTimeText(rtctime_t time)
this->giveSerialMutex();
}
// TODO : Implement
// user remote var
// appdeamon
void ISEDisplay::updateWeather(uint8_t weather_code, float outside_temp)
{
}
@ -265,9 +309,66 @@ void ISEDisplay::setPMstate(bool is_pm_on, uint8_t pm_fan_speed)
void ISEDisplay::setACstate(uint8_t ac_fan_speed, uint8_t ac_mode, uint8_t ac_temperature)
{
this->climateCard->setTemperature(ac_temperature);
this->climateCard->setFanSpeed(ac_fan_speed);
this->climateCard->setMode(ac_mode);
updateACState();
}
void ISEDisplay::toggleLightGroupState()
{
// Get the current group state
bool state = calculateLightGroupState();
// Toggle light
for (uint8_t i = 1; i <= 4; i++)
{
setLightLevel(i, state ? 0 : 3);
}
}
void ISEDisplay::togglePM()
{
// Get the current group state
bool state = this->outputCard->getState(5);
// Toggle the state
state = !state;
// Set the state
this->outputCard->setState(5, state);
}
void ISEDisplay::toggleAC()
{
// Get the current group state
uint8_t mode = this->climateCard->getMode();
// Toggle the state
if (mode == 0)
{
this->climateCard->setMode(user_mode);
}
else
{
// update user mode to new mode
user_mode = mode;
// change actual mode to off
this->climateCard->setMode(0);
}
}
void ISEDisplay::changeUserACmode()
{
// Get the current group state
uint8_t mode = this->climateCard->getMode();
// Toggle the state
// user mode alternate between 1 and 2
user_mode = (user_mode + 1) % 2 + 1;
if (mode != 0)
{
// update mode to new mode
mode = user_mode;
}
else
{ // ie mode is off
// do nothing as the state is keep in user_mode
// the mode will change to user_mode when turn on by toggleAC()
}
updateuserACmode(); // call to update mode part of the display seperately
}
void ISEDisplay::setLightLevel(uint8_t row, uint8_t level)
{
for (uint8_t i = 1; i <= 4; i++)
@ -390,51 +491,8 @@ void ISEDisplay::updateAirPurifierState()
this->giveSerialMutex();
}
void ISEDisplay::updateACState()
void ISEDisplay::updateuserACmode()
{
//TODO : The cognitive complexity here is so high, maybe break up the method a bit?
// Get the state
uint8_t mode = this->climateCard->getMode();
if (mode == 0)
{
}
uint8_t fan_speed = this->climateCard->getFanSpeed();
uint8_t temperature = this->climateCard->getTemperature();
this->takeSerialMutex();
// Send the state to the display
if (mode == 0)
{
this->displayAdapter->print("ac_temp.pco=");
this->displayAdapter->print(33841);
this->sendStopBytes();
this->displayAdapter->print("ac_temp.pic=");
this->displayAdapter->print(COMPONENT_AC_STATUS_OFF);
this->sendStopBytes();
}
else
{
this->displayAdapter->print("ac_temp.pco=");
this->displayAdapter->print(34486);
this->sendStopBytes();
this->displayAdapter->print("ac_temp.pic=");
this->displayAdapter->print(COMPONENT_AC_STATUS_ON);
this->sendStopBytes();
user_mode = mode;
}
this->displayAdapter->print("ac_sw.pic=");
this->displayAdapter->print(mode != 0 ? COMPONENT_AC_TOGGLE_PIC_ON : COMPONENT_AC_TOGGLE_PIC_OFF);
this->sendStopBytes();
this->displayAdapter->print("ac_sw.pic2=");
this->displayAdapter->print(mode != 0 ? COMPONENT_AC_TOGGLE_PIC_ON_PRESSED : COMPONENT_AC_TOGGLE_PIC_OFF_PRESSED);
this->sendStopBytes();
switch (user_mode)
{
case 1:
@ -456,6 +514,10 @@ void ISEDisplay::updateACState()
default:
break;
}
}
void ISEDisplay::updateACfanSpeed()
{
uint8_t fan_speed = this->climateCard->getFanSpeed();
switch (fan_speed)
{
case 0:
@ -493,6 +555,50 @@ void ISEDisplay::updateACState()
default:
break;
}
}
void ISEDisplay::updateACState()
{
// TODOlater : The cognitive complexity here is so high, maybe break up the method a bit?
// Get the state
uint8_t mode = this->climateCard->getMode();
uint8_t temperature = this->climateCard->getTemperature();
this->takeSerialMutex();
// Send the state to the display
if (mode == 0)
{
this->displayAdapter->print("ac_temp.pco=");
this->displayAdapter->print(33841);
this->sendStopBytes();
this->displayAdapter->print("ac_temp.pic=");
this->displayAdapter->print(COMPONENT_AC_STATUS_OFF);
this->sendStopBytes();
}
else
{
this->displayAdapter->print("ac_temp.pco=");
this->displayAdapter->print(34486);
this->sendStopBytes();
this->displayAdapter->print("ac_temp.pic=");
this->displayAdapter->print(COMPONENT_AC_STATUS_ON);
this->sendStopBytes();
user_mode = mode;
}
this->displayAdapter->print("ac_sw.pic=");
this->displayAdapter->print(mode != 0 ? COMPONENT_AC_TOGGLE_PIC_ON : COMPONENT_AC_TOGGLE_PIC_OFF);
this->sendStopBytes();
this->displayAdapter->print("ac_sw.pic2=");
this->displayAdapter->print(mode != 0 ? COMPONENT_AC_TOGGLE_PIC_ON_PRESSED : COMPONENT_AC_TOGGLE_PIC_OFF_PRESSED);
this->sendStopBytes();
updateuserACmode();
updateACfanSpeed();
this->displayAdapter->print("ac_temp.txt=\"");
this->displayAdapter->print(temperature);