From a6ab1504058304012791281f9eb42c262745888f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 19 May 2023 21:21:27 +1000 Subject: Add tinyfsm, start converting core functions to an FSM-based event loop --- src/main/main.cpp | 114 +++++------------------------------------------------- 1 file changed, 9 insertions(+), 105 deletions(-) (limited to 'src/main/main.cpp') diff --git a/src/main/main.cpp b/src/main/main.cpp index 29ac2c7f..df1eb8b2 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -1,114 +1,18 @@ -#include -#include -#include - -#include -#include -#include - -#include "driver/gpio.h" -#include "driver/i2c.h" -#include "driver/sdspi_host.h" -#include "driver/spi_common.h" -#include "driver/spi_master.h" -#include "driver_cache.hpp" -#include "esp_freertos_hooks.h" -#include "esp_heap_caps.h" -#include "esp_intr_alloc.h" -#include "esp_log.h" #include "freertos/portmacro.h" -#include "freertos/projdefs.h" -#include "hal/gpio_types.h" -#include "hal/spi_types.h" -#include "app_console.hpp" -#include "audio_playback.hpp" -#include "battery.hpp" -#include "dac.hpp" -#include "database.hpp" -#include "display.hpp" -#include "display_init.hpp" -#include "gpio_expander.hpp" -#include "i2c.hpp" -#include "lvgl_task.hpp" -#include "samd.hpp" -#include "spi.hpp" -#include "storage.hpp" -#include "touchwheel.hpp" +#include "tinyfsm.hpp" -static const char* TAG = "MAIN"; +#include "audio_fsm.hpp" +#include "event_queue.hpp" +#include "system_fsm.hpp" +#include "ui_fsm.hpp" extern "C" void app_main(void) { - ESP_LOGI(TAG, "Initialising peripherals"); - - ESP_ERROR_CHECK(drivers::init_i2c()); - ESP_ERROR_CHECK(drivers::init_spi()); - std::unique_ptr drivers = - std::make_unique(); - - ESP_LOGI(TAG, "Init GPIOs"); - drivers::GpioExpander* expander = drivers->AcquireGpios(); - - ESP_LOGI(TAG, "Init SAMD comms"); - drivers::Samd samd; - ESP_LOGI(TAG, "It might have worked? Let's read something!"); - auto res = samd.ReadChargeStatus(); - if (res) { - ESP_LOGI(TAG, "Charge status is %d", static_cast(*res)); - } else { - ESP_LOGI(TAG, "no charge status?"); - } - - ESP_LOGI(TAG, "Enable power rails for development"); - expander->with( - [&](auto& gpio) { gpio.set_pin(drivers::GpioExpander::AMP_EN, 1); }); - - ESP_LOGI(TAG, "Init battery measurement"); - drivers::Battery* battery = new drivers::Battery(); - ESP_LOGI(TAG, "it's reading %d mV!", (int)battery->Millivolts()); - - ESP_LOGI(TAG, "Init SD card"); - auto storage = drivers->AcquireStorage(); - if (!storage) { - ESP_LOGE(TAG, "Failed! Do you have an SD card?"); - } - - ESP_LOGI(TAG, "Init touch wheel"); - std::shared_ptr touchwheel = - drivers->AcquireTouchWheel(); - - std::atomic lvgl_quit; - TaskHandle_t lvgl_task_handle; - ui::StartLvgl(drivers.get(), &lvgl_quit, &lvgl_task_handle); - - std::unique_ptr playback; - if (storage) { - ESP_LOGI(TAG, "Init audio pipeline"); - playback = std::make_unique(drivers.get()); - } - - ESP_LOGI(TAG, "Init database"); - std::unique_ptr db; - auto db_res = database::Database::Open(); - if (db_res.has_value()) { - db.reset(db_res.value()); - } - - ESP_LOGI(TAG, "Waiting for background tasks before launching console..."); - vTaskDelay(pdMS_TO_TICKS(1000)); - - ESP_LOGI(TAG, "Launch console"); - console::AppConsole console(playback.get(), db.get()); - console.Launch(); + tinyfsm::FsmList::start(); - uint8_t prev_position = 0; + auto& queue = events::EventQueue::GetInstance(); while (1) { - touchwheel->Update(); - auto wheel_data = touchwheel->GetTouchWheelData(); - if (wheel_data.wheel_position != prev_position) { - prev_position = wheel_data.wheel_position; - ESP_LOGI(TAG, "Touch wheel pos: %u", prev_position); - } - vTaskDelay(pdMS_TO_TICKS(100)); + queue.Service(portMAX_DELAY); } } -- cgit v1.2.3