74 lines
1.5 KiB
C++
74 lines
1.5 KiB
C++
/*
|
|
* Detector.cpp
|
|
*
|
|
* Created on: Apr 5, 2018
|
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
|
*/
|
|
|
|
#include "Detector.h"
|
|
|
|
#include <iostream>
|
|
#include <math.h>
|
|
|
|
#include "../utilities/config.h"
|
|
|
|
namespace Input
|
|
{
|
|
|
|
Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> events, IEventNotifier* event_notifier) :
|
|
input_gpio_interface(input_gpio_interface), events(events), is_running(true), event_notifier(event_notifier)
|
|
{
|
|
detect_thread = std::thread(&Detector::detect, this);
|
|
|
|
CLOG(WARNING, INPUT_LOGGER) << "Created Detector";
|
|
}
|
|
|
|
Detector::~Detector()
|
|
{
|
|
is_running = false;
|
|
|
|
detect_thread.join();
|
|
|
|
delete this->input_gpio_interface;
|
|
this->input_gpio_interface = NULL;
|
|
|
|
delete this->event_notifier;
|
|
this->event_notifier = NULL;
|
|
}
|
|
|
|
// Cycles over all s and enqueues an event if detected.
|
|
void Detector::detect()
|
|
{
|
|
while(is_running)
|
|
{
|
|
char address;
|
|
if(check_inputs(address))
|
|
{
|
|
try
|
|
{
|
|
Event& event = events.at(address);
|
|
event_notifier->distribute_event(event);
|
|
}
|
|
catch(std::out_of_range& e)
|
|
{
|
|
CLOG(WARNING, INPUT_LOGGER) << "Did not found event for address: " << address << " check config-file";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Detector::check_inputs(char& address)
|
|
{
|
|
for(int pin = 0; pin < (INPUT_MATRIX_SIZE * INPUT_MATRIX_SIZE); pin++)
|
|
{
|
|
if(input_gpio_interface->read_input_data(pin))
|
|
{
|
|
address = pin;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|