From f9aec8b6906599296417af5414b1c72a3cf53e73 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 22 Feb 2024 14:16:33 +1100 Subject: split the lua thread registry into its own files --- src/lua/registry.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/lua/registry.cpp (limited to 'src/lua/registry.cpp') 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 -- cgit v1.2.3