ESPMegaPRO-v3-SDK/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp

236 lines
7.0 KiB
C++
Raw Normal View History

2023-12-27 18:33:37 +00:00
#include <ESPMegaPRO_OOP.hpp>
2023-12-30 19:18:57 +00:00
// Reserve FRAM address 0 - 1000 for ESPMegaPRO Internal Use
// (34 Bytes) Address 0-33 for Built-in Digital Output Card
// (266 Bytes) Address 34-300 for ESPMegaPRO IoT Module
2023-12-30 19:18:57 +00:00
2023-12-31 18:56:49 +00:00
/**
* @brief Create a new ESPMegaPRO object
*
* @warning Only one ESPMegaPRO object can be created, creating more than one will result in undefined behavior
*/
2023-12-27 18:33:37 +00:00
ESPMegaPRO::ESPMegaPRO() {
}
2023-12-31 18:56:49 +00:00
/**
* @brief Initializes the ESPMegaPRO object.
*
* This function initializes the ESPMegaPRO object and all of its components.
* It also initializes the built-in Digital Input and Digital Output cards.
*
* @return True if the initialization is successful, false otherwise.
*/
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-28 13:20:49 +00:00
fram.begin(FRAM_ADDRESS);
2023-12-27 19:18:21 +00:00
Serial.begin(115200);
2023-12-28 13:20:49 +00:00
this->installCard(1, &outputs);
outputs.bindFRAM(&fram,0);
outputs.loadFromFRAM();
2023-12-30 15:50:19 +00:00
outputs.setAutoSaveToFRAM(true);
2023-12-27 20:09:41 +00:00
if(!this->installCard(0, &inputs)) {
2023-12-30 11:47:52 +00:00
ESP_LOGE("ESPMegaPRO", "Failed to initialize inputs");
ESP_LOGE("ESPMegaPRO", "Is this an ESPMegaPRO device?");
2023-12-27 19:18:21 +00:00
return false;
}
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
}
2023-12-31 18:56:49 +00:00
/**
* @brief The main loop for the ESPMegaPRO object.
*
* @note This function must be called in the main loop of the program.
*
* It will call the loop() function of all installed expansion cards, the ESPMegaIoT module, and the internal display.
*
*/
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
}
2023-12-30 15:50:19 +00:00
if(iotEnabled) {
iot->loop();
}
if(internalDisplayEnabled) {
display->loop();
}
2023-12-27 18:33:37 +00:00
}
2023-12-31 18:56:49 +00:00
/**
* @brief Installs an expansion card to the specified slot.
*
* @note This function automatically initializes the expansion card.
*
* @param slot The slot to install the card to.
* @param card Pointer to the ExpansionCard object.
*
* @return True if the installation is successful, false otherwise.
*/
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]) {
2023-12-30 11:47:52 +00:00
ESP_LOGE("ESPMegaPRO", "Card already installed at slot %d", slot);
2023-12-27 19:18:21 +00:00
return false;
}
if (!card->begin()) {
2023-12-30 11:47:52 +00:00
ESP_LOGE("ESPMegaPRO", "Failed to initialize card at slot %d", slot);
2023-12-27 19:18:21 +00:00
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
}
2023-12-31 18:56:49 +00:00
/**
* @brief Updates the internal RTC from NTP.
*
* @note Network must be connected before calling this function (see ESPMegaPRO.ESPMegaIoT::connectNetwork()).
*
* @return True if the update is successful, false otherwise.
*/
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;
}
2023-12-31 18:56:49 +00:00
/**
* @brief Gets the current time from the internal RTC.
*
* @return The current time as a rtctime_t struct.
*/
2023-12-27 18:33:37 +00:00
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;
}
2023-12-31 18:56:49 +00:00
/**
* @brief Sets the current time of the internal RTC.
*
* @param hours The hours.
* @param minutes The minutes.
* @param seconds The seconds.
* @param day The day.
* @param month The month.
* @param year The year.
*/
2023-12-27 18:33:37 +00:00
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-31 18:56:49 +00:00
/**
* @brief Enables, Instanitates, and Initializes the ESPMegaIoT module.
*
* @note This function must be called before using the ESPMegaIoT module.
*/
2023-12-28 07:52:52 +00:00
void ESPMegaPRO::enableIotModule() {
if (iotEnabled) return;
this->iot = new ESPMegaIoT();
2023-12-30 19:18:57 +00:00
this->iot->bindFRAM(&fram);
this->iot->intr_begin(cards);
iotEnabled = true;
2023-12-29 17:49:09 +00:00
}
2023-12-31 18:56:49 +00:00
/**
* @brief Gets the expansion card installed at the specified slot.
*
* @param slot The slot to get the card from.
*
* @return Pointer to the ExpansionCard object, or nullptr if no card is installed at the specified slot.
*/
2023-12-29 17:49:09 +00:00
ExpansionCard* ESPMegaPRO::getCard(uint8_t slot) {
if (slot > 255) return nullptr;
if (!cardInstalled[slot]) return nullptr;
return cards[slot];
}
2023-12-31 18:56:49 +00:00
/**
* @brief Enables, Instanitates, and Initializes the internal display.
*
* @note &Serial is used for the internal display on ESPMegaPRO R3.
2023-12-31 18:56:49 +00:00
* @note This function can only be called if the ESPMegaIoT module is enabled.
* @note This function must be called before using the internal display.
*
* @param serial Pointer to the HardwareSerial object to use for the internal display (Serial for ESPMegaPRO R3).
*/
void ESPMegaPRO::enableInternalDisplay(HardwareSerial *serial) {
if (internalDisplayEnabled) return;
if (!iotEnabled) {
2023-12-30 11:47:52 +00:00
ESP_LOGE("ESPMegaPRO", "Cannot enable internal display without IoT module enabled");
return;
}
2023-12-30 11:47:52 +00:00
ESP_LOGD("ESPMegaPRO", "Enabling Internal Display");
display = new InternalDisplay(serial);
2023-12-30 11:47:52 +00:00
ESP_LOGD("ESPMegaPRO", "Binding Internal Display to IoT Module");
auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, this);
2023-12-30 11:47:52 +00:00
ESP_LOGD("ESPMegaPRO", "Binding Internal Display to Input/Output Cards");
2023-12-30 11:27:39 +00:00
display->bindInputCard(&inputs);
display->bindOutputCard(&outputs);
display->begin(this->iot,bindedGetTime);
internalDisplayEnabled = true;
2023-12-30 11:47:52 +00:00
ESP_LOGD("ESPMegaPRO", "Internal Display Enabled");
2023-12-30 19:59:25 +00:00
}
2023-12-31 18:56:49 +00:00
/**
* @brief Dumps the contents of the internal FRAM to the serial port.
*
* @param start The starting address.
* @param end The ending address.
*/
2023-12-30 19:59:25 +00:00
void ESPMegaPRO::dumpFRAMtoSerial(uint16_t start, uint16_t end) {
for (int i = start; i <=end; i++) {
if (i % 16 == 0) {
Serial.printf("\n%03d: ", i);
}
Serial.printf("%03d ", this->fram.read8(i));
}
}
2023-12-31 18:56:49 +00:00
/**
* @brief Dumps the contents of the internal FRAM to the serial port in ASCII.
*
* @param start The starting address.
* @param end The ending address.
*/
2023-12-30 19:59:25 +00:00
void ESPMegaPRO::dumpFRAMtoSerialASCII(uint16_t start, uint16_t end) {
for (int i = 0; i < 500; i++) {
Serial.printf("%d: %c\n", i,this->fram.read8(i));
}
2023-12-28 07:52:52 +00:00
}