lua-rtos-libs/libs/settings.lua

52 lines
1.0 KiB
Lua

local function settings(name, defaults)
for key, value in pairs(defaults) do
if not nvs.exists(name, key) then
nvs.write(name, key, value)
end
end
local function get(key)
if nvs.exists(name, key) then
return nvs.read(name, key)
else
return nil
end
end
local function set(key, value)
if nvs.exists(name, key) then
return nvs.write(name, key, value)
end
end
local function restore()
for key, value in pairs(defaults) do
nvs.write(name, key, value)
end
end
local function load()
local s = {}
for key, _ in pairs(defaults) do
s[key] = get(key)
end
return s
end
local function save(s)
for key, value in pairs(s) do
set(key, value)
end
end
return {
get = get,
set = set,
load = load,
save = save,
restore = restore
}
end
return settings