From 1573a8c4cde1cd9528b422b2dcc598e37ffe94a7 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 2 May 2024 19:12:26 +1000 Subject: WIP merge cyclically dependent components into one big component --- src/system_fsm/include/service_locator.hpp | 153 ----------------------------- src/system_fsm/include/system_events.hpp | 89 ----------------- src/system_fsm/include/system_fsm.hpp | 146 --------------------------- 3 files changed, 388 deletions(-) delete mode 100644 src/system_fsm/include/service_locator.hpp delete mode 100644 src/system_fsm/include/system_events.hpp delete mode 100644 src/system_fsm/include/system_fsm.hpp (limited to 'src/system_fsm/include') diff --git a/src/system_fsm/include/service_locator.hpp b/src/system_fsm/include/service_locator.hpp deleted file mode 100644 index 5978578c..00000000 --- a/src/system_fsm/include/service_locator.hpp +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 2023 jacqueline - * - * SPDX-License-Identifier: GPL-3.0-only - */ - -#pragma once - -#include - -#include "battery.hpp" -#include "bluetooth.hpp" -#include "collation.hpp" -#include "database.hpp" -#include "gpios.hpp" -#include "haptics.hpp" -#include "nvs.hpp" -#include "samd.hpp" -#include "storage.hpp" -#include "tag_parser.hpp" -#include "tasks.hpp" -#include "touchwheel.hpp" -#include "track_queue.hpp" - -namespace system_fsm { - -class ServiceLocator { - public: - ServiceLocator(); - - auto gpios() -> drivers::Gpios& { - assert(gpios_ != nullptr); - return *gpios_; - } - - auto gpios(std::unique_ptr i) { gpios_ = std::move(i); } - - auto samd() -> drivers::Samd& { - assert(samd_ != nullptr); - return *samd_; - } - - auto samd(std::unique_ptr i) { samd_ = std::move(i); } - - auto nvs() -> drivers::NvsStorage& { - assert(nvs_ != nullptr); - return *nvs_; - } - - auto nvs(std::unique_ptr i) { nvs_ = std::move(i); } - - auto sd() -> drivers::SdState& { return sd_; } - - auto sd(drivers::SdState s) { sd_ = s; } - - auto bluetooth() -> drivers::Bluetooth& { - assert(bluetooth_ != nullptr); - return *bluetooth_; - } - - auto bluetooth(std::unique_ptr i) { - bluetooth_ = std::move(i); - } - - auto battery() -> battery::Battery& { - assert(battery_ != nullptr); - return *battery_; - } - - auto battery(std::unique_ptr i) { battery_ = std::move(i); } - - auto touchwheel() -> std::optional { - if (!touchwheel_) { - return {}; - } - return touchwheel_.get(); - } - - auto touchwheel(std::unique_ptr i) { - touchwheel_ = std::move(i); - } - - auto haptics() -> drivers::Haptics& { return *haptics_; } - - auto haptics(std::unique_ptr i) { haptics_ = std::move(i); } - - auto database() -> std::weak_ptr { return database_; } - - auto database(std::unique_ptr i) { - database_ = std::move(i); - } - - auto tag_parser() -> database::ITagParser& { - assert(tag_parser_ != nullptr); - return *tag_parser_; - } - - auto tag_parser(std::unique_ptr i) { - tag_parser_ = std::move(i); - } - - auto track_queue() -> audio::TrackQueue& { - assert(queue_ != nullptr); - return *queue_; - } - - auto track_queue(std::unique_ptr i) { - queue_ = std::move(i); - } - - auto collator() -> locale::ICollator& { - assert(collator_ != nullptr); - return *collator_; - } - - auto collator(std::unique_ptr i) { - collator_ = std::move(i); - } - - auto bg_worker() -> tasks::WorkerPool& { - assert(bg_worker_ != nullptr); - return *bg_worker_; - } - - auto bg_worker(std::unique_ptr w) -> void { - bg_worker_ = std::move(w); - } - - // Not copyable or movable. - ServiceLocator(const ServiceLocator&) = delete; - ServiceLocator& operator=(const ServiceLocator&) = delete; - - private: - std::unique_ptr gpios_; - std::unique_ptr samd_; - std::unique_ptr nvs_; - std::unique_ptr touchwheel_; - std::unique_ptr haptics_; - std::unique_ptr bluetooth_; - - std::unique_ptr queue_; - std::unique_ptr battery_; - - std::shared_ptr database_; - std::unique_ptr tag_parser_; - std::unique_ptr collator_; - - std::unique_ptr bg_worker_; - - drivers::SdState sd_; -}; - -} // namespace system_fsm diff --git a/src/system_fsm/include/system_events.hpp b/src/system_fsm/include/system_events.hpp deleted file mode 100644 index 22e3b6bd..00000000 --- a/src/system_fsm/include/system_events.hpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2023 jacqueline - * - * SPDX-License-Identifier: GPL-3.0-only - */ - -#pragma once - -#include - -#include "battery.hpp" -#include "bluetooth_types.hpp" -#include "database.hpp" -#include "ff.h" -#include "haptics.hpp" -#include "samd.hpp" -#include "service_locator.hpp" -#include "tinyfsm.hpp" - -namespace system_fsm { - -struct DisplayReady : tinyfsm::Event {}; - -/* - * Sent by SysState when the system has finished with its boot and self-test, - * and is now ready to run normally. - */ -struct BootComplete : tinyfsm::Event { - std::shared_ptr services; -}; - -/* - * May be sent by any component to indicate that the system has experienced an - * unrecoverable error. This should be used sparingly, as it essentially brings - * down the device. - */ -struct FatalError : tinyfsm::Event {}; - -struct OnIdle : tinyfsm::Event {}; - -/* - * Sent by SysState when the system storage has been successfully mounted. - */ -struct StorageMounted : tinyfsm::Event {}; - -struct StorageError : tinyfsm::Event { - FRESULT error; -}; - -struct KeyLockChanged : tinyfsm::Event { - bool locking; -}; -struct HasPhonesChanged : tinyfsm::Event { - bool has_headphones; -}; -struct SdDetectChanged : tinyfsm::Event { - bool has_sd_card; -}; - -struct SamdUsbMscChanged : tinyfsm::Event { - bool en; -}; -struct SamdUsbStatusChanged : tinyfsm::Event { - drivers::Samd::UsbStatus new_status; -}; - -struct BatteryStateChanged : tinyfsm::Event { - battery::Battery::BatteryState new_state; -}; - -struct BluetoothEvent : tinyfsm::Event { - drivers::bluetooth::Event event; -}; - -struct HapticTrigger : tinyfsm::Event { - drivers::Haptics::Effect effect; -}; - -namespace internal { - -struct GpioInterrupt : tinyfsm::Event {}; -struct SamdInterrupt : tinyfsm::Event {}; - -struct IdleTimeout : tinyfsm::Event {}; -struct UnmountTimeout : tinyfsm::Event {}; - -} // namespace internal - -} // namespace system_fsm diff --git a/src/system_fsm/include/system_fsm.hpp b/src/system_fsm/include/system_fsm.hpp deleted file mode 100644 index f01afb3f..00000000 --- a/src/system_fsm/include/system_fsm.hpp +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright 2023 jacqueline - * - * SPDX-License-Identifier: GPL-3.0-only - */ - -#pragma once - -#include - -#include "app_console.hpp" -#include "audio_events.hpp" -#include "battery.hpp" -#include "bluetooth.hpp" -#include "database.hpp" -#include "db_events.hpp" -#include "display.hpp" -#include "gpios.hpp" -#include "nvs.hpp" -#include "samd.hpp" -#include "service_locator.hpp" -#include "storage.hpp" -#include "tag_parser.hpp" -#include "tinyfsm.hpp" -#include "touchwheel.hpp" - -#include "freertos/FreeRTOS.h" -#include "freertos/timers.h" - -#include "system_events.hpp" -#include "track_queue.hpp" - -namespace system_fsm { - -void check_interrupts_cb(TimerHandle_t timer); - -/* - * State machine for the overall system state. Responsible for managing - * peripherals, and bringing the rest of the system up and down. - */ -class SystemState : public tinyfsm::Fsm { - public: - virtual ~SystemState() {} - - virtual void entry() {} - virtual void exit() {} - - /* Fallback event handler. Does nothing. */ - void react(const tinyfsm::Event& ev) {} - - void react(const FatalError&); - void react(const internal::GpioInterrupt&); - void react(const internal::SamdInterrupt&); - - void react(const HapticTrigger&); - - virtual void react(const DisplayReady&) {} - virtual void react(const BootComplete&) {} - virtual void react(const StorageMounted&) {} - virtual void react(const StorageError&) {} - virtual void react(const KeyLockChanged&) {} - virtual void react(const SdDetectChanged&) {} - virtual void react(const SamdUsbMscChanged&) {} - virtual void react(const database::event::UpdateFinished&) {} - virtual void react(const audio::PlaybackUpdate&) {} - virtual void react(const internal::IdleTimeout&) {} - virtual void react(const internal::UnmountTimeout&) {} - - protected: - auto IdleCondition() -> bool; - - static std::shared_ptr sServices; - static std::unique_ptr sStorage; - - static console::AppConsole* sAppConsole; -}; - -namespace states { - -/* - * Initial state. Initialises peripherals, starts up lvgl, checks everything - * looks good. - */ -class Booting : public SystemState { - public: - void entry() override; - void exit() override; - - void react(const BootComplete&) override; - using SystemState::react; -}; - -/* - * Most common state. Everything is going full bore! - */ -class Running : public SystemState { - public: - void entry() override; - void exit() override; - - void react(const KeyLockChanged&) override; - void react(const SdDetectChanged&) override; - void react(const audio::PlaybackUpdate&) override; - void react(const database::event::UpdateFinished&) override; - void react(const SamdUsbMscChanged&) override; - void react(const internal::UnmountTimeout&) override; - void react(const StorageError&) override; - - using SystemState::react; - - private: - auto checkIdle() -> void; - - auto mountStorage() -> bool; - auto unmountStorage() -> void; - - bool storage_mounted_; -}; - -/** - * State for when the screen is off, controls locked, and music paused. Prelude - * to shutting off power completely. - */ -class Idle : public SystemState { - public: - void entry() override; - void exit() override; - - void react(const KeyLockChanged&) override; - void react(const internal::IdleTimeout&) override; - - using SystemState::react; - - private: - TimerHandle_t sIdleTimeout; -}; - -/* - * Something unrecoverably bad went wrong. Shows an error (if possible), awaits - * reboot. - */ -class Error : public SystemState {}; - -} // namespace states - -} // namespace system_fsm -- cgit v1.2.3