Files
bobbycar-boardcomputer-firm…/main/main.cpp
T

146 lines
3.9 KiB
C++
Raw Normal View History

2021-07-20 14:10:26 +02:00
constexpr const char * const TAG = "BOBBY";
2021-06-28 21:43:20 +02:00
// system includes
#include <cstdio>
// esp-idf includes
#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>
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
2021-11-02 01:43:58 +01:00
#include "displays/statusdisplay.h"
#include "displays/lockscreen.h"
2021-12-30 06:36:35 +01:00
#include "displays/potiscalibratedisplay.h"
#include "displays/buttoncalibratedisplay.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
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;
}
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
initScreen();
2021-08-17 22:06:13 +02:00
bootLabel.redraw("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-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");
2021-12-05 00:50:37 +01:00
for (const auto &task : schedulerTasks)
{
2022-02-18 21:45:30 +01:00
if (checkEnabledByName(task.name()))
{
2022-02-18 01:21:54 +01:00
bootLabel.redraw(task.name());
task.setup();
}
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
2021-08-09 20:59:55 +02:00
bootLabel.redraw("switchScreen");
2021-12-30 06:36:35 +01:00
if (configs.dpadMappingLeft.value == INPUT_MAPPING_NONE ||
configs.dpadMappingRight.value == INPUT_MAPPING_NONE ||
configs.dpadMappingUp.value == INPUT_MAPPING_NONE ||
configs.dpadMappingDown.value == INPUT_MAPPING_NONE)
{
espgui::switchScreen<ButtonCalibrateDisplay>(true);
}
2022-01-03 03:03:40 +01:00
else if (configs.lockscreen.keepLockedAfterReboot.value && configs.lockscreen.locked.value)
2021-12-17 22:40:08 +01:00
{
espgui::switchScreen<Lockscreen>();
}
2021-06-28 21:43:20 +02:00
else
{
2021-12-17 22:40:08 +01:00
if (!gas || !brems || *gas > 200.f || *brems > 200.f)
2021-12-30 06:36:35 +01:00
espgui::switchScreen<PotisCalibrateDisplay>(true);
else
{
espgui::switchScreen<StatusDisplay>();
}
}
2021-06-28 21:43:20 +02:00
while (true)
{
const auto now = espchrono::millis_clock::now();
// 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
}
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)
{
battery::bootBatPercentage = getBatteryPercentage(*avgVoltage, BatteryCellType(configs.battery.cellType.value));
battery::bootBatWh = getRemainingWattHours();
}
}
2021-12-11 05:05:46 +01:00
}
}
2021-06-28 21:43:20 +02:00
}
}