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

146 lines
4.0 KiB
C++
Raw Normal View History

2021-07-20 14:10:26 +02:00
constexpr const char * const TAG = "BOBBY";
2021-12-08 22:49:25 +01:00
#ifndef OTA_USERNAME
#error No OTA username!
#endif
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-11-02 01:43:58 +01:00
#include "displays/calibratedisplay.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;
stringSettings = presets::makeDefaultStringSettings();
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");
bootLabel.redraw("deviceName");
if (const auto result = wifi_stack::get_default_mac_addr())
std::sprintf(deviceName, STRING(DEVICE_PREFIX) "_%02hhx%02hhx%02hhx", result->at(3), result->at(4), result->at(5));
else
ESP_LOGE("MAIN", "get_default_mac_addr() failed: %.*s", result.error().size(), result.error().data());
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-06-28 21:43:20 +02:00
#if defined(FEATURE_DPAD_5WIRESW) && defined(DPAD_5WIRESW_DEBUG)
switchScreen<DPad5WireDebugDisplay>();
2021-09-17 02:45:20 +02:00
#elif defined(FEATURE_DPAD_5WIRESW_2OUT) && defined(DPAD_5WIRESW_DEBUG)
switchScreen<DPad5Wire2OutDebugDisplay>();
2021-09-30 23:17:42 +02:00
#elif defined(FEATURE_DPAD_6WIRESW) && defined(DPAD_6WIRESW_DEBUG)
switchScreen<DPad6WireDebugDisplay>();
2021-08-09 20:59:55 +02:00
#else
2021-06-28 21:43:20 +02:00
2021-12-17 22:40:08 +01:00
if (settings.lockscreen.keepLockedAfterReboot && settings.lockscreen.locked)
{
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)
espgui::switchScreen<CalibrateDisplay>(true);
else
{
espgui::switchScreen<StatusDisplay>();
}
}
2021-08-09 20:59:55 +02:00
#endif
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/settings.boardcomputerHardware.timersSettings.statsUpdateRate)
{
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
}
}