Files

225 lines
5.8 KiB
C++
Raw Permalink Normal View History

constexpr const char * const TAG = "BOBBY";
2021-06-28 21:43:20 +02:00
// system includes
#include <cstdio>
// esp-idf includes
2022-05-16 00:09:52 +02:00
#include <esp32/pm.h>
#include <esp_pm.h>
#include <esp_chip_info.h>
#include <esp_log.h>
2021-06-28 21:43:20 +02:00
// 3rdparty lib includes
#include <espchrono.h>
using namespace std::chrono_literals;
#include <espwifistack.h>
2021-12-05 00:50:37 +01:00
#include <schedulertask.h>
#include <screenmanager.h>
2022-10-01 17:34:13 +02:00
#include <tickchrono.h>
#include <espstrutils.h>
2021-06-28 21:43:20 +02:00
// local includes
#include "bobbycar-common.h"
#include "bobbycar-serial.h"
#include "macros_bobbycar.h"
2021-06-28 21:43:20 +02:00
#include "globals.h"
#include "screens.h"
#include "presets.h"
#include "statistics.h"
2022-01-09 07:43:54 +01:00
#ifdef FEATURE_JOYSTICK
#include "modes/wheelchairmode.h"
#else
2021-11-02 01:43:58 +01:00
#include "modes/defaultmode.h"
2022-01-09 07:43:54 +01:00
#endif
#include "displays/lockscreen.h"
2022-10-01 17:34:13 +02:00
#include "displays/menus/recoverymenu.h"
2021-12-30 06:36:35 +01:00
#include "displays/potiscalibratedisplay.h"
2022-10-13 22:00:16 +02:00
#include "displays/setup/information.h"
#include "displays/setup/basic_buttons.h"
#include "displays/setup/calibrate_potis.h"
2022-10-01 17:34:13 +02:00
#include "displays/statusdisplay.h"
2021-11-21 23:08:32 +01:00
#include "newsettings.h"
2021-12-05 00:50:37 +01:00
#include "taskmanager.h"
2021-06-28 21:43:20 +02:00
2022-10-21 15:42:26 +02:00
#define BOOT_PROGRESS(s) \
bootLabel.redraw(s); \
ESP_LOGI("BOOT", "%s", s);
2021-06-28 21:43:20 +02:00
namespace {
2021-12-19 23:25:52 +01:00
espchrono::millis_clock::time_point lastStatsPush;
2021-06-28 21:43:20 +02:00
std::optional<espchrono::millis_clock::time_point> lastStatsUpdate;
2022-10-01 17:34:13 +02:00
RTC_NOINIT_ATTR bool recovery;
2022-03-05 22:12:08 +01:00
} // namespace
2021-06-28 21:43:20 +02:00
extern "C" void app_main()
{
2021-08-09 20:59:55 +02:00
#ifdef FEATURE_LEDBACKLIGHT
pinMode(PINS_LEDBACKLIGHT, OUTPUT);
digitalWrite(PINS_LEDBACKLIGHT, ledBacklightInverted ? LOW : HIGH);
2021-06-28 21:43:20 +02:00
#endif
2022-10-01 17:34:13 +02:00
if (const auto reset_reason = esp_reset_reason(); reset_reason == ESP_RST_POWERON)
{
recovery = false;
}
if (recovery)
{
initScreen();
ESP_LOGE(TAG, "Recovery mode (%s)", espcpputils::toString(esp_reset_reason()).c_str());
bootLabel.redraw("Entering recovery mode");
if (const auto result = configs.init("bobbycar"); result != ESP_OK)
ESP_LOGE(TAG, "config_init_settings() failed with %s", esp_err_to_name(result));
for (auto &task : schedulerTasks)
{
task.setup(recovery);
}
espgui::switchScreen<RecoveryMenu>();
recovery = false;
while (true)
{
const auto now = espchrono::millis_clock::now();
for (auto &schedulerTask : schedulerTasks)
{
if (schedulerTask.isInitialized())
schedulerTask.loop();
}
espcpputils::delay(1ms);
};
}
else
{
recovery = true;
}
2022-10-21 15:42:26 +02:00
BOOT_PROGRESS("settings");
2021-11-21 23:08:32 +01:00
if (const auto result = configs.init("bobbycar"); result != ESP_OK)
ESP_LOGE(TAG, "config_init_settings() failed with %s", esp_err_to_name(result));
2022-10-02 23:14:15 +02:00
initScreen();
2022-10-02 22:54:19 +02:00
2022-01-03 17:13:45 +01:00
profileSettings = presets::defaultProfileSettings;
2021-08-17 22:06:13 +02:00
if (settingsPersister.init())
{
if (!settingsPersister.openProfile(0))
ESP_LOGE("BOBBY", "openProfile(0) failed");
2022-01-03 17:13:45 +01:00
loadProfileSettings();
2021-08-17 22:06:13 +02:00
}
else
ESP_LOGE("BOBBY", "init() failed");
2022-10-01 17:34:13 +02:00
for (auto &task : schedulerTasks)
2021-12-05 00:50:37 +01:00
{
2022-02-18 21:45:30 +01:00
if (checkEnabledByName(task.name()))
{
2022-10-21 15:42:26 +02:00
BOOT_PROGRESS(task.name());
2022-10-01 17:34:13 +02:00
task.setup(false);
2022-02-18 01:21:54 +01:00
}
2021-12-05 00:50:37 +01:00
}
2021-06-28 21:43:20 +02:00
2022-01-09 07:43:54 +01:00
#ifdef FEATURE_JOYSTICK
currentMode = &modes::wheelchairMode;
#else
2021-06-28 21:43:20 +02:00
currentMode = &modes::defaultMode;
2022-01-09 07:43:54 +01:00
#endif
2021-06-28 21:43:20 +02:00
2022-10-21 15:42:26 +02:00
BOOT_PROGRESS("switchScreen");
2021-08-09 20:59:55 +02:00
2022-10-06 22:37:31 +02:00
if (const auto result = checkIfInCalibration(); result)
2021-12-30 06:36:35 +01:00
{
2022-10-13 22:00:16 +02:00
switch(*result)
{
case SetupStep::INFORMATION:
2022-10-21 15:42:26 +02:00
BOOT_PROGRESS("Calibtration");
2022-10-13 22:00:16 +02:00
espgui::switchScreen<SetupInformationDisplay>();
break;
case SetupStep::BASIC_BUTTONS:
2022-10-21 15:42:26 +02:00
BOOT_PROGRESS("Calibtration");
2022-10-13 22:00:16 +02:00
espgui::switchScreen<SetupBasicButtonsDisplay>(true);
break;
2022-10-26 19:03:55 +02:00
case SetupStep::CALIBRATE_POTIS:
2022-10-13 22:00:16 +02:00
espgui::switchScreen<SetupCalibratePotisDisplay>(true);
2022-10-26 19:03:55 +02:00
break;
2022-10-13 22:00:16 +02:00
default:;
}
2021-12-30 06:36:35 +01:00
}
2022-04-29 22:40:49 +02:00
else if (configs.lockscreen.keepLockedAfterReboot.value() && configs.lockscreen.locked.value())
2021-12-17 22:40:08 +01:00
{
2022-10-21 15:42:26 +02:00
BOOT_PROGRESS("Locked");
2022-06-12 15:54:54 +02:00
espgui::switchScreen<StatusDisplay>();
espgui::pushScreen<Lockscreen>();
2021-12-17 22:40:08 +01:00
}
2021-06-28 21:43:20 +02:00
else
{
2022-10-21 15:42:26 +02:00
BOOT_PROGRESS("StatusDisplay")
2022-10-06 22:37:31 +02:00
espgui::switchScreen<StatusDisplay>();
}
2021-06-28 21:43:20 +02:00
2022-05-16 00:09:52 +02:00
esp_chip_info(&chip_info);
esp_pm_get_configuration(&pm_config);
2022-10-21 15:42:26 +02:00
ESP_LOGI(TAG, "Entering main loop...");
2021-06-28 21:43:20 +02:00
while (true)
{
const auto now = espchrono::millis_clock::now();
2022-10-01 17:34:13 +02:00
if (recovery && now.time_since_epoch() > 5s)
{
ESP_LOGI(TAG, "Booting successful, disabling recovery...");
recovery = false;
}
// if (!heap_caps_check_integrity_all(true))
// ESP_LOGW(TAG, "OIS IM OARSCH!!!!!");
2021-12-05 00:50:37 +01:00
for (auto &schedulerTask : schedulerTasks)
2021-08-09 20:59:55 +02:00
{
2022-01-17 13:01:09 +01:00
if (schedulerTask.isInitialized())
schedulerTask.loop();
2021-06-28 21:43:20 +02:00
}
2022-04-29 22:40:49 +02:00
if (!lastStatsUpdate || now - *lastStatsUpdate >= 1000ms/configs.boardcomputerHardware.timersSettings.statsUpdateRate.value())
2021-06-28 21:43:20 +02:00
{
updateAccumulators();
pushStats();
lastStatsUpdate = now;
}
2021-12-19 23:25:52 +01:00
if (now - lastStatsPush >= 1s)
2021-06-28 21:43:20 +02:00
{
2021-12-19 20:32:37 +01:00
sched_pushStats(false);
2021-12-19 23:25:52 +01:00
lastStatsPush = now;
2021-06-28 21:43:20 +02:00
}
2022-01-03 23:47:55 +01:00
if (!battery::bootBatPercentage)
2021-12-11 05:05:46 +01:00
{
if(controllers.front.feedbackValid && controllers.back.feedbackValid)
{
2022-01-27 01:09:52 +01:00
if (const auto avgVoltage = controllers.getAvgVoltage(); avgVoltage)
2021-12-11 05:05:46 +01:00
{
2022-01-27 01:09:52 +01:00
if (avgVoltage > 30)
{
2022-04-29 22:40:49 +02:00
battery::bootBatPercentage = getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value()));
2022-01-27 01:09:52 +01:00
battery::bootBatWh = getRemainingWattHours();
}
}
2021-12-11 05:05:46 +01:00
}
}
2022-10-01 17:34:13 +02:00
espcpputils::delay(1ms);
2021-06-28 21:43:20 +02:00
}
}