From 1862887d301128d823dc94b460174aba33588eab Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 28 Dec 2023 01:44:15 +0700 Subject: [PATCH] completed OOP demo --- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp | 7 +++- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp | 4 +- Template Project/src/cardsdemo.cpp | 37 +++++++------------ 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index 260740f..e7ebb21 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -7,7 +7,8 @@ void ESPMegaPRO::begin() { inputs.begin(); outputs.begin(); fram.begin(FRAM_ADDRESS); - + uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8}; + inputs.loadPinMap(pinMap); } void ESPMegaPRO::loop() { inputs.loop(); @@ -22,6 +23,7 @@ void ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) { cards[slot] = card; cardInstalled[slot] = true; cardCount++; + card->begin(); } bool ESPMegaPRO::updateTimeFromNTP() { struct tm timeinfo; @@ -63,4 +65,5 @@ void ESPMegaPRO::setTime(int hours, int minutes, int seconds, int day, int month timeElement.Month = month; timeElement.Year = year - 1970; RTC.write(timeElement); -} \ No newline at end of file +} + diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp index 5f25502..eddb582 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -36,10 +36,10 @@ class ESPMegaPRO { rtctime_t getTime(); void setTime(int hours, int minutes, int seconds, int day, int month, int year); FRAM fram; + DigitalInputCard inputs = DigitalInputCard(INPUT_BANK_A_ADDRESS, INPUT_BANK_B_ADDRESS); + DigitalOutputCard outputs = DigitalOutputCard(PWM_BANK_ADDRESS); private: ExpansionCard* cards[255]; bool cardInstalled[255]; uint8_t cardCount = 0; - DigitalInputCard inputs = DigitalInputCard(INPUT_BANK_A_ADDRESS, INPUT_BANK_B_ADDRESS); - DigitalOutputCard outputs = DigitalOutputCard(PWM_BANK_ADDRESS); }; \ No newline at end of file diff --git a/Template Project/src/cardsdemo.cpp b/Template Project/src/cardsdemo.cpp index 72b8445..9dce1dc 100644 --- a/Template Project/src/cardsdemo.cpp +++ b/Template Project/src/cardsdemo.cpp @@ -1,14 +1,13 @@ -#include +#include #include #include #include // This code demonstrates how to use the cards -uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 14, 13, 12}; -DigitalInputCard icard = DigitalInputCard(0x21, 0x22); -DigitalOutputCard ocard = DigitalOutputCard(0x5F); +ESPMegaPRO espmega = ESPMegaPRO(); +AnalogCard analogCard = AnalogCard(); void inputCallback(uint8_t pin, bool state) { Serial.print("Input "); @@ -22,40 +21,30 @@ void printInputs() { Serial.print("Input "); Serial.print(i); Serial.print(": "); - Serial.print(icard.digitalRead(i)); + Serial.print(espmega.inputs.digitalRead(i)); Serial.println(); } } void setup() { Serial.begin(115200); - Wire.begin(14, 33); - // Instantiate Input Card - Serial.println("Initializing Input Card"); - icard.begin(); - Serial.println("Initialized Input Card"); - // Instantiate Output Card - Serial.println("Initializing Output Card"); - ocard.begin(); - Serial.println("Initialized Output Card"); - - icard.loadPinMap(pinMap); - + // Instantiate ESPMega + espmega.begin(); // Read all the inputs and print them printInputs(); - // Turn on all the outputs for (int i = 0; i < 16; i++) { - ocard.digitalWrite(i, true); + espmega.outputs.digitalWrite(i, true); } - // Set the debounce time for all inputs to 200ms for (int i = 0; i < 16; i++) { - icard.setDebounceTime(i, 200); + espmega.inputs.setDebounceTime(i, 200); } - // Register the callback function - icard.registerCallback(inputCallback); + espmega.inputs.registerCallback(inputCallback); + + // Install the analog card + espmega.installCard(0, &analogCard); } unsigned long previousMillis = 0; @@ -68,6 +57,6 @@ void loop() { previousMillis = currentMillis; printInputs(); } - icard.loop(); + espmega.loop(); } \ No newline at end of file