refactoring

This commit is contained in:
Jonas Zeunert
2018-05-06 19:26:59 +02:00
parent 20b5be8bfa
commit c2fb59139b
5 changed files with 39 additions and 38 deletions

View File

@@ -16,17 +16,14 @@
namespace Input
{
Detector::Detector(std::map<std::string, char> input_config, std::set<InputEvent> events) :
gpio(input_config), input_events(events), is_running(true)
Detector::Detector(std::map<std::string, char> input_config, std::map<char, InputEvent> events) :
gpio(input_config), input_events(events)
{
detect_thread = std::thread(&Detector::detect, this);
}
Detector::~Detector()
{
is_running = false;
detect_thread.join();
}
void Detector::register_input_event_handler(InputEventHandler* handler)
@@ -41,14 +38,11 @@ void Detector::unregister_input_event_handler(InputEventHandler* handler)
void Detector::detect()
{
while(is_running)
char address;
if(check_inputs(address))
{
char address;
if(check_inputs(address))
{
InputEvent event = find_event(address);
notify_handlers(event);
}
InputEvent& event = input_events.at(address);
notify_handlers(event);
}
}
@@ -75,7 +69,7 @@ bool Detector::check_inputs(char& address)
digitalWrite(gpio["COL_C"], col & 0b100);
// wait for mux to set address
std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_DURATION));
std::this_thread::sleep_for(std::chrono::nanoseconds(SLEEP_DURATION_NANO));
if(digitalRead(gpio["INPUT"]))
{
address = pow(2, row) + col;
@@ -86,12 +80,4 @@ bool Detector::check_inputs(char& address)
return false;
}
InputEvent& Detector::find_event(char address)
{
// TODO this is shit
return *std::find_if(input_events.begin(), input_events.end(),
[address] (InputEvent& e) { return e == address; }
);
}
}