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 namespace Input
{ {
Detector::Detector(std::map<std::string, char> input_config, std::set<InputEvent> events) : Detector::Detector(std::map<std::string, char> input_config, std::map<char, InputEvent> events) :
gpio(input_config), input_events(events), is_running(true) gpio(input_config), input_events(events)
{ {
detect_thread = std::thread(&Detector::detect, this);
} }
Detector::~Detector() Detector::~Detector()
{ {
is_running = false;
detect_thread.join();
} }
void Detector::register_input_event_handler(InputEventHandler* handler) void Detector::register_input_event_handler(InputEventHandler* handler)
@@ -41,15 +38,12 @@ void Detector::unregister_input_event_handler(InputEventHandler* handler)
void Detector::detect() void Detector::detect()
{ {
while(is_running)
{
char address; char address;
if(check_inputs(address)) if(check_inputs(address))
{ {
InputEvent event = find_event(address); InputEvent& event = input_events.at(address);
notify_handlers(event); notify_handlers(event);
} }
}
} }
void Detector::notify_handlers(InputEvent& event) void Detector::notify_handlers(InputEvent& event)
@@ -75,7 +69,7 @@ bool Detector::check_inputs(char& address)
digitalWrite(gpio["COL_C"], col & 0b100); digitalWrite(gpio["COL_C"], col & 0b100);
// wait for mux to set address // 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"])) if(digitalRead(gpio["INPUT"]))
{ {
address = pow(2, row) + col; address = pow(2, row) + col;
@@ -86,12 +80,4 @@ bool Detector::check_inputs(char& address)
return false; 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 <vector>
#include <thread> #include <thread>
#include <set>
#include <map> #include <map>
#include "InputEvent.h" #include "InputEvent.h"
#include "InputEventHandler.h" #include "InputEventHandler.h"
#define SLEEP_DURATION 10 #define SLEEP_DURATION_NANO 900
namespace Input namespace Input
{ {
class Detector class Detector
{ {
public: 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(); ~Detector();
void detect();
void register_input_event_handler(InputEventHandler* handler); void register_input_event_handler(InputEventHandler* handler);
void unregister_input_event_handler(InputEventHandler* handler); void unregister_input_event_handler(InputEventHandler* handler);
private: private:
void detect();
bool check_inputs(char& address); bool check_inputs(char& address);
void notify_handlers(InputEvent& event); void notify_handlers(InputEvent& event);
InputEvent& find_event(char address);
private: private:
std::map<std::string, char> gpio; std::map<std::string, char> gpio;
std::set<InputEvent> input_events; std::map<char, InputEvent> input_events;
std::set<InputEventHandler*> event_handler; std::set<InputEventHandler*> event_handler;
std::thread detect_thread;
bool is_running;
}; };
} }

View File

@@ -13,12 +13,34 @@
namespace Input namespace Input
{ {
class InputEventHandler class InputEventHandler
{ {
class Detector
{
public: 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; virtual void handle(InputEvent& event) = 0;
private:
std::shared_ptr<Detector> detector;
}; };
} }

View File

@@ -22,7 +22,7 @@ class InputFactory
{ {
public: 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); std::ifstream input_config_stream(input_config_path);
json input_config; json input_config;
@@ -34,7 +34,7 @@ public:
std::vector<InputEvent> input_events = create_input_events(matrix_config); 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: private:

View File

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