diff --git a/door.cpp b/door.cpp index 50c705b..0f6b2d1 100644 --- a/door.cpp +++ b/door.cpp @@ -1,26 +1,26 @@ #include + #include +#include #include "logger.h" #include "door.h" using namespace std; - -constexpr int LOCKPIN = 10; - -Door::Door() : _l(Logger::get()) +Door::Door() : + _l(Logger::get()) { _l(LogLevel::info, "Initializing Raspberry Pi GPIOs"); wiringPiSetup(); - pinMode(LOCKPIN, OUTPUT); - + pinMode(_LOCKPIN, OUTPUT); + pinMode(_SCHNAPPER, OUTPUT); lock(); } Door::~Door() { - + lock(); } Door &Door::get() @@ -31,12 +31,44 @@ Door &Door::get() void Door::lock() { + digitalWrite(_SCHNAPPER, HIGH); _l(LogLevel::info, "Door closed"); - digitalWrite(LOCKPIN, HIGH); + if (_open == true) + { + _open = false; + _heartbeat.join(); + } } void Door::unlock() { + _schnapper = true; + + if (_open == true) + { + return; + } + + _open = true; + _heartbeat = std::thread([this] () { + digitalWrite(_SCHNAPPER, HIGH); + auto beat = [] () { + digitalWrite(_LOCKPIN, HIGH); + usleep(10000); + digitalWrite(_LOCKPIN, LOW); + usleep(10000); + }; + while (_open) { + if (_schnapper == true) + { + digitalWrite(_SCHNAPPER, LOW); + for (int i = 0; i < 32 ; i++) beat(); + digitalWrite(_SCHNAPPER, HIGH); + _schnapper = false; + } + beat(); + } + }); + _l(LogLevel::info, "Door opened"); - digitalWrite(LOCKPIN, LOW); } diff --git a/door.h b/door.h index 0a9e36a..d59c2a1 100644 --- a/door.h +++ b/door.h @@ -2,6 +2,7 @@ #define DOOR_H #include +#include #include "logger.h" @@ -20,6 +21,14 @@ private: Door(); const Logger &_l; + + bool _open = { false }; + std::thread _heartbeat = { }; + + bool _schnapper = { false }; + + static constexpr int _LOCKPIN = 10; + static constexpr int _SCHNAPPER = 7; }; #endif diff --git a/logic.cpp b/logic.cpp index b8cb8ee..d710b41 100644 --- a/logic.cpp +++ b/logic.cpp @@ -149,16 +149,16 @@ Logic::Response Logic::_lock() Logic::Response Logic::_unlock() { - if (_state == UNLOCKED) - { - _logger(LogLevel::warning, "Unable to unlock: already unlocked"); - return AlreadyUnlocked; - } - _door.unlock(); _state = UNLOCKED; _createNewToken(false); + if (_state == UNLOCKED) + { + _logger(LogLevel::warning, "Unable to unlock: already unlocked"); + return AlreadyUnlocked; + } + return Success; } diff --git a/main.cpp b/main.cpp index 89fcad2..34b61df 100644 --- a/main.cpp +++ b/main.cpp @@ -113,6 +113,9 @@ int main(int argc, char** argv) l(LogLevel::notice, "Starting doorlockd"); + system("/usr/bin/gpio load spi"); + system("/usr/bin/gpio load i2c"); + try { unsigned int timeout; po::options_description desc("usage: doorlockd");