ESPMegaPRO-v3-SDK/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp

90 lines
2.5 KiB
C++
Raw Normal View History

2023-12-27 18:33:37 +00:00
#include <ESPMegaPRO_OOP.hpp>
ESPMegaPRO::ESPMegaPRO() {
}
2023-12-27 19:18:21 +00:00
bool ESPMegaPRO::begin() {
2023-12-27 18:33:37 +00:00
Wire.begin(14, 33);
2023-12-27 19:18:21 +00:00
Serial.begin(115200);
2023-12-27 20:09:41 +00:00
if(!this->installCard(0, &inputs)) {
2023-12-27 19:18:21 +00:00
Serial.println("Failed to initialize inputs");
Serial.println("Is this an ESPMegaPRO device?");
return false;
}
2023-12-27 20:09:41 +00:00
this->installCard(1, &outputs);
2023-12-28 08:18:37 +00:00
fram.begin(FRAM_ADDRESS);
2023-12-27 18:44:15 +00:00
uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8};
inputs.loadPinMap(pinMap);
2023-12-27 19:18:21 +00:00
return true;
2023-12-27 18:33:37 +00:00
}
void ESPMegaPRO::loop() {
inputs.loop();
outputs.loop();
2023-12-27 19:18:21 +00:00
for (int i = 0; i < 255; i++) {
2023-12-27 18:33:37 +00:00
if (cardInstalled[i]) {
cards[i]->loop();
}
2023-12-28 07:52:52 +00:00
}
iot.loop();
2023-12-27 18:33:37 +00:00
}
2023-12-27 19:18:21 +00:00
bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard* card) {
2023-12-27 19:18:57 +00:00
if (slot > 255) return false;
2023-12-27 19:18:21 +00:00
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;
}
2023-12-27 18:33:37 +00:00
cards[slot] = card;
cardInstalled[slot] = true;
cardCount++;
2023-12-27 19:18:21 +00:00
return true;
2023-12-27 18:33:37 +00:00
}
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);
2023-12-27 18:44:15 +00:00
}
2023-12-28 07:52:52 +00:00
void ESPMegaPRO::enableIotModule() {
iot.intr_begin(cards);
}