Start using the lock switch polarity bit in nvs

custom
jacqueline 1 year ago
parent 46e6743771
commit a05d93a1e2
  1. 18
      src/drivers/gpios.cpp
  2. 9
      src/drivers/include/gpios.hpp
  3. 17
      src/system_fsm/booting.cpp

@ -63,8 +63,8 @@ constexpr std::pair<uint8_t, uint8_t> unpack(uint16_t ba) {
static constexpr gpio_num_t kIntPin = GPIO_NUM_34; static constexpr gpio_num_t kIntPin = GPIO_NUM_34;
auto Gpios::Create() -> Gpios* { auto Gpios::Create(bool invert_lock) -> Gpios* {
Gpios* instance = new Gpios(); Gpios* instance = new Gpios(invert_lock);
// Read and write initial values on initialisation so that we do not have a // Read and write initial values on initialisation so that we do not have a
// strange partially-initialised state. // strange partially-initialised state.
if (!instance->Flush() || !instance->Read()) { if (!instance->Flush() || !instance->Read()) {
@ -73,7 +73,10 @@ auto Gpios::Create() -> Gpios* {
return instance; 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); gpio_set_direction(kIntPin, GPIO_MODE_INPUT);
} }
@ -108,6 +111,15 @@ auto Gpios::Get(Pin pin) const -> bool {
return (inputs_ & (1 << static_cast<int>(pin))) > 0; return (inputs_ & (1 << static_cast<int>(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 { auto Gpios::Read() -> bool {
uint8_t input_a, input_b; uint8_t input_a, input_b;

@ -79,12 +79,12 @@ class IGpios {
*/ */
virtual auto Get(Pin) const -> bool = 0; 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 { class Gpios : public IGpios {
public: public:
static auto Create() -> Gpios*; static auto Create(bool invert_lock_switch) -> Gpios*;
~Gpios(); ~Gpios();
/* /*
@ -106,6 +106,8 @@ class Gpios : public IGpios {
auto Get(Pin) const -> bool override; auto Get(Pin) const -> bool override;
auto IsLocked() const -> bool override;
/** /**
* Reads from the GPIO expander, populating `inputs` with the most recent * Reads from the GPIO expander, populating `inputs` with the most recent
* values. * values.
@ -118,10 +120,11 @@ class Gpios : public IGpios {
Gpios& operator=(const Gpios&) = delete; Gpios& operator=(const Gpios&) = delete;
private: private:
Gpios(); Gpios(bool invert_lock);
std::atomic<uint16_t> ports_; std::atomic<uint16_t> ports_;
std::atomic<uint16_t> inputs_; std::atomic<uint16_t> inputs_;
const bool invert_lock_switch_;
}; };
} // namespace drivers } // namespace drivers

@ -57,14 +57,19 @@ auto Booting::entry() -> void {
sServices.reset(new ServiceLocator()); sServices.reset(new ServiceLocator());
ESP_LOGI(kTag, "installing early drivers"); 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>(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 // I2C and SPI are both always needed. We can't even power down or show an
// error without these. // error without these.
ESP_ERROR_CHECK(drivers::init_spi()); ESP_ERROR_CHECK(drivers::init_spi());
sServices->gpios(std::unique_ptr<drivers::Gpios>(drivers::Gpios::Create())); sServices->gpios(std::unique_ptr<drivers::Gpios>(
drivers::Gpios::Create(sServices->nvs().LockPolarity())));
// NVS is needed early so that we can correctly initialise the display.
sServices->nvs(
std::unique_ptr<drivers::NvsStorage>(drivers::NvsStorage::OpenSync()));
ESP_LOGI(kTag, "starting ui"); ESP_LOGI(kTag, "starting ui");
if (!ui::UiState::InitBootSplash(sServices->gpios(), sServices->nvs())) { if (!ui::UiState::InitBootSplash(sServices->gpios(), sServices->nvs())) {
@ -102,8 +107,6 @@ auto Booting::entry() -> void {
sServices->bluetooth().Enable(); sServices->bluetooth().Enable();
} }
sServices->nvs().LockPolarity(false);
BootComplete ev{.services = sServices}; BootComplete ev{.services = sServices};
events::Audio().Dispatch(ev); events::Audio().Dispatch(ev);
events::Ui().Dispatch(ev); events::Ui().Dispatch(ev);

Loading…
Cancel
Save