From c224aba193dbd01cea3a938dee91ce64006f9861 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Sat, 30 Dec 2023 15:56:05 +0700 Subject: [PATCH] refactor for integration of InternalDisplay --- .../lib/ESPMegaPRO/ClimateCard.cpp | 3 +- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp | 20 ++++++++++-- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp | 6 +++- .../lib/ESPMegaPRO/Untitled-1.cpp | 32 ------------------- ESPMegaPRO-firmware/src/iot_framdemo.cpp | 15 +++++---- 5 files changed, 33 insertions(+), 43 deletions(-) delete mode 100644 ESPMegaPRO-firmware/lib/ESPMegaPRO/Untitled-1.cpp diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp index 274ac17..a29664f 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp @@ -50,11 +50,12 @@ bool ClimateCard::begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_p break; } updateAirConditioner(); + return true; } bool ClimateCard::begin(AirConditioner ac) { - this->begin(ac, AC_SENSOR_TYPE_NONE, 0); + return this->begin(ac, AC_SENSOR_TYPE_NONE, 0); } void ClimateCard::loop() diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index a70d3ef..1226e5e 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -26,7 +26,7 @@ void ESPMegaPRO::loop() { cards[i]->loop(); } } - iot.loop(); + iot->loop(); } bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) { if (slot > 255) return false; @@ -88,11 +88,27 @@ void ESPMegaPRO::setTime(int hours, int minutes, int seconds, int day, int month } void ESPMegaPRO::enableIotModule() { - iot.intr_begin(cards); + if (iotEnabled) return; + this->iot = new ESPMegaIoT(); + this->iot->intr_begin(cards); + iotEnabled = true; } ExpansionCard* ESPMegaPRO::getCard(uint8_t slot) { if (slot > 255) return nullptr; if (!cardInstalled[slot]) return nullptr; return cards[slot]; +} + +void ESPMegaPRO::enableInternalDisplay(HardwareSerial *serial) { + if (internalDisplayEnabled) return; + if (!iotEnabled) { + Serial.println("Cannot Enable Internal Display without IoT Module being enabled!"); + return; + } + display = new InternalDisplay(serial); + auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, this); + display->begin(this->iot,bindedGetTime); + internalDisplayEnabled = true; + } \ No newline at end of file diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp index f5162b4..e8a03d1 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -29,14 +29,18 @@ class ESPMegaPRO { bool installCard(uint8_t slot, ExpansionCard* card); bool updateTimeFromNTP(); void enableIotModule(); + void enableInternalDisplay(HardwareSerial *serial); rtctime_t getTime(); void setTime(int hours, int minutes, int seconds, int day, int month, int year); ExpansionCard* getCard(uint8_t slot); FRAM fram; DigitalInputCard inputs = DigitalInputCard(INPUT_BANK_A_ADDRESS, INPUT_BANK_B_ADDRESS); DigitalOutputCard outputs = DigitalOutputCard(PWM_BANK_ADDRESS); - ESPMegaIoT iot = ESPMegaIoT(); + InternalDisplay *display; + ESPMegaIoT *iot; private: + bool iotEnabled = false; + bool internalDisplayEnabled = false; ExpansionCard* cards[255]; bool cardInstalled[255]; uint8_t cardCount = 0; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/Untitled-1.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/Untitled-1.cpp deleted file mode 100644 index ab7d635..0000000 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/Untitled-1.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "driver/rmt.h" - -#define RMT_TX_CHANNEL RMT_CHANNEL_0 -#define RMT_TX_GPIO_NUM GPIO_NUM_4 - -void app_main() { - // Configure RMT transmitter - rmt_config_t config = RMT_DEFAULT_CONFIG_TX(RMT_TX_GPIO_NUM, RMT_TX_CHANNEL); - config.clk_div = 80; // Set clock divider for desired frequency (80MHz / 80 = 1MHz) - rmt_config(&config); - rmt_driver_install(config.channel, 0, 0); - - // Define NEC IR protocol parameters - uint16_t irTimingArray[] = { /* Your IR timing array here */ }; - size_t itemCount = sizeof(irTimingArray) / sizeof(irTimingArray[0]); - rmt_item32_t items[itemCount]; - - // Convert IR timing array to RMT items - for (size_t i = 0; i < itemCount; i++) { - items[i].level0 = 1; - items[i].duration0 = irTimingArray[i]; - items[i].level1 = 0; - items[i].duration1 = irTimingArray[i]; - } - - // Send IR signal - rmt_write_items(config.channel, items, itemCount, true); - rmt_wait_tx_done(config.channel, portMAX_DELAY); - - // Cleanup RMT driver - rmt_driver_uninstall(config.channel); -} diff --git a/ESPMegaPRO-firmware/src/iot_framdemo.cpp b/ESPMegaPRO-firmware/src/iot_framdemo.cpp index f7a85d0..0f01881 100644 --- a/ESPMegaPRO-firmware/src/iot_framdemo.cpp +++ b/ESPMegaPRO-firmware/src/iot_framdemo.cpp @@ -16,7 +16,7 @@ void setup() { espmega.begin(); espmega.enableIotModule(); ETH.begin(); - espmega.iot.bindEthernetInterface(Ð); + espmega.iot->bindEthernetInterface(Ð); NetworkConfig config = { .ip = {192, 168, 0, 11}, .gateway = {192, 168, 0, 1}, @@ -31,9 +31,9 @@ void setup() { strcpy(config.password, "password"); strcpy(config.hostname, "espmega"); Serial.println("Setting network config"); - espmega.iot.setNetworkConfig(config); + espmega.iot->setNetworkConfig(config); Serial.println("Connecting to network"); - espmega.iot.connectNetwork(); + espmega.iot->connectNetwork(); Serial.println("Begin MQTT Modules"); MqttConfig mqtt_config = { .mqtt_port = 1883, @@ -43,14 +43,15 @@ void setup() { strcpy(mqtt_config.mqtt_server, "192.168.0.26"); strcpy(mqtt_config.base_topic, "/espmegaoop"); Serial.println("Loading MQTT Config Struct to IoT Module"); - espmega.iot.setMqttConfig(mqtt_config); + espmega.iot->setMqttConfig(mqtt_config); Serial.println("Connecting to MQTT"); - espmega.iot.connectToMqtt(); + espmega.iot->connectToMqtt(); Serial.println("Registering cards"); - espmega.iot.registerCard(0); - espmega.iot.registerCard(1); + espmega.iot->registerCard(0); + espmega.iot->registerCard(1); Serial.println("Initialization Routine Complete"); espmega.inputs.registerCallback(input_change_callback); + espmega.enableInternalDisplay(&Serial); } void loop() {