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

456 lines
11 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_wifi_types.h>
#include <esp_log.h>
2021-06-28 21:43:20 +02:00
// Arduino includes
#include <Arduino.h>
// 3rdparty lib includes
#include <espchrono.h>
using namespace std::chrono_literals;
#include <espwifistack.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 "dpad.h"
2021-08-11 19:26:48 +02:00
#ifdef FEATURE_DPAD_3WIRESW
2021-06-28 21:43:20 +02:00
#include "dpad3wire.h"
2021-08-11 19:26:48 +02:00
#endif
#ifdef FEATURE_DPAD_5WIRESW
2021-06-28 21:43:20 +02:00
#include "dpad5wire.h"
2021-08-11 19:26:48 +02:00
#endif
2021-09-17 02:45:20 +02:00
#ifdef FEATURE_DPAD_5WIRESW_2OUT
#include "dpad5wire_2out.h"
#endif
2021-09-30 23:17:42 +02:00
#ifdef FEATURE_DPAD_6WIRESW
#include "dpad6wire.h"
#endif
2021-08-11 19:26:48 +02:00
#ifdef FEATURE_ROTARY
2021-06-28 21:43:20 +02:00
#include "rotary.h"
2021-08-11 19:26:48 +02:00
#endif
2021-06-28 21:43:20 +02:00
#include "serialhandler.h"
2021-08-11 19:26:48 +02:00
#ifdef FEATURE_OTA
2021-06-28 21:43:20 +02:00
#include "ota.h"
2021-10-18 12:59:38 +02:00
#include "displays/menus/selectbuildservermenu.h"
2021-08-11 19:26:48 +02:00
#endif
2021-06-28 21:43:20 +02:00
#include "presets.h"
#include "statistics.h"
#ifdef FEATURE_BLUETOOTH
#include "actions/bluetoothbeginaction.h"
#include "actions/bluetoothbeginmasteraction.h"
2021-08-11 19:26:48 +02:00
#ifdef FEATURE_BMS
2021-06-28 21:43:20 +02:00
#include "actions/bluetoothconnectbmsaction.h"
#endif
2021-08-11 19:26:48 +02:00
#endif
2021-07-17 05:10:16 +02:00
#ifdef FEATURE_BLE
2021-09-18 18:02:57 +02:00
#include "ble_bobby.h"
2021-07-17 05:10:16 +02:00
#endif
#ifdef FEATURE_WEBSERVER
#include "webserver.h"
#endif
2021-06-28 21:43:20 +02:00
#ifdef FEATURE_CAN
#include "can.h"
#endif
#ifdef FEATURE_CLOUD
#include "cloud.h"
2021-11-21 01:37:51 +01:00
#include "udpcloud.h"
2021-06-28 21:43:20 +02:00
#endif
2021-06-29 10:59:44 +02:00
#include "wifi_bobbycar.h"
2021-08-11 22:41:37 +02:00
#include "time_bobbycar.h"
2021-08-17 22:06:13 +02:00
#ifdef FEATURE_LEDSTRIP
#include "ledstrip.h"
#endif
2021-11-02 01:43:58 +01:00
#include "modes/defaultmode.h"
#include "displays/statusdisplay.h"
#include "displays/calibratedisplay.h"
2021-11-14 03:20:21 +01:00
#ifdef FEATURE_DNS_NS
#include "dnsannounce.h"
2021-11-14 03:20:21 +01:00
#endif
#include "drivingstatistics.h"
2021-11-21 23:08:32 +01:00
#include "newsettings.h"
2021-06-28 21:43:20 +02:00
namespace {
2021-08-09 20:59:55 +02:00
std::optional<espchrono::millis_clock::time_point> lastWifiUpdate;
2021-06-28 21:43:20 +02:00
std::optional<espchrono::millis_clock::time_point> lastPotiRead;
std::optional<espchrono::millis_clock::time_point> lastModeUpdate;
std::optional<espchrono::millis_clock::time_point> lastStatsUpdate;
std::optional<espchrono::millis_clock::time_point> lastDisplayUpdate;
std::optional<espchrono::millis_clock::time_point> lastDisplayRedraw;
2021-08-09 20:59:55 +02:00
#ifdef FEATURE_CAN
std::optional<espchrono::millis_clock::time_point> lastCanParse;
#endif
2021-07-17 05:10:16 +02:00
#ifdef FEATURE_BLE
std::optional<espchrono::millis_clock::time_point> lastBleUpdate;
#endif
2021-08-10 05:03:50 +02:00
#ifdef FEATURE_CLOUD
2021-09-21 21:45:20 +02:00
std::optional<espchrono::millis_clock::time_point> lastCloudCollect;
std::optional<espchrono::millis_clock::time_point> lastCloudSend;
2021-08-10 05:03:50 +02:00
#endif
2021-08-12 00:23:41 +02:00
#ifdef FEATURE_NTP
std::optional<espchrono::millis_clock::time_point> lastNtpUpdate;
#endif
2021-08-17 22:06:13 +02:00
#ifdef FEATURE_LEDSTRIP
std::optional<espchrono::millis_clock::time_point> lastLedstripUpdate;
#endif
2021-06-28 21:43:20 +02:00
}
extern "C" void app_main()
{
Serial.begin(115200);
//Serial.setDebugOutput(true);
//Serial.println("setup()");
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
pinMode(3, INPUT_PULLUP);
currentlyReverseBeeping = false;
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());
bootLabel.redraw("wifi");
wifi_begin();
2021-06-28 21:43:20 +02:00
#ifdef FEATURE_DPAD
bootLabel.redraw("dpad");
dpad::init();
#endif
#ifdef FEATURE_DPAD_3WIRESW
bootLabel.redraw("dpad3wire");
dpad3wire::init();
#endif
#ifdef FEATURE_DPAD_5WIRESW
bootLabel.redraw("dpad5wire");
dpad5wire::init();
#endif
2021-09-17 02:45:20 +02:00
#ifdef FEATURE_DPAD_5WIRESW_2OUT
bootLabel.redraw("dpad5wire_2out");
dpad5wire_2out::init();
#endif
2021-09-30 23:17:42 +02:00
#ifdef FEATURE_DPAD_6WIRESW
bootLabel.redraw("dpad6wire");
dpad6wire::init();
#endif
2021-06-28 21:43:20 +02:00
#ifdef FEATURE_ROTARY
bootLabel.redraw("rotary");
initRotary();
#endif
#ifdef FEATURE_MOSFETS
bootLabel.redraw("mosfets");
pinMode(PINS_MOSFET0, OUTPUT);
pinMode(PINS_MOSFET1, OUTPUT);
pinMode(PINS_MOSFET2, OUTPUT);
digitalWrite(PINS_MOSFET0, LOW);
digitalWrite(PINS_MOSFET1, LOW);
digitalWrite(PINS_MOSFET2, LOW);
#endif
2021-06-29 10:59:44 +02:00
#ifdef FEATURE_SERIAL
bootLabel.redraw("swap front back");
updateSwapFrontBack();
#endif
2021-06-28 21:43:20 +02:00
#ifdef FEATURE_BLUETOOTH
if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Master)
{
bootLabel.redraw("bluetooth begin master");
BluetoothBeginMasterAction{}.triggered();
#ifdef FEATURE_BMS
if (settings.autoConnectBms)
{
bootLabel.redraw("connect BMS");
BluetoothConnectBmsAction{}.triggered();
}
#endif
}
else if (settings.bluetoothSettings.autoBluetoothMode == BluetoothMode::Slave)
{
bootLabel.redraw("bluetooth begin");
BluetoothBeginAction{}.triggered();
}
#endif
#ifdef FEATURE_CAN
2021-08-17 22:06:13 +02:00
bootLabel.redraw("can");
2021-06-28 21:43:20 +02:00
can::initCan();
#endif
#ifdef FEATURE_SERIAL
bootLabel.redraw("front Serial begin");
controllers.front.serial.get().begin(38400, SERIAL_8N1, PINS_RX1, PINS_TX1);
bootLabel.redraw("back Serial begin");
controllers.back.serial.get().begin(38400, SERIAL_8N1, PINS_RX2, PINS_TX2);
#endif
2021-08-17 22:06:13 +02:00
#ifdef FEATURE_LEDSTRIP
bootLabel.redraw("LED strip");
initLedStrip();
#endif
2021-06-28 21:43:20 +02:00
raw_gas = std::nullopt;
raw_brems = std::nullopt;
gas = std::nullopt;
brems = std::nullopt;
for (Controller &controller : controllers)
controller.command.buzzer = {};
currentMode = &modes::defaultMode;
2021-08-09 12:57:40 +02:00
#ifdef FEATURE_OTA
2021-06-28 21:43:20 +02:00
bootLabel.redraw("ota");
initOta();
#endif
2021-07-17 05:10:16 +02:00
#ifdef FEATURE_BLE
bootLabel.redraw("ble");
initBle();
#endif
2021-06-28 21:43:20 +02:00
#ifdef FEATURE_WEBSERVER
bootLabel.redraw("webserver");
initWebserver();
#endif
bootLabel.redraw("potis");
readPotis();
#ifdef FEATURE_CLOUD
2021-08-10 05:03:50 +02:00
bootLabel.redraw("cloud");
initCloud();
2021-06-28 21:43:20 +02:00
#endif
2021-08-12 00:23:41 +02:00
#ifdef FEATURE_NTP
bootLabel.redraw("time");
initTime();
#endif
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
if (!gas || !brems || *gas > 200.f || *brems > 200.f)
2021-11-02 01:43:58 +01:00
espgui::switchScreen<CalibrateDisplay>(true);
2021-06-28 21:43:20 +02:00
else
2021-11-02 01:43:58 +01:00
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-08-09 20:59:55 +02:00
if (!lastWifiUpdate || now - *lastWifiUpdate >= 100ms)
{
wifi_update();
lastWifiUpdate = now;
}
2021-06-29 10:59:44 +02:00
InputDispatcher::update();
2021-06-28 21:43:20 +02:00
#ifdef FEATURE_DPAD
dpad::update();
#endif
#ifdef FEATURE_DPAD_3WIRESW
dpad3wire::update();
#endif
#ifdef FEATURE_DPAD_5WIRESW
dpad5wire::update();
#endif
2021-09-17 02:45:20 +02:00
#ifdef FEATURE_DPAD_5WIRESW_2OUT
dpad5wire_2out::update();
#endif
2021-09-30 23:17:42 +02:00
#ifdef FEATURE_DPAD_6WIRESW
dpad6wire::update();
#endif
2021-06-28 21:43:20 +02:00
if (!lastPotiRead || now - *lastPotiRead >= 1000ms/settings.boardcomputerHardware.timersSettings.potiReadRate)
{
readPotis();
lastPotiRead = now;
}
if (!lastModeUpdate || now - *lastModeUpdate >= 1000ms/settings.boardcomputerHardware.timersSettings.modeUpdateRate)
{
if (lastMode != currentMode)
{
if (lastMode)
lastMode->stop();
lastMode = currentMode;
if (currentMode)
currentMode->start();
}
if (currentMode)
currentMode->update();
lastModeUpdate = now;
performance.current++;
}
if (!lastStatsUpdate || now - *lastStatsUpdate >= 1000ms/settings.boardcomputerHardware.timersSettings.statsUpdateRate)
{
updateAccumulators();
pushStats();
lastStatsUpdate = now;
}
if (!lastDisplayUpdate || now - *lastDisplayUpdate >= 1000ms/settings.boardcomputerHardware.timersSettings.displayUpdateRate)
{
updateDisplay();
lastDisplayUpdate = now;
}
if (!lastDisplayRedraw || now - *lastDisplayRedraw >= 1000ms/settings.boardcomputerHardware.timersSettings.displayRedrawRate)
{
redrawDisplay();
lastDisplayRedraw = now;
}
if (now - performance.lastTime >= 1000ms)
{
performance.last = performance.current;
performance.current = 0;
performance.lastTime = now;
}
#ifdef FEATURE_CAN
2021-08-09 23:15:26 +02:00
if (!lastCanParse || now - *lastCanParse >= 1000ms/settings.boardcomputerHardware.timersSettings.canReceiveRate)
2021-08-09 20:59:55 +02:00
{
2021-08-09 23:15:26 +02:00
//can::tryParseCanInput();
2021-08-09 20:59:55 +02:00
can::parseCanInput();
lastCanParse = now;
}
2021-06-28 21:43:20 +02:00
#endif
#ifdef FEATURE_SERIAL
for (Controller &controller : controllers)
controller.parser.update();
#endif
handleSerial();
2021-08-09 12:57:40 +02:00
#ifdef FEATURE_OTA
2021-06-28 21:43:20 +02:00
handleOta();
#endif
2021-07-17 05:10:16 +02:00
#ifdef FEATURE_BLE
if (!lastBleUpdate || now - *lastBleUpdate >= 250ms)
2021-07-17 05:10:16 +02:00
{
handleBle();
lastBleUpdate = now;
}
#endif
2021-08-10 05:03:50 +02:00
#ifdef FEATURE_CLOUD
2021-09-21 21:45:20 +02:00
if (!lastCloudCollect || now - *lastCloudCollect >= std::chrono::milliseconds{settings.boardcomputerHardware.timersSettings.cloudCollectRate})
2021-08-10 05:03:50 +02:00
{
2021-09-21 21:45:20 +02:00
cloudCollect();
2021-08-10 05:03:50 +02:00
2021-09-21 21:45:20 +02:00
lastCloudCollect = now;
}
if (!lastCloudSend || now - *lastCloudSend >= 1000ms/settings.boardcomputerHardware.timersSettings.cloudSendRate)
{
cloudSend();
lastCloudSend = now;
2021-08-10 05:03:50 +02:00
}
#endif
2021-08-12 00:23:41 +02:00
#ifdef FEATURE_NTP
if (!lastNtpUpdate || now - *lastNtpUpdate >= 100ms)
{
updateTime();
lastNtpUpdate = now;
}
#endif
2021-06-28 21:43:20 +02:00
#ifdef FEATURE_WEBSERVER
handleWebserver();
#endif
#ifdef FEATURE_BMS
bms::update();
#endif
2021-08-17 22:06:13 +02:00
#ifdef FEATURE_LEDSTRIP
if (!lastLedstripUpdate || now - *lastLedstripUpdate >= 1000ms / 60)
{
updateLedStrip();
lastLedstripUpdate = now;
}
2021-11-14 03:20:21 +01:00
#endif
#ifdef FEATURE_DNS_NS
handle_dns_announce();
2021-08-17 22:06:13 +02:00
#endif
calculateStatistics();
2021-11-21 02:08:43 +01:00
#ifdef FEATURE_CLOUD
2021-11-21 14:34:55 +01:00
if (settings.cloudSettings.udpCloudEnabled)
sendUdpCloudPacket();
2021-11-21 02:08:43 +01:00
#endif
2021-06-28 21:43:20 +02:00
}
}