card not installed handling

This commit is contained in:
Siwat Sirichai 2024-11-03 22:32:36 +07:00
parent 268218f546
commit a22c8ef55c
5 changed files with 44 additions and 4 deletions

View File

@ -60,11 +60,13 @@ bool DigitalInputCard::begin()
if (!this->inputBankA.begin()) if (!this->inputBankA.begin())
{ {
ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank A"); ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank A");
this->initOk = false;
return false; return false;
} }
if (!this->inputBankB.begin()) if (!this->inputBankB.begin())
{ {
ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank B"); ESP_LOGE("DigitalInputCard", "Input Card ERROR: Failed to install input bank B");
this->initOk = false;
return false; return false;
} }
// Set the debounce time for all pins to 50ms // Set the debounce time for all pins to 50ms
@ -79,6 +81,7 @@ bool DigitalInputCard::begin()
this->pinMap[i] = i; this->pinMap[i] = i;
this->virtualPinMap[i] = i; this->virtualPinMap[i] = i;
} }
this->initOk = true;
return true; return true;
} }
@ -102,6 +105,11 @@ bool DigitalInputCard::digitalRead(uint8_t pin)
*/ */
bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh) 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]; pin = pinMap[pin];
// First check if the pin is in bank A or B // First check if the pin is in bank A or B
if (pin >= 0 && pin <= 7) if (pin >= 0 && pin <= 7)
@ -120,7 +128,7 @@ bool DigitalInputCard::digitalRead(uint8_t pin, bool refresh)
// Extract the bit from the buffer // Extract the bit from the buffer
return ((inputBufferB >> (15 - pin)) & 1); return ((inputBufferB >> (15 - pin)) & 1);
} }
return 255; return false;
} }
/** /**
@ -291,6 +299,11 @@ uint8_t DigitalInputCard::registerCallback(std::function<void(uint8_t, bool)> ca
*/ */
void DigitalInputCard::refreshInputBankA() void DigitalInputCard::refreshInputBankA()
{ {
if(!this->initOk)
{
ESP_LOGE("DigitalInputCard", "Input Card ERROR: Card not initialized");
return;
}
inputBufferA = inputBankA.read8(); inputBufferA = inputBankA.read8();
} }
@ -299,6 +312,11 @@ void DigitalInputCard::refreshInputBankA()
*/ */
void DigitalInputCard::refreshInputBankB() void DigitalInputCard::refreshInputBankB()
{ {
if(!this->initOk)
{
ESP_LOGE("DigitalInputCard", "Input Card ERROR: Card not initialized");
return;
}
inputBufferB = inputBankB.read8(); inputBufferB = inputBankB.read8();
} }
@ -370,4 +388,14 @@ void DigitalInputCard::preloadInputBuffer()
refreshInputBankB(); refreshInputBankB();
previousInputBufferA = inputBufferA; previousInputBufferA = inputBufferA;
previousInputBufferB = inputBufferB; previousInputBufferB = inputBufferB;
}
/**
* @brief Get the status of the card
*
* @return True if the card is initialized, false otherwise
*/
bool DigitalInputCard::getStatus()
{
return this->initOk;
} }

View File

@ -43,9 +43,12 @@ class DigitalInputCard : public ExpansionCard {
void loadPinMap(uint8_t pinMap[16]); void loadPinMap(uint8_t pinMap[16]);
// Preload previousInputBuffer and inputBuffer // Preload previousInputBuffer and inputBuffer
void preloadInputBuffer(); void preloadInputBuffer();
// Status of card
bool getStatus();
// Get type of card // Get type of card
uint8_t getType(); uint8_t getType();
private: private:
bool initOk = false;
PCF8574 inputBankA; PCF8574 inputBankA;
PCF8574 inputBankB; PCF8574 inputBankB;
uint8_t address_a; uint8_t address_a;

View File

@ -17,6 +17,9 @@
*/ */
bool DigitalInputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) { bool DigitalInputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) {
this->card = (DigitalInputCard *)card; this->card = (DigitalInputCard *)card;
if(!this->card->getStatus()) {
return false;
}
this->card_id = card_id; this->card_id = card_id;
this->mqtt = mqtt; this->mqtt = mqtt;
this->base_topic = base_topic; this->base_topic = base_topic;

View File

@ -144,6 +144,12 @@ void ESPMegaIoT::registerCard(uint8_t card_id)
{ {
return; 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 // Get the card type
uint8_t card_type = cards[card_id]->getType(); uint8_t card_type = cards[card_id]->getType();
// Create the respective IoT component // Create the respective IoT component

View File

@ -42,7 +42,7 @@ void handleExternalDigitalInput(uint8_t pin, uint8_t state)
Serial.printf("Digital Input External %d: %d\n", pin, 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 #endif
// Remote Variable // Remote Variable
@ -231,8 +231,8 @@ void setup()
espmega.iot->registerCard(6); espmega.iot->registerCard(6);
#endif #endif
#ifdef EXTERNAL_DIGITAL_INPUT_CARD_ENABLE #ifdef EXTERNAL_DIGITAL_INPUT_CARD_ENABLE
// ESP_LOGI("Initializer", "Registering cards 7"); ESP_LOGI("Initializer", "Registering cards 7");
// espmega.iot->registerCard(7); espmega.iot->registerCard(7);
#endif #endif
#endif #endif
ESP_LOGI("Initializer", "Registering Input change callback"); ESP_LOGI("Initializer", "Registering Input change callback");