threading again
This commit is contained in:
21
FlippR-Driver/json_example/gpio_config.json
Normal file
21
FlippR-Driver/json_example/gpio_config.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"input":
|
||||
{
|
||||
"row":
|
||||
{
|
||||
"A":"GPIO_13",
|
||||
"B":"GPIO_14",
|
||||
"C":"GPIO_15",
|
||||
},
|
||||
"column":
|
||||
{
|
||||
"A":"GPIO_17",
|
||||
"B":"GPIO_18",
|
||||
"C":"GPIO_19",
|
||||
"input":"GPIO_20"
|
||||
}
|
||||
},
|
||||
"output":
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -17,41 +17,66 @@ namespace Input
|
||||
{
|
||||
|
||||
Detector::Detector(std::map<std::string, char> input_config, std::map<char, InputEvent> events) :
|
||||
gpio(input_config), input_events(events)
|
||||
gpio(input_config), input_events(events), is_running(true)
|
||||
{
|
||||
detect_thread = std::thread(&Detector::detect, this);
|
||||
notify_thread = std::thread(&Detector::notify_handlers, this);
|
||||
}
|
||||
|
||||
|
||||
Detector::~Detector()
|
||||
{
|
||||
is_running = false;
|
||||
|
||||
detect_thread.join();
|
||||
notify_thread.join();
|
||||
}
|
||||
|
||||
void Detector::register_input_event_handler(InputEventHandler* handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
event_handler.insert(handler);
|
||||
}
|
||||
|
||||
void Detector::unregister_input_event_handler(InputEventHandler* handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
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.pop();
|
||||
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
|
||||
for(auto* handler : event_handler)
|
||||
{
|
||||
handler->handle(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Detector::detect()
|
||||
{
|
||||
while(is_running)
|
||||
{
|
||||
char address;
|
||||
if(check_inputs(address))
|
||||
{
|
||||
InputEvent& event = input_events.at(address);
|
||||
notify_handlers(event);
|
||||
event_queue.queue(event);
|
||||
}
|
||||
}
|
||||
|
||||
void Detector::notify_handlers(InputEvent& event)
|
||||
{
|
||||
for(auto* handler : event_handler)
|
||||
{
|
||||
handler->handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
bool Detector::check_inputs(char& address)
|
||||
|
||||
@@ -8,18 +8,25 @@
|
||||
#ifndef DETECTOR_H_
|
||||
#define DETECTOR_H_
|
||||
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
|
||||
#include "InputEvent.h"
|
||||
#include "InputEventHandler.h"
|
||||
|
||||
#define SLEEP_DURATION_NANO 900
|
||||
|
||||
namespace Input
|
||||
{
|
||||
|
||||
class InputEventHandler
|
||||
{
|
||||
public:
|
||||
void handle(InputEvent& event);
|
||||
};
|
||||
|
||||
class Detector
|
||||
{
|
||||
|
||||
@@ -27,21 +34,27 @@ public:
|
||||
Detector(std::map<std::string, char> input_config, std::map<char, InputEvent> events);
|
||||
~Detector();
|
||||
|
||||
void detect();
|
||||
|
||||
void register_input_event_handler(InputEventHandler* handler);
|
||||
void unregister_input_event_handler(InputEventHandler* handler);
|
||||
|
||||
private:
|
||||
void notify_handlers();
|
||||
|
||||
void detect();
|
||||
bool check_inputs(char& address);
|
||||
void notify_handlers(InputEvent& event);
|
||||
|
||||
private:
|
||||
std::map<std::string, char> gpio;
|
||||
|
||||
std::map<char, InputEvent> input_events;
|
||||
|
||||
std::queue<InputEvent> event_queue;
|
||||
std::set<InputEventHandler*> event_handler;
|
||||
|
||||
bool is_running;
|
||||
std::thread detect_thread;
|
||||
std::thread notify_thread;
|
||||
std::mutex event_handler_mutex;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#define INPUTEVENTHANDLER_H_
|
||||
|
||||
#include "InputEvent.h"
|
||||
#include "Detector.h"
|
||||
|
||||
namespace Input
|
||||
{
|
||||
@@ -17,13 +18,6 @@ class InputEventHandler
|
||||
{
|
||||
|
||||
|
||||
class Detector
|
||||
{
|
||||
public:
|
||||
void register_input_event_handler(InputEventHandler* handler);
|
||||
void unregister_input_event_handler(InputEventHandler* handler);
|
||||
|
||||
};
|
||||
public:
|
||||
InputEventHandler(std::shared_ptr<Detector> detector) :
|
||||
detector(detector)
|
||||
|
||||
3104
FlippR-Driver/src/lib/easylogging/easylogging++.cc
Normal file
3104
FlippR-Driver/src/lib/easylogging/easylogging++.cc
Normal file
File diff suppressed because it is too large
Load Diff
4560
FlippR-Driver/src/lib/easylogging/easylogging++.h
Normal file
4560
FlippR-Driver/src/lib/easylogging/easylogging++.h
Normal file
File diff suppressed because it is too large
Load Diff
BIN
FlippR-Driver/src/lib/libwiringPi.so.2.44
Executable file
BIN
FlippR-Driver/src/lib/libwiringPi.so.2.44
Executable file
Binary file not shown.
16
FlippR-Driver/src/tests/TestDetector.cpp
Normal file
16
FlippR-Driver/src/tests/TestDetector.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* TestDetector.cpp
|
||||
*
|
||||
* Created on: May 3, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
#include "catch.hpp"
|
||||
#include "../input/Detector.h"
|
||||
#include "../input/InputEvent.h"
|
||||
|
||||
#include <set>
|
||||
|
||||
using namespace Input;
|
||||
|
||||
|
||||
|
||||
13050
FlippR-Driver/src/tests/catch.hpp
Normal file
13050
FlippR-Driver/src/tests/catch.hpp
Normal file
File diff suppressed because it is too large
Load Diff
9
FlippR-Driver/src/tests/tests-main.cpp
Normal file
9
FlippR-Driver/src/tests/tests-main.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
/*
|
||||
* tests-main.cpp
|
||||
*
|
||||
* Created on: May 6, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch.hpp"
|
||||
40
FlippR-Driver/src/utilities/GPIOInterface.hpp
Normal file
40
FlippR-Driver/src/utilities/GPIOInterface.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* GPIOInterface.hpp
|
||||
*
|
||||
* Responsible for communicating with the actual GPIO hardware.
|
||||
*
|
||||
* Gets a JSON file with following style:
|
||||
* TODO
|
||||
*
|
||||
* Created on: May 6, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
|
||||
#ifndef SRC_UTILITIES_GPIOINTERFACE_HPP_
|
||||
#define SRC_UTILITIES_GPIOINTERFACE_HPP_
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <../lib/wiringPi/wiringPi.h>
|
||||
#include "../lib/json/json.hpp"
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
class GPIOInterface
|
||||
{
|
||||
public:
|
||||
GPIOInterface(std::string config_path);
|
||||
~GPIOInterface();
|
||||
|
||||
void write_address();
|
||||
void read_pin();
|
||||
|
||||
private:
|
||||
json config;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* SRC_UTILITIES_GPIOINTERFACE_HPP_ */
|
||||
Reference in New Issue
Block a user