From ff87c9217577783b60ee4cf466a3c59777a2fc40 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Fri, 27 Dec 2024 23:23:49 -0800 Subject: [PATCH] Write settings to nvs when changing from the setting screen Otherwise, settings may not actually be saved if the device crashes/ reboots unexpectedly. Fixes #148 --- lua/settings.lua | 4 ++++ luals-stubs/nvs.lua | 9 +++++++++ src/tangara/lua/bridge.cpp | 2 ++ src/tangara/lua/lua_nvs.cpp | 39 +++++++++++++++++++++++++++++++++++++ src/tangara/lua/lua_nvs.hpp | 15 ++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 luals-stubs/nvs.lua create mode 100644 src/tangara/lua/lua_nvs.cpp create mode 100644 src/tangara/lua/lua_nvs.hpp diff --git a/lua/settings.lua b/lua/settings.lua index 8547aa19..9018d884 100644 --- a/lua/settings.lua +++ b/lua/settings.lua @@ -13,6 +13,7 @@ local usb = require("usb") local font = require("font") local main_menu = require("main_menu") local img = require("images") +local nvs = require("nvs") local settings = {} @@ -33,6 +34,9 @@ local SettingsScreen = widgets.MenuScreen:new { pad_left = 4, pad_right = 4, } + end, + on_hide = function(self) + nvs.write(); end } diff --git a/luals-stubs/nvs.lua b/luals-stubs/nvs.lua new file mode 100644 index 00000000..9e6eb8ee --- /dev/null +++ b/luals-stubs/nvs.lua @@ -0,0 +1,9 @@ +--- @meta + +--- @class nvs +local nvs = {} + +--- Saves current nvs +function nvs.write() end + +return nvs diff --git a/src/tangara/lua/bridge.cpp b/src/tangara/lua/bridge.cpp index 1c757a22..9521f265 100644 --- a/src/tangara/lua/bridge.cpp +++ b/src/tangara/lua/bridge.cpp @@ -25,6 +25,7 @@ #include "lua/lua_database.hpp" #include "lua/lua_filesystem.hpp" #include "lua/lua_font.hpp" +#include "lua/lua_nvs.hpp" #include "lua/lua_queue.hpp" #include "lua/lua_screen.hpp" #include "lua/lua_testing.hpp" @@ -84,6 +85,7 @@ auto Bridge::installBaseModules(lua_State* L) -> void { RegisterVersionModule(L); RegisterThemeModule(L); RegisterScreenModule(L); + RegisterNvsModule(L); } auto Bridge::installLvgl(lua_State* L) -> void { diff --git a/src/tangara/lua/lua_nvs.cpp b/src/tangara/lua/lua_nvs.cpp new file mode 100644 index 00000000..e38e58d9 --- /dev/null +++ b/src/tangara/lua/lua_nvs.cpp @@ -0,0 +1,39 @@ +/* + * Copyright 2024 Clayton Craft + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "lua/lua_version.hpp" + +#include "lua.hpp" +#include "lua/bridge.hpp" + +#include "lua.h" +#include "lua/lua_thread.hpp" + +namespace lua { + +[[maybe_unused]] static constexpr char kTag[] = "lua_nvs"; + +static auto write(lua_State* L) -> int { + Bridge* instance = Bridge::Get(L); + instance->services().nvs().Write(); + + return 1; +} + +static const struct luaL_Reg kNvsFuncs[] = {{"write", write}, + {NULL, NULL}}; + +static auto lua_nvs(lua_State* L) -> int { + luaL_newlib(L, kNvsFuncs); + return 1; +} + +auto RegisterNvsModule(lua_State* L) -> void { + luaL_requiref(L, "nvs", lua_nvs, true); + lua_pop(L, 1); +} + +} // namespace lua diff --git a/src/tangara/lua/lua_nvs.hpp b/src/tangara/lua/lua_nvs.hpp new file mode 100644 index 00000000..1275fcc7 --- /dev/null +++ b/src/tangara/lua/lua_nvs.hpp @@ -0,0 +1,15 @@ +/* + * Copyright 2024 Clayton Craft + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include "lua.hpp" + +namespace lua { + +auto RegisterNvsModule(lua_State*) -> void; + +} // namespace lua