completed OOP demo

This commit is contained in:
Siwat Sirichai 2023-12-28 01:44:15 +07:00
parent 9c3d4e7bab
commit 1862887d30
3 changed files with 20 additions and 28 deletions

View File

@ -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;
@ -64,3 +66,4 @@ void ESPMegaPRO::setTime(int hours, int minutes, int seconds, int day, int month
timeElement.Year = year - 1970;
RTC.write(timeElement);
}

View File

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

View File

@ -1,14 +1,13 @@
#include <ESPMegaPRO.h>
#include <ESPMegaPRO_OOP.hpp>
#include <DigitalInputCard.hpp>
#include <DigitalOutputCard.hpp>
#include <AnalogCard.hpp>
// 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();
}