finished input

This commit is contained in:
Jonas Zeunert
2018-04-27 01:46:43 +02:00
parent 65bf3e5958
commit 20b5be8bfa
4 changed files with 17373 additions and 21 deletions

View File

@@ -16,8 +16,8 @@
namespace Input namespace Input
{ {
Detector::Detector(std::set<InputEvent*> events) : Detector::Detector(std::map<std::string, char> input_config, std::set<InputEvent> events) :
input_events(events), is_running(true) gpio(input_config), input_events(events), is_running(true)
{ {
detect_thread = std::thread(&Detector::detect, this); detect_thread = std::thread(&Detector::detect, this);
} }
@@ -46,10 +46,8 @@ void Detector::detect()
char address; char address;
if(check_inputs(address)) if(check_inputs(address))
{ {
if(InputEvent* event = find_event(address)) InputEvent event = find_event(address);
{ notify_handlers(event);
notify_handlers(*event);
}
} }
} }
} }
@@ -77,7 +75,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::milliseconds(SLEEP_DURATION)); std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_DURATION));
if(digitalRead(gpio["INPUT"])) if(digitalRead(gpio["INPUT"]))
{ {
address = pow(2, row) + col; address = pow(2, row) + col;
@@ -88,10 +86,11 @@ bool Detector::check_inputs(char& address)
return false; return false;
} }
InputEvent* Detector::find_event(char address) InputEvent& Detector::find_event(char address)
{ {
// TODO this is shit
return *std::find_if(input_events.begin(), input_events.end(), return *std::find_if(input_events.begin(), input_events.end(),
[address] (InputEvent* e) { return *e == address; } [address] (InputEvent& e) { return e == address; }
); );
} }

View File

@@ -16,7 +16,7 @@
#include "InputEvent.h" #include "InputEvent.h"
#include "InputEventHandler.h" #include "InputEventHandler.h"
#define SLEEP_DURATION 0.1f #define SLEEP_DURATION 10
namespace Input namespace Input
{ {
@@ -24,7 +24,7 @@ namespace Input
class Detector class Detector
{ {
public: public:
Detector(std::set<InputEvent*> events); Detector(std::map<std::string, char> input_config, std::set<InputEvent> events);
~Detector(); ~Detector();
void register_input_event_handler(InputEventHandler* handler); void register_input_event_handler(InputEventHandler* handler);
@@ -35,12 +35,12 @@ private:
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); 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::set<InputEvent> input_events;
std::set<InputEventHandler*> event_handler; std::set<InputEventHandler*> event_handler;
std::thread detect_thread; std::thread detect_thread;

View File

@@ -7,9 +7,14 @@
#ifndef INPUTFACTORY_H_ #ifndef INPUTFACTORY_H_
#define INPUTFACTORY_H_ #define INPUTFACTORY_H_
#include <vector>
#include "InputEvent.h" #include <fstream>
#include "Detector.h"
#include "../lib/json/json.hpp"
using namespace nlohmann;
namespace Input namespace Input
{ {
@@ -17,17 +22,35 @@ class InputFactory
{ {
public: public:
InputFactory(); static Detector* get_detector(std::string& input_config_path, std::string& matrix_config_path)
~InputFactory(); {
std::ifstream input_config_stream(input_config_path);
json input_config;
input_config << input_config_stream;
std::vector<InputEvent*> get_input_events(); std::ifstream matrix_config_stream(input_config_path);
json matrix_config;
matrix_config << matrix_config_stream;
std::vector<InputEvent> input_events = create_input_events(matrix_config);
return new Detector(input_config, input_events);
}
private: private:
std::vector<InputEvent*> input_events; static std::vector<InputEvent> create_input_events(json matrix_config)
{
std::vector<InputEvent> input_events;
for(auto& key : matrix_config)
{
InputEvent event(matrix_config[key], key);
}
return input_events;
}
}; };
} }
#endif /* INPUTFACTORY_H_ */ #endif /* INPUTFACTORY_H_ */

File diff suppressed because it is too large Load Diff