finished input
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
namespace Input
|
||||
{
|
||||
|
||||
Detector::Detector(std::set<InputEvent*> events) :
|
||||
input_events(events), is_running(true)
|
||||
Detector::Detector(std::map<std::string, char> input_config, std::set<InputEvent> events) :
|
||||
gpio(input_config), input_events(events), is_running(true)
|
||||
{
|
||||
detect_thread = std::thread(&Detector::detect, this);
|
||||
}
|
||||
@@ -46,10 +46,8 @@ void Detector::detect()
|
||||
char address;
|
||||
if(check_inputs(address))
|
||||
{
|
||||
if(InputEvent* event = find_event(address))
|
||||
{
|
||||
notify_handlers(*event);
|
||||
}
|
||||
InputEvent event = find_event(address);
|
||||
notify_handlers(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,7 +75,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::milliseconds(SLEEP_DURATION));
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(SLEEP_DURATION));
|
||||
if(digitalRead(gpio["INPUT"]))
|
||||
{
|
||||
address = pow(2, row) + col;
|
||||
@@ -88,10 +86,11 @@ bool Detector::check_inputs(char& address)
|
||||
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(),
|
||||
[address] (InputEvent* e) { return *e == address; }
|
||||
[address] (InputEvent& e) { return e == address; }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include "InputEvent.h"
|
||||
#include "InputEventHandler.h"
|
||||
|
||||
#define SLEEP_DURATION 0.1f
|
||||
#define SLEEP_DURATION 10
|
||||
|
||||
namespace Input
|
||||
{
|
||||
@@ -24,7 +24,7 @@ namespace Input
|
||||
class Detector
|
||||
{
|
||||
public:
|
||||
Detector(std::set<InputEvent*> events);
|
||||
Detector(std::map<std::string, char> input_config, std::set<InputEvent> events);
|
||||
~Detector();
|
||||
|
||||
void register_input_event_handler(InputEventHandler* handler);
|
||||
@@ -35,12 +35,12 @@ private:
|
||||
|
||||
bool check_inputs(char& address);
|
||||
void notify_handlers(InputEvent& event);
|
||||
InputEvent* find_event(char address);
|
||||
InputEvent& find_event(char address);
|
||||
|
||||
private:
|
||||
std::map<std::string, char> gpio;
|
||||
|
||||
std::set<InputEvent*> input_events;
|
||||
std::set<InputEvent> input_events;
|
||||
std::set<InputEventHandler*> event_handler;
|
||||
|
||||
std::thread detect_thread;
|
||||
|
||||
@@ -7,9 +7,14 @@
|
||||
|
||||
#ifndef 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
|
||||
{
|
||||
@@ -17,17 +22,35 @@ class InputFactory
|
||||
{
|
||||
|
||||
public:
|
||||
InputFactory();
|
||||
~InputFactory();
|
||||
static Detector* get_detector(std::string& input_config_path, std::string& matrix_config_path)
|
||||
{
|
||||
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:
|
||||
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_ */
|
||||
|
||||
17330
FlippR-Driver/src/lib/json/json.hpp
Normal file
17330
FlippR-Driver/src/lib/json/json.hpp
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user