From a05d93a1e26181237a76da5ce398c6b08497d591 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 20 Mar 2024 11:43:33 +1100 Subject: [PATCH] Start using the lock switch polarity bit in nvs --- src/drivers/gpios.cpp | 18 +++++++++++++++--- src/drivers/include/gpios.hpp | 9 ++++++--- src/system_fsm/booting.cpp | 17 ++++++++++------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/drivers/gpios.cpp b/src/drivers/gpios.cpp index 5c255204..aab932a7 100644 --- a/src/drivers/gpios.cpp +++ b/src/drivers/gpios.cpp @@ -63,8 +63,8 @@ constexpr std::pair unpack(uint16_t ba) { static constexpr gpio_num_t kIntPin = GPIO_NUM_34; -auto Gpios::Create() -> Gpios* { - Gpios* instance = new Gpios(); +auto Gpios::Create(bool invert_lock) -> Gpios* { + Gpios* instance = new Gpios(invert_lock); // Read and write initial values on initialisation so that we do not have a // strange partially-initialised state. if (!instance->Flush() || !instance->Read()) { @@ -73,7 +73,10 @@ auto Gpios::Create() -> Gpios* { return instance; } -Gpios::Gpios() : ports_(pack(kPortADefault, kPortBDefault)), inputs_(0) { +Gpios::Gpios(bool invert_lock) + : ports_(pack(kPortADefault, kPortBDefault)), + inputs_(0), + invert_lock_switch_(invert_lock) { gpio_set_direction(kIntPin, GPIO_MODE_INPUT); } @@ -108,6 +111,15 @@ auto Gpios::Get(Pin pin) const -> bool { return (inputs_ & (1 << static_cast(pin))) > 0; } +auto Gpios::IsLocked() const -> bool { + bool pin = Get(Pin::kKeyLock); + if (invert_lock_switch_) { + return pin; + } else { + return !pin; + } +} + auto Gpios::Read() -> bool { uint8_t input_a, input_b; diff --git a/src/drivers/include/gpios.hpp b/src/drivers/include/gpios.hpp index 55486be7..e27a3ade 100644 --- a/src/drivers/include/gpios.hpp +++ b/src/drivers/include/gpios.hpp @@ -79,12 +79,12 @@ class IGpios { */ virtual auto Get(Pin) const -> bool = 0; - virtual auto IsLocked() const -> bool { return Get(Pin::kKeyLock); } + virtual auto IsLocked() const -> bool = 0; }; class Gpios : public IGpios { public: - static auto Create() -> Gpios*; + static auto Create(bool invert_lock_switch) -> Gpios*; ~Gpios(); /* @@ -106,6 +106,8 @@ class Gpios : public IGpios { auto Get(Pin) const -> bool override; + auto IsLocked() const -> bool override; + /** * Reads from the GPIO expander, populating `inputs` with the most recent * values. @@ -118,10 +120,11 @@ class Gpios : public IGpios { Gpios& operator=(const Gpios&) = delete; private: - Gpios(); + Gpios(bool invert_lock); std::atomic ports_; std::atomic inputs_; + const bool invert_lock_switch_; }; } // namespace drivers diff --git a/src/system_fsm/booting.cpp b/src/system_fsm/booting.cpp index e911932c..eb931192 100644 --- a/src/system_fsm/booting.cpp +++ b/src/system_fsm/booting.cpp @@ -57,14 +57,19 @@ auto Booting::entry() -> void { sServices.reset(new ServiceLocator()); ESP_LOGI(kTag, "installing early drivers"); + // NVS is needed first because it contains information about what specific + // hardware configuration we're running on. + sServices->nvs( + std::unique_ptr(drivers::NvsStorage::OpenSync())); + + // HACK: fix up the switch polarity on newer dev units + sServices->nvs().LockPolarity(false); + // I2C and SPI are both always needed. We can't even power down or show an // error without these. ESP_ERROR_CHECK(drivers::init_spi()); - sServices->gpios(std::unique_ptr(drivers::Gpios::Create())); - - // NVS is needed early so that we can correctly initialise the display. - sServices->nvs( - std::unique_ptr(drivers::NvsStorage::OpenSync())); + sServices->gpios(std::unique_ptr( + drivers::Gpios::Create(sServices->nvs().LockPolarity()))); ESP_LOGI(kTag, "starting ui"); if (!ui::UiState::InitBootSplash(sServices->gpios(), sServices->nvs())) { @@ -102,8 +107,6 @@ auto Booting::entry() -> void { sServices->bluetooth().Enable(); } - sServices->nvs().LockPolarity(false); - BootComplete ev{.services = sServices}; events::Audio().Dispatch(ev); events::Ui().Dispatch(ev);