/* * Detector.cpp * * Created on: Apr 5, 2018 * Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht */ #include "Detector.h" #include #include namespace Input { Detector::Detector(GPIOInterface& gpio_interface, std::map events, InputEventNotifier& input_event_notifier) : gpio_interface(gpio_interface), input_events(events), is_running(true), input_event_notifier(input_event_notifier) { detect_thread = std::thread(&Detector::detect, this); } Detector::~Detector() { is_running = false; detect_thread.join(); } // Cycles over all inputs and enqueues an input event if detected. void Detector::detect() { while(is_running) { char address; if(check_inputs(address)) { try { InputEvent& event = input_events.at(address); input_event_notifier.distribute_event(event); } catch(std::out_of_range& e) { // todo log exception! } } } } bool Detector::check_inputs(char& address) { for(int row = 0; row < 8; row++) { gpio_interface.write_input_row(row); for(int col = 0; col < 8; col++) { gpio_interface.write_input_col(col); // wait for mux to set address std::this_thread::sleep_for(std::chrono::nanoseconds(SLEEP_DURATION_NANO)); if(gpio_interface.read_input_data()) { address = pow(2, row) + col; return true; } } } return false; } }