Merge branch 'main' of codeberg.org:cool-tech-zone/tangara-fw

custom
ailurux 1 year ago
commit f20ca9583a
  1. 21
      lib/leveldb/util/cache.cc
  2. 55
      lua/settings.lua
  3. 19
      src/audio/audio_fsm.cpp
  4. 24
      src/audio/bt_audio_output.cpp
  5. 23
      src/audio/i2s_audio_output.cpp
  6. 3
      src/audio/include/audio_sink.hpp
  7. 2
      src/audio/include/bt_audio_output.hpp
  8. 2
      src/audio/include/i2s_audio_output.hpp
  9. 8
      src/database/database.cpp
  10. 4
      src/drivers/include/nvs.hpp
  11. 4
      src/drivers/include/samd.hpp
  12. 14
      src/drivers/nvs.cpp
  13. 2
      src/drivers/samd.cpp
  14. 12
      src/lua/property.cpp
  15. 4
      src/system_fsm/include/system_events.hpp
  16. 2
      src/system_fsm/running.cpp
  17. 2
      src/system_fsm/system_fsm.cpp
  18. 3
      src/ui/include/ui_fsm.hpp
  19. 23
      src/ui/ui_fsm.cpp
  20. 2
      tools/cmake/common.cmake

@ -8,6 +8,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include "esp_heap_caps.h"
#include "port/port.h" #include "port/port.h"
#include "port/thread_annotations.h" #include "port/thread_annotations.h"
#include "util/hash.h" #include "util/hash.h"
@ -157,7 +158,9 @@ class LRUCache {
void SetCapacity(size_t capacity) { capacity_ = capacity; } void SetCapacity(size_t capacity) { capacity_ = capacity; }
// Like Cache methods, but with an extra "hash" parameter. // Like Cache methods, but with an extra "hash" parameter.
Cache::Handle* Insert(const Slice& key, uint32_t hash, void* value, Cache::Handle* Insert(const Slice& key,
uint32_t hash,
void* value,
size_t charge, size_t charge,
void (*deleter)(const Slice& key, void* value)); void (*deleter)(const Slice& key, void* value));
Cache::Handle* Lookup(const Slice& key, uint32_t hash); Cache::Handle* Lookup(const Slice& key, uint32_t hash);
@ -264,14 +267,16 @@ void LRUCache::Release(Cache::Handle* handle) {
Unref(reinterpret_cast<LRUHandle*>(handle)); Unref(reinterpret_cast<LRUHandle*>(handle));
} }
Cache::Handle* LRUCache::Insert(const Slice& key, uint32_t hash, void* value, Cache::Handle* LRUCache::Insert(const Slice& key,
uint32_t hash,
void* value,
size_t charge, size_t charge,
void (*deleter)(const Slice& key, void (*deleter)(const Slice& key,
void* value)) { void* value)) {
MutexLock l(&mutex_); MutexLock l(&mutex_);
LRUHandle* e = LRUHandle* e = reinterpret_cast<LRUHandle*>(
reinterpret_cast<LRUHandle*>(malloc(sizeof(LRUHandle) - 1 + key.size())); heap_caps_malloc(sizeof(LRUHandle) - 1 + key.size(), MALLOC_CAP_SPIRAM));
e->value = value; e->value = value;
e->deleter = deleter; e->deleter = deleter;
e->charge = charge; e->charge = charge;
@ -356,7 +361,9 @@ class ShardedLRUCache : public Cache {
} }
} }
~ShardedLRUCache() override {} ~ShardedLRUCache() override {}
Handle* Insert(const Slice& key, void* value, size_t charge, Handle* Insert(const Slice& key,
void* value,
size_t charge,
void (*deleter)(const Slice& key, void* value)) override { void (*deleter)(const Slice& key, void* value)) override {
const uint32_t hash = HashSlice(key); const uint32_t hash = HashSlice(key);
return shard_[Shard(hash)].Insert(key, hash, value, charge, deleter); return shard_[Shard(hash)].Insert(key, hash, value, charge, deleter);
@ -396,6 +403,8 @@ class ShardedLRUCache : public Cache {
} // end anonymous namespace } // end anonymous namespace
Cache* NewLRUCache(size_t capacity) { return new ShardedLRUCache(capacity); } Cache* NewLRUCache(size_t capacity) {
return new ShardedLRUCache(capacity);
}
} // namespace leveldb } // namespace leveldb

@ -24,7 +24,7 @@ local function SettingsScreen(title)
align_items = "flex-start", align_items = "flex-start",
align_content = "flex-start", align_content = "flex-start",
}, },
w = lvgl.PCT(90), w = lvgl.PCT(100),
flex_grow = 1, flex_grow = 1,
pad_left = 4, pad_left = 4,
pad_right = 4, pad_right = 4,
@ -40,7 +40,7 @@ local BluetoothSettings = screen:new {
flex = { flex = {
flex_direction = "row", flex_direction = "row",
justify_content = "flex-start", justify_content = "flex-start",
align_items = "content", align_items = "center",
align_content = "flex-start", align_content = "flex-start",
}, },
w = lvgl.PCT(100), w = lvgl.PCT(100),
@ -282,10 +282,10 @@ local MassStorageSettings = screen:new {
createUi = function(self) createUi = function(self)
self.menu = SettingsScreen("USB Storage") self.menu = SettingsScreen("USB Storage")
local version = require("version").samd() local version = require("version").samd()
if tonumber(version) < 2 then if tonumber(version) < 3 then
self.menu.content:Label { self.menu.content:Label {
w = lvgl.PCT(100), w = lvgl.PCT(100),
text = "Usb Mass Storage requires a SAMD21 firmware version >=2." text = "Usb Mass Storage requires a SAMD21 firmware version >=3."
} }
return return
end end
@ -294,7 +294,7 @@ local MassStorageSettings = screen:new {
flex = { flex = {
flex_direction = "row", flex_direction = "row",
justify_content = "flex-start", justify_content = "flex-start",
align_items = "content", align_items = "center",
align_content = "flex-start", align_content = "flex-start",
}, },
w = lvgl.PCT(100), w = lvgl.PCT(100),
@ -304,6 +304,12 @@ local MassStorageSettings = screen:new {
enable_container:Label { text = "Enable", flex_grow = 1 } enable_container:Label { text = "Enable", flex_grow = 1 }
local enable_sw = enable_container:Switch {} local enable_sw = enable_container:Switch {}
local busy_text = self.menu.content:Label {
w = lvgl.PCT(100),
text = "USB is currently busy. Do not unplug or remove the SD card.",
long_mode = lvgl.LABEL.LONG_WRAP,
}
local bind_switch = function() local bind_switch = function()
if usb.msc_enabled:get() then if usb.msc_enabled:get() then
enable_sw:add_state(lvgl.STATE.CHECKED) enable_sw:add_state(lvgl.STATE.CHECKED)
@ -313,12 +319,21 @@ local MassStorageSettings = screen:new {
end end
enable_sw:onevent(lvgl.EVENT.VALUE_CHANGED, function() enable_sw:onevent(lvgl.EVENT.VALUE_CHANGED, function()
if not usb.msc_busy:get() then
usb.msc_enabled:set(enable_sw:enabled()) usb.msc_enabled:set(enable_sw:enabled())
end
bind_switch() bind_switch()
end) end)
self.bindings = { self.bindings = {
usb.msc_enabled:bind(bind_switch), usb.msc_enabled:bind(bind_switch),
usb.msc_busy:bind(function(busy)
if busy then
busy_text:clear_flag(lvgl.FLAG.HIDDEN)
else
busy_text:add_flag(lvgl.FLAG.HIDDEN)
end
end)
} }
end, end,
canPop = function() canPop = function()
@ -333,6 +348,24 @@ 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 auto_update_container = self.menu.content:Object {
flex = {
flex_direction = "row",
justify_content = "flex-start",
align_items = "flex-start",
align_content = "flex-start",
},
w = lvgl.PCT(100),
h = lvgl.SIZE_CONTENT,
}
auto_update_container:add_style(styles.list_item)
auto_update_container:Label { text = "Auto update", flex_grow = 1 }
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 { local actions_container = self.menu.content:Object {
w = lvgl.PCT(100), w = lvgl.PCT(100),
h = lvgl.SIZE_CONTENT, h = lvgl.SIZE_CONTENT,
@ -348,10 +381,20 @@ local DatabaseSettings = screen:new {
actions_container:add_style(styles.list_item) 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
} }

@ -276,7 +276,24 @@ void AudioState::react(const system_fsm::HasPhonesChanged& ev) {
} }
void AudioState::react(const SetVolume& ev) { void AudioState::react(const SetVolume& ev) {
// TODO. if (ev.db.has_value()) {
if (sOutput->SetVolumeDb(ev.db.value())) {
commitVolume();
events::Ui().Dispatch(VolumeChanged{
.percent = sOutput->GetVolumePct(),
.db = sOutput->GetVolumeDb(),
});
}
} else if (ev.percent.has_value()) {
if (sOutput->SetVolumePct(ev.percent.value())) {
commitVolume();
events::Ui().Dispatch(VolumeChanged{
.percent = sOutput->GetVolumePct(),
.db = sOutput->GetVolumeDb(),
});
}
}
} }
void AudioState::react(const SetVolumeLimit& ev) { void AudioState::react(const SetVolumeLimit& ev) {

@ -9,6 +9,7 @@
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <cmath>
#include <memory> #include <memory>
#include <variant> #include <variant>
@ -54,11 +55,30 @@ auto BluetoothAudioOutput::GetVolume() -> uint16_t {
} }
auto BluetoothAudioOutput::GetVolumePct() -> uint_fast8_t { auto BluetoothAudioOutput::GetVolumePct() -> uint_fast8_t {
return static_cast<uint_fast8_t>(static_cast<int>(volume_) * 100 / 0x7f); return static_cast<uint_fast8_t>(round(static_cast<int>(volume_) * 100.0 / 0x7f));
}
auto BluetoothAudioOutput::SetVolumePct(uint_fast8_t val) -> bool {
if (val > 100) {
return false;
}
uint16_t vol = (val * (0x7f))/100;
SetVolume(vol);
return true;
} }
auto BluetoothAudioOutput::GetVolumeDb() -> int_fast16_t { auto BluetoothAudioOutput::GetVolumeDb() -> int_fast16_t {
return 0; double pct = GetVolumePct()/100.0;
if (pct <= 0) {
pct = 0.01;
}
int_fast16_t db = log(pct) * 20;
return db;
}
auto BluetoothAudioOutput::SetVolumeDb(int_fast16_t val) -> bool {
double pct = exp(val / 20.0) * 100;
return SetVolumePct(pct);
} }
auto BluetoothAudioOutput::AdjustVolumeUp() -> bool { auto BluetoothAudioOutput::AdjustVolumeUp() -> bool {

@ -63,16 +63,21 @@ auto I2SAudioOutput::changeMode(Modes mode) -> void {
return; return;
} }
if (mode == Modes::kOff) { if (mode == Modes::kOff) {
if (dac_) {
dac_->Stop(); dac_->Stop();
dac_.reset(); dac_.reset();
}
return; return;
} else if (current_mode_ == Modes::kOff) { }
if (current_mode_ == Modes::kOff) {
if (!dac_) {
auto instance = drivers::I2SDac::create(expander_); auto instance = drivers::I2SDac::create(expander_);
if (!instance) { if (!instance) {
return; return;
} }
SetVolume(GetVolume());
dac_.reset(*instance); dac_.reset(*instance);
}
SetVolume(GetVolume());
dac_->SetSource(stream()); dac_->SetSource(stream());
dac_->Start(); dac_->Start();
} }
@ -111,6 +116,15 @@ auto I2SAudioOutput::GetVolumePct() -> uint_fast8_t {
return (current_volume_ - kMinVolume) * 100 / (max_volume_ - kMinVolume); return (current_volume_ - kMinVolume) * 100 / (max_volume_ - kMinVolume);
} }
auto I2SAudioOutput::SetVolumePct(uint_fast8_t val) -> bool {
if (val > 100) {
return false;
}
uint16_t vol = (val * (max_volume_ - kMinVolume))/100 + kMinVolume;
SetVolume(vol);
return true;
}
auto I2SAudioOutput::GetVolumeDb() -> int_fast16_t { auto I2SAudioOutput::GetVolumeDb() -> int_fast16_t {
// Add two before dividing in order to round correctly. // Add two before dividing in order to round correctly.
return (static_cast<int>(current_volume_) - return (static_cast<int>(current_volume_) -
@ -118,6 +132,11 @@ auto I2SAudioOutput::GetVolumeDb() -> int_fast16_t {
4; 4;
} }
auto I2SAudioOutput::SetVolumeDb(int_fast16_t val) -> bool {
SetVolume(val * 4 + static_cast<int>(drivers::wm8523::kLineLevelReferenceVolume) - 2);
return true;
}
auto I2SAudioOutput::AdjustVolumeUp() -> bool { auto I2SAudioOutput::AdjustVolumeUp() -> bool {
if (GetVolume() >= max_volume_) { if (GetVolume() >= max_volume_) {
return false; return false;

@ -59,6 +59,9 @@ class IAudioOutput {
virtual auto GetVolumePct() -> uint_fast8_t = 0; virtual auto GetVolumePct() -> uint_fast8_t = 0;
virtual auto GetVolumeDb() -> int_fast16_t = 0; virtual auto GetVolumeDb() -> int_fast16_t = 0;
virtual auto SetVolumePct(uint_fast8_t) -> bool = 0;
virtual auto SetVolumeDb(int_fast16_t) -> bool = 0;
virtual auto AdjustVolumeUp() -> bool = 0; virtual auto AdjustVolumeUp() -> bool = 0;
virtual auto AdjustVolumeDown() -> bool = 0; virtual auto AdjustVolumeDown() -> bool = 0;

@ -35,7 +35,9 @@ class BluetoothAudioOutput : public IAudioOutput {
auto GetVolume() -> uint16_t override; auto GetVolume() -> uint16_t override;
auto GetVolumePct() -> uint_fast8_t override; auto GetVolumePct() -> uint_fast8_t override;
auto SetVolumePct(uint_fast8_t val) -> bool override;
auto GetVolumeDb() -> int_fast16_t override; auto GetVolumeDb() -> int_fast16_t override;
auto SetVolumeDb(int_fast16_t) -> bool override;
auto AdjustVolumeUp() -> bool override; auto AdjustVolumeUp() -> bool override;
auto AdjustVolumeDown() -> bool override; auto AdjustVolumeDown() -> bool override;

@ -33,7 +33,9 @@ class I2SAudioOutput : public IAudioOutput {
auto GetVolume() -> uint16_t override; auto GetVolume() -> uint16_t override;
auto GetVolumePct() -> uint_fast8_t override; auto GetVolumePct() -> uint_fast8_t override;
auto SetVolumePct(uint_fast8_t val) -> bool override;
auto GetVolumeDb() -> int_fast16_t override; auto GetVolumeDb() -> int_fast16_t override;
auto SetVolumeDb(int_fast16_t) -> bool override;
auto AdjustVolumeUp() -> bool override; auto AdjustVolumeUp() -> bool override;
auto AdjustVolumeDown() -> bool override; auto AdjustVolumeDown() -> bool override;

@ -139,14 +139,14 @@ auto Database::Open(IFileGatherer& gatherer,
[&]() -> cpp::result<Database*, DatabaseError> { [&]() -> cpp::result<Database*, DatabaseError> {
leveldb::DB* db; leveldb::DB* db;
std::unique_ptr<leveldb::Cache> cache{ std::unique_ptr<leveldb::Cache> cache{
leveldb::NewLRUCache(4 * 1024)}; leveldb::NewLRUCache(256 * 1024)};
leveldb::Options options; leveldb::Options options;
options.env = sEnv.env(); options.env = sEnv.env();
options.write_buffer_size = 4 * 1024; options.write_buffer_size = 4 * 1024;
options.max_file_size = 32; options.max_file_size = 16 * 1024;
options.block_cache = cache.get(); options.block_cache = cache.get();
options.block_size = 512; options.block_size = 2048;
auto status = leveldb::DB::Open(options, kDbPath, &db); auto status = leveldb::DB::Open(options, kDbPath, &db);
if (!status.ok()) { if (!status.ok()) {
@ -299,7 +299,7 @@ auto Database::updateIndexes() -> void {
UpdateNotifier notifier{is_updating_}; UpdateNotifier notifier{is_updating_};
leveldb::ReadOptions read_options; leveldb::ReadOptions read_options;
read_options.fill_cache = false; read_options.fill_cache = true;
// Stage 1: verify all existing tracks are still valid. // Stage 1: verify all existing tracks are still valid.
ESP_LOGI(kTag, "verifying existing tracks"); ESP_LOGI(kTag, "verifying existing tracks");

@ -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_;

@ -48,8 +48,8 @@ class Samd {
// There is a compatible usb host attached, but USB MSC is not currently // There is a compatible usb host attached, but USB MSC is not currently
// in use by the SAMD. // in use by the SAMD.
kAttachedIdle, kAttachedIdle,
// The SAMD is currently exposing the SD card via USB MSC. // The SAMD is currently writing to the SD card via USB MSC.
kAttachedMounted, kAttachedBusy,
}; };
auto GetUsbStatus() -> UsbStatus; auto GetUsbStatus() -> UsbStatus;

@ -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)

@ -113,7 +113,7 @@ auto Samd::UpdateUsbStatus() -> void {
usb_status_ = UsbStatus::kDetached; usb_status_ = UsbStatus::kDetached;
} }
usb_status_ = usb_status_ =
(raw_res & 0b10) ? UsbStatus::kAttachedMounted : UsbStatus::kAttachedIdle; (raw_res & 0b10) ? UsbStatus::kAttachedBusy : UsbStatus::kAttachedIdle;
} }
auto Samd::ResetToFlashSamd() -> void { auto Samd::ResetToFlashSamd() -> void {

@ -379,22 +379,22 @@ auto Property::Update(const LuaValue& v) -> void {
for (int i = bindings_.size() - 1; i >= 0; i--) { for (int i = bindings_.size() - 1; i >= 0; i--) {
auto& b = bindings_[i]; auto& b = bindings_[i];
int top = lua_gettop(b.first);
lua_pushstring(b.first, kBindingsTable); lua_pushstring(b.first, kBindingsTable);
lua_gettable(b.first, LUA_REGISTRYINDEX); // REGISTRY[kBindingsTable] lua_gettable(b.first, LUA_REGISTRYINDEX); // REGISTRY[kBindingsTable]
int type = lua_rawgeti(b.first, -1, b.second); // push bindings[i] int type = lua_rawgeti(b.first, -1, b.second); // push bindings[i]
// Has closure has been GCed? // Has closure has been GCed?
if (type == LUA_TNIL) { if (type == LUA_TNIL) {
// Clean up after ourselves.
lua_pop(b.first, 1);
// Remove the binding. // Remove the binding.
bindings_.erase(bindings_.begin() + i); bindings_.erase(bindings_.begin() + i);
continue; } else {
}
PushValue(*b.first); // push the argument PushValue(*b.first); // push the argument
CallProtected(b.first, 1, 0); // invoke the closure CallProtected(b.first, 1, 0); // invoke the closure
lua_pop(b.first, 1); // pop the bindings table }
lua_settop(b.first, top); // clean up after ourselves
} }
} }

@ -12,6 +12,7 @@
#include "bluetooth_types.hpp" #include "bluetooth_types.hpp"
#include "database.hpp" #include "database.hpp"
#include "haptics.hpp" #include "haptics.hpp"
#include "samd.hpp"
#include "service_locator.hpp" #include "service_locator.hpp"
#include "tinyfsm.hpp" #include "tinyfsm.hpp"
@ -56,6 +57,9 @@ struct SdDetectChanged : tinyfsm::Event {
struct SamdUsbMscChanged : tinyfsm::Event { struct SamdUsbMscChanged : tinyfsm::Event {
bool en; bool en;
}; };
struct SamdUsbStatusChanged : tinyfsm::Event {
drivers::Samd::UsbStatus new_status;
};
struct BatteryStateChanged : tinyfsm::Event { struct BatteryStateChanged : tinyfsm::Event {
battery::Battery::BatteryState new_state; battery::Battery::BatteryState new_state;

@ -166,6 +166,7 @@ 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.
if (sServices->nvs().DbAutoIndex()) {
sServices->bg_worker().Dispatch<void>([&]() { sServices->bg_worker().Dispatch<void>([&]() {
auto db = sServices->database().lock(); auto db = sServices->database().lock();
if (!db) { if (!db) {
@ -173,6 +174,7 @@ auto Running::mountStorage() -> bool {
} }
db->updateIndexes(); db->updateIndexes();
}); });
}
return true; return true;
} }

@ -88,7 +88,7 @@ void SystemState::react(const internal::SamdInterrupt&) {
sServices->battery().Update(); sServices->battery().Update();
} }
if (usb_status != prev_usb_status) { if (usb_status != prev_usb_status) {
ESP_LOGI(kTag, "usb status changed"); events::Ui().Dispatch(SamdUsbStatusChanged{.new_status = usb_status});
} }
} }

@ -65,6 +65,7 @@ class UiState : public tinyfsm::Fsm<UiState> {
void react(const audio::VolumeLimitChanged&); void react(const audio::VolumeLimitChanged&);
void react(const system_fsm::KeyLockChanged&); void react(const system_fsm::KeyLockChanged&);
void react(const system_fsm::SamdUsbStatusChanged&);
void react(const internal::DismissAlerts&); void react(const internal::DismissAlerts&);
void react(const internal::ControlSchemeChanged&); void react(const internal::ControlSchemeChanged&);
@ -130,8 +131,10 @@ 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;
static lua::Property sUsbMassStorageBusy;
}; };
namespace states { namespace states {

@ -43,6 +43,7 @@
#include "nvs.hpp" #include "nvs.hpp"
#include "property.hpp" #include "property.hpp"
#include "relative_wheel.hpp" #include "relative_wheel.hpp"
#include "samd.hpp"
#include "screen.hpp" #include "screen.hpp"
#include "screen_lua.hpp" #include "screen_lua.hpp"
#include "screen_splash.hpp" #include "screen_splash.hpp"
@ -282,6 +283,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) {
@ -294,6 +303,8 @@ lua::Property UiState::sUsbMassStorageEnabled{
return true; return true;
}}; }};
lua::Property UiState::sUsbMassStorageBusy{false};
auto UiState::InitBootSplash(drivers::IGpios& gpios, drivers::NvsStorage& nvs) auto UiState::InitBootSplash(drivers::IGpios& gpios, drivers::NvsStorage& nvs)
-> bool { -> bool {
// Init LVGL first, since the display driver registers itself with LVGL. // Init LVGL first, since the display driver registers itself with LVGL.
@ -352,6 +363,11 @@ void UiState::react(const system_fsm::KeyLockChanged& ev) {
sLockSwitch.Update(ev.locking); sLockSwitch.Update(ev.locking);
} }
void UiState::react(const system_fsm::SamdUsbStatusChanged& ev) {
sUsbMassStorageBusy.Update(ev.new_status ==
drivers::Samd::UsbStatus::kAttachedBusy);
}
void UiState::react(const internal::ControlSchemeChanged&) { void UiState::react(const internal::ControlSchemeChanged&) {
if (!sInput) { if (!sInput) {
return; return;
@ -557,14 +573,19 @@ 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},
{"msc_busy", &sUsbMassStorageBusy},
}); });
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());

@ -5,7 +5,7 @@
# For more information about build system see # For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html # https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
set(PROJECT_VER "0.8.0") set(PROJECT_VER "0.8.1")
# esp-idf sets the C++ standard weird. Set cmake vars to match. # esp-idf sets the C++ standard weird. Set cmake vars to match.
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)

Loading…
Cancel
Save