change logging method

This commit is contained in:
Siwat Sirichai 2023-12-30 18:47:52 +07:00
parent b5806498ea
commit 53937dd0ed
8 changed files with 58 additions and 39 deletions

View File

@ -1,4 +1,4 @@
.pio
.pio/
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json

View File

@ -1,4 +1,5 @@
#include <AnalogCard.hpp>
#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;

View File

@ -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<void(uint8_t, bool)> 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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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");
}

View File

@ -2,37 +2,38 @@
void InternalDisplay::begin(ESPMegaIoT *iot, std::function<rtctime_t()> 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();

View File

@ -3,6 +3,8 @@
#include <ETH.h>
#include <ClimateCard.hpp>
// 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();