refactored InputName
This commit is contained in:
@@ -13,8 +13,8 @@
|
|||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
|
|
||||||
Detector::Detector(InputGPIOInterface* in_gpio_interface, std::map<char, InputEvent> events, InputEventNotifier* input_event_notifier) :
|
Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> events, EventNotifier* event_notifier) :
|
||||||
in_gpio_interface(in_gpio_interface), input_events(events), is_running(true), input_event_notifier(input_event_notifier)
|
input_gpio_interface(input_gpio_interface), events(events), is_running(true), event_notifier(event_notifier)
|
||||||
{
|
{
|
||||||
detect_thread = std::thread(&Detector::detect, this);
|
detect_thread = std::thread(&Detector::detect, this);
|
||||||
}
|
}
|
||||||
@@ -24,13 +24,13 @@ Detector::~Detector()
|
|||||||
is_running = false;
|
is_running = false;
|
||||||
|
|
||||||
detect_thread.join();
|
detect_thread.join();
|
||||||
delete this->in_gpio_interface;
|
delete this->input_gpio_interface;
|
||||||
this->in_gpio_interface = NULL;
|
this->input_gpio_interface = NULL;
|
||||||
delete this->input_event_notifier;
|
delete this->event_notifier;
|
||||||
this->input_event_notifier = NULL;
|
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()
|
void Detector::detect()
|
||||||
{
|
{
|
||||||
while(is_running)
|
while(is_running)
|
||||||
@@ -40,8 +40,8 @@ void Detector::detect()
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
InputEvent& event = input_events.at(address);
|
Event& event = events.at(address);
|
||||||
input_event_notifier->distribute_event(event);
|
event_notifier->distribute_event(event);
|
||||||
}
|
}
|
||||||
catch(std::out_of_range& e)
|
catch(std::out_of_range& e)
|
||||||
{
|
{
|
||||||
@@ -53,24 +53,16 @@ void Detector::detect()
|
|||||||
|
|
||||||
bool Detector::check_inputs(char& address)
|
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);
|
if(input_gpio_interface->read_input_data(pin))
|
||||||
|
|
||||||
for(int col = 0; col < 8; col++)
|
|
||||||
{
|
{
|
||||||
in_gpio_interface->write_input_col(col);
|
address = pin;
|
||||||
|
|
||||||
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,17 +26,17 @@
|
|||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
|
|
||||||
class InputEventHandler
|
class EventHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void handle(InputEvent& event);
|
void handle(Event& event);
|
||||||
};
|
};
|
||||||
|
|
||||||
class Detector
|
class Detector
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Detector(InputGPIOInterface* in_gpio_interface, std::map<char, InputEvent> events, InputEventNotifier* input_event_notifier);
|
Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> events, EventNotifier* event_notifier);
|
||||||
~Detector();
|
~Detector();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -44,11 +44,11 @@ private:
|
|||||||
bool check_inputs(char& address);
|
bool check_inputs(char& address);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
InputGPIOInterface* in_gpio_interface;
|
InputGPIOInterface* input_gpio_interface;
|
||||||
|
|
||||||
std::map<char, InputEvent> input_events;
|
std::map<char, Event> events;
|
||||||
|
|
||||||
InputEventNotifier* input_event_notifier;
|
EventNotifier* event_notifier;
|
||||||
|
|
||||||
bool is_running;
|
bool is_running;
|
||||||
std::thread detect_thread;
|
std::thread detect_thread;
|
||||||
|
|||||||
@@ -14,17 +14,17 @@
|
|||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
|
|
||||||
class InputEvent
|
class Event
|
||||||
{
|
{
|
||||||
public:
|
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;
|
return this->name == other.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const InputEvent& other)
|
bool operator<(const Event& other)
|
||||||
{
|
{
|
||||||
return this->priority < other.priority;
|
return this->priority < other.priority;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,24 +12,24 @@
|
|||||||
#ifndef INPUTEVENTHANDLER_H_
|
#ifndef INPUTEVENTHANDLER_H_
|
||||||
#define INPUTEVENTHANDLER_H_
|
#define INPUTEVENTHANDLER_H_
|
||||||
|
|
||||||
#include "Detector.h"
|
#include "InputDriver.h"
|
||||||
#include "Event.hpp"
|
#include "Event.hpp"
|
||||||
|
|
||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
|
|
||||||
class InputEventHandler
|
class EventHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InputEventHandler(std::shared_ptr<Detector> detector) :
|
EventHandler(std::shared_ptr<Detector> detector) :
|
||||||
detector(detector)
|
detector(detector)
|
||||||
{
|
{
|
||||||
this->detector->register_input_event_handler(this);
|
this->detector->register_event_handler(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~InputEventHandler()
|
virtual ~InputEventHandler()
|
||||||
{
|
{
|
||||||
this->detector->unregister_input_event_handler(this);
|
this->detector->unregister_event_handler(this);
|
||||||
this->detector = NULL;
|
this->detector = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* InputEventNotifier.cpp
|
* EventNotifier.cpp
|
||||||
*
|
*
|
||||||
* Created on: May 17, 2018
|
* Created on: May 17, 2018
|
||||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||||
@@ -10,41 +10,41 @@
|
|||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
|
|
||||||
InputEventNotifier::InputEventNotifier()
|
EventNotifier::EventNotifier()
|
||||||
: is_running(true)
|
: is_running(true)
|
||||||
{
|
{
|
||||||
notify_thread = std::thread(&InputEventNotifier::notify, this);
|
notify_thread = std::thread(&EventNotifier::notify, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
InputEventNotifier::~InputEventNotifier()
|
EventNotifier::~EventNotifier()
|
||||||
{
|
{
|
||||||
is_running = false;
|
is_running = false;
|
||||||
|
|
||||||
notify_thread.join();
|
notify_thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputEventNotifier::register_input_event_handler(InputEventHandler* handler)
|
void EventNotifier::register_event_handler(EventHandler* handler)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||||
event_handler.insert(handler);
|
event_handler.insert(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputEventNotifier::unregister_input_event_handler(InputEventHandler* handler)
|
void EventNotifier::unregister_event_handler(EventHandler* handler)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||||
event_handler.erase(handler);
|
event_handler.erase(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputEventNotifier::distribute_event(InputEvent& event)
|
void EventNotifier::distribute_event(Event& event)
|
||||||
{
|
{
|
||||||
event_queue.push(event);
|
event_queue.push(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputEventNotifier::notify()
|
void EventNotifier::notify()
|
||||||
{
|
{
|
||||||
while(is_running)
|
while(is_running)
|
||||||
{
|
{
|
||||||
InputEvent event = event_queue.pop();
|
Event event = event_queue.pop();
|
||||||
|
|
||||||
// getting a guard and calling all registered handlers
|
// getting a guard and calling all registered handlers
|
||||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||||
|
|||||||
@@ -18,23 +18,25 @@
|
|||||||
|
|
||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
class InputEventNotifier
|
|
||||||
|
class EventNotifier
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
InputEventNotifier();
|
EventNotifier();
|
||||||
~InputEventNotifier();
|
~EventNotifier();
|
||||||
|
|
||||||
void register_input_event_handler(InputEventHandler* handler);
|
void register_event_handler(EventHandler* handler);
|
||||||
void unregister_input_event_handler(InputEventHandler* handler);
|
void unregister_event_handler(EventHandler* handler);
|
||||||
|
|
||||||
void distribute_event(InputEvent& event);
|
void distribute_event(Event& event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void notify();
|
void notify();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BlockingQueue<InputEvent> event_queue;
|
BlockingQueue<Event> event_queue;
|
||||||
std::set<InputEventHandler*> event_handler;
|
std::set<EventHandler*> event_handler;
|
||||||
|
|
||||||
bool is_running;
|
bool is_running;
|
||||||
std::thread notify_thread;
|
std::thread notify_thread;
|
||||||
|
|||||||
@@ -8,8 +8,16 @@
|
|||||||
#ifndef SRC_INPUT_INPUTDRIVER_HPP_
|
#ifndef SRC_INPUT_INPUTDRIVER_HPP_
|
||||||
#define 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_ */
|
#endif /* SRC_INPUT_INPUTDRIVER_HPP_ */
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ bool InputGPIOInterface::read_input_data(char pin)
|
|||||||
write_input_row(pin / MATRIX_SIZE);
|
write_input_row(pin / MATRIX_SIZE);
|
||||||
write_input_col(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);
|
return read_pin(this->input_data_address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user