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)
@@ -40,17 +37,14 @@ void Detector::unregister_input_event_handler(InputEventHandler* handler)
}
void Detector::detect()
{
while(is_running)
{
char address;
if(check_inputs(address))
{
InputEvent event = find_event(address);
InputEvent& event = input_events.at(address);
notify_handlers(event);
}
}
}
void Detector::notify_handlers(InputEvent& 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; }
);
}
}

View File

@@ -10,41 +10,38 @@
#include <vector>
#include <thread>
#include <set>
#include <map>
#include "InputEvent.h"
#include "InputEventHandler.h"
#define SLEEP_DURATION 10
#define SLEEP_DURATION_NANO 900
namespace Input
{
class Detector
{
public:
Detector(std::map<std::string, char> input_config, std::set<InputEvent> events);
Detector(std::map<std::string, char> input_config, std::map<char, InputEvent> events);
~Detector();
void detect();
void register_input_event_handler(InputEventHandler* handler);
void unregister_input_event_handler(InputEventHandler* handler);
private:
void detect();
bool check_inputs(char& address);
void notify_handlers(InputEvent& event);
InputEvent& find_event(char address);
private:
std::map<std::string, char> gpio;
std::set<InputEvent> input_events;
std::map<char, InputEvent> input_events;
std::set<InputEventHandler*> event_handler;
std::thread detect_thread;
bool is_running;
};
}

View File

@@ -13,12 +13,34 @@
namespace Input
{
class InputEventHandler
{
class Detector
{
public:
virtual ~InputEventHandler(){};
void register_input_event_handler(InputEventHandler* handler);
void unregister_input_event_handler(InputEventHandler* handler);
};
public:
InputEventHandler(std::shared_ptr<Detector> detector) :
detector(detector)
{
this->detector->register_input_event_handler(this);
}
virtual ~InputEventHandler()
{
this->detector->unregister_input_event_handler(this);
this->detector = NULL;
}
virtual void handle(InputEvent& event) = 0;
private:
std::shared_ptr<Detector> detector;
};
}

View File

@@ -22,7 +22,7 @@ class InputFactory
{
public:
static Detector* get_detector(std::string& input_config_path, std::string& matrix_config_path)
static std::shared_ptr<Detector> get_detector(std::string& input_config_path, std::string& matrix_config_path)
{
std::ifstream input_config_stream(input_config_path);
json input_config;
@@ -34,7 +34,7 @@ public:
std::vector<InputEvent> input_events = create_input_events(matrix_config);
return new Detector(input_config, input_events);
return std::shared_ptr<Detector>(Detector(input_config, input_events));
}
private:

View File

@@ -4,9 +4,5 @@
#include <iostream>
int main()
{
Input::Detector(std::set<Input::InputEvent*>());
std::printf("hallo");
return 0;
}