Compare commits

..

2 Commits

Author SHA1 Message Date
Siwat Sirichai a22c8ef55c card not installed handling 2024-11-03 22:32:36 +07:00
Siwat Sirichai 268218f546 input (not working) 2024-11-03 21:51:35 +07:00
5 changed files with 59 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -27,6 +27,7 @@
// #define CT_ENABLE
// #define SMART_VARIABLE_ENABLE
#define EXTERNAL_DIGITAL_OUTPUT_CARD_ENABLE
#define EXTERNAL_DIGITAL_INPUT_CARD_ENABLE
// Demo PLC firmware using the ESPMegaPRO OOP library
ESPMegaPRO espmega = ESPMegaPRO();
@ -35,6 +36,15 @@ ESPMegaPRO espmega = ESPMegaPRO();
DigitalOutputCard externalDigitalOutputCard = DigitalOutputCard(1, 0, 1, 1, 0);
#endif
#ifdef EXTERNAL_DIGITAL_INPUT_CARD_ENABLE
void handleExternalDigitalInput(uint8_t pin, uint8_t state)
{
Serial.printf("Digital Input External %d: %d\n", pin, state);
}
DigitalInputCard externalDigitalInputCard = DigitalInputCard(0, 0, 1, 0, 1, 1);
#endif
// Remote Variable
#ifdef REMOTE_VARIABLE_ENABLE
RemoteVariable testVar = RemoteVariable();
@ -172,6 +182,10 @@ void setup()
espmega.begin();
#ifdef EXTERNAL_DIGITAL_OUTPUT_CARD_ENABLE
espmega.installCard(6, &externalDigitalOutputCard);
#endif
#ifdef EXTERNAL_DIGITAL_INPUT_CARD_ENABLE
espmega.installCard(7, &externalDigitalInputCard);
externalDigitalInputCard.registerCallback(handleExternalDigitalInput);
#endif
espmega.setTimezone("UTC-7");
ESP_LOGI("Initializer", "Enabling IOT module");
@ -216,6 +230,10 @@ void setup()
ESP_LOGI("Initializer", "Registering cards 6");
espmega.iot->registerCard(6);
#endif
#ifdef EXTERNAL_DIGITAL_INPUT_CARD_ENABLE
ESP_LOGI("Initializer", "Registering cards 7");
espmega.iot->registerCard(7);
#endif
#endif
ESP_LOGI("Initializer", "Registering Input change callback");
espmega.inputs.registerCallback(input_change_callback);