From 9c3d4e7bababfeccf99e24425eec9dfa63b46a3e Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 28 Dec 2023 01:33:37 +0700 Subject: [PATCH] OOP Variant of ESPMega --- .../lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp | 66 +++++++++++++++++++ .../lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp | 2 + .../lib/ESPMegaPRO/ExpansionCard.hpp | 2 +- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index e69de29..260740f 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -0,0 +1,66 @@ +#include +ESPMegaPRO::ESPMegaPRO() { + +} +void ESPMegaPRO::begin() { + Wire.begin(14, 33); + inputs.begin(); + outputs.begin(); + fram.begin(FRAM_ADDRESS); + +} +void ESPMegaPRO::loop() { + inputs.loop(); + outputs.loop(); + for (int i = 0; i < 256; i++) { + if (cardInstalled[i]) { + cards[i]->loop(); + } + } +} +void ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) { + cards[slot] = card; + cardInstalled[slot] = true; + cardCount++; +} +bool ESPMegaPRO::updateTimeFromNTP() { + struct tm timeinfo; + if (getLocalTime(&timeinfo)) + { + rtctime_t rtctime = this->getTime(); + if (rtctime.hours != timeinfo.tm_hour || rtctime.minutes != timeinfo.tm_min || + rtctime.seconds != timeinfo.tm_sec || rtctime.day != timeinfo.tm_mday || + rtctime.month != timeinfo.tm_mon + 1 || rtctime.year != timeinfo.tm_year + 1900) + { + this->setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, + timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900); + + } + return true; + } + return false; +} +rtctime_t ESPMegaPRO::getTime() { + tmElements_t timeElement; + RTC.read(timeElement); + rtctime_t time; + time.hours = timeElement.Hour; + time.minutes = timeElement.Minute; + time.seconds = timeElement.Second; + time.day = timeElement.Day; + time.month = timeElement.Month; + time.year = timeElement.Year + 1970; + return time; +} + +void ESPMegaPRO::setTime(int hours, int minutes, int seconds, int day, int month, int year) +{ + tmElements_t timeElement; + timeElement.Hour = hours; + timeElement.Minute = minutes; + timeElement.Second = seconds; + timeElement.Day = day; + 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 953d626..5f25502 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.hpp @@ -34,9 +34,11 @@ class ESPMegaPRO { void 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); FRAM fram; 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); diff --git a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp index ab21939..5537945 100644 --- a/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp +++ b/Template Project/lib/ESPMegaPRO/ExpansionCard.hpp @@ -1,5 +1,5 @@ #pragma once -#include +#include class ExpansionCard { public: