diff --git a/ESPMegaPRO-firmware/.gitignore b/ESPMegaPRO-firmware/.gitignore index 89cc49c..46344a8 100644 --- a/ESPMegaPRO-firmware/.gitignore +++ b/ESPMegaPRO-firmware/.gitignore @@ -1,4 +1,4 @@ -.pio +.pio/ .vscode/.browse.c_cpp.db* .vscode/c_cpp_properties.json .vscode/launch.json diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp index 2311e24..3508b3d 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/AnalogCard.cpp @@ -1,4 +1,5 @@ #include +#include "esp_log.h" AnalogCard::AnalogCard() : dac0(DAC0_ADDRESS), dac1(DAC1_ADDRESS), @@ -12,12 +13,14 @@ AnalogCard::AnalogCard() : dac0(DAC0_ADDRESS), void AnalogCard::dacWrite(uint8_t pin, uint16_t value) { + ESP_LOGV("AnalogCard", "DAC Write: %d, %d", pin, value); this->setDACState(pin, value > 0); this->setDACValue(pin, value); } void AnalogCard::setDACState(uint8_t pin, bool state) { + ESP_LOGD("AnalogCard", "Setting DAC state: %d, %d", pin, state); this->dac_state[pin] = state; this->sendDataToDAC(pin, this->dac_value[pin]*state); for (int i = 0; i < this->dac_change_callbacks.size(); i++) @@ -28,6 +31,7 @@ void AnalogCard::setDACState(uint8_t pin, bool state) void AnalogCard::setDACValue(uint8_t pin, uint16_t value) { + ESP_LOGD("AnalogCard", "Setting DAC value: %d, %d", pin, value); this->dac_value[pin] = value; this->sendDataToDAC(pin, value*this->dac_state[pin]); for (int i = 0; i < this->dac_change_callbacks.size(); i++) @@ -80,32 +84,32 @@ bool AnalogCard::begin() { if (!this->dac0.begin()) { - Serial.println("Card Analog ERROR: Failed to install DAC0"); + ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC0"); return false; } if (!this->dac1.begin()) { - Serial.println("Card Analog ERROR: Failed to install DAC1"); + ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC1"); return false; } if (!this->dac2.begin()) { - Serial.println("Card Analog ERROR: Failed to install DAC2"); + ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC2"); return false; } if (!this->dac3.begin()) { - Serial.println("Card Analog ERROR: Failed to install DAC3"); + ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC3"); return false; } if (!this->analogInputBankA.begin()) { - Serial.println("Card Analog ERROR: Failed to install analog input bank A"); + ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install analog input bank A"); return false; } if (!this->analogInputBankB.begin()) { - Serial.println("Card Analog ERROR: Failed to install analog input bank B"); + ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install analog input bank B"); return false; } return true; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp index 8098760..aec6dd8 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -34,11 +34,11 @@ bool DigitalInputCard::begin() this->inputBankA = PCF8574(this->address_a); this->inputBankB = PCF8574(this->address_b); if (!this->inputBankA.begin()) { - Serial.println("Input Card ERROR: Failed to install input bank A"); + ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank A"); return false; } if (!this->inputBankB.begin()) { - Serial.println("Input Card ERROR: Failed to install input bank B"); + ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank B"); return false; } // Set the debounce time for all pins to 50ms @@ -158,9 +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"); + ESP_LOGD("DigitalInputCard", "Registering callback"); callbacks.push_back(callback); - Serial.println("Pushed back on Input Card Callbacks"); + ESP_LOGD("DigitalInputCard", "Callback registered"); } // Refresh the input buffer for bank A diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp index 2ab8d03..452f32d 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/DigitalOutputIoT.cpp @@ -12,7 +12,7 @@ DigitalOutputIoT::~DigitalOutputIoT() { } bool DigitalOutputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) { - Serial.println("DigitalOutputIoT::begin"); + ESP_LOGD("DigitalOutputIoT", "Beginning DigitalOutputIoT"); this->mqtt = mqtt; this->base_topic = base_topic; this->card = (DigitalOutputCard *) card; @@ -25,11 +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"); + ESP_LOGV("DigitalOutputIoT", "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"); + ESP_LOGV("DigitalOutputIoT", "DigitalOutputIoT::begin complete"); return true; } diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp index 4d34d30..14d9e07 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -125,7 +125,7 @@ void ESPMegaIoT::registerCard(uint8_t card_id) } break; default: - Serial.println("Unsupported card type"); + ESP_LOGE("ESPMegaIoT", "Registering card %d failed: Unknown card", card_id); return; } } @@ -214,10 +214,11 @@ bool ESPMegaIoT::connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt components[i]->publishReport(); } } + ESP_LOGI("ESPMegaIoT", "MQTT Connected"); mqtt_connected = true; return true; } - Serial.println("Failed to connect to mqtt"); + ESP_LOGW("ESPMegaIoT", "MQTT Connection failed: %d", mqtt.state()); mqtt_connected = false; return false; } diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index 5546da4..3a05463 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -10,8 +10,8 @@ bool ESPMegaPRO::begin() { outputs.bindFRAM(&fram,0); outputs.loadFromFRAM(); if(!this->installCard(0, &inputs)) { - Serial.println("Failed to initialize inputs"); - Serial.println("Is this an ESPMegaPRO device?"); + ESP_LOGE("ESPMegaPRO", "Failed to initialize inputs"); + ESP_LOGE("ESPMegaPRO", "Is this an ESPMegaPRO device?"); return false; } uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8}; @@ -31,12 +31,11 @@ void ESPMegaPRO::loop() { bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) { if (slot > 255) return false; if (cardInstalled[slot]) { - Serial.println("Card already installed"); + ESP_LOGE("ESPMegaPRO", "Card already installed at slot %d", slot); return false; } if (!card->begin()) { - Serial.print("Failed to install card at slot "); - Serial.println(slot); + ESP_LOGE("ESPMegaPRO", "Failed to initialize card at slot %d", slot); return false; } cards[slot] = card; @@ -103,18 +102,18 @@ ExpansionCard* ESPMegaPRO::getCard(uint8_t slot) { void ESPMegaPRO::enableInternalDisplay(HardwareSerial *serial) { if (internalDisplayEnabled) return; if (!iotEnabled) { - Serial.println("Cannot Enable Internal Display without IoT Module being enabled!"); + ESP_LOGE("ESPMegaPRO", "Cannot enable internal display without IoT module enabled"); return; } - Serial.println("Insantiating Internal Display"); + ESP_LOGD("ESPMegaPRO", "Enabling Internal Display"); display = new InternalDisplay(serial); - Serial.println("Binding Internal Display to IoT Module"); + ESP_LOGD("ESPMegaPRO", "Binding Internal Display to IoT Module"); auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, this); - Serial.println("Initializing Internal Display"); + ESP_LOGD("ESPMegaPRO", "Binding Internal Display to Input/Output Cards"); display->bindInputCard(&inputs); display->bindOutputCard(&outputs); display->begin(this->iot,bindedGetTime); internalDisplayEnabled = true; - Serial.println("Internal Display Enabled"); + ESP_LOGD("ESPMegaPRO", "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 16b9a5a..7b321f7 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/InternalDisplay.cpp @@ -2,37 +2,38 @@ void InternalDisplay::begin(ESPMegaIoT *iot, std::function getRtcTime) { - Serial.println("Assigning IoT Module and RTC Time Getter"); + ESP_LOGD("InternalDisplay", "Assigning IoT Module and RTC Time Getter"); this->iot = iot; this->getRtcTime = getRtcTime; - Serial.println("Aqquring Network and MQTT Configs"); + ESP_LOGD("InternalDisplay", "Acquiring Network and MQTT Configs"); this->mqttConfig = this->iot->getMqttConfig(); this->networkConfig = this->iot->getNetworkConfig(); // Register callbacks - Serial.println("Registering Callbacks"); - Serial.println("Binding Page Change Callback"); + ESP_LOGD("InternalDisplay", "Registering Callbacks"); + ESP_LOGD("InternalDisplay", "Binding Page Change Callback"); auto bindedInputStateChangeCallback = std::bind(&InternalDisplay::handleInputStateChange, this, std::placeholders::_1, std::placeholders::_2); - Serial.println("Binding Input State Change Callback"); + ESP_LOGD("InternalDisplay", "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"); + ESP_LOGD("InternalDisplay", "Registering inputCard Callbacks"); this->inputCard->registerCallback(bindedInputStateChangeCallback); - Serial.println("Registering outputCard Callbacks"); + ESP_LOGD("InternalDisplay", "Registering outputCard Callbacks"); this->outputCard->registerChangeCallback(bindedPwmStateChangeCallback); // Initialize the display - Serial.println("Initializing DisplayAdapter"); + ESP_LOGD("InternalDisplay", "Initializing DisplayAdapter"); this->displayAdapter->begin(115200); - Serial.println("Setting DisplayAdapter Timeout"); + ESP_LOGD("InternalDisplay", "Setting DisplayAdapter Timeout"); this->displayAdapter->setTimeout(100); - Serial.println("Flushing DisplayAdapter"); + ESP_LOGD("InternalDisplay", "Flushing DisplayAdapter"); this->displayAdapter->flush(); - Serial.println("Resetting Display"); + ESP_LOGD("InternalDisplay", "Resetting Display"); this->reset(); delay(500); - Serial.println("Jumping to Page 1"); + ESP_LOGD("InternalDisplay", "Jumping to Page 1"); this->jumpToPage(1); - Serial.println("Display Initialization Complete"); + ESP_LOGD("InternalDisplay", "Display Initialization Complete"); } + void InternalDisplay::loop() { // Keep reading the Serial Adapter this->recieveSerialCommand(); diff --git a/ESPMegaPRO-firmware/src/iot_framdemo.cpp b/ESPMegaPRO-firmware/src/iot_framdemo.cpp index 3f0a84b..f2e8c43 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(); @@ -15,6 +17,18 @@ void input_change_callback(uint8_t pin, uint8_t value) { } void setup() { + // Set each class's log level + esp_log_level_set("ESPMegaPRO", ESP_LOG_VERBOSE); + esp_log_level_set("DigitalOutputCard", ESP_LOG_VERBOSE); + esp_log_level_set("DigitalOutputIoT", ESP_LOG_VERBOSE); + esp_log_level_set("DigitalInputCard", ESP_LOG_VERBOSE); + esp_log_level_set("DigitalInputIoT", ESP_LOG_VERBOSE); + esp_log_level_set("AnalogCard", ESP_LOG_VERBOSE); + esp_log_level_set("AnalogIoT", ESP_LOG_VERBOSE); + esp_log_level_set("ClimateCard", ESP_LOG_VERBOSE); + esp_log_level_set("ClimateIoT", ESP_LOG_VERBOSE); + esp_log_level_set("InternalDisplay", ESP_LOG_VERBOSE); + esp_log_level_set("InternalDisplayIoT", ESP_LOG_VERBOSE); espmega.begin(); espmega.enableIotModule(); ETH.begin();