Warn and block MSC disable if the sd card is busy

custom
jacqueline 1 year ago
parent 251c0ba96d
commit 5f1a6507d8
  1. 21
      lua/settings.lua
  2. 4
      src/drivers/include/samd.hpp
  3. 2
      src/drivers/samd.cpp
  4. 4
      src/system_fsm/include/system_events.hpp
  5. 2
      src/system_fsm/system_fsm.cpp
  6. 2
      src/ui/include/ui_fsm.hpp
  7. 9
      src/ui/ui_fsm.cpp

@ -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
@ -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()
usb.msc_enabled:set(enable_sw:enabled()) if not usb.msc_busy:get() then
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()

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

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

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

@ -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&);
@ -133,6 +134,7 @@ class UiState : public tinyfsm::Fsm<UiState> {
static lua::Property sDatabaseAutoUpdate; 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"
@ -302,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.
@ -360,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;
@ -573,6 +581,7 @@ void Lua::entry() {
registry.AddPropertyModule("usb", registry.AddPropertyModule("usb",
{ {
{"msc_enabled", &sUsbMassStorageEnabled}, {"msc_enabled", &sUsbMassStorageEnabled},
{"msc_busy", &sUsbMassStorageBusy},
}); });
sDatabaseAutoUpdate.Update(sServices->nvs().DbAutoIndex()); sDatabaseAutoUpdate.Update(sServices->nvs().DbAutoIndex());

Loading…
Cancel
Save