diff --git a/FlippR-Driver/src/input/Detector.cpp b/FlippR-Driver/src/input/Detector.cpp index 822cc47..c7ad263 100644 --- a/FlippR-Driver/src/input/Detector.cpp +++ b/FlippR-Driver/src/input/Detector.cpp @@ -16,17 +16,14 @@ namespace Input { -Detector::Detector(std::map input_config, std::set events) : - gpio(input_config), input_events(events), is_running(true) +Detector::Detector(std::map input_config, std::map 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) @@ -41,14 +38,11 @@ void Detector::unregister_input_event_handler(InputEventHandler* handler) void Detector::detect() { - while(is_running) + char address; + if(check_inputs(address)) { - char address; - if(check_inputs(address)) - { - InputEvent event = find_event(address); - notify_handlers(event); - } + InputEvent& event = input_events.at(address); + notify_handlers(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; } - ); -} - } diff --git a/FlippR-Driver/src/input/Detector.h b/FlippR-Driver/src/input/Detector.h index 13ef4e9..f87c92f 100644 --- a/FlippR-Driver/src/input/Detector.h +++ b/FlippR-Driver/src/input/Detector.h @@ -10,41 +10,38 @@ #include #include -#include #include #include "InputEvent.h" #include "InputEventHandler.h" -#define SLEEP_DURATION 10 +#define SLEEP_DURATION_NANO 900 namespace Input { class Detector { + public: - Detector(std::map input_config, std::set events); + Detector(std::map input_config, std::map 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 gpio; - std::set input_events; + std::map input_events; std::set event_handler; - - std::thread detect_thread; - bool is_running; }; } diff --git a/FlippR-Driver/src/input/InputEventHandler.h b/FlippR-Driver/src/input/InputEventHandler.h index 1857a32..2350de4 100644 --- a/FlippR-Driver/src/input/InputEventHandler.h +++ b/FlippR-Driver/src/input/InputEventHandler.h @@ -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) + { + 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; }; } diff --git a/FlippR-Driver/src/input/InputFactory.h b/FlippR-Driver/src/input/InputFactory.h index 5ed95e9..070bd1d 100644 --- a/FlippR-Driver/src/input/InputFactory.h +++ b/FlippR-Driver/src/input/InputFactory.h @@ -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 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 input_events = create_input_events(matrix_config); - return new Detector(input_config, input_events); + return std::shared_ptr(Detector(input_config, input_events)); } private: diff --git a/FlippR-Driver/src/input/main.cpp b/FlippR-Driver/src/input/main.cpp index 0fc28ce..2450b1f 100644 --- a/FlippR-Driver/src/input/main.cpp +++ b/FlippR-Driver/src/input/main.cpp @@ -4,9 +4,5 @@ #include int main() { - Input::Detector(std::set()); - - std::printf("hallo"); - return 0; }