renamed impl to detail
This commit is contained in:
55
FlippR-Driver/src/input/detail/Detector.cpp
Normal file
55
FlippR-Driver/src/input/detail/Detector.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Detector.cpp
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#include "Detector.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "utility/config.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
Detector::Detector(std::unique_ptr<InputPinController> input_gpio_interface, std::vector<std::shared_ptr<DistributingEvent>> events)
|
||||
:
|
||||
input_gpio_interface(std::move(input_gpio_interface)), events(std::move(events)), is_running(true)
|
||||
{
|
||||
this->detect_thread = std::thread(&Detector::detect, this);
|
||||
|
||||
CLOG(INFO, INPUT_LOGGER) << "Created Detector and started detecting!";
|
||||
}
|
||||
|
||||
Detector::~Detector()
|
||||
{
|
||||
this->is_running = false;
|
||||
|
||||
this->detect_thread.join();
|
||||
}
|
||||
|
||||
void Detector::detect() const
|
||||
{
|
||||
while(this->is_running)
|
||||
{
|
||||
check_inputs();
|
||||
}
|
||||
}
|
||||
|
||||
void Detector::check_inputs() const
|
||||
{
|
||||
for(auto &event : events)
|
||||
{
|
||||
input_gpio_interface->read_data(event->address) ? event->active() : event->inactive();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
58
FlippR-Driver/src/input/detail/Detector.h
Normal file
58
FlippR-Driver/src/input/detail/Detector.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Detector.h
|
||||
*
|
||||
* Responsible for detecting InputEvents.
|
||||
*
|
||||
* Creates two Threads;
|
||||
* One cycles over the inputs gpios and pushes detected input events to a queue.
|
||||
* The other cycles over the queue and notifies input event handlers.
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#ifndef INPUT_IMPL_DETECTOR_H_
|
||||
#define INPUT_IMPL_DETECTOR_H_
|
||||
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "input/InputPinController.h"
|
||||
|
||||
#include "input/Detector.h"
|
||||
#include "input/DistributingEvent.h"
|
||||
#include "input/EventNotifier.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class Detector : public input::Detector
|
||||
{
|
||||
|
||||
public:
|
||||
Detector(std::unique_ptr<input::InputPinController> input_gpio_interface, std::vector<std::shared_ptr<DistributingEvent>> events);
|
||||
~Detector() override;
|
||||
|
||||
private:
|
||||
void detect() const;
|
||||
void check_inputs() const;
|
||||
|
||||
private:
|
||||
const std::unique_ptr<input::InputPinController> input_gpio_interface;
|
||||
|
||||
const std::vector<std::shared_ptr<DistributingEvent>> events;
|
||||
|
||||
bool is_running;
|
||||
std::thread detect_thread;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
38
FlippR-Driver/src/input/detail/EventHandler.cpp
Normal file
38
FlippR-Driver/src/input/detail/EventHandler.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* EventHandler.cpp
|
||||
*
|
||||
* Created on: Jun 14, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
#include "EventHandler.h"
|
||||
#include "utility/config.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
EventHandler::EventHandler(std::shared_ptr<InputDriver> input_driver) :
|
||||
input_driver(std::move(input_driver))
|
||||
{
|
||||
this->input_driver->register_event_handler(this);
|
||||
|
||||
CLOG(INFO, INPUT_LOGGER) << "Created EventHandler";
|
||||
}
|
||||
|
||||
EventHandler::~EventHandler()
|
||||
{
|
||||
this->input_driver->unregister_event_handler(this);
|
||||
}
|
||||
|
||||
// This function is intended to be non pure, if it is called when the derived class doesn't exist anymore
|
||||
void EventHandler::handle(Event &event)
|
||||
{
|
||||
CLOG(WARNING, INPUT_LOGGER) << "Called EventHandler parent class";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
43
FlippR-Driver/src/input/detail/EventHandler.h
Normal file
43
FlippR-Driver/src/input/detail/EventHandler.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* InputEventHandler.h
|
||||
*
|
||||
* This interface must be implemented to be informed about input events.
|
||||
*
|
||||
* Please be aware that handle must be implemented thread safe!
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#ifndef INPUT_IMPL_EVENTHANDLER_H_
|
||||
#define INPUT_IMPL_EVENTHANDLER_H_
|
||||
|
||||
#include "input/InputDriver.h"
|
||||
|
||||
#include "input/EventHandler.h"
|
||||
#include "input/Event.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class EventHandler;
|
||||
|
||||
class EventHandler : public input::EventHandler
|
||||
{
|
||||
public:
|
||||
explicit EventHandler(std::shared_ptr<InputDriver> input_driver);
|
||||
~EventHandler() override;
|
||||
void handle(Event &event) override;
|
||||
|
||||
private:
|
||||
const std::shared_ptr<InputDriver> input_driver;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
88
FlippR-Driver/src/input/detail/EventNotifier.cpp
Normal file
88
FlippR-Driver/src/input/detail/EventNotifier.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* EventNotifier.cpp
|
||||
*
|
||||
* Created on: May 17, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include "utility/config.h"
|
||||
|
||||
#include "EventNotifier.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
EventNotifier::EventNotifier(utility::IBlockingQueue<Event> *queue) :
|
||||
is_running(true),
|
||||
event_queue(queue)
|
||||
{
|
||||
this->notify_thread = std::thread(&EventNotifier::notify, this);
|
||||
|
||||
CLOG(INFO, INPUT_LOGGER) << "Created EventNotifier and started thread";
|
||||
}
|
||||
|
||||
EventNotifier::~EventNotifier()
|
||||
{
|
||||
this->is_running = false;
|
||||
|
||||
Event end_event(0, 0, "END");
|
||||
this->event_queue->push(end_event);
|
||||
|
||||
this->notify_thread.join();
|
||||
|
||||
delete this->event_queue;
|
||||
}
|
||||
|
||||
void EventNotifier::register_event_handler(EventHandler *handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
this->event_handlers.insert(handler);
|
||||
}
|
||||
|
||||
void EventNotifier::unregister_event_handler(EventHandler *handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
this->event_handlers.erase(handler);
|
||||
}
|
||||
|
||||
void EventNotifier::distribute_event(const Event &event)
|
||||
{
|
||||
this->event_queue->push(event);
|
||||
}
|
||||
|
||||
void EventNotifier::notify()
|
||||
{
|
||||
while(this->is_running)
|
||||
{
|
||||
Event event = this->event_queue->pop();
|
||||
|
||||
// TODO schoener machen
|
||||
if(event.name == "END")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// getting a guard and calling all registered handlers
|
||||
std::lock_guard<std::mutex> event_handler_guard(this->event_handler_mutex);
|
||||
for(auto handler : this->event_handlers)
|
||||
{
|
||||
boost::thread handler_caller(boost::bind(&EventHandler::handle, handler, event));
|
||||
|
||||
if(!handler_caller.timed_join(boost::posix_time::milliseconds(INPUT_HANDLER_TIMEOUT_MILLI)))
|
||||
{
|
||||
CLOG(WARNING, INPUT_LOGGER) << "Handler " << typeid(handler).name() << " didn't finish in "
|
||||
<< INPUT_HANDLER_TIMEOUT_MILLI << " milliseconds. Aborting Execution!";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
58
FlippR-Driver/src/input/detail/EventNotifier.h
Normal file
58
FlippR-Driver/src/input/detail/EventNotifier.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* InputEventNotifier.h
|
||||
*
|
||||
* Created on: May 17, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#ifndef INPUT_IMPL_EVENTNOTIFIER_H_
|
||||
#define INPUT_IMPL_EVENTNOTIFIER_H_
|
||||
|
||||
#include "input/EventNotifier.h"
|
||||
|
||||
#include "input/Event.h"
|
||||
#include "input/EventHandler.h"
|
||||
|
||||
#include "utility/BlockingQueue.hpp"
|
||||
#include "utility/IBlockingQueue.h"
|
||||
|
||||
#include <set>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class EventNotifier : public input::EventNotifier
|
||||
{
|
||||
|
||||
public:
|
||||
explicit EventNotifier(utility::IBlockingQueue<Event> *queue);
|
||||
~EventNotifier() override;
|
||||
|
||||
void register_event_handler(EventHandler *handler) override;
|
||||
void unregister_event_handler(EventHandler *handler) override;
|
||||
|
||||
void distribute_event(const Event &event) override;
|
||||
|
||||
private:
|
||||
void notify();
|
||||
|
||||
private:
|
||||
utility::IBlockingQueue<Event> *event_queue;
|
||||
std::set<EventHandler *> event_handlers;
|
||||
|
||||
bool is_running;
|
||||
std::thread notify_thread;
|
||||
std::mutex event_handler_mutex;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
51
FlippR-Driver/src/input/detail/InputDriver.cpp
Normal file
51
FlippR-Driver/src/input/detail/InputDriver.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* InputDriver.cpp
|
||||
*
|
||||
* Created on: Jun 15, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
#include "InputDriver.h"
|
||||
|
||||
#include "utility/config.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
InputDriver::InputDriver(std::shared_ptr<EventNotifier> event_notifier, std::unique_ptr<Detector> detector,
|
||||
std::map<std::string, std::shared_ptr<Event>> events) :
|
||||
event_notifier(std::move(event_notifier)), detector(std::move(detector)), events(std::move(events))
|
||||
{
|
||||
CLOG(INFO, INPUT_LOGGER) << "Created InputDriver";
|
||||
}
|
||||
|
||||
void InputDriver::register_event_handler(EventHandler *handler)
|
||||
{
|
||||
this->event_notifier->register_event_handler(handler);
|
||||
}
|
||||
|
||||
void InputDriver::unregister_event_handler(EventHandler *handler)
|
||||
{
|
||||
this->event_notifier->unregister_event_handler(handler);
|
||||
}
|
||||
|
||||
boost::optional<std::shared_ptr<Event>> InputDriver::get_event(std::string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return this->events.at(name);
|
||||
}
|
||||
catch(std::out_of_range &e)
|
||||
{
|
||||
CLOG_N_TIMES(1, WARNING, OUTPUT_LOGGER) << "Did not found event " << name << " please check config file!";
|
||||
}
|
||||
return boost::optional<std::shared_ptr<Event>> {};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
45
FlippR-Driver/src/input/detail/InputDriver.h
Normal file
45
FlippR-Driver/src/input/detail/InputDriver.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* InputDriver.hpp
|
||||
*
|
||||
* Created on: May 31, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
#ifndef INPUT_IMPL_INPUTDRIVER_H_
|
||||
#define INPUT_IMPL_INPUTDRIVER_H_
|
||||
|
||||
#include "input/InputDriver.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "input/EventNotifier.h"
|
||||
#include "input/Detector.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class InputDriver : public input::InputDriver
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
InputDriver(std::shared_ptr<EventNotifier> event_notifier, std::unique_ptr<Detector> detector, std::map<std::string, std::shared_ptr<Event>> events);
|
||||
void register_event_handler(EventHandler *handler) override;
|
||||
void unregister_event_handler(EventHandler *handler) override;
|
||||
|
||||
boost::optional<std::shared_ptr<Event>> get_event(std::string name) override;
|
||||
|
||||
private:
|
||||
const std::shared_ptr<EventNotifier> event_notifier;
|
||||
const std::unique_ptr<Detector> detector;
|
||||
|
||||
const std::map<std::string, std::shared_ptr<Event>> events;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
72
FlippR-Driver/src/input/detail/InputPinController.cpp
Normal file
72
FlippR-Driver/src/input/detail/InputPinController.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* InputPinController.cpp
|
||||
*
|
||||
* Created on: May 31, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#include "InputPinController.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "json/json.hpp"
|
||||
#include "easylogging/easylogging++.h"
|
||||
#include "utility/config.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
InputPinController::InputPinController(std::map<std::string, uint8_t> pins)
|
||||
: pins(std::move(pins))
|
||||
{
|
||||
init_pins();
|
||||
}
|
||||
|
||||
bool InputPinController::read_data(uint8_t pin) const
|
||||
{
|
||||
// setting address to read
|
||||
write_row(pin / INPUT_MATRIX_SIZE);
|
||||
write_col(pin % INPUT_MATRIX_SIZE);
|
||||
|
||||
// wait for mux to set address
|
||||
std::this_thread::sleep_for(std::chrono::nanoseconds(INPUT_SLEEP_DURATION_NANO));
|
||||
|
||||
return read_pin(this->pins.at("data_address"));
|
||||
}
|
||||
|
||||
void InputPinController::write_row(uint8_t data) const
|
||||
{
|
||||
write_pin(this->pins.at("row_address_A"), data & 0b001u);
|
||||
write_pin(this->pins.at("row_address_B"), data & 0b010u);
|
||||
write_pin(this->pins.at("row_address_C"), data & 0b100u);
|
||||
}
|
||||
|
||||
void InputPinController::write_col(uint8_t data) const
|
||||
{
|
||||
write_pin(this->pins.at("col_address_A"), data & 0b001u);
|
||||
write_pin(this->pins.at("col_address_B"), data & 0b010u);
|
||||
write_pin(this->pins.at("col_address_C"), data & 0b100u);
|
||||
}
|
||||
|
||||
void InputPinController::init_pins() const
|
||||
{
|
||||
initialize_output_pin(this->pins.at("col_address_A"));
|
||||
initialize_output_pin(this->pins.at("col_address_B"));
|
||||
initialize_output_pin(this->pins.at("col_address_C"));
|
||||
|
||||
initialize_output_pin(this->pins.at("row_address_A"));
|
||||
initialize_output_pin(this->pins.at("row_address_B"));
|
||||
initialize_output_pin(this->pins.at("row_address_C"));
|
||||
|
||||
initialize_input_pin(this->pins.at("data_address"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
44
FlippR-Driver/src/input/detail/InputPinController.h
Normal file
44
FlippR-Driver/src/input/detail/InputPinController.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* InputPinController.h
|
||||
*
|
||||
* Created on: May 31, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
#ifndef SRC_UTILITIES_INPUTGPIOINTERFACE_H_
|
||||
#define SRC_UTILITIES_INPUTGPIOINTERFACE_H_
|
||||
|
||||
#include "input/InputPinController.h"
|
||||
|
||||
#include <istream>
|
||||
#include <map>
|
||||
|
||||
#include "PinController.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace input
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class InputPinController : public input::InputPinController, public PinController
|
||||
{
|
||||
public:
|
||||
explicit InputPinController(std::map<std::string, uint8_t> pins);
|
||||
bool read_data(uint8_t pin) const override;
|
||||
|
||||
private:
|
||||
void init_pins() const;
|
||||
void write_row(uint8_t data) const;
|
||||
void write_col(uint8_t data) const;
|
||||
|
||||
private:
|
||||
const std::map<std::string, uint8_t> pins;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user