finished detector
This commit is contained in:
98
FlippR-Driver/src/input/Detector.cpp
Normal file
98
FlippR-Driver/src/input/Detector.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Detector.cpp
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
|
||||
#include "Detector.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
#include "../lib/wiringPi/wiringPi.h"
|
||||
|
||||
namespace Input
|
||||
{
|
||||
|
||||
Detector::Detector(std::set<InputEvent*> events) :
|
||||
input_events(events), is_running(true)
|
||||
{
|
||||
detect_thread = std::thread(&Detector::detect, this);
|
||||
}
|
||||
|
||||
|
||||
Detector::~Detector()
|
||||
{
|
||||
is_running = false;
|
||||
detect_thread.join();
|
||||
}
|
||||
|
||||
void Detector::register_input_event_handler(InputEventHandler* handler)
|
||||
{
|
||||
event_handler.insert(handler);
|
||||
}
|
||||
|
||||
void Detector::unregister_input_event_handler(InputEventHandler* handler)
|
||||
{
|
||||
event_handler.erase(handler);
|
||||
}
|
||||
|
||||
void Detector::detect()
|
||||
{
|
||||
while(is_running)
|
||||
{
|
||||
char address;
|
||||
if(check_inputs(address))
|
||||
{
|
||||
if(InputEvent* event = find_event(address))
|
||||
{
|
||||
notify_handlers(*event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Detector::notify_handlers(InputEvent& event)
|
||||
{
|
||||
for(auto* handler : event_handler)
|
||||
{
|
||||
handler->handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
bool Detector::check_inputs(char& address)
|
||||
{
|
||||
for(int row = 0; row < 8; row++)
|
||||
{
|
||||
digitalWrite(gpio["ROW_A"], row & 0b001);
|
||||
digitalWrite(gpio["ROW_B"], row & 0b010);
|
||||
digitalWrite(gpio["ROW_C"], row & 0b100);
|
||||
|
||||
for(int col = 0; col < 8; col++)
|
||||
{
|
||||
digitalWrite(gpio["COL_A"], col & 0b001);
|
||||
digitalWrite(gpio["COL_B"], col & 0b010);
|
||||
digitalWrite(gpio["COL_C"], col & 0b100);
|
||||
|
||||
// wait for mux to set address
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_DURATION));
|
||||
if(digitalRead(gpio["INPUT"]))
|
||||
{
|
||||
address = pow(2, row) + col;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
InputEvent* Detector::find_event(char address)
|
||||
{
|
||||
return *std::find_if(input_events.begin(), input_events.end(),
|
||||
[address] (InputEvent* e) { return *e == address; }
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
54
FlippR-Driver/src/input/Detector.h
Normal file
54
FlippR-Driver/src/input/Detector.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Detector.h
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
|
||||
#ifndef DETECTOR_H_
|
||||
#define DETECTOR_H_
|
||||
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <set>
|
||||
#include <map>
|
||||
|
||||
#include "InputEvent.h"
|
||||
#include "InputEventHandler.h"
|
||||
|
||||
#define SLEEP_DURATION 0.1f
|
||||
|
||||
namespace Input
|
||||
{
|
||||
|
||||
class Detector
|
||||
{
|
||||
public:
|
||||
Detector(std::set<InputEvent*> events);
|
||||
~Detector();
|
||||
|
||||
void register_input_event_handler(InputEventHandler* handler);
|
||||
void unregister_input_event_handler(InputEventHandler* handler);
|
||||
|
||||
private:
|
||||
void detect();
|
||||
|
||||
bool check_inputs(char& address);
|
||||
void notify_handlers(InputEvent& event);
|
||||
InputEvent* find_event(char address);
|
||||
|
||||
private:
|
||||
std::map<std::string, char> gpio;
|
||||
|
||||
std::set<InputEvent*> input_events;
|
||||
std::set<InputEventHandler*> event_handler;
|
||||
|
||||
std::thread detect_thread;
|
||||
bool is_running;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif /* DETECTOR_H_ */
|
||||
44
FlippR-Driver/src/input/InputEvent.h
Normal file
44
FlippR-Driver/src/input/InputEvent.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* InputEvent.h
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
|
||||
#ifndef INPUTEVENT_H_
|
||||
#define INPUTEVENT_H_
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
namespace Input
|
||||
{
|
||||
|
||||
class InputEvent
|
||||
{
|
||||
public:
|
||||
InputEvent(char address, std::string name) : address(address), name(name){}
|
||||
|
||||
bool operator==(const char other)
|
||||
{
|
||||
return this->address == other;
|
||||
}
|
||||
|
||||
bool operator==(const std::string other)
|
||||
{
|
||||
return this->name == other;
|
||||
}
|
||||
|
||||
bool operator<(const InputEvent& other)
|
||||
{
|
||||
return this->address < other.address;
|
||||
}
|
||||
private:
|
||||
const char address;
|
||||
const std::string name;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif /* INPUTEVENT_H_ */
|
||||
26
FlippR-Driver/src/input/InputEventHandler.h
Normal file
26
FlippR-Driver/src/input/InputEventHandler.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* InputEventHandler.h
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
|
||||
#ifndef INPUTEVENTHANDLER_H_
|
||||
#define INPUTEVENTHANDLER_H_
|
||||
|
||||
#include "InputEvent.h"
|
||||
|
||||
namespace Input
|
||||
{
|
||||
|
||||
|
||||
class InputEventHandler
|
||||
{
|
||||
public:
|
||||
virtual ~InputEventHandler(){};
|
||||
virtual void handle(InputEvent& event) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif /* INPUTEVENTHANDLER_H_ */
|
||||
33
FlippR-Driver/src/input/InputFactory.h
Normal file
33
FlippR-Driver/src/input/InputFactory.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* InputFactory.h
|
||||
*
|
||||
* Created on: Apr 5, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
|
||||
*/
|
||||
|
||||
#ifndef INPUTFACTORY_H_
|
||||
#define INPUTFACTORY_H_
|
||||
#include <vector>
|
||||
|
||||
#include "InputEvent.h"
|
||||
|
||||
namespace Input
|
||||
{
|
||||
class InputFactory
|
||||
{
|
||||
|
||||
public:
|
||||
InputFactory();
|
||||
~InputFactory();
|
||||
|
||||
std::vector<InputEvent*> get_input_events();
|
||||
|
||||
private:
|
||||
std::vector<InputEvent*> input_events;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif /* INPUTFACTORY_H_ */
|
||||
12
FlippR-Driver/src/input/main.cpp
Normal file
12
FlippR-Driver/src/input/main.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <vector>
|
||||
#include "InputEvent.h"
|
||||
#include "Detector.h"
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
Input::Detector(std::set<Input::InputEvent*>());
|
||||
|
||||
std::printf("hallo");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user