#include #include #include #include "logger.h" #include "door.h" using namespace std; Door::Door() : _l(Logger::get()) { _l(LogLevel::info, "Initializing Raspberry Pi GPIOs"); wiringPiSetup(); pinMode(_HEARTBEATPIN, OUTPUT); pinMode(_SCHNAPPERPIN, OUTPUT); lock(); } Door::~Door() { lock(); } Door &Door::get() { static Door d; return d; } void Door::lock() { digitalWrite(_SCHNAPPERPIN, HIGH); _l(LogLevel::info, "Door closed"); if (_state == State::Unlocked) { // Stop the Heartbeat Thread _state = State::Locked; _heartbeat.join(); } // Turn off all lights system("wget -O /dev/null --timeout 3 \"http://homer.binary.kitchen:8080/set?color=000000\" > /dev/null 2>&1"); } void Door::unlock() { // In any case, klacker the schnapper _schnapper = true; // If heartbeat is already running, return if (_state == State::Unlocked) { return; } // If not, first set state to unlocked _state = State::Unlocked; // Start the Heartbeat Thread _heartbeat = std::thread([this] () { // One "beat" is one complete cycle of the heartbeat clock auto beat = [] () { digitalWrite(_HEARTBEATPIN, HIGH); usleep(10000); digitalWrite(_HEARTBEATPIN, LOW); usleep(10000); }; // The default of the Schnapperpin: always high digitalWrite(_SCHNAPPERPIN, HIGH); // Heartbeat while the state is unlocked while (_state == State::Unlocked) { // In case of schnapper, send 0x55 resp. 0xaa to the schnapperpin if (_schnapper == true) { for (int i = 0; i < 32 ; i++) { // Set '0' digitalWrite(_SCHNAPPERPIN, LOW); // cycle and send beat(); // Set '1' digitalWrite(_SCHNAPPERPIN, HIGH); // cycle and send beat(); } // Reset schnapperpin digitalWrite(_SCHNAPPERPIN, HIGH); // and deactivate schnapper for the next round _schnapper = false; } // Heartbeat beat(); } }); _l(LogLevel::info, "Door opened"); }