ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.cpp

358 lines
10 KiB
C++
Raw Normal View History

2024-01-01 16:44:07 +00:00
#include <ESPMegaProOS.hpp>
2024-05-21 16:50:21 +00:00
#include "esp_sntp.h"
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
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @warning Only one ESPMegaPRO object can be created, creating more than one will result in undefined behavior
*/
2024-01-11 15:26:57 +00:00
ESPMegaPRO::ESPMegaPRO()
{
2024-05-21 16:50:21 +00:00
2023-12-27 18:33:37 +00:00
}
2023-12-31 18:56:49 +00:00
/**
* @brief Initializes the ESPMegaPRO object.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* This function initializes the ESPMegaPRO object and all of its components.
* It also initializes the built-in Digital Input and Digital Output cards.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @return True if the initialization is successful, false otherwise.
*/
2024-01-11 15:26:57 +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);
2024-05-19 06:07:16 +00:00
outputs.bindFRAM(&fram, 0);
2024-02-05 16:40:48 +00:00
uint8_t outputPinMap[16] = {8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7};
outputs.loadPinMap(outputPinMap);
2023-12-28 13:20:49 +00:00
outputs.loadFromFRAM();
2023-12-30 15:50:19 +00:00
outputs.setAutoSaveToFRAM(true);
2024-01-11 15:26:57 +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;
}
2024-01-27 14:59:36 +00:00
uint8_t inputPinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 14, 13, 12};
2024-01-11 15:26:57 +00:00
inputs.loadPinMap(inputPinMap);
2024-05-19 06:07:16 +00:00
// Detect GPIO2 Reset
gpio_num_t buttonPin = GPIO_NUM_2;
gpio_pad_select_gpio(buttonPin);
gpio_set_direction(buttonPin, GPIO_MODE_INPUT);
gpio_set_pull_mode(buttonPin, GPIO_PULLUP_ONLY);
if (gpio_get_level(buttonPin) == 0)
{
ESP_LOGW("ESPMegaPRO", "GPIO2 is low, if this condition persists for 5 more seconds, the device will factory reset");
bool shouldReset = true;
for (int i = 0; i < 50; i++)
{
vTaskDelay(100 / portTICK_PERIOD_MS);
if (gpio_get_level(buttonPin) == 1)
{
ESP_LOGI("ESPMegaPRO", "Reset condition cleared, Continuing boot process");
shouldReset = false;
break;
}
}
if (shouldReset)
{
ESP_LOGW("ESPMegaPRO", "Reset condition met, Factory Resetting device");
for (int i = 0; i < fram.getSizeBytes(); i++)
{
if (i % 1024 == 0)
ESP_LOGV("ESPMegaPRO", "Clearing FRAM Address %d", i);
fram.write8(i, 0);
}
ESP_LOGI("ESPMegaPRO", "Factory Reset Complete");
ESP_LOGI("ESPMegaPRO", "Rebooting device");
esp_restart();
}
}
// Recovery Mode
recovery.bindFRAM(&fram, 600);
recovery.begin();
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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @note This function must be called in the main loop of the program.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* It will call the loop() function of all installed expansion cards, the ESPMegaIoT module, and the internal display.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
*/
2024-01-11 15:26:57 +00:00
void ESPMegaPRO::loop()
{
2023-12-27 18:33:37 +00:00
inputs.loop();
outputs.loop();
recovery.loop();
2024-01-11 15:26:57 +00:00
for (int i = 0; i < 255; i++)
{
if (cardInstalled[i])
{
2023-12-27 18:33:37 +00:00
cards[i]->loop();
}
2023-12-28 07:52:52 +00:00
}
2024-01-11 15:26:57 +00:00
if (iotEnabled)
{
2023-12-30 15:50:19 +00:00
iot->loop();
2024-05-21 16:50:21 +00:00
static int64_t lastNTPUpdate = (esp_timer_get_time() / 1000) - NTP_UPDATE_INTERVAL_MS + NTP_INITIAL_SYNC_DELAY_MS;
if ((esp_timer_get_time() / 1000) - lastNTPUpdate > NTP_UPDATE_INTERVAL_MS)
{
ESP_LOGV("ESPMegaPRO", "Updating time from NTP");
lastNTPUpdate = esp_timer_get_time() / 1000;
this->updateTimeFromNTP();
}
2023-12-30 15:50:19 +00:00
}
2024-01-11 15:26:57 +00:00
if (internalDisplayEnabled)
{
2023-12-30 15:50:19 +00:00
display->loop();
}
2024-01-11 15:26:57 +00:00
if (webServerEnabled)
{
2024-01-01 06:28:15 +00:00
webServer->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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @note This function automatically initializes the expansion card.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @param slot The slot to install the card to.
* @param card Pointer to the ExpansionCard object.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @return True if the installation is successful, false otherwise.
*/
2024-01-11 15:26:57 +00:00
bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard *card)
{
if (slot > 255)
return false;
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;
}
2024-01-11 15:26:57 +00:00
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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @note Network must be connected before calling this function (see ESPMegaPRO.ESPMegaIoT::connectNetwork()).
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @return True if the update is successful, false otherwise.
*/
2024-01-11 15:26:57 +00:00
bool ESPMegaPRO::updateTimeFromNTP()
{
2023-12-27 18:33:37 +00:00
struct tm timeinfo;
2024-05-21 16:50:21 +00:00
uint32_t start = esp_timer_get_time() / 1000;
time_t now;
time(&now);
localtime_r(&now, &timeinfo);
if (!(timeinfo.tm_year > (2016 - 1900)))
2023-12-27 18:33:37 +00:00
{
2024-05-21 16:50:21 +00:00
ESP_LOGI("ESPMegaPRO", "NTP is not ready yet!");
return false;
}
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);
2023-12-27 18:33:37 +00:00
}
2024-05-21 16:50:21 +00:00
ESP_LOGV("ESPMegaPRO", "Time updated from NTP: %s", asctime(&timeinfo));
return true;
}
2024-05-21 17:06:12 +00:00
/**
* @brief Sets the timezone for the internal RTC.
*
* @note This function takes POSIX timezone strings (e.g. "EST5EDT,M3.2.0,M11.1.0").
*/
void ESPMegaPRO::setTimezone(const char* offset)
2024-05-21 16:50:21 +00:00
{
2024-05-21 17:06:12 +00:00
setenv("TZ", offset, 1);
2023-12-27 18:33:37 +00:00
}
2023-12-31 18:56:49 +00:00
/**
* @brief Gets the current time from the internal RTC.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @return The current time as a rtctime_t struct.
*/
2024-01-11 15:26:57 +00:00
rtctime_t ESPMegaPRO::getTime()
{
2023-12-27 18:33:37 +00:00
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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @note This function must be called before using the ESPMegaIoT module.
*/
2024-01-11 15:26:57 +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;
2024-05-21 16:50:21 +00:00
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, this->iot->getMqttConfig()->mqtt_server);
sntp_setservername(1, "pool.ntp.org");
sntp_init();
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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @param slot The slot to get the card from.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @return Pointer to the ExpansionCard object, or nullptr if no card is installed at the specified slot.
*/
2024-01-11 15:26:57 +00:00
ExpansionCard *ESPMegaPRO::getCard(uint8_t slot)
{
if (slot > 255)
return nullptr;
if (!cardInstalled[slot])
return nullptr;
2023-12-29 17:49:09 +00:00
return cards[slot];
}
2023-12-31 18:56:49 +00:00
/**
* @brief Enables, Instanitates, and Initializes the internal display.
2024-01-11 15:26:57 +00:00
*
* @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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @param serial Pointer to the HardwareSerial object to use for the internal display (Serial for ESPMegaPRO R3).
*/
2024-01-11 15:26:57 +00:00
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);
2024-01-11 15:26:57 +00:00
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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @param start The starting address.
* @param end The ending address.
*/
2024-01-11 15:26:57 +00:00
void ESPMegaPRO::dumpFRAMtoSerial(uint16_t start, uint16_t end)
{
for (int i = start; i <= end; i++)
{
if (i % 16 == 0)
{
2023-12-30 19:59:25 +00:00
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.
2024-01-11 15:26:57 +00:00
*
2023-12-31 18:56:49 +00:00
* @param start The starting address.
* @param end The ending address.
*/
2024-01-11 15:26:57 +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-30 19:59:25 +00:00
}
2024-01-01 06:28:15 +00:00
}
/**
* @brief Enables the internal web server.
2024-01-11 15:26:57 +00:00
*
2024-01-01 06:28:15 +00:00
* @note This function can only be called if the ESPMegaIoT module is enabled.
* @note This function can only be called once.
2024-01-11 15:26:57 +00:00
*
2024-01-01 06:28:15 +00:00
* @param port The port to use for the web server.
*/
2024-01-11 15:26:57 +00:00
void ESPMegaPRO::enableWebServer(uint16_t port)
{
if (!iotEnabled)
{
2024-01-01 06:28:15 +00:00
ESP_LOGE("ESPMegaPRO", "Cannot enable web server without IoT module enabled");
return;
}
2024-01-11 15:26:57 +00:00
if (webServerEnabled)
{
2024-01-01 06:28:15 +00:00
ESP_LOGE("ESPMegaPRO", "Web server already enabled");
return;
}
webServer = new ESPMegaWebServer(port, this->iot);
2024-01-01 13:30:17 +00:00
webServer->bindFRAM(&fram);
2024-01-01 06:28:15 +00:00
webServer->begin();
webServerEnabled = true;
2023-12-28 07:52:52 +00:00
}