Support disabling automatic database updates

custom
jacqueline 1 year ago
parent 1e278d55c4
commit 654fde5f68
  1. 136
      lua/settings.lua
  2. 4
      src/drivers/include/nvs.hpp
  3. 14
      src/drivers/nvs.cpp
  4. 16
      src/system_fsm/running.cpp
  5. 1
      src/ui/include/ui_fsm.hpp
  6. 18
      src/ui/ui_fsm.cpp

@ -54,10 +54,10 @@ local BluetoothSettings = screen:new {
bluetooth.enabled:set(enabled) bluetooth.enabled:set(enabled)
end) end)
theme.set_style(self.menu.content:Label { theme.set_style(self.menu.content:Label {
text = "Paired Device", text = "Paired Device",
pad_bottom = 1, pad_bottom = 1,
}, "settings_title") }, "settings_title")
local paired_container = self.menu.content:Object { local paired_container = self.menu.content:Object {
flex = { flex = {
@ -80,10 +80,10 @@ local BluetoothSettings = screen:new {
bluetooth.paired_device:set() bluetooth.paired_device:set()
end) end)
theme.set_style(self.menu.content:Label { theme.set_style(self.menu.content:Label {
text = "Nearby Devices", text = "Nearby Devices",
pad_bottom = 1, pad_bottom = 1,
}, "settings_title") }, "settings_title")
local devices = self.menu.content:List { local devices = self.menu.content:List {
w = lvgl.PCT(100), w = lvgl.PCT(100),
@ -123,9 +123,9 @@ local HeadphonesSettings = screen:new {
createUi = function(self) createUi = function(self)
self.menu = SettingsScreen("Headphones") self.menu = SettingsScreen("Headphones")
theme.set_style(self.menu.content:Label { theme.set_style(self.menu.content:Label {
text = "Maxiumum volume limit", text = "Maxiumum volume limit",
}, "settings_title") }, "settings_title")
local volume_chooser = self.menu.content:Dropdown { local volume_chooser = self.menu.content:Dropdown {
options = "Line Level (-10 dB)\nCD Level (+6 dB)\nMaximum (+10dB)", options = "Line Level (-10 dB)\nCD Level (+6 dB)\nMaximum (+10dB)",
@ -138,9 +138,9 @@ local HeadphonesSettings = screen:new {
volume.limit_db:set(limits[selection]) volume.limit_db:set(limits[selection])
end) end)
theme.set_style(self.menu.content:Label { theme.set_style(self.menu.content:Label {
text = "Left/Right balance", text = "Left/Right balance",
}, "settings_title") }, "settings_title")
local balance = self.menu.content:Slider { local balance = self.menu.content:Slider {
w = lvgl.PCT(100), w = lvgl.PCT(100),
@ -186,20 +186,20 @@ local DisplaySettings = screen:new {
createUi = function(self) createUi = function(self)
self.menu = SettingsScreen("Display") self.menu = SettingsScreen("Display")
local brightness_title = self.menu.content:Object { local brightness_title = self.menu.content:Object {
flex = { flex = {
flex_direction = "row", flex_direction = "row",
justify_content = "flex-start", justify_content = "flex-start",
align_items = "flex-start", align_items = "flex-start",
align_content = "flex-start", align_content = "flex-start",
}, },
w = lvgl.PCT(100), w = lvgl.PCT(100),
h = lvgl.SIZE_CONTENT, h = lvgl.SIZE_CONTENT,
} }
brightness_title:Label { text = "Brightness", flex_grow = 1 } brightness_title:Label { text = "Brightness", flex_grow = 1 }
local brightness_pct = brightness_title:Label {} local brightness_pct = brightness_title:Label {}
theme.set_style(brightness_pct, "settings_title") theme.set_style(brightness_pct, "settings_title")
local brightness = self.menu.content:Slider { local brightness = self.menu.content:Slider {
w = lvgl.PCT(100), w = lvgl.PCT(100),
@ -223,9 +223,9 @@ local InputSettings = screen:new {
createUi = function(self) createUi = function(self)
self.menu = SettingsScreen("Input Method") self.menu = SettingsScreen("Input Method")
theme.set_style(self.menu.content:Label { theme.set_style(self.menu.content:Label {
text = "Control scheme", text = "Control scheme",
}, "settings_title") }, "settings_title")
local schemes = controls.schemes() local schemes = controls.schemes()
local option_to_scheme = {} local option_to_scheme = {}
@ -261,9 +261,9 @@ local InputSettings = screen:new {
controls.scheme:set(scheme) controls.scheme:set(scheme)
end) end)
theme.set_style(self.menu.content:Label { theme.set_style(self.menu.content:Label {
text = "Scroll Sensitivity", text = "Scroll Sensitivity",
}, "settings_title") }, "settings_title")
local slider_scale = 4; -- Power steering local slider_scale = 4; -- Power steering
local sensitivity = self.menu.content:Slider { local sensitivity = self.menu.content:Slider {
@ -333,25 +333,53 @@ local DatabaseSettings = screen:new {
widgets.Row(self.menu.content, "Schema version", db.version()) widgets.Row(self.menu.content, "Schema version", db.version())
widgets.Row(self.menu.content, "Size on disk", string.format("%.1f KiB", db.size() / 1024)) widgets.Row(self.menu.content, "Size on disk", string.format("%.1f KiB", db.size() / 1024))
local actions_container = self.menu.content:Object { local auto_update_container = self.menu.content:Object {
w = lvgl.PCT(100), flex = {
h = lvgl.SIZE_CONTENT, flex_direction = "row",
flex = { justify_content = "flex-start",
flex_direction = "row", align_items = "flex-start",
justify_content = "center", align_content = "flex-start",
align_items = "space-evenly", },
align_content = "center", w = lvgl.PCT(100),
}, h = lvgl.SIZE_CONTENT,
pad_top = 4, }
pad_column = 4, auto_update_container:add_style(styles.list_item)
} auto_update_container:Label { text = "Auto update", flex_grow = 1 }
actions_container:add_style(styles.list_item) local auto_update_sw = auto_update_container:Switch {}
auto_update_sw:onevent(lvgl.EVENT.VALUE_CHANGED, function()
database.auto_update:set(auto_update_sw:enabled())
end)
local actions_container = self.menu.content:Object {
w = lvgl.PCT(100),
h = lvgl.SIZE_CONTENT,
flex = {
flex_direction = "row",
justify_content = "center",
align_items = "space-evenly",
align_content = "center",
},
pad_top = 4,
pad_column = 4,
}
actions_container:add_style(styles.list_item)
local update = actions_container:Button {} local update = actions_container:Button {}
update:Label { text = "Update" } update:Label { text = "Update now" }
update:onClicked(function() update:onClicked(function()
database.update() database.update()
end) end)
self.bindings = {
database.auto_update:bind(function(en)
if en then
auto_update_sw:add_state(lvgl.STATE.CHECKED)
else
auto_update_sw:clear_state(lvgl.STATE.CHECKED)
end
end),
}
end end
} }
@ -383,13 +411,13 @@ return screen:new {
flex_grow = 1, flex_grow = 1,
} }
local function section(name) local function section(name)
local elem = self.list:Label { local elem = self.list:Label {
text = name, text = name,
pad_left = 4, pad_left = 4,
} }
theme.set_style(elem, "settings_title") theme.set_style(elem, "settings_title")
end end
local function submenu(name, class) local function submenu(name, class)
local item = self.list:add_btn(nil, name) local item = self.list:add_btn(nil, name)

@ -114,6 +114,9 @@ class NvsStorage {
auto PrimaryInput() -> InputModes; auto PrimaryInput() -> InputModes;
auto PrimaryInput(InputModes) -> void; auto PrimaryInput(InputModes) -> void;
auto DbAutoIndex() -> bool;
auto DbAutoIndex(bool) -> void;
explicit NvsStorage(nvs_handle_t); explicit NvsStorage(nvs_handle_t);
~NvsStorage(); ~NvsStorage();
@ -136,6 +139,7 @@ class NvsStorage {
Setting<uint8_t> input_mode_; Setting<uint8_t> input_mode_;
Setting<uint8_t> output_mode_; Setting<uint8_t> output_mode_;
Setting<bluetooth::MacAndName> bt_preferred_; Setting<bluetooth::MacAndName> bt_preferred_;
Setting<uint8_t> db_auto_index_;
util::LruCache<10, bluetooth::mac_addr_t, uint8_t> bt_volumes_; util::LruCache<10, bluetooth::mac_addr_t, uint8_t> bt_volumes_;
bool bt_volumes_dirty_; bool bt_volumes_dirty_;

@ -39,6 +39,7 @@ static constexpr char kKeyScrollSensitivity[] = "scroll";
static constexpr char kKeyLockPolarity[] = "lockpol"; static constexpr char kKeyLockPolarity[] = "lockpol";
static constexpr char kKeyDisplayCols[] = "dispcols"; static constexpr char kKeyDisplayCols[] = "dispcols";
static constexpr char kKeyDisplayRows[] = "disprows"; static constexpr char kKeyDisplayRows[] = "disprows";
static constexpr char kKeyDbAutoIndex[] = "dbautoindex";
static auto nvs_get_string(nvs_handle_t nvs, const char* key) static auto nvs_get_string(nvs_handle_t nvs, const char* key)
-> std::optional<std::string> { -> std::optional<std::string> {
@ -173,6 +174,7 @@ NvsStorage::NvsStorage(nvs_handle_t handle)
input_mode_(kKeyPrimaryInput), input_mode_(kKeyPrimaryInput),
output_mode_(kKeyOutput), output_mode_(kKeyOutput),
bt_preferred_(kKeyBluetoothPreferred), bt_preferred_(kKeyBluetoothPreferred),
db_auto_index_(kKeyDbAutoIndex),
bt_volumes_(), bt_volumes_(),
bt_volumes_dirty_(false) {} bt_volumes_dirty_(false) {}
@ -194,6 +196,7 @@ auto NvsStorage::Read() -> void {
input_mode_.read(handle_); input_mode_.read(handle_);
output_mode_.read(handle_); output_mode_.read(handle_);
bt_preferred_.read(handle_); bt_preferred_.read(handle_);
db_auto_index_.read(handle_);
readBtVolumes(); readBtVolumes();
} }
@ -210,6 +213,7 @@ auto NvsStorage::Write() -> bool {
input_mode_.write(handle_); input_mode_.write(handle_);
output_mode_.write(handle_); output_mode_.write(handle_);
bt_preferred_.write(handle_); bt_preferred_.write(handle_);
db_auto_index_.write(handle_);
writeBtVolumes(); writeBtVolumes();
return nvs_commit(handle_) == ESP_OK; return nvs_commit(handle_) == ESP_OK;
} }
@ -370,6 +374,16 @@ auto NvsStorage::PrimaryInput(InputModes mode) -> void {
input_mode_.set(static_cast<uint8_t>(mode)); input_mode_.set(static_cast<uint8_t>(mode));
} }
auto NvsStorage::DbAutoIndex() -> bool {
std::lock_guard<std::mutex> lock{mutex_};
return db_auto_index_.get().value_or(true);
}
auto NvsStorage::DbAutoIndex(bool en) -> void {
std::lock_guard<std::mutex> lock{mutex_};
db_auto_index_.set(static_cast<uint8_t>(en));
}
class VolumesParseClient : public cppbor::ParseClient { class VolumesParseClient : public cppbor::ParseClient {
public: public:
VolumesParseClient(util::LruCache<10, bluetooth::mac_addr_t, uint8_t>& out) VolumesParseClient(util::LruCache<10, bluetooth::mac_addr_t, uint8_t>& out)

@ -166,13 +166,15 @@ auto Running::mountStorage() -> bool {
// Tell the database to refresh so that we pick up any changes from the newly // Tell the database to refresh so that we pick up any changes from the newly
// mounted card. // mounted card.
sServices->bg_worker().Dispatch<void>([&]() { if (sServices->nvs().DbAutoIndex()) {
auto db = sServices->database().lock(); sServices->bg_worker().Dispatch<void>([&]() {
if (!db) { auto db = sServices->database().lock();
return; if (!db) {
} return;
db->updateIndexes(); }
}); db->updateIndexes();
});
}
return true; return true;
} }

@ -130,6 +130,7 @@ class UiState : public tinyfsm::Fsm<UiState> {
static lua::Property sLockSwitch; static lua::Property sLockSwitch;
static lua::Property sDatabaseUpdating; static lua::Property sDatabaseUpdating;
static lua::Property sDatabaseAutoUpdate;
static lua::Property sUsbMassStorageEnabled; static lua::Property sUsbMassStorageEnabled;
}; };

@ -282,6 +282,14 @@ lua::Property UiState::sScrollSensitivity{
lua::Property UiState::sLockSwitch{false}; lua::Property UiState::sLockSwitch{false};
lua::Property UiState::sDatabaseUpdating{false}; lua::Property UiState::sDatabaseUpdating{false};
lua::Property UiState::sDatabaseAutoUpdate{
false, [](const lua::LuaValue& val) {
if (!std::holds_alternative<bool>(val)) {
return false;
}
sServices->nvs().DbAutoIndex(std::get<bool>(val));
return true;
}};
lua::Property UiState::sUsbMassStorageEnabled{ lua::Property UiState::sUsbMassStorageEnabled{
false, [](const lua::LuaValue& val) { false, [](const lua::LuaValue& val) {
@ -557,14 +565,18 @@ void Lua::entry() {
"time", { "time", {
{"ticks", [&](lua_State* s) { return Ticks(s); }}, {"ticks", [&](lua_State* s) { return Ticks(s); }},
}); });
registry.AddPropertyModule("database", { registry.AddPropertyModule("database",
{"updating", &sDatabaseUpdating}, {
}); {"updating", &sDatabaseUpdating},
{"auto_update", &sDatabaseAutoUpdate},
});
registry.AddPropertyModule("usb", registry.AddPropertyModule("usb",
{ {
{"msc_enabled", &sUsbMassStorageEnabled}, {"msc_enabled", &sUsbMassStorageEnabled},
}); });
sDatabaseAutoUpdate.Update(sServices->nvs().DbAutoIndex());
auto bt = sServices->bluetooth(); auto bt = sServices->bluetooth();
sBluetoothEnabled.Update(bt.IsEnabled()); sBluetoothEnabled.Update(bt.IsEnabled());
sBluetoothConnected.Update(bt.IsConnected()); sBluetoothConnected.Update(bt.IsConnected());

Loading…
Cancel
Save