change callbacks to vector

This commit is contained in:
Siwat Sirichai 2023-12-30 00:49:09 +07:00
parent 71475ef2f1
commit 011710fe82
19 changed files with 207 additions and 109 deletions

View file

@ -1,31 +1,39 @@
#include <InternalDisplay.hpp>
void InternalDisplay::begin() {
void InternalDisplay::begin(ESPMegaIoT *iot, std::function<rtctime_t()> getRtcTime) {
this->iot = iot;
this->getRtcTime = getRtcTime;
this->mqttConfig = this->iot->getMqttConfig();
this->networkConfig = this->iot->getNetworkConfig();
// Register callbacks
this->inputCard->registerCallback(std::bind(&InternalDisplay::handleInputStateChange, this, std::placeholders::_1, std::placeholders::_2));
this->outputCard->registerChangeCallback(std::bind(&InternalDisplay::handlePwmStateChange, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}
void InternalDisplay::loop() {
// TODO: implementation
}
void InternalDisplay::bindInputCard(uint8_t card_id) {
this->bindedInputCard = card_id;
if(currentPage == INTERNAL_DISPLAY_INPUT_PAGE) {
refreshInput();
// 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) {
this->updateStatusIcons(this->iot->networkConnected(), this->iot->mqttConnected());
lastTopBarRefresh = millis();
}
// Refresh the clock every 10 seconds
static uint32_t lastClockRefresh;
if (millis() - lastClockRefresh > INTERNAL_DISPLAY_CLOCK_REFRESH_INTERVAL) {
this->updateClock();
lastClockRefresh = millis();
}
}
void InternalDisplay::bindOutputCard(uint8_t card_id) {
this->bindedOutputCard = card_id;
if(currentPage == INTERNAL_DISPLAY_OUTPUT_PAGE) {
refreshOutput();
}
}
void InternalDisplay::handleInputStateChange(uint8_t pin, bool state) {
// If the input card is binded to the display and the current page is the input page
// then update the respective input component
if (!this->bindedInputCard || this->currentPage != INTERNAL_DISPLAY_INPUT_PAGE) return;
if (this->inputCard!=nullptr || this->currentPage != INTERNAL_DISPLAY_INPUT_PAGE) return;
// Update the input state
this->setInputMarker(pin, state);
}
@ -33,7 +41,7 @@ void InternalDisplay::handleInputStateChange(uint8_t pin, bool state) {
void InternalDisplay::handlePwmStateChange(uint8_t pin, bool state, uint16_t value) {
// If the output card is binded to the display and the current page is the output page
// then update the respective output component
if (!this->bindedOutputCard || this->currentPage != INTERNAL_DISPLAY_OUTPUT_PAGE) return;
if (this->outputCard!=nullptr || this->currentPage != INTERNAL_DISPLAY_OUTPUT_PAGE) return;
// Update the output state
this->setOutputBar(pin, value);
this->setOutputStateColor(pin, state);
@ -96,29 +104,38 @@ void InternalDisplay::refreshDashboard() {
// 2. IP Address
// 3. MQTT Server with port
// 4. MQTT Connection status
this->iot->
this->setString("hostname.txt", this->networkConfig->hostname);
// Construct the IP address string
static char ip_address[25];
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(this->mqttConfig->mqtt_server);
this->displayAdapter->print(":");
this->displayAdapter->print(this->mqttConfig->mqtt_port);
this->sendStopBytes();
// Send the MQTT connection status
this->setString("status_txt.txt", this->iot->mqttConnected() ? MSG_MQTT_CONNECTED : MSG_MQTT_DISCONNECTED);
}
void InternalDisplay::refreshInput() {
// TODO: implementation
for (uint8_t i=0; i<16; i++) {
this->setInputMarker(i, this->inputCard->digitalRead(i));
}
}
void InternalDisplay::refreshOutput() {
// TODO: implementation
for (uint8_t i=0; i<16; i++) {
this->setOutputBar(i, this->outputCard->getValue(i));
this->setOutputStateColor(i, this->outputCard->getState(i));
}
}
void InternalDisplay::refreshAC() {
// TODO: implementation
}
void InternalDisplay::setOutputBar(uint8_t pin, uint16_t value) {
// Write the value to the output bar
this->displayAdapter->print("j");
this->displayAdapter->print(pin);
this->displayAdapter->print(".val=");
this->displayAdapter->print(value);
this->sendStopBytes();
}
void InternalDisplay::setPWMAdjustmentSlider(uint16_t value) {
// TODO: implementation
@ -132,16 +149,32 @@ void InternalDisplay::setPWMAdjustmentButton(bool state) {
// TODO: implementation
}
void InternalDisplay::setOutputBar(uint8_t pin, uint16_t value) {
// Write the value to the output bar
this->displayAdapter->print("j");
this->displayAdapter->print(pin);
this->displayAdapter->print(".val=");
this->displayAdapter->print((int)(value*100/4095));
this->sendStopBytes();
}
void InternalDisplay::setOutputStateColor(uint8_t pin, bool state) {
// TODO: implementation
this->displayAdapter->print("j");
this->displayAdapter->print(pin);
this->displayAdapter->print(".ppic=");
this->displayAdapter->print(state ? PIC_PWM_BAR_ON : PIC_PWM_BAR_OFF);
this->sendStopBytes();
}
void InternalDisplay::setInputMarker(uint8_t pin, bool state) {
// TODO: implementation
this->displayAdapter->print("I");
this->displayAdapter->print(pin);
this->displayAdapter->print(".val=");
this->displayAdapter->print(state ? 0:1);
this->sendStopBytes();
}
InternalDisplay::InternalDisplay(HardwareSerial *displayAdapter) : ESPMegaDisplay(displayAdapter) {
this->currentPage = INTERNAL_DISPLAY_DASHBOARD_PAGE;
this->bindedInputCard = 0;
this->bindedOutputCard = 0;
}