diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp index b68972a..fc9f2cf 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -60,11 +60,13 @@ bool DigitalInputCard::begin() if (!this->inputBankA.begin()) { ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank A"); + this->initOk = false; return false; } if (!this->inputBankB.begin()) { ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank B"); + this->initOk = false; return false; } // Set the debounce time for all pins to 50ms @@ -79,6 +81,7 @@ bool DigitalInputCard::begin() this->pinMap[i] = i; this->virtualPinMap[i] = i; } + this->initOk = true; return true; } @@ -102,6 +105,11 @@ bool DigitalInputCard::digitalRead(uint8_t pin) */ bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh) { + if(!this->initOk) + { + ESP_LOGE("DigitalInputCard", "Input Card ERROR: Card not initialized"); + return false; + } pin = pinMap[pin]; // First check if the pin is in bank A or B if (pin >= 0 && pin <= 7) @@ -120,7 +128,7 @@ bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh) // Extract the bit from the buffer return ((inputBufferB >> (15 - pin)) & 1); } - return 255; + return false; } /** @@ -291,6 +299,11 @@ uint8_t DigitalInputCard::registerCallback(std::function ca */ void DigitalInputCard::refreshInputBankA() { + if(!this->initOk) + { + ESP_LOGE("DigitalInputCard", "Input Card ERROR: Card not initialized"); + return; + } inputBufferA = inputBankA.read8(); } @@ -299,6 +312,11 @@ void DigitalInputCard::refreshInputBankA() */ void DigitalInputCard::refreshInputBankB() { + if(!this->initOk) + { + ESP_LOGE("DigitalInputCard", "Input Card ERROR: Card not initialized"); + return; + } inputBufferB = inputBankB.read8(); } @@ -370,4 +388,14 @@ void DigitalInputCard::preloadInputBuffer() refreshInputBankB(); previousInputBufferA = inputBufferA; previousInputBufferB = inputBufferB; +} + +/** + * @brief Get the status of the card + * + * @return True if the card is initialized, false otherwise + */ +bool DigitalInputCard::getStatus() +{ + return this->initOk; } \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp index f5587ba..d73f639 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputCard.hpp @@ -43,9 +43,12 @@ class DigitalInputCard : public ExpansionCard { void loadPinMap(uint8_t pinMap[16]); // Preload previousInputBuffer and inputBuffer void preloadInputBuffer(); + // Status of card + bool getStatus(); // Get type of card uint8_t getType(); private: + bool initOk = false; PCF8574 inputBankA; PCF8574 inputBankB; uint8_t address_a; diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputIoT.cpp index f138e52..ac1ba0c 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputIoT.cpp @@ -17,6 +17,9 @@ */ bool DigitalInputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) { this->card = (DigitalInputCard *)card; + if(!this->card->getStatus()) { + return false; + } this->card_id = card_id; this->mqtt = mqtt; this->base_topic = base_topic; diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index 4107ba0..d0837f8 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -144,6 +144,12 @@ void ESPMegaIoT::registerCard(uint8_t card_id) { return; } + // Check if the physical card is installed + if (cards[card_id] == NULL) + { + ESP_LOGE("ESPMegaIoT", "Registering card %d failed: Card not installed", card_id); + return; + } // Get the card type uint8_t card_type = cards[card_id]->getType(); // Create the respective IoT component diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index 7385028..62e7079 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -42,7 +42,7 @@ void handleExternalDigitalInput(uint8_t pin, uint8_t state) Serial.printf("Digital Input External %d: %d\n", pin, state); } -DigitalInputCard externalDigitalInputCard = DigitalInputCard(1, 0, 1, 1, 1, 0); +DigitalInputCard externalDigitalInputCard = DigitalInputCard(0, 0, 1, 0, 1, 1); #endif // Remote Variable @@ -231,8 +231,8 @@ void setup() espmega.iot->registerCard(6); #endif #ifdef EXTERNAL_DIGITAL_INPUT_CARD_ENABLE - // ESP_LOGI("Initializer", "Registering cards 7"); - // espmega.iot->registerCard(7); + ESP_LOGI("Initializer", "Registering cards 7"); + espmega.iot->registerCard(7); #endif #endif ESP_LOGI("Initializer", "Registering Input change callback");