weis nimmer
This commit is contained in:
@@ -10,62 +10,22 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "../lib/wiringPi/wiringPi.h"
|
|
||||||
|
|
||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
|
|
||||||
Detector::Detector(std::map<std::string, char> input_config, std::map<char, InputEvent> events) :
|
Detector::Detector(GPIOInterface& gpio_interface, std::map<char, InputEvent> events, InputEventNotifier& input_event_notifier) :
|
||||||
gpio(input_config), input_events(events), is_running(true)
|
gpio_interface(gpio_interface), input_events(events), is_running(true), input_event_notifier(input_event_notifier)
|
||||||
{
|
{
|
||||||
detect_thread = std::thread(&Detector::detect, this);
|
detect_thread = std::thread(&Detector::detect, this);
|
||||||
notify_thread = std::thread(&Detector::notify_handlers, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Detector::~Detector()
|
Detector::~Detector()
|
||||||
{
|
{
|
||||||
is_running = false;
|
is_running = false;
|
||||||
|
|
||||||
notify_thread.join();
|
|
||||||
detect_thread.join();
|
detect_thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Detector::register_input_event_handler(InputEventHandler* handler)
|
|
||||||
{
|
|
||||||
LOCK_EVENT_HANDLER();
|
|
||||||
event_handler.insert(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Detector::unregister_input_event_handler(InputEventHandler* handler)
|
|
||||||
{
|
|
||||||
LOCK_EVENT_HANDLER();
|
|
||||||
event_handler.erase(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Detector::notify_handlers()
|
|
||||||
{
|
|
||||||
while(is_running)
|
|
||||||
{
|
|
||||||
// return if no event in queue
|
|
||||||
if(event_queue.empty())
|
|
||||||
{
|
|
||||||
std::this_thread::yield();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
InputEvent event = event_queue.front();
|
|
||||||
event_queue.pop();
|
|
||||||
|
|
||||||
// getting a guard and calling all registered handlers
|
|
||||||
LOCK_EVENT_HANDLER();
|
|
||||||
for(auto handler : event_handler)
|
|
||||||
{
|
|
||||||
// todo timeout!
|
|
||||||
handler->handle(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cycles over all inputs and enqueues an input event if detected.
|
// Cycles over all inputs and enqueues an input event if detected.
|
||||||
void Detector::detect()
|
void Detector::detect()
|
||||||
{
|
{
|
||||||
@@ -77,7 +37,7 @@ void Detector::detect()
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
InputEvent& event = input_events.at(address);
|
InputEvent& event = input_events.at(address);
|
||||||
event_queue.emplace(event);
|
input_event_notifier.distribute_event(event);
|
||||||
}
|
}
|
||||||
catch(std::out_of_range& e)
|
catch(std::out_of_range& e)
|
||||||
{
|
{
|
||||||
@@ -87,24 +47,19 @@ void Detector::detect()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo work with gpiointerface
|
|
||||||
bool Detector::check_inputs(char& address)
|
bool Detector::check_inputs(char& address)
|
||||||
{
|
{
|
||||||
for(int row = 0; row < 8; row++)
|
for(int row = 0; row < 8; row++)
|
||||||
{
|
{
|
||||||
digitalWrite(gpio["ROW_A"], row & 0b001);
|
gpio_interface.write_input_row(row);
|
||||||
digitalWrite(gpio["ROW_B"], row & 0b010);
|
|
||||||
digitalWrite(gpio["ROW_C"], row & 0b100);
|
|
||||||
|
|
||||||
for(int col = 0; col < 8; col++)
|
for(int col = 0; col < 8; col++)
|
||||||
{
|
{
|
||||||
digitalWrite(gpio["COL_A"], col & 0b001);
|
gpio_interface.write_input_col(col);
|
||||||
digitalWrite(gpio["COL_B"], col & 0b010);
|
|
||||||
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::nanoseconds(SLEEP_DURATION_NANO));
|
std::this_thread::sleep_for(std::chrono::nanoseconds(SLEEP_DURATION_NANO));
|
||||||
if(digitalRead(gpio["INPUT"]))
|
if(gpio_interface.read_input_data())
|
||||||
{
|
{
|
||||||
address = pow(2, row) + col;
|
address = pow(2, row) + col;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -14,19 +14,16 @@
|
|||||||
#ifndef DETECTOR_H_
|
#ifndef DETECTOR_H_
|
||||||
#define DETECTOR_H_
|
#define DETECTOR_H_
|
||||||
|
|
||||||
#include <queue>
|
|
||||||
#include <vector>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
|
||||||
|
#include "../utilities/GPIOInterface.h"
|
||||||
|
|
||||||
#include "InputEvent.h"
|
#include "InputEvent.h"
|
||||||
|
#include "InputEventNotifier.h"
|
||||||
#define LOCK_EVENT_HANDLER() std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex)
|
|
||||||
|
|
||||||
#define SLEEP_DURATION_NANO 900
|
#define SLEEP_DURATION_NANO 900
|
||||||
|
|
||||||
|
|
||||||
namespace Input
|
namespace Input
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -40,30 +37,22 @@ class Detector
|
|||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Detector(std::map<std::string, char> input_config, std::map<char, InputEvent> events);
|
Detector(GPIOInterface& gpio_interface, std::map<char, InputEvent> events, InputEventNotifier& input_event_notifier);
|
||||||
~Detector();
|
~Detector();
|
||||||
|
|
||||||
void register_input_event_handler(InputEventHandler* handler);
|
|
||||||
void unregister_input_event_handler(InputEventHandler* handler);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void notify_handlers();
|
|
||||||
|
|
||||||
void detect();
|
void detect();
|
||||||
bool check_inputs(char& address);
|
bool check_inputs(char& address);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, char> gpio;
|
GPIOInterface& gpio_interface;
|
||||||
|
|
||||||
std::map<char, InputEvent> input_events;
|
std::map<char, InputEvent> input_events;
|
||||||
|
|
||||||
std::queue<InputEvent> event_queue;
|
InputEventNotifier& input_event_notifier;
|
||||||
std::set<InputEventHandler*> event_handler;
|
|
||||||
|
|
||||||
bool is_running;
|
bool is_running;
|
||||||
std::thread detect_thread;
|
std::thread detect_thread;
|
||||||
std::thread notify_thread;
|
|
||||||
std::mutex event_handler_mutex;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,24 +17,21 @@ namespace Input
|
|||||||
class InputEvent
|
class InputEvent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InputEvent(char address, std::string name) : address(address), name(name){}
|
InputEvent(char address, char priority, std::string name) : address(address), priority(priority), name(name){}
|
||||||
|
|
||||||
bool operator==(const char other)
|
bool operator==(const InputEvent& other)
|
||||||
{
|
{
|
||||||
return this->address == other;
|
return this->name == other.name;
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const std::string other)
|
|
||||||
{
|
|
||||||
return this->name == other;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const InputEvent& other)
|
bool operator<(const InputEvent& other)
|
||||||
{
|
{
|
||||||
return this->address < other.address;
|
return this->priority < other.priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char address;
|
const char address;
|
||||||
|
const char priority;
|
||||||
const std::string name;
|
const std::string name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
59
FlippR-Driver/src/input/InputEventNotifier.cpp
Normal file
59
FlippR-Driver/src/input/InputEventNotifier.cpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* InputEventNotifier.cpp
|
||||||
|
*
|
||||||
|
* Created on: May 17, 2018
|
||||||
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "InputEventNotifier.h"
|
||||||
|
|
||||||
|
namespace Input
|
||||||
|
{
|
||||||
|
|
||||||
|
InputEventNotifier::InputEventNotifier()
|
||||||
|
: is_running(true)
|
||||||
|
{
|
||||||
|
notify_thread = std::thread(&InputEventNotifier::notify, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
InputEventNotifier::~InputEventNotifier()
|
||||||
|
{
|
||||||
|
is_running = false;
|
||||||
|
|
||||||
|
notify_thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputEventNotifier::register_input_event_handler(InputEventHandler* handler)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||||
|
event_handler.insert(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputEventNotifier::unregister_input_event_handler(InputEventHandler* handler)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||||
|
event_handler.erase(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputEventNotifier::distribute_event(InputEvent& event)
|
||||||
|
{
|
||||||
|
event_queue.push(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputEventNotifier::notify()
|
||||||
|
{
|
||||||
|
while(is_running)
|
||||||
|
{
|
||||||
|
InputEvent event = event_queue.pop();
|
||||||
|
|
||||||
|
// getting a guard and calling all registered handlers
|
||||||
|
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||||
|
for(auto handler : event_handler)
|
||||||
|
{
|
||||||
|
// todo timeout!
|
||||||
|
handler->handle(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
47
FlippR-Driver/src/input/InputEventNotifier.h
Normal file
47
FlippR-Driver/src/input/InputEventNotifier.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* InputEventNotifier.h
|
||||||
|
*
|
||||||
|
* Created on: May 17, 2018
|
||||||
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SRC_INPUT_INPUTEVENTNOTIFIER_H_
|
||||||
|
#define SRC_INPUT_INPUTEVENTNOTIFIER_H_
|
||||||
|
|
||||||
|
#include <set>
|
||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
#include "../utilities/BlockingQueue.hpp"
|
||||||
|
#include "InputEventHandler.h"
|
||||||
|
#include "InputEvent.h"
|
||||||
|
|
||||||
|
namespace Input
|
||||||
|
{
|
||||||
|
class InputEventNotifier
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
InputEventNotifier();
|
||||||
|
~InputEventNotifier();
|
||||||
|
|
||||||
|
void register_input_event_handler(InputEventHandler* handler);
|
||||||
|
void unregister_input_event_handler(InputEventHandler* handler);
|
||||||
|
|
||||||
|
void distribute_event(InputEvent& event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void notify();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BlockingQueue<InputEvent> event_queue;
|
||||||
|
std::set<InputEventHandler*> event_handler;
|
||||||
|
|
||||||
|
bool is_running;
|
||||||
|
std::thread notify_thread;
|
||||||
|
std::mutex event_handler_mutex;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* SRC_INPUT_INPUTEVENTNOTIFIER_H_ */
|
||||||
@@ -35,7 +35,7 @@ public:
|
|||||||
|
|
||||||
std::map<char, InputEvent> input_events = create_input_events(matrix_config);
|
std::map<char, InputEvent> input_events = create_input_events(matrix_config);
|
||||||
|
|
||||||
return std::shared_ptr<Detector>(Detector(input_config, input_events));
|
return std::shared_ptr<Detector*>(new Detector(input_config, input_events));
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
43
FlippR-Driver/src/utilities/BlockingQueue.hpp
Normal file
43
FlippR-Driver/src/utilities/BlockingQueue.hpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* BlockingQueue.hpp
|
||||||
|
*
|
||||||
|
* Created on: May 17, 2018
|
||||||
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SRC_UTILITIES_BLOCKINGQUEUE_HPP_
|
||||||
|
#define SRC_UTILITIES_BLOCKINGQUEUE_HPP_
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <deque>
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class BlockingQueue
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::mutex d_mutex;
|
||||||
|
std::condition_variable d_condition;
|
||||||
|
std::priority_queue<T> d_queue;
|
||||||
|
public:
|
||||||
|
void push(T const& value) {
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(this->d_mutex);
|
||||||
|
d_queue.push_front(value);
|
||||||
|
}
|
||||||
|
this->d_condition.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
T pop() {
|
||||||
|
std::unique_lock<std::mutex> lock(this->d_mutex);
|
||||||
|
this->d_condition.wait(lock, [=]{ return !this->d_queue.empty(); });
|
||||||
|
T rc(std::move(this->d_queue.back()));
|
||||||
|
this->d_queue.pop_back();
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* SRC_UTILITIES_BLOCKINGQUEUE_HPP_ */
|
||||||
48
FlippR-Driver/src/utilities/GPIOInterface.cpp
Normal file
48
FlippR-Driver/src/utilities/GPIOInterface.cpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
* GPIOInterface.cpp
|
||||||
|
*
|
||||||
|
* Created on: May 17, 2018
|
||||||
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "GPIOInterface.h"
|
||||||
|
|
||||||
|
#include "../lib/wiringPi/wiringPi.h"
|
||||||
|
|
||||||
|
GPIOInterface::GPIOInterface(std::string config_path)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
GPIOInterface::~GPIOInterface()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GPIOInterface::read_input_data()
|
||||||
|
{
|
||||||
|
return read_input_pin(input_data_address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GPIOInterface::write_input_row(char data)
|
||||||
|
{
|
||||||
|
write_pin(input_row_address, data & 0b001);
|
||||||
|
write_pin(input_row_address, data & 0b010);
|
||||||
|
write_pin(input_row_address, data & 0b100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GPIOInterface::write_input_col(char data)
|
||||||
|
{
|
||||||
|
write_pin(input_col_address, data & 0b001);
|
||||||
|
write_pin(input_col_address, data & 0b010);
|
||||||
|
write_pin(input_col_address, data & 0b100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GPIOInterface::write_pin(char address, bool data)
|
||||||
|
{
|
||||||
|
digitalWrite(address, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GPIOInterface::read_input_pin(char address)
|
||||||
|
{
|
||||||
|
return digitalRead(address);
|
||||||
|
}
|
||||||
@@ -10,12 +10,11 @@
|
|||||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef SRC_UTILITIES_GPIOINTERFACE_HPP_
|
#ifndef SRC_UTILITIES_GPIOINTERFACE_H_
|
||||||
#define SRC_UTILITIES_GPIOINTERFACE_HPP_
|
#define SRC_UTILITIES_GPIOINTERFACE_H_
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include <../lib/wiringPi/wiringPi.h>
|
|
||||||
#include "../lib/json/json.hpp"
|
#include "../lib/json/json.hpp"
|
||||||
|
|
||||||
using namespace nlohmann;
|
using namespace nlohmann;
|
||||||
@@ -26,18 +25,24 @@ public:
|
|||||||
GPIOInterface(std::string config_path);
|
GPIOInterface(std::string config_path);
|
||||||
~GPIOInterface();
|
~GPIOInterface();
|
||||||
|
|
||||||
void write_address();
|
void write_input_row(char data);
|
||||||
void read_pin();
|
void write_input_col(char data);
|
||||||
|
bool read_input_data();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void write_pin(char address, bool data);
|
||||||
|
static bool read_input_pin(char address);
|
||||||
|
|
||||||
public:
|
|
||||||
// todo some mapping class interface shit that a user can ask for what is actually usable \
|
|
||||||
something like: A has gpio interface at gpio -> gpio.input.column. just dont work with strings
|
|
||||||
private:
|
private:
|
||||||
json config;
|
json config;
|
||||||
|
|
||||||
|
char input_row_address;
|
||||||
|
char input_col_address;
|
||||||
|
char input_data_address;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* SRC_UTILITIES_GPIOINTERFACE_HPP_ */
|
#endif /* SRC_UTILITIES_GPIOINTERFACE_H_ */
|
||||||
Reference in New Issue
Block a user