summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/gpios.cpp18
-rw-r--r--src/drivers/include/gpios.hpp9
2 files changed, 21 insertions, 6 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<uint8_t, uint8_t> 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<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 {
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<uint16_t> ports_;
std::atomic<uint16_t> inputs_;
+ const bool invert_lock_switch_;
};
} // namespace drivers