finished detector
This commit is contained in:
98
FlippR-Driver/src/input/Detector.cpp
Normal file
98
FlippR-Driver/src/input/Detector.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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 <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
#include "../lib/wiringPi/wiringPi.h"
|
||||
|
||||
namespace Input
|
||||
{
|
||||
|
||||
Detector::Detector(std::set<InputEvent*> events) :
|
||||
input_events(events), is_running(true)
|
||||
{
|
||||
detect_thread = std::thread(&Detector::detect, this);
|
||||
}
|
||||
|
||||
|
||||
Detector::~Detector()
|
||||
{
|
||||
is_running = false;
|
||||
detect_thread.join();
|
||||
}
|
||||
|
||||
void Detector::register_input_event_handler(InputEventHandler* handler)
|
||||
{
|
||||
event_handler.insert(handler);
|
||||
}
|
||||
|
||||
void Detector::unregister_input_event_handler(InputEventHandler* handler)
|
||||
{
|
||||
event_handler.erase(handler);
|
||||
}
|
||||
|
||||
void Detector::detect()
|
||||
{
|
||||
while(is_running)
|
||||
{
|
||||
char address;
|
||||
if(check_inputs(address))
|
||||
{
|
||||
if(InputEvent* event = find_event(address))
|
||||
{
|
||||
notify_handlers(*event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Detector::notify_handlers(InputEvent& event)
|
||||
{
|
||||
for(auto* handler : event_handler)
|
||||
{
|
||||
handler->handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
bool Detector::check_inputs(char& address)
|
||||
{
|
||||
for(int row = 0; row < 8; row++)
|
||||
{
|
||||
digitalWrite(gpio["ROW_A"], row & 0b001);
|
||||
digitalWrite(gpio["ROW_B"], row & 0b010);
|
||||
digitalWrite(gpio["ROW_C"], row & 0b100);
|
||||
|
||||
for(int col = 0; col < 8; col++)
|
||||
{
|
||||
digitalWrite(gpio["COL_A"], col & 0b001);
|
||||
digitalWrite(gpio["COL_B"], col & 0b010);
|
||||
digitalWrite(gpio["COL_C"], col & 0b100);
|
||||
|
||||
// wait for mux to set address
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_DURATION));
|
||||
if(digitalRead(gpio["INPUT"]))
|
||||
{
|
||||
address = pow(2, row) + col;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
InputEvent* Detector::find_event(char address)
|
||||
{
|
||||
return *std::find_if(input_events.begin(), input_events.end(),
|
||||
[address] (InputEvent* e) { return *e == address; }
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user