mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 02:14:26 +01:00
Added Schnapper functionality, added i2c and spi load stuff
This commit is contained in:
parent
ae8f2b83b5
commit
f04b45e3eb
50
door.cpp
50
door.cpp
@ -1,26 +1,26 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
9
door.h
9
door.h
@ -2,6 +2,7 @@
|
||||
#define DOOR_H
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#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
|
||||
|
12
logic.cpp
12
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user