diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp index aeec23a..2311e24 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp @@ -5,7 +5,8 @@ AnalogCard::AnalogCard() : dac0(DAC0_ADDRESS), dac2(DAC2_ADDRESS), dac3(DAC3_ADDRESS), analogInputBankA(), - analogInputBankB() + analogInputBankB(), + dac_change_callbacks() { } diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp index 8c4324f..23fbfba 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogIoT.cpp @@ -1,6 +1,6 @@ #include -AnalogIoT::AnalogIoT() { +AnalogIoT::AnalogIoT() : adc_conversion_callbacks() { for (uint8_t i = 0; i < 8; i++) { adc_publish_enabled[i] = false; adc_conversion_interval[i] = 1000; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp index a29664f..e5fc47d 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp @@ -19,6 +19,9 @@ ClimateCard::ClimateCard(uint8_t ir_pin) this->state.ac_temperature = 25; this->state.ac_mode = 0; this->state.ac_fan_speed = 0; + // Initialize callbacks + this->callbacks = std::vector>(); + this->sensor_callbacks = std::vector>(); // Initialize RMT gpio_num_t gpio_num = gpio_num_t(ir_pin); rmt_config_t rmt_tx = RMT_DEFAULT_CONFIG_TX(gpio_num, RMT_TX_CHANNEL); diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp index 08b95f9..8098760 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -1,7 +1,7 @@ #include // Instantiate the card with the specified address -DigitalInputCard::DigitalInputCard(uint8_t address_a, uint8_t address_b) +DigitalInputCard::DigitalInputCard(uint8_t address_a, uint8_t address_b) : callbacks() { this->address_a = address_a; this->address_b = address_b; @@ -158,7 +158,9 @@ uint8_t DigitalInputCard::getInputBufferB() // Register a callback function to be called when a pin changes void DigitalInputCard::registerCallback(std::function callback) { + Serial.println("Pushing back on Input Card Callbacks"); callbacks.push_back(callback); + Serial.println("Pushed back on Input Card Callbacks"); } // Refresh the input buffer for bank A diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp index fabf1ec..7ff3a0f 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputCard.cpp @@ -1,6 +1,6 @@ #include -DigitalOutputCard::DigitalOutputCard(uint8_t address) { +DigitalOutputCard::DigitalOutputCard(uint8_t address) : change_callbacks(){ this->address = address; // load default pin map for (int i = 0; i < 16; i++) { diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp index f74eb61..2ab8d03 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp @@ -12,6 +12,7 @@ DigitalOutputIoT::~DigitalOutputIoT() { } bool DigitalOutputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) { + Serial.println("DigitalOutputIoT::begin"); this->mqtt = mqtt; this->base_topic = base_topic; this->card = (DigitalOutputCard *) card; @@ -24,9 +25,11 @@ bool DigitalOutputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient this->publish_enable_length = strlen(PUBLISH_ENABLE_TOPIC); strcpy(this->state_report_topic, "00/state"); strcpy(this->value_report_topic, "00/value"); + Serial.println("Registering callbacks inside DigitalOutputIoT::begin"); // Register callbacks auto bindedCallback = std::bind(&DigitalOutputIoT::handleValueChange, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); this->card->registerChangeCallback(bindedCallback); + Serial.println("DigitalOutputIoT::begin complete"); return true; } diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index 1226e5e..5546da4 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -106,9 +106,15 @@ void ESPMegaPRO::enableInternalDisplay(HardwareSerial *serial) { Serial.println("Cannot Enable Internal Display without IoT Module being enabled!"); return; } + Serial.println("Insantiating Internal Display"); display = new InternalDisplay(serial); + Serial.println("Binding Internal Display to IoT Module"); auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, this); + Serial.println("Initializing Internal Display"); + display->bindInputCard(&inputs); + display->bindOutputCard(&outputs); display->begin(this->iot,bindedGetTime); internalDisplayEnabled = true; + Serial.println("Internal Display Enabled"); } \ No newline at end of file diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.cpp index 0254d19..16b9a5a 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.cpp @@ -2,13 +2,35 @@ void InternalDisplay::begin(ESPMegaIoT *iot, std::function getRtcTime) { + Serial.println("Assigning IoT Module and RTC Time Getter"); this->iot = iot; this->getRtcTime = getRtcTime; + Serial.println("Aqquring Network and MQTT Configs"); 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)); + Serial.println("Registering Callbacks"); + Serial.println("Binding Page Change Callback"); + auto bindedInputStateChangeCallback = std::bind(&InternalDisplay::handleInputStateChange, this, std::placeholders::_1, std::placeholders::_2); + Serial.println("Binding Input State Change Callback"); + auto bindedPwmStateChangeCallback = std::bind(&InternalDisplay::handlePwmStateChange, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + Serial.println("Registering inputCard Callbacks"); + this->inputCard->registerCallback(bindedInputStateChangeCallback); + Serial.println("Registering outputCard Callbacks"); + this->outputCard->registerChangeCallback(bindedPwmStateChangeCallback); + // Initialize the display + Serial.println("Initializing DisplayAdapter"); + this->displayAdapter->begin(115200); + Serial.println("Setting DisplayAdapter Timeout"); + this->displayAdapter->setTimeout(100); + Serial.println("Flushing DisplayAdapter"); + this->displayAdapter->flush(); + Serial.println("Resetting Display"); + this->reset(); + delay(500); + Serial.println("Jumping to Page 1"); + this->jumpToPage(1); + Serial.println("Display Initialization Complete"); } void InternalDisplay::loop() { @@ -177,4 +199,12 @@ void InternalDisplay::setInputMarker(uint8_t pin, bool state) { InternalDisplay::InternalDisplay(HardwareSerial *displayAdapter) : ESPMegaDisplay(displayAdapter) { this->currentPage = INTERNAL_DISPLAY_DASHBOARD_PAGE; +} + +void InternalDisplay::bindInputCard(DigitalInputCard *inputCard) { + this->inputCard = inputCard; +} + +void InternalDisplay::bindOutputCard(DigitalOutputCard *outputCard) { + this->outputCard = outputCard; } \ No newline at end of file diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.hpp index 0a331b7..476894f 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.hpp @@ -38,9 +38,6 @@ class InternalDisplay : public ESPMegaDisplay { private: DigitalInputCard *inputCard; DigitalOutputCard *outputCard; - // Previously registered callbacks of input and output cards - // We need to call them when the respective card is binded to the display - // Because we will replace the callbacks with the display's own callbacks void handleInputStateChange(uint8_t pin, bool state); void handlePwmStateChange(uint8_t pin, bool state, uint16_t value); void handlePageChange(uint8_t page); diff --git a/ESPMegaPRO-firmware/src/iot_framdemo.cpp b/ESPMegaPRO-firmware/src/iot_framdemo.cpp index 0f01881..3f0a84b 100644 --- a/ESPMegaPRO-firmware/src/iot_framdemo.cpp +++ b/ESPMegaPRO-firmware/src/iot_framdemo.cpp @@ -3,6 +3,8 @@ #include #include +// Demo PLC firmware using the ESPMegaPRO OOP library + ESPMegaPRO espmega = ESPMegaPRO(); void input_change_callback(uint8_t pin, uint8_t value) { @@ -46,12 +48,15 @@ void setup() { espmega.iot->setMqttConfig(mqtt_config); Serial.println("Connecting to MQTT"); espmega.iot->connectToMqtt(); - Serial.println("Registering cards"); + Serial.println("Registering Output Card"); espmega.iot->registerCard(0); + Serial.println("Registering Input Card"); espmega.iot->registerCard(1); - Serial.println("Initialization Routine Complete"); + Serial.println("Registering Input Change Callback"); espmega.inputs.registerCallback(input_change_callback); + Serial.println("Enabling Internal Display"); espmega.enableInternalDisplay(&Serial); + Serial.println("Initialization Routine Complete"); } void loop() {