oop output card display binding

This commit is contained in:
Siwat Sirichai 2023-12-30 22:50:19 +07:00
parent 59b1ade59d
commit 2d0d38ecf3
6 changed files with 355 additions and 124 deletions

View file

@ -10,6 +10,10 @@ void InternalDisplay::begin(ESPMegaIoT *iot, std::function<rtctime_t()> getRtcTi
this->networkConfig = this->iot->getNetworkConfig();
// Register callbacks
ESP_LOGD("InternalDisplay", "Registering Callbacks");
auto bindedPageChangeCallback = std::bind(&InternalDisplay::handlePageChange, this, std::placeholders::_1);
this->registerPageChangeCallback(bindedPageChangeCallback);
auto bindedTouchCallback = std::bind(&InternalDisplay::handleTouch, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
this->registerTouchCallback(bindedTouchCallback);
ESP_LOGD("InternalDisplay", "Binding Page Change Callback");
auto bindedInputStateChangeCallback = std::bind(&InternalDisplay::handleInputStateChange, this, std::placeholders::_1, std::placeholders::_2);
ESP_LOGD("InternalDisplay", "Binding Input State Change Callback");
@ -37,6 +41,7 @@ void InternalDisplay::begin(ESPMegaIoT *iot, std::function<rtctime_t()> getRtcTi
void InternalDisplay::loop() {
// Keep reading the Serial Adapter
this->recieveSerialCommand();
// Refresh the top bar every 5 seconds
static uint32_t lastTopBarRefresh;
if (millis() - lastTopBarRefresh > INTERNAL_DISPLAY_TOP_BAR_REFRESH_INTERVAL) {
@ -118,6 +123,11 @@ void InternalDisplay::refreshPage(uint8_t page) {
case INTERNAL_DISPLAY_AC_PAGE:
this->refreshAC();
break;
case INTERNAL_DISPLAY_PWM_ADJUSTMENT_PAGE:
this->refreshPWMAdjustment();
break;
default:
break;
}
}
@ -133,10 +143,11 @@ void InternalDisplay::refreshDashboard() {
sprintf(ip_address, "%d.%d.%d.%d", this->networkConfig->ip[0], this->networkConfig->ip[1], this->networkConfig->ip[2], this->networkConfig->ip[3]);
this->setString("ip_address.txt", ip_address);
// Send the MQTT server and port
this->displayAdapter->print("server_address.txt=");
this->displayAdapter->print("server_address.txt=\"");
this->displayAdapter->print(this->mqttConfig->mqtt_server);
this->displayAdapter->print(":");
this->displayAdapter->print(this->mqttConfig->mqtt_port);
this->displayAdapter->print("\"");
this->sendStopBytes();
// Send the MQTT connection status
this->setString("status_txt.txt", this->iot->mqttConnected() ? MSG_MQTT_CONNECTED : MSG_MQTT_DISCONNECTED);
@ -144,7 +155,7 @@ void InternalDisplay::refreshDashboard() {
void InternalDisplay::refreshInput() {
for (uint8_t i=0; i<16; i++) {
this->setInputMarker(i, this->inputCard->digitalRead(i));
this->setInputMarker(i, this->inputCard->digitalRead(i, false));
}
}
@ -159,7 +170,6 @@ void InternalDisplay::refreshAC() {
// TODO: implementation
}
void InternalDisplay::setPWMAdjustmentSlider(uint16_t value) {
// TODO: implementation
}
@ -200,6 +210,11 @@ void InternalDisplay::setInputMarker(uint8_t pin, bool state) {
InternalDisplay::InternalDisplay(HardwareSerial *displayAdapter) : ESPMegaDisplay(displayAdapter) {
this->currentPage = INTERNAL_DISPLAY_DASHBOARD_PAGE;
this->iot = nullptr;
this->inputCard = nullptr;
this->outputCard = nullptr;
this->climateCard = nullptr;
this->pmwAdjustmentPin = 0;
}
void InternalDisplay::bindInputCard(DigitalInputCard *inputCard) {
@ -208,4 +223,101 @@ void InternalDisplay::bindInputCard(DigitalInputCard *inputCard) {
void InternalDisplay::bindOutputCard(DigitalOutputCard *outputCard) {
this->outputCard = outputCard;
}
void InternalDisplay::bindClimateCard(ClimateCard *climateCard) {
this->climateCard = climateCard;
}
void InternalDisplay::refreshPWMAdjustment() {
// The PWM Adjustment page have the following components:
// pwm_value -> a slider to adjust the PWM value
// pwm_state -> a button to toggle the PWM state
// pwm_id -> a text to show the PWM pin
// Refresh the PWM pin
this->refreshPWMAdjustmentId();
// Refresh the PWM value
this->refreshPWMAdjustmentSlider();
// Refresh the PWM state
this->refreshPWMAdjustmentState();
}
void InternalDisplay::refreshPWMAdjustmentId() {
// Send the PWM pin
this->displayAdapter->print("pwm_id.txt=\"P");
this->displayAdapter->print(pmwAdjustmentPin);
this->displayAdapter->print("\"");
this->sendStopBytes();
}
void InternalDisplay::refreshPWMAdjustmentSlider() {
// Send the PWM value
this->displayAdapter->print("pwm_value.val=");
this->displayAdapter->print(this->outputCard->getValue(this->pmwAdjustmentPin));
this->sendStopBytes();
}
void InternalDisplay::refreshPWMAdjustmentState() {
// Send the PWM state
this->displayAdapter->print("pwm_state.txt=\"");
this->displayAdapter->print(this->outputCard->getState(this->pmwAdjustmentPin) ? MSG_PWM_ADJUSTMENT_STATE_ON : MSG_PWM_ADJUSTMENT_STATE_OFF);
this->displayAdapter->print("\"");
this->sendStopBytes();
}
void InternalDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t type) {
// Switch based on the page
switch (page) {
case INTERNAL_DISPLAY_AC_PAGE:
this->handleACTouch(type, component);
break;
case INTERNAL_DISPLAY_PWM_ADJUSTMENT_PAGE:
this->handlePWMAdjustmentTouch(type, component);
break;
default:
break;
}
}
void InternalDisplay::handleACTouch(uint8_t type, uint8_t component) {
}
void InternalDisplay::handlePWMAdjustmentTouch(uint8_t type, uint8_t component) {
// b0 [component 5] -> decrement the PWM id if its greater than 0, else set it to 15
// b1 [component 6] -> increment the PWM id if its less than 15, else set it to 0
// pwm_state [component 4] -> toggle the PWM state
// pwm_value [component 1] -> set the PWM value based on the slider value
// If the type is not release then return
if (type != TOUCH_TYPE_RELEASE) return;
uint16_t val = 0;
// switch based on the component
switch (component) {
case 5:
// Decrement the PWM id
this->pmwAdjustmentPin = this->pmwAdjustmentPin > 0 ? this->pmwAdjustmentPin - 1 : 15;
this->refreshPWMAdjustment();
break;
case 6:
// Increment the PWM id
this->pmwAdjustmentPin = this->pmwAdjustmentPin < 15 ? this->pmwAdjustmentPin + 1 : 0;
this->refreshPWMAdjustment();
break;
case 4:
// Toggle the PWM state
this->outputCard->setState(this->pmwAdjustmentPin, !this->outputCard->getState(this->pmwAdjustmentPin));
this->refreshPWMAdjustmentState();
break;
case 1:
// Set the PWM value
val = (uint16_t)this -> getNumber("pwm_value.val");
this->outputCard->setValue(this->pmwAdjustmentPin, val);
break;
default:
break;
}
}