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

134 lines
3.6 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"
2021-11-02 01:43:58 +01:00
#include "modes/defaultmode.h"
#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));
2021-08-17 22:06:13 +02:00
settings = presets::defaultSettings;
if (settingsPersister.init())
{
if (!settingsPersister.openCommon())
ESP_LOGE("BOBBY", "openCommon() failed");
if (!settingsPersister.openProfile(0))
ESP_LOGE("BOBBY", "openProfile(0) failed");
loadSettings();
}
else
ESP_LOGE("BOBBY", "init() failed");
2021-12-05 00:50:37 +01:00
for (const auto &task : schedulerTasks)
{
bootLabel.redraw(task.name());
task.setup();
}
2021-06-28 21:43:20 +02:00
currentMode = &modes::defaultMode;
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);
}
else if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked)
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();
2021-12-05 00:50:37 +01:00
for (auto &schedulerTask : schedulerTasks)
2021-08-09 20:59:55 +02:00
{
2021-12-05 00:50:37 +01:00
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
}
2021-12-11 05:05:46 +01:00
if (battery::bootBatPercentage == -1)
{
if(controllers.front.feedbackValid && controllers.back.feedbackValid)
{
float avgVoltage = 0;
for (auto &controller : controllers)
{
avgVoltage += controller.getCalibratedVoltage();
}
avgVoltage = avgVoltage / controllers.size();
if (avgVoltage > 30)
battery::bootBatPercentage = getBatteryPercentage(avgVoltage, BatteryCellType(settings.battery.cellType));
}
}
2021-06-28 21:43:20 +02:00
}
}