From 1481c08d3a6a34c0950a7fae462e3648b90ebd72 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 28 Dec 2023 02:18:21 +0700 Subject: [PATCH] card installation checking --- .../lib/ESPMegaPRO/AnalogCard.cpp | 47 +++++++++++++++---- .../lib/ESPMegaPRO/AnalogCard.hpp | 3 +- .../lib/ESPMegaPRO/DigitalInputCard.cpp | 13 +++-- .../lib/ESPMegaPRO/DigitalInputCard.hpp | 2 +- .../lib/ESPMegaPRO/DigitalOutputCard.cpp | 13 ++++- .../lib/ESPMegaPRO/DigitalOutputCard.hpp | 4 +- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp | 33 ++++++++++--- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp | 4 +- .../lib/ESPMegaPRO/ExpansionCard.hpp | 2 +- Template Project/src/cardsdemo.cpp | 38 ++++++++++----- 10 files changed, 122 insertions(+), 37 deletions(-) diff --git a/Template Project/lib/ESPMegaPRO/AnalogCard.cpp b/Template Project/lib/ESPMegaPRO/AnalogCard.cpp index 682e4c6..739fcd0 100644 --- a/Template Project/lib/ESPMegaPRO/AnalogCard.cpp +++ b/Template Project/lib/ESPMegaPRO/AnalogCard.cpp @@ -3,7 +3,9 @@ AnalogCard::AnalogCard() : dac0(DAC0_ADDRESS), dac1(DAC1_ADDRESS), dac2(DAC2_ADDRESS), - dac3(DAC3_ADDRESS) + dac3(DAC3_ADDRESS), + analogInputBankA(), + analogInputBankB() { } @@ -36,16 +38,45 @@ uint16_t AnalogCard::analogRead(uint8_t pin) return this->analogInputBankB.readADC_SingleEnded(pin - 4); } } -void AnalogCard::begin() +bool AnalogCard::begin() { this->dac0 = MCP4725(DAC0_ADDRESS); this->dac1 = MCP4725(DAC1_ADDRESS); this->dac2 = MCP4725(DAC2_ADDRESS); this->dac3 = MCP4725(DAC3_ADDRESS); - this->dac0.begin(); - this->dac1.begin(); - this->dac2.begin(); - this->dac3.begin(); - this->analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS); - this->analogInputBankB.begin(ANALOG_INPUT_BANK_B_ADDRESS); + if (!this->dac0.begin()) + { + Serial.println("Card Analog ERROR: Failed to install DAC0"); + return false; + } + if (!this->dac1.begin()) + { + Serial.println("Card Analog ERROR: Failed to install DAC1"); + return false; + } + if (!this->dac2.begin()) + { + Serial.println("Card Analog ERROR: Failed to install DAC2"); + return false; + } + if (!this->dac3.begin()) + { + Serial.println("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"); + return false; + } + if (!this->analogInputBankB.begin()) + { + Serial.println("Card Analog ERROR: Failed to install analog input bank B"); + return false; + } + return true; +} + +void AnalogCard::loop() +{ } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/AnalogCard.hpp b/Template Project/lib/ESPMegaPRO/AnalogCard.hpp index 6c0854e..c5115cf 100644 --- a/Template Project/lib/ESPMegaPRO/AnalogCard.hpp +++ b/Template Project/lib/ESPMegaPRO/AnalogCard.hpp @@ -15,7 +15,8 @@ class AnalogCard : public ExpansionCard { AnalogCard(); void dacWrite(uint8_t pin, uint16_t value); uint16_t analogRead(uint8_t pin); - void begin(); + bool begin(); + void loop(); private: MCP4725 dac0; MCP4725 dac1; diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp b/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp index 68cf825..cebb198 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputCard.cpp @@ -30,12 +30,18 @@ DigitalInputCard::DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, b this->address_b += 4; } // Initialize the card -void DigitalInputCard::begin() +bool DigitalInputCard::begin() { this->inputBankA = PCF8574(this->address_a); this->inputBankB = PCF8574(this->address_b); - this->inputBankA.begin(); - this->inputBankB.begin(); + if (!this->inputBankA.begin()) { + Serial.println("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"); + return false; + } // Set the debounce time for all pins to 50ms for (int i = 0; i < 16; i++) { @@ -48,6 +54,7 @@ void DigitalInputCard::begin() this->pinMap[i] = i; this->virtualPinMap[i] = i; } + return true; } // Refresh and Read the input from the specified pin, always refresh the input buffers bool DigitalInputCard::digitalRead(uint8_t pin) diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp b/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp index f022bff..63aa85c 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputCard.hpp @@ -9,7 +9,7 @@ class DigitalInputCard : public ExpansionCard { // Instantiate the card with the specified position on the dip switch DigitalInputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4, bool bit5); // Initialize the card - void begin(); + bool begin(); // Refresh and Read the input from the specified pin, always refresh the input buffers bool digitalRead(uint8_t pin); // Read the input from the specified pin, also refresh the input buffers if refresh is true diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp index 4f9cb21..89a2562 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp +++ b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.cpp @@ -15,9 +15,14 @@ DigitalOutputCard::DigitalOutputCard(bool bit0, bool bit1, bool bit2, bool bit3, } // Initialize the card -void DigitalOutputCard::begin() { +bool DigitalOutputCard::begin() { this->pwm = Adafruit_PWMServoDriver(this->address); - this->pwm.begin(); + this->pwm.begin(); + pwm.reset(); + pwm.setOutputMode(true); + // Output card don't send ack, we can't check if it's connected + // so we just return true + return true; } // Set the output to the specified state void DigitalOutputCard::digitalWrite(uint8_t pin, bool state) { @@ -29,4 +34,8 @@ void DigitalOutputCard::analogWrite(uint8_t pin, uint16_t value) { if (value > 4095) value = 4095; // Set the pwm value this->pwm.setPin(pin, value); +} + +// Dummy loop function +void DigitalOutputCard::loop() { } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp index 8cc445b..fc2b14b 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalOutputCard.hpp @@ -9,7 +9,9 @@ public: // Instantiate the card with the specified position on the dip switch DigitalOutputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4); // Initialize the card - void begin(); + bool begin(); + // Dummy loop function + void loop(); // Set the output to the specified state void digitalWrite(uint8_t pin, bool state); // Set the output to the specified pwm value diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index e7ebb21..0aa8649 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -2,28 +2,49 @@ ESPMegaPRO::ESPMegaPRO() { } -void ESPMegaPRO::begin() { +bool ESPMegaPRO::begin() { Wire.begin(14, 33); - inputs.begin(); + Serial.begin(115200); + if(!inputs.begin()) { + Serial.println("Failed to initialize inputs"); + Serial.println("Is this an ESPMegaPRO device?"); + return false; + } outputs.begin(); - fram.begin(FRAM_ADDRESS); + if(!fram.begin(FRAM_ADDRESS)) { + Serial.println("Failed to initialize FRAM"); + Serial.println("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}; inputs.loadPinMap(pinMap); + return true; } void ESPMegaPRO::loop() { inputs.loop(); outputs.loop(); - for (int i = 0; i < 256; i++) { + for (int i = 0; i < 255; i++) { if (cardInstalled[i]) { cards[i]->loop(); } } } -void ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) { +bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) { + if (slot > 255) return; + if (cardInstalled[slot]) { + Serial.println("Card already installed"); + return false; + } + if (!card->begin()) { + Serial.print("Failed to install card at slot "); + Serial.println(slot); + return false; + } cards[slot] = card; cardInstalled[slot] = true; cardCount++; - card->begin(); + return true; + } bool ESPMegaPRO::updateTimeFromNTP() { struct tm timeinfo; diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp index eddb582..d4420ce 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -29,9 +29,9 @@ struct rtctime_t { class ESPMegaPRO { public: ESPMegaPRO(); - void begin(); + bool begin(); void loop(); - void installCard(uint8_t slot, ExpansionCard* card); + bool installCard(uint8_t slot, ExpansionCard* card); bool updateTimeFromNTP(); rtctime_t getTime(); void setTime(int hours, int minutes, int seconds, int day, int month, int year); diff --git a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp index 5537945..2f3a6bc 100644 --- a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp +++ b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp @@ -5,7 +5,7 @@ class ExpansionCard { public: // Instantiate the card with the specified address ExpansionCard() {} - virtual void begin() {} + virtual bool begin() {} // Preform a loop to refresh the input buffers virtual void loop() {} }; \ No newline at end of file diff --git a/Template Project/src/cardsdemo.cpp b/Template Project/src/cardsdemo.cpp index 9dce1dc..6f95020 100644 --- a/Template Project/src/cardsdemo.cpp +++ b/Template Project/src/cardsdemo.cpp @@ -5,19 +5,21 @@ // This code demonstrates how to use the cards - ESPMegaPRO espmega = ESPMegaPRO(); AnalogCard analogCard = AnalogCard(); -void inputCallback(uint8_t pin, bool state) { +void inputCallback(uint8_t pin, bool state) +{ Serial.print("Input "); Serial.print(pin); Serial.print(" changed to "); Serial.println(state); } -void printInputs() { - for (int i = 0; i < 16; i++) { +void printInputs() +{ + for (int i = 0; i < 16; i++) + { Serial.print("Input "); Serial.print(i); Serial.print(": "); @@ -26,37 +28,49 @@ void printInputs() { } } -void setup() { - Serial.begin(115200); +void setup() +{ // Instantiate ESPMega espmega.begin(); + Serial.println("ESPMega initialized"); // Read all the inputs and print them printInputs(); // Turn on all the outputs - for (int i = 0; i < 16; i++) { + for (int i = 0; i < 16; i++) + { espmega.outputs.digitalWrite(i, true); } // Set the debounce time for all inputs to 200ms - for (int i = 0; i < 16; i++) { + for (int i = 0; i < 16; i++) + { espmega.inputs.setDebounceTime(i, 200); } // Register the callback function espmega.inputs.registerCallback(inputCallback); // Install the analog card - espmega.installCard(0, &analogCard); + Serial.println("Installing analog card"); + if (espmega.installCard(0, &analogCard)) + { + Serial.println("Analog card installed"); + } + else + { + Serial.println("Failed to install analog card"); + } } unsigned long previousMillis = 0; const unsigned long interval = 1000; // 1 second -void loop() { +void loop() +{ unsigned long currentMillis = millis(); - if (currentMillis - previousMillis >= interval) { + if (currentMillis - previousMillis >= interval) + { previousMillis = currentMillis; printInputs(); } espmega.loop(); - } \ No newline at end of file