From f9aec8b6906599296417af5414b1c72a3cf53e73 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 22 Feb 2024 14:16:33 +1100 Subject: [PATCH] split the lua thread registry into its own files --- src/app_console/app_console.cpp | 2 +- src/lua/CMakeLists.txt | 2 +- src/lua/include/lua_registry.hpp | 51 ++++++++++++++++++++++ src/lua/include/lua_thread.hpp | 34 --------------- src/lua/lua_queue.cpp | 3 +- src/lua/lua_thread.cpp | 47 -------------------- src/lua/registry.cpp | 73 ++++++++++++++++++++++++++++++++ src/ui/ui_fsm.cpp | 1 + 8 files changed, 129 insertions(+), 84 deletions(-) create mode 100644 src/lua/include/lua_registry.hpp create mode 100644 src/lua/registry.cpp diff --git a/src/app_console/app_console.cpp b/src/app_console/app_console.cpp index 4e6c97ba..94a48955 100644 --- a/src/app_console/app_console.cpp +++ b/src/app_console/app_console.cpp @@ -40,7 +40,7 @@ #include "freertos/projdefs.h" #include "haptics.hpp" #include "index.hpp" -#include "lua_thread.hpp" +#include "lua_registry.hpp" #include "memory_resource.hpp" #include "samd.hpp" #include "service_locator.hpp" diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt index d89b22e8..ff0831c9 100644 --- a/src/lua/CMakeLists.txt +++ b/src/lua/CMakeLists.txt @@ -4,7 +4,7 @@ idf_component_register( SRCS "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp" - "lua_queue.cpp" "lua_version.cpp" "lua_controls.cpp" + "lua_queue.cpp" "lua_version.cpp" "lua_controls.cpp" "registry.cpp" INCLUDE_DIRS "include" REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" "esp_timer" "battery" "esp-idf-lua" "luavgl" "lua-linenoise" "lua-term" diff --git a/src/lua/include/lua_registry.hpp b/src/lua/include/lua_registry.hpp new file mode 100644 index 00000000..abc5063e --- /dev/null +++ b/src/lua/include/lua_registry.hpp @@ -0,0 +1,51 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include + +#include "lua.hpp" + +#include "bridge.hpp" +#include "lua_thread.hpp" +#include "service_locator.hpp" + +namespace lua { + +class Registry { + public: + static auto instance(system_fsm::ServiceLocator&) -> Registry&; + + auto uiThread() -> std::shared_ptr; + auto newThread() -> std::shared_ptr; + + auto AddPropertyModule( + const std::string&, + std::vector>>) + -> void; + + Registry(const Registry&) = delete; + Registry& operator=(const Registry&) = delete; + + private: + Registry(system_fsm::ServiceLocator&); + + system_fsm::ServiceLocator& services_; + std::unique_ptr bridge_; + + std::shared_ptr ui_thread_; + std::list> threads_; + + std::vector< + std::pair>>>> + modules_; +}; + +} // namespace lua diff --git a/src/lua/include/lua_thread.hpp b/src/lua/include/lua_thread.hpp index c12a0bfc..384de61d 100644 --- a/src/lua/include/lua_thread.hpp +++ b/src/lua/include/lua_thread.hpp @@ -10,9 +10,7 @@ #include #include "lua.hpp" -#include "lvgl.h" -#include "bridge.hpp" #include "service_locator.hpp" namespace lua { @@ -43,36 +41,4 @@ class LuaThread { lua_State* state_; }; -class Registry { - public: - static auto instance(system_fsm::ServiceLocator&) -> Registry&; - - auto uiThread() -> std::shared_ptr; - auto newThread() -> std::shared_ptr; - - auto AddPropertyModule( - const std::string&, - std::vector< - std::pair>>) - -> void; - - Registry(const Registry&) = delete; - Registry& operator=(const Registry&) = delete; - - private: - Registry(system_fsm::ServiceLocator&); - - system_fsm::ServiceLocator& services_; - std::unique_ptr bridge_; - - std::shared_ptr ui_thread_; - std::list> threads_; - - std::vector< - std::pair>>>> - modules_; -}; - } // namespace lua diff --git a/src/lua/lua_queue.cpp b/src/lua/lua_queue.cpp index 69d3b03d..dfb820c2 100644 --- a/src/lua/lua_queue.cpp +++ b/src/lua/lua_queue.cpp @@ -16,6 +16,7 @@ #include "lua.h" #include "lvgl.h" +#include "bridge.hpp" #include "database.hpp" #include "event_queue.hpp" #include "index.hpp" @@ -70,4 +71,4 @@ auto RegisterQueueModule(lua_State* s) -> void { lua_pop(s, 1); } -} // namespace lua \ No newline at end of file +} // namespace lua diff --git a/src/lua/lua_thread.cpp b/src/lua/lua_thread.cpp index 7d64e3c5..dd72e41d 100644 --- a/src/lua/lua_thread.cpp +++ b/src/lua/lua_thread.cpp @@ -184,51 +184,4 @@ auto CallProtected(lua_State* s, int nargs, int nresults) -> int { return ret; } -auto Registry::instance(system_fsm::ServiceLocator& s) -> Registry& { - static Registry sRegistry{s}; - return sRegistry; -} - -Registry::Registry(system_fsm::ServiceLocator& services) - : services_(services), bridge_(new Bridge(services)) {} - -auto Registry::uiThread() -> std::shared_ptr { - if (!ui_thread_) { - ui_thread_ = newThread(); - bridge_->installLvgl(ui_thread_->state()); - } - return ui_thread_; -} - -auto Registry::newThread() -> std::shared_ptr { - std::shared_ptr thread{LuaThread::Start(services_)}; - bridge_->installBaseModules(thread->state()); - for (auto& module : modules_) { - bridge_->installPropertyModule(thread->state(), module.first, - module.second); - } - threads_.push_back(thread); - return thread; -} - -auto Registry::AddPropertyModule( - const std::string& name, - std::vector>> - properties) -> void { - modules_.push_back(std::make_pair(name, properties)); - - // Any live threads will need to be updated to include the new module. - auto it = threads_.begin(); - while (it != threads_.end()) { - auto thread = it->lock(); - if (!thread) { - // Thread has been destroyed; stop tracking it. - it = threads_.erase(it); - } else { - bridge_->installPropertyModule(thread->state(), name, properties); - it++; - } - } -} - } // namespace lua diff --git a/src/lua/registry.cpp b/src/lua/registry.cpp new file mode 100644 index 00000000..a6487858 --- /dev/null +++ b/src/lua/registry.cpp @@ -0,0 +1,73 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "lua_registry.hpp" + +#include +#include + +#include "esp_heap_caps.h" +#include "esp_log.h" +#include "lua.hpp" + +#include "bridge.hpp" +#include "event_queue.hpp" +#include "memory_resource.hpp" +#include "service_locator.hpp" +#include "ui_events.hpp" + +namespace lua { + +[[maybe_unused]] static constexpr char kTag[] = "lua"; + +auto Registry::instance(system_fsm::ServiceLocator& s) -> Registry& { + static Registry sRegistry{s}; + return sRegistry; +} + +Registry::Registry(system_fsm::ServiceLocator& services) + : services_(services), bridge_(new Bridge(services)) {} + +auto Registry::uiThread() -> std::shared_ptr { + if (!ui_thread_) { + ui_thread_ = newThread(); + bridge_->installLvgl(ui_thread_->state()); + } + return ui_thread_; +} + +auto Registry::newThread() -> std::shared_ptr { + std::shared_ptr thread{LuaThread::Start(services_)}; + bridge_->installBaseModules(thread->state()); + for (auto& module : modules_) { + bridge_->installPropertyModule(thread->state(), module.first, + module.second); + } + threads_.push_back(thread); + return thread; +} + +auto Registry::AddPropertyModule( + const std::string& name, + std::vector>> + properties) -> void { + modules_.push_back(std::make_pair(name, properties)); + + // Any live threads will need to be updated to include the new module. + auto it = threads_.begin(); + while (it != threads_.end()) { + auto thread = it->lock(); + if (!thread) { + // Thread has been destroyed; stop tracking it. + it = threads_.erase(it); + } else { + bridge_->installPropertyModule(thread->state(), name, properties); + it++; + } + } +} + +} // namespace lua diff --git a/src/ui/ui_fsm.cpp b/src/ui/ui_fsm.cpp index e46832ba..9668b0f3 100644 --- a/src/ui/ui_fsm.cpp +++ b/src/ui/ui_fsm.cpp @@ -36,6 +36,7 @@ #include "encoder_input.hpp" #include "event_queue.hpp" #include "gpios.hpp" +#include "lua_registry.hpp" #include "lvgl_task.hpp" #include "nvs.hpp" #include "property.hpp"