diff --git a/doorlockd/door.cpp b/doorlockd/door.cpp index 35ceb17..96ac903 100644 --- a/doorlockd/door.cpp +++ b/doorlockd/door.cpp @@ -32,7 +32,7 @@ Door &Door::get() return d; } -const Door::State &Door::state() const +Door::State Door::state() const { return _state; } @@ -82,13 +82,6 @@ void Door::unlock() usleep(10000); digitalWrite(_HEARTBEATPIN, LOW); usleep(10000); - - if (!digitalRead(_LOCKPIN)) { - std::thread([this] () { - _l(LogLevel::info, "Incoming door close request on button press"); - lock(); - }).detach(); - } }; // The default of the Schnapperpin: always high @@ -120,6 +113,16 @@ void Door::unlock() // Heartbeat beat(); + + if (!digitalRead(_LOCKPIN)) { + std::thread([this] () { + _l(LogLevel::info, "Incoming door close request on button press"); + lock(); + }).detach(); + + // Busy wait till door is locked + while(_state == State::Unlocked); + } } }); diff --git a/doorlockd/door.h b/doorlockd/door.h index f8ba736..614720a 100644 --- a/doorlockd/door.h +++ b/doorlockd/door.h @@ -29,7 +29,7 @@ public: enum class State {Locked, Unlocked}; // Current state of the door - const State &state() const; + State state() const; // Lock the door void lock(); @@ -44,7 +44,7 @@ private: const Logger &_l; // Indicates the internal state: Door is open or locked - State _state = { Door::State::Locked }; + volatile State _state = { Door::State::Locked }; // A Heartbeat thread is started when the door is unlocked std::thread _heartbeat = { };