/* * Detector.cpp * * Created on: Apr 5, 2018 * Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht */ #include "Detector.h" #include #include #include "utilities/config.h" namespace FlippR_Driver { namespace Input { Detector::Detector(std::unique_ptr input_gpio_interface, std::map> events, std::shared_ptr event_notifier) : input_gpio_interface(std::move(input_gpio_interface)), events(events), is_running(true), event_notifier(event_notifier) { this->detect_thread = std::thread(&Detector::detect, this); CLOG(INFO, INPUT_LOGGER) << "Created Detector and started detecting!"; } Detector::~Detector() { this->is_running = false; this->detect_thread.join(); } // Cycles over all s and enqueues an event if detected. void Detector::detect() { while(this->is_running) { char address; if(this->check_inputs(address)) { try { auto event = events.at(address); CLOG(DEBUG, INPUT_LOGGER) << "Found event " << event->name << "(" << event->address << ")."; event_notifier->distribute_event(event); } catch(std::out_of_range& e) { CLOG_N_TIMES(1, WARNING, INPUT_LOGGER) << "Did not found event for address: " << address << " check config-file"; } } } } bool Detector::check_inputs(char& address) { for(char pin = 0; pin < (INPUT_MATRIX_SIZE * INPUT_MATRIX_SIZE); pin++) { if(input_gpio_interface->read_data(pin)) { CLOG(DEBUG, INPUT_LOGGER) << "Pin " << pin << " is triggered."; address = pin; return true; } } return false; } } }