refactored utility namespace

This commit is contained in:
Jonas Zeunert
2018-10-17 22:00:56 +02:00
parent 8708663cd6
commit c5867acd52
15 changed files with 154 additions and 61 deletions

View File

@@ -0,0 +1,57 @@
/*
* 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 <boost/heap/priority_queue.hpp>
#include "IBlockingQueue.h"
using namespace boost;
namespace FlippR_Driver
{
namespace utility
{
template<typename T>
class BlockingQueue : public IBlockingQueue<T>
{
private:
std::mutex d_mutex;
std::condition_variable d_condition;
heap::priority_queue<T, heap::stable<true>> p_queue;
public:
void push(T const &value)
{
{
std::unique_lock<std::mutex> lock(this->d_mutex);
p_queue.push(value);
}
this->d_condition.notify_one();
}
T pop()
{
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=]
{ return !this->p_queue.empty(); });
T rc = *this->p_queue.begin();
this->p_queue.pop();
return rc;
}
};
}
}
#endif

View File

@@ -0,0 +1,47 @@
/*
* GPIOInterface.cpp
*
* Created on: Jun 15, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#include "GPIOInterface.h"
#include "config.h"
#include "wiringPi/wiringPi.h"
#include "json/json.hpp"
namespace FlippR_Driver
{
namespace utility
{
std::once_flag GPIOInterface::GPIO_LIB_INITIALIZED;
void GPIOInterface::initialize_input_pin(char address)
{
pinMode(address, INPUT);
}
void GPIOInterface::write_pin(char address, char data)
{
digitalWrite(address, data);
}
bool GPIOInterface::read_pin(char address)
{
return PULLDOWN == digitalRead(address);
}
void GPIOInterface::initialize_output_pin(char address)
{
pinMode(address, OUTPUT);
}
GPIOInterface::GPIOInterface()
{
std::call_once(GPIO_LIB_INITIALIZED, wiringPiSetup);
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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_H_
#define SRC_UTILITIES_GPIOINTERFACE_H_
#include <mutex>
#include <fstream>
namespace FlippR_Driver
{
namespace utility
{
class GPIOInterface
{
public:
GPIOInterface();
virtual ~GPIOInterface()
{};
static void initialize_input_pin(char address);
static void initialize_output_pin(char address);
static void write_pin(char address, char data);
static bool read_pin(char address);
public:
static std::once_flag GPIO_LIB_INITIALIZED;
};
}
}
#endif

View File

@@ -0,0 +1,30 @@
/*
* BlockingQueue.hpp
*
* Created on: May 17, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_IBLOCKINGQUEUE_H_
#define SRC_UTILITIES_IBLOCKINGQUEUE_H_
namespace FlippR_Driver
{
namespace utility
{
template<typename T>
class IBlockingQueue
{
public:
virtual ~IBlockingQueue()
{};
virtual void push(T const &value) = 0;
virtual T pop() = 0;
};
}
}
#endif

View File

@@ -0,0 +1,28 @@
/*
* InputGPIOInterface.h
*
* Created on: May 31, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_IINPUTGPIOINTERFACE_H_
#define SRC_UTILITIES_IINPUTGPIOINTERFACE_H_
namespace FlippR_Driver
{
namespace utility
{
class IInputGPIOInterface
{
public:
virtual ~IInputGPIOInterface()
{};
virtual bool read_data(char pin) = 0;
};
}
}
#endif

View File

@@ -0,0 +1,20 @@
//
// Created by rhetenor on 10.10.18.
//
#ifndef FLIPPR_DRIVER_IOUTPUTGPIOINTERFACE_H
#define FLIPPR_DRIVER_IOUTPUTGPIOINTERFACE_H
namespace FlippR_Driver
{
namespace utility
{
class IOutputGPIOInterface
{
};
}
}
#endif //FLIPPR_DRIVER_IOUTPUTGPIOINTERFACE_H

View File

@@ -0,0 +1,99 @@
/*
* InputGPIOInterface.cpp
*
* Created on: May 31, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#include <fstream>
#include <thread>
#include "InputGPIOInterface.h"
#include "json/json.hpp"
#include "easylogging/easylogging++.h"
#include "config.h"
#include <string>
namespace FlippR_Driver
{
namespace utility
{
using namespace nlohmann;
bool InputGPIOInterface::read_data(char pin)
{
// 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->data_address);
}
void InputGPIOInterface::write_row(char data)
{
write_pin(this->row_address_A, data & 0b001);
write_pin(this->row_address_B, data & 0b010);
write_pin(this->row_address_C, data & 0b100);
}
void InputGPIOInterface::write_col(char data)
{
write_pin(this->col_address_A, data & 0b001);
write_pin(this->col_address_B, data & 0b010);
write_pin(this->col_address_C, data & 0b100);
}
InputGPIOInterface::InputGPIOInterface(std::istream &input_config_stream)
{
init_members(input_config_stream);
init_pins();
}
void InputGPIOInterface::init_members(std::istream &input_config_stream)
{
json input_config;
input_config_stream >> input_config;
try
{
json row_json = input_config.at("row");
row_address_A = row_json.at("A").get<json::number_integer_t>();
row_address_B = row_json.at("B").get<json::number_integer_t>();
row_address_C = row_json.at("C").get<json::number_integer_t>();
json col_json = input_config.at("col");
col_address_A = col_json.at("A").get<json::number_integer_t>();
col_address_B = col_json.at("B").get<json::number_integer_t>();
col_address_C = col_json.at("C").get<json::number_integer_t>();
data_address = input_config.at("data").get<json::number_integer_t>();
}
catch(json::type_error &e)
{
CLOG(ERROR, INPUT_LOGGER) << e.what();
}
catch(json::out_of_range &e)
{
CLOG(ERROR, INPUT_LOGGER) << e.what();
}
}
void InputGPIOInterface::init_pins()
{
initialize_output_pin(col_address_A);
initialize_output_pin(col_address_B);
initialize_output_pin(col_address_C);
initialize_output_pin(row_address_A);
initialize_output_pin(row_address_B);
initialize_output_pin(row_address_C);
initialize_input_pin(data_address);
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* InputGPIOInterface.h
*
* Created on: May 31, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_INPUTGPIOINTERFACE_H_
#define SRC_UTILITIES_INPUTGPIOINTERFACE_H_
#include <exception>
#include "IInputGPIOInterface.h"
#include "GPIOInterface.h"
namespace FlippR_Driver
{
namespace utility
{
class InputGPIOInterface : public IInputGPIOInterface, GPIOInterface
{
public:
InputGPIOInterface(std::istream &input_config_stream);
bool read_data(char pin);
private:
void init_members(std::istream &input_config_stream);
void init_pins();
void write_row(char data);
void write_col(char data);
private:
char row_address_A;
char row_address_B;
char row_address_C;
char col_address_A;
char col_address_B;
char col_address_C;
char data_address;
};
}
}
#endif

View File

@@ -0,0 +1,70 @@
//
// Created by rhetenor on 13.09.18.
//
#include "LoggerFactory.h"
#include "config.h"
#ifndef EASYLOGGING_IS_INITIALIZED
#define EASYLOGGING_IS_INITIALIZED
INITIALIZE_EASYLOGGINGPP
#endif
namespace FlippR_Driver
{
namespace utility
{
namespace LoggerFactory
{
void CreateInputTestLogger(el::Level level)
{
el::Loggers::getLogger(INPUT_LOGGER);
el::Configurations conf;
conf.setToDefault();
conf.set(level, el::ConfigurationType::ToFile, "false");
conf.set(level, el::ConfigurationType::Format, "%datetime [%level] [%func] : %msg");
el::Loggers::reconfigureAllLoggers(conf);
}
el::Configurations createConfig(el::Level level)
{
el::Configurations conf;
conf.setToDefault();
conf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
conf.set(level, el::ConfigurationType::ToStandardOutput, "true");
conf.set(level, el::ConfigurationType::ToFile, "true");
conf.set(level, el::ConfigurationType::Filename, LOGGER_FILE);
conf.set(level, el::ConfigurationType::Format, "%datetime [%level] [%func] : %msg");
return conf;
}
void CreateInputLogger(el::Level level)
{
el::Loggers::getLogger(INPUT_LOGGER);
el::Configurations conf = createConfig(level);
el::Loggers::reconfigureLogger(INPUT_LOGGER, conf);
}
void CreateOutputLogger(el::Level level)
{
el::Loggers::getLogger(OUTPUT_LOGGER);
el::Configurations conf = createConfig(level);
el::Loggers::reconfigureLogger(OUTPUT_LOGGER, conf);
}
};
}
}

View File

@@ -0,0 +1,29 @@
/*
* LoggerFactory.hpp
*
* Created on: Jun 19, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_LOGGERFACTORY_HPP_
#define SRC_UTILITIES_LOGGERFACTORY_HPP_
#include "config.h"
namespace FlippR_Driver
{
namespace utility
{
namespace LoggerFactory
{
void CreateInputTestLogger(el::Level level = el::Level::Global);
void CreateInputLogger(el::Level level = el::Level::Info);
void CreateOutputLogger(el::Level level = el::Level::Info);
};
}
}
#endif

View File

@@ -0,0 +1,34 @@
/*
* OutputGPIOInterface.h
*
* Created on: May 31, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_OUTPUTGPIOINTERFACE_H_
#define SRC_UTILITIES_OUTPUTGPIOINTERFACE_H_
#include "GPIOInterface.h"
#include <mcp23017.h>
namespace FlippR_Driver
{
namespace utility
{
class OutputGPIOInterface : GPIOInterface
{
public:
void activate_cabinet_item(CabinetItem &item);
void write_display(Display &display);
private:
void
private:
IBlockingQueue <CabinetItem> event_queue;
};
}
}
#endif

View File

@@ -0,0 +1,26 @@
/*
* BlockingQueue.hpp
*
* Created on: May 17, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#include "easylogging++.h"
#define INPUT_LOGGER "driver_logger"
#define OUTPUT_LOGGER "output_logger"
#define LOGGER_FILE "driver.log"
#define DRIVER_CONF_FILE "/var/log/flippr_driver.conf"
#define HIGHEST_LOG_VERBOSITY 10
#define INPUT_MATRIX_SIZE 8
#define INPUT_SLEEP_DURATION_NANO 800
#define PULLDOWN false
#define HIGHEST_INPUT_EVENT_PRIORITY 0