Update display.cpp

This commit is contained in:
Siwat Sirichai 2024-03-30 23:07:12 +07:00
parent b5b0c866f8
commit b4afcd58f8
1 changed files with 19 additions and 6 deletions

View File

@ -125,7 +125,6 @@ void CUDDisplay::begin(cud_display_cards_t cards)
sprintf(ac_temp_lower_bound_request_topic, "%s%s/request", base_topic, AC_TEMP_LOWER_BOUND_RELATIVE_TOPIC);
this->ac_temp_lower_bound.enableValueRequest(ac_temp_lower_bound_request_topic);
auto binded_ac_temp_lower_bound_callback = std::bind(&CUDDisplay::handle_bound_change, this, std::placeholders::_1);
this->ac_temp_lower_bound.registerCallback(binded_ac_temp_lower_bound_callback);
// AC Temp Upper Bound
this->ac_temp_upper_bound.begin(6);
this->ac_temp_upper_bound.bindFRAM(this->cards.fram, AC_DISPLAY_TEMP_UPPER_BOUND_ADDR);
@ -140,7 +139,7 @@ void CUDDisplay::begin(cud_display_cards_t cards)
sprintf(ac_temp_upper_bound_request_topic, "%s%s/request", base_topic, AC_TEMP_UPPER_BOUND_RELATIVE_TOPIC);
this->ac_temp_upper_bound.enableValueRequest(ac_temp_upper_bound_request_topic);
auto binded_ac_temp_upper_bound_callback = std::bind(&CUDDisplay::handle_bound_change, this, std::placeholders::_1);
this->ac_temp_upper_bound.registerCallback(binded_ac_temp_upper_bound_callback);
// Check Bound Validity
// Lower Bound must be less than Upper Bound
@ -154,6 +153,9 @@ void CUDDisplay::begin(cud_display_cards_t cards)
this->ac_temp_upper_bound.setIntValue(DEFAULT_TEMP_UPPER_BOUND);
}
this->ac_temp_lower_bound.registerCallback(binded_ac_temp_lower_bound_callback);
this->ac_temp_upper_bound.registerCallback(binded_ac_temp_upper_bound_callback);
// Initialize the display
ESP_LOGV("CUD Display", "Initializing display");
this->display_init();
@ -540,15 +542,26 @@ void CUDDisplay::handle_bound_change(char *value)
// Lower Bound can't be less than AC_MIN_TEMP
// Upper Bound can't be more than AC_MAX_TEMP
// If any of the bound is invalid, set all to default
if (this->ac_temp_lower_bound.getIntValue() < AC_MIN_TEMP || this->ac_temp_lower_bound.getIntValue() >= this->ac_temp_upper_bound.getIntValue())
// Check if the lower bound is less than AC_MIN_TEMP or greater than or equal to the upper bound
// Check if the lower bound is less than AC_MIN_TEMP
if (this->ac_temp_lower_bound.getIntValue() < AC_MIN_TEMP)
{
ESP_LOGV("CUD Display", "Invalid Lower Bound, Setting to Default");
ESP_LOGV("CUD Display", "Invalid Lower Bound %d Setting to Default", this->ac_temp_lower_bound.getIntValue());
this->ac_temp_lower_bound.setIntValue(DEFAULT_TEMP_LOWER_BOUND);
}
if (this->ac_temp_upper_bound.getIntValue() > AC_MAX_TEMP || this->ac_temp_lower_bound.getIntValue() >= this->ac_temp_upper_bound.getIntValue())
// Check if the upper bound is greater than AC_MAX_TEMP
if (this->ac_temp_upper_bound.getIntValue() > AC_MAX_TEMP)
{
ESP_LOGV("CUD Display", "Invalid Upper Bound, Setting to Default");
ESP_LOGV("CUD Display", "Invalid Upper Bound %d Setting to Default", this->ac_temp_upper_bound.getIntValue());
this->ac_temp_upper_bound.setIntValue(DEFAULT_TEMP_UPPER_BOUND);
}
// After correcting invalid lower or upper bounds, check if lower bound is still greater than or equal to upper bound
if (this->ac_temp_lower_bound.getIntValue() >= this->ac_temp_upper_bound.getIntValue())
{
ESP_LOGV("CUD Display", "Lower Bound %d is greater than Upper Bound %d. Setting both to defaults", this->ac_temp_lower_bound.getIntValue(), this->ac_temp_upper_bound.getIntValue());
this->ac_temp_lower_bound.setIntValue(DEFAULT_TEMP_LOWER_BOUND);
this->ac_temp_upper_bound.setIntValue(DEFAULT_TEMP_UPPER_BOUND);
}
// Does the current temperature exceed the new upper bound?, if it does, bound it