From 01b0d990f3c8c853f78fded8c57f2364c2f47a2d Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Thu, 31 May 2018 17:42:43 +0200 Subject: [PATCH 1/5] refactored InputName --- FlippR-Driver/src/input/Detector.cpp | 36 ++++++++----------- FlippR-Driver/src/input/Detector.h | 12 +++---- FlippR-Driver/src/input/Event.hpp | 8 ++--- FlippR-Driver/src/input/EventHandler.hpp | 10 +++--- FlippR-Driver/src/input/EventNotifier.cpp | 18 +++++----- FlippR-Driver/src/input/EventNotifier.h | 18 +++++----- FlippR-Driver/src/input/InputDriver.hpp | 10 +++++- .../src/utilities/InputGPIOInterface.cpp | 3 ++ 8 files changed, 60 insertions(+), 55 deletions(-) diff --git a/FlippR-Driver/src/input/Detector.cpp b/FlippR-Driver/src/input/Detector.cpp index 9d6dc0b..f8a6fa3 100644 --- a/FlippR-Driver/src/input/Detector.cpp +++ b/FlippR-Driver/src/input/Detector.cpp @@ -13,8 +13,8 @@ namespace Input { -Detector::Detector(InputGPIOInterface* in_gpio_interface, std::map events, InputEventNotifier* input_event_notifier) : - in_gpio_interface(in_gpio_interface), input_events(events), is_running(true), input_event_notifier(input_event_notifier) +Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map events, EventNotifier* event_notifier) : + input_gpio_interface(input_gpio_interface), events(events), is_running(true), event_notifier(event_notifier) { detect_thread = std::thread(&Detector::detect, this); } @@ -24,13 +24,13 @@ Detector::~Detector() is_running = false; detect_thread.join(); - delete this->in_gpio_interface; - this->in_gpio_interface = NULL; - delete this->input_event_notifier; - this->input_event_notifier = NULL; + delete this->input_gpio_interface; + this->input_gpio_interface = NULL; + delete this->event_notifier; + this->event_notifier = NULL; } -// Cycles over all inputs and enqueues an input event if detected. +// Cycles over all s and enqueues an event if detected. void Detector::detect() { while(is_running) @@ -40,8 +40,8 @@ void Detector::detect() { try { - InputEvent& event = input_events.at(address); - input_event_notifier->distribute_event(event); + Event& event = events.at(address); + event_notifier->distribute_event(event); } catch(std::out_of_range& e) { @@ -53,24 +53,16 @@ void Detector::detect() bool Detector::check_inputs(char& address) { - for(int row = 0; row < 8; row++) + for(int pin = 0; pin < MATRIX_SIZE * MATRIX_SIZE; pin++) { - in_gpio_interface->write_input_row(row); - - for(int col = 0; col < 8; col++) + if(input_gpio_interface->read_input_data(pin)) { - in_gpio_interface->write_input_col(col); - - // wait for mux to set address - std::this_thread::sleep_for(std::chrono::nanoseconds(SLEEP_DURATION_NANO)); - if(in_gpio_interface->read_input_data()) - { - address = pow(2, row) + col; - return true; - } + address = pin; + return true; } } return false; + } } diff --git a/FlippR-Driver/src/input/Detector.h b/FlippR-Driver/src/input/Detector.h index 0fdba63..25592d5 100644 --- a/FlippR-Driver/src/input/Detector.h +++ b/FlippR-Driver/src/input/Detector.h @@ -26,17 +26,17 @@ namespace Input { -class InputEventHandler +class EventHandler { public: - void handle(InputEvent& event); + void handle(Event& event); }; class Detector { public: - Detector(InputGPIOInterface* in_gpio_interface, std::map events, InputEventNotifier* input_event_notifier); + Detector(InputGPIOInterface* input_gpio_interface, std::map events, EventNotifier* event_notifier); ~Detector(); private: @@ -44,11 +44,11 @@ private: bool check_inputs(char& address); private: - InputGPIOInterface* in_gpio_interface; + InputGPIOInterface* input_gpio_interface; - std::map input_events; + std::map events; - InputEventNotifier* input_event_notifier; + EventNotifier* event_notifier; bool is_running; std::thread detect_thread; diff --git a/FlippR-Driver/src/input/Event.hpp b/FlippR-Driver/src/input/Event.hpp index e017791..7b87424 100644 --- a/FlippR-Driver/src/input/Event.hpp +++ b/FlippR-Driver/src/input/Event.hpp @@ -14,17 +14,17 @@ namespace Input { -class InputEvent +class Event { public: - InputEvent(char address, char priority, std::string name) : address(address), priority(priority), name(name){} + Event(char address, char priority, std::string name) : address(address), priority(priority), name(name){} - bool operator==(const InputEvent& other) + bool operator==(const Event& other) { return this->name == other.name; } - bool operator<(const InputEvent& other) + bool operator<(const Event& other) { return this->priority < other.priority; } diff --git a/FlippR-Driver/src/input/EventHandler.hpp b/FlippR-Driver/src/input/EventHandler.hpp index bb97913..c25fb0c 100644 --- a/FlippR-Driver/src/input/EventHandler.hpp +++ b/FlippR-Driver/src/input/EventHandler.hpp @@ -12,24 +12,24 @@ #ifndef INPUTEVENTHANDLER_H_ #define INPUTEVENTHANDLER_H_ -#include "Detector.h" +#include "InputDriver.h" #include "Event.hpp" namespace Input { -class InputEventHandler +class EventHandler { public: - InputEventHandler(std::shared_ptr detector) : + EventHandler(std::shared_ptr detector) : detector(detector) { - this->detector->register_input_event_handler(this); + this->detector->register_event_handler(this); } virtual ~InputEventHandler() { - this->detector->unregister_input_event_handler(this); + this->detector->unregister_event_handler(this); this->detector = NULL; } diff --git a/FlippR-Driver/src/input/EventNotifier.cpp b/FlippR-Driver/src/input/EventNotifier.cpp index 4e20f64..8c1e1fe 100644 --- a/FlippR-Driver/src/input/EventNotifier.cpp +++ b/FlippR-Driver/src/input/EventNotifier.cpp @@ -1,5 +1,5 @@ /* - * InputEventNotifier.cpp + * EventNotifier.cpp * * Created on: May 17, 2018 * Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht @@ -10,41 +10,41 @@ namespace Input { -InputEventNotifier::InputEventNotifier() +EventNotifier::EventNotifier() : is_running(true) { - notify_thread = std::thread(&InputEventNotifier::notify, this); + notify_thread = std::thread(&EventNotifier::notify, this); } -InputEventNotifier::~InputEventNotifier() +EventNotifier::~EventNotifier() { is_running = false; notify_thread.join(); } -void InputEventNotifier::register_input_event_handler(InputEventHandler* handler) +void EventNotifier::register_event_handler(EventHandler* handler) { std::lock_guard event_handler_guard(event_handler_mutex); event_handler.insert(handler); } -void InputEventNotifier::unregister_input_event_handler(InputEventHandler* handler) +void EventNotifier::unregister_event_handler(EventHandler* handler) { std::lock_guard event_handler_guard(event_handler_mutex); event_handler.erase(handler); } -void InputEventNotifier::distribute_event(InputEvent& event) +void EventNotifier::distribute_event(Event& event) { event_queue.push(event); } -void InputEventNotifier::notify() +void EventNotifier::notify() { while(is_running) { - InputEvent event = event_queue.pop(); + Event event = event_queue.pop(); // getting a guard and calling all registered handlers std::lock_guard event_handler_guard(event_handler_mutex); diff --git a/FlippR-Driver/src/input/EventNotifier.h b/FlippR-Driver/src/input/EventNotifier.h index b0c5c28..bdbca2e 100644 --- a/FlippR-Driver/src/input/EventNotifier.h +++ b/FlippR-Driver/src/input/EventNotifier.h @@ -18,23 +18,25 @@ namespace Input { -class InputEventNotifier + +class EventNotifier { + public: - InputEventNotifier(); - ~InputEventNotifier(); + EventNotifier(); + ~EventNotifier(); - void register_input_event_handler(InputEventHandler* handler); - void unregister_input_event_handler(InputEventHandler* handler); + void register_event_handler(EventHandler* handler); + void unregister_event_handler(EventHandler* handler); - void distribute_event(InputEvent& event); + void distribute_event(Event& event); private: void notify(); private: - BlockingQueue event_queue; - std::set event_handler; + BlockingQueue event_queue; + std::set event_handler; bool is_running; std::thread notify_thread; diff --git a/FlippR-Driver/src/input/InputDriver.hpp b/FlippR-Driver/src/input/InputDriver.hpp index ba0c520..55ea7a9 100644 --- a/FlippR-Driver/src/input/InputDriver.hpp +++ b/FlippR-Driver/src/input/InputDriver.hpp @@ -8,8 +8,16 @@ #ifndef SRC_INPUT_INPUTDRIVER_HPP_ #define SRC_INPUT_INPUTDRIVER_HPP_ +#include "EventHandler.h" +namespace Input +{ +class InputDriver +{ + void register_event_handler(EventHandler* handler); + void unregister_event_handler(EventHandler* handler); +}; - +} #endif /* SRC_INPUT_INPUTDRIVER_HPP_ */ diff --git a/FlippR-Driver/src/utilities/InputGPIOInterface.cpp b/FlippR-Driver/src/utilities/InputGPIOInterface.cpp index c40a087..26a69c8 100644 --- a/FlippR-Driver/src/utilities/InputGPIOInterface.cpp +++ b/FlippR-Driver/src/utilities/InputGPIOInterface.cpp @@ -15,6 +15,9 @@ bool InputGPIOInterface::read_input_data(char pin) write_input_row(pin / MATRIX_SIZE); write_input_col(pin % MATRIX_SIZE); + // wait for mux to set address + std::this_thread::sleep_for(std::chrono::nanoseconds(SLEEP_DURATION_NANO)); + return read_pin(this->input_data_address); } From ed83469dc3392bf6fd79f1520e619eb80801204d Mon Sep 17 00:00:00 2001 From: Neeflix Date: Thu, 31 May 2018 17:53:40 +0200 Subject: [PATCH 2/5] minior changes --- FlippR-Driver/src/input/EventHandler.hpp | 19 ++++++++++--------- FlippR-Driver/src/input/InputDriver.hpp | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/FlippR-Driver/src/input/EventHandler.hpp b/FlippR-Driver/src/input/EventHandler.hpp index c25fb0c..630da7d 100644 --- a/FlippR-Driver/src/input/EventHandler.hpp +++ b/FlippR-Driver/src/input/EventHandler.hpp @@ -12,8 +12,9 @@ #ifndef INPUTEVENTHANDLER_H_ #define INPUTEVENTHANDLER_H_ -#include "InputDriver.h" +#include "InputDriver.hpp" #include "Event.hpp" +#include "Detector.h" namespace Input { @@ -21,22 +22,22 @@ namespace Input class EventHandler { public: - EventHandler(std::shared_ptr detector) : - detector(detector) + EventHandler(std::shared_ptr input_driver) : + input_driver(input_driver) { - this->detector->register_event_handler(this); + this->input_driver->register_event_handler(this); } - virtual ~InputEventHandler() + virtual ~EventHandler() { - this->detector->unregister_event_handler(this); - this->detector = NULL; + this->input_driver->unregister_event_handler(this); + this->input_driver = NULL; } - virtual void handle(InputEvent& event); + virtual void handle(Event& event); private: - std::shared_ptr detector; + std::shared_ptr input_driver; }; } diff --git a/FlippR-Driver/src/input/InputDriver.hpp b/FlippR-Driver/src/input/InputDriver.hpp index 55ea7a9..95701a1 100644 --- a/FlippR-Driver/src/input/InputDriver.hpp +++ b/FlippR-Driver/src/input/InputDriver.hpp @@ -8,7 +8,7 @@ #ifndef SRC_INPUT_INPUTDRIVER_HPP_ #define SRC_INPUT_INPUTDRIVER_HPP_ -#include "EventHandler.h" +#include "EventHandler.hpp" namespace Input { From 25590b3de2a0126a6b5e4bce9473b392de930393 Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Thu, 31 May 2018 18:04:47 +0200 Subject: [PATCH 3/5] refactored json bib --- FlippR-Driver/src/utilities/GPIOInterface.hpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/FlippR-Driver/src/utilities/GPIOInterface.hpp b/FlippR-Driver/src/utilities/GPIOInterface.hpp index cac0b82..c047ea7 100644 --- a/FlippR-Driver/src/utilities/GPIOInterface.hpp +++ b/FlippR-Driver/src/utilities/GPIOInterface.hpp @@ -15,11 +15,8 @@ #include -#include "../lib/json/json.hpp" #include "../lib/wiringPi/wiringPi.h" -using namespace nlohmann; - class GPIOInterface { public: @@ -35,9 +32,6 @@ public: { return digitalRead(address); } - -protected: - json config; }; From d8fcc75f19e982b025eb8da203f3514f6ad94826 Mon Sep 17 00:00:00 2001 From: Neeflix Date: Thu, 31 May 2018 18:26:17 +0200 Subject: [PATCH 4/5] added config.h --- FlippR-Driver/src/input/Detector.cpp | 4 +++- FlippR-Driver/src/input/Detector.h | 2 ++ FlippR-Driver/src/input/InputDriverFactory.hpp | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/FlippR-Driver/src/input/Detector.cpp b/FlippR-Driver/src/input/Detector.cpp index f8a6fa3..c247594 100644 --- a/FlippR-Driver/src/input/Detector.cpp +++ b/FlippR-Driver/src/input/Detector.cpp @@ -10,6 +10,8 @@ #include #include +#include "../utilities/config.h" + namespace Input { @@ -45,7 +47,7 @@ void Detector::detect() } catch(std::out_of_range& e) { - // todo log exception! + CLOG(WARNING, INPUT_LOGGER) << "Did not found event for address: " << address << " check config-file"; } } } diff --git a/FlippR-Driver/src/input/Detector.h b/FlippR-Driver/src/input/Detector.h index 25592d5..63b8f87 100644 --- a/FlippR-Driver/src/input/Detector.h +++ b/FlippR-Driver/src/input/Detector.h @@ -17,6 +17,8 @@ #include #include +#include "../lib/easylogging/easylogging++.h" + #include "../utilities/InputGPIOInterface.h" #include "Event.hpp" #include "EventNotifier.h" diff --git a/FlippR-Driver/src/input/InputDriverFactory.hpp b/FlippR-Driver/src/input/InputDriverFactory.hpp index 1289273..e843f33 100644 --- a/FlippR-Driver/src/input/InputDriverFactory.hpp +++ b/FlippR-Driver/src/input/InputDriverFactory.hpp @@ -14,6 +14,7 @@ #include "../utilities/InputGPIOInterface.h" #include "../lib/json/json.hpp" +#include "../lib/easylogging/easylogging++.h" #include "EventNotifier.h" using namespace nlohmann; @@ -54,6 +55,11 @@ private: return input_events; } + static void ConfigureLogger() + { + + } + }; } From 0dfe0209343ae6180fe5b17c54c8e936410e652f Mon Sep 17 00:00:00 2001 From: Neeflix Date: Thu, 31 May 2018 18:57:52 +0200 Subject: [PATCH 5/5] added config.h --- FlippR-Driver/src/input/InputDriverFactory.hpp | 10 ++++++++++ FlippR-Driver/src/utilities/config.h | 8 ++++++++ 2 files changed, 18 insertions(+) create mode 100644 FlippR-Driver/src/utilities/config.h diff --git a/FlippR-Driver/src/input/InputDriverFactory.hpp b/FlippR-Driver/src/input/InputDriverFactory.hpp index e843f33..05cf0fd 100644 --- a/FlippR-Driver/src/input/InputDriverFactory.hpp +++ b/FlippR-Driver/src/input/InputDriverFactory.hpp @@ -13,12 +13,16 @@ #include "Detector.h" #include "../utilities/InputGPIOInterface.h" +#include "../utilities/config.h" #include "../lib/json/json.hpp" #include "../lib/easylogging/easylogging++.h" #include "EventNotifier.h" using namespace nlohmann; + +INITIALIZE_EASYLOGGINGPP + namespace Input { class InputFactory @@ -57,7 +61,13 @@ private: static void ConfigureLogger() { + el::Loggers::getLogger(INPUT_LOGGER); + //TODO may configure? + el::Configurations defaultConf; + defaultConf.setToDefault(); + + el::Loggers::reconfigureLogger(INPUT_LOGGER,defaultConf); } }; diff --git a/FlippR-Driver/src/utilities/config.h b/FlippR-Driver/src/utilities/config.h new file mode 100644 index 0000000..4979a97 --- /dev/null +++ b/FlippR-Driver/src/utilities/config.h @@ -0,0 +1,8 @@ +/* + * BlockingQueue.hpp + * + * Created on: May 17, 2018 + * Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht + */ + +#define INPUT_LOGGER "input_logger"