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

@@ -10,6 +10,9 @@
#include "ICabinetItem.h"
#include "utilities/IOutputGPIOInterface.h"
#include <memory>
#include <string>
namespace FlippR_Driver
@@ -17,21 +20,22 @@ namespace FlippR_Driver
namespace output
{
class CabinetItem : public ICabinetItem
{
public:
CabinetItem();
virtual ~CabinetItem();
virtual bool isActivated();
virtual bool activate();
virtual bool deactivate();
class CabinetItem : public ICabinetItem
{
public:
CabinetItem(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, int address, std::string name);
virtual ~CabinetItem();
protected:
int address;
std::string name;
virtual bool isActivated();
virtual bool activate();
virtual bool deactivate();
bool activated;
};
protected:
int address;
std::string name;
bool activated;
};
} /* namespace output */
}

View File

@@ -9,15 +9,16 @@
namespace FlippR_Driver
{
namespace output {
namespace output
{
Sound::Sound() {
// TODO Auto-generated constructor stub
Sound::Sound(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, int address, std::string name)
: output_gpio_interface(output_gpio_interface), address(address), name(name)
{}
}
Sound::~Sound() {
// TODO Auto-generated destructor stub
void Sound::play()
{
this->output_gpio_interface->activate(this);
}
} /* namespace output */

View File

@@ -10,8 +10,11 @@
#include "ISound.h"
#include <memory>
#include <string>
#include "utilities/IOutputGPIOInterface.h"
namespace FlippR_Driver
{
namespace output
@@ -20,12 +23,14 @@ namespace output
class Sound : ISound
{
public:
Sound();
virtual ~Sound();
Sound(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, int address, std::string name);
virtual ~Sound() = default;
virtual void play();
private:
std::shared_ptr<IOutputGPIOInterface> output_gpio_interface;
int address;
std::string name;
};

View File

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

View File

@@ -12,6 +12,11 @@
#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)
@@ -24,7 +29,6 @@ void GPIOInterface::write_pin(char address, char data)
digitalWrite(address, data);
}
bool GPIOInterface::read_pin(char address)
{
return PULLDOWN == digitalRead(address);
@@ -39,3 +43,5 @@ GPIOInterface::GPIOInterface()
{
std::call_once(GPIO_LIB_INITIALIZED, wiringPiSetup);
}
}
}

View File

@@ -15,13 +15,18 @@
#include <mutex>
#include <fstream>
namespace FlippR_Driver
{
namespace utility
{
class GPIOInterface
{
public:
GPIOInterface();
virtual ~GPIOInterface() {};
virtual ~GPIOInterface()
{};
static void initialize_input_pin(char address);
@@ -35,7 +40,8 @@ public:
static std::once_flag GPIO_LIB_INITIALIZED;
};
}
}
#endif

View File

@@ -8,15 +8,23 @@
#ifndef SRC_UTILITIES_IBLOCKINGQUEUE_H_
#define SRC_UTILITIES_IBLOCKINGQUEUE_H_
template <typename T>
namespace FlippR_Driver
{
namespace utility
{
template<typename T>
class IBlockingQueue
{
public:
virtual ~IBlockingQueue(){};
virtual ~IBlockingQueue()
{};
virtual void push(T const& value) = 0;
virtual void push(T const &value) = 0;
virtual T pop() = 0;
};
}
}
#endif

View File

@@ -8,13 +8,21 @@
#ifndef SRC_UTILITIES_IINPUTGPIOINTERFACE_H_
#define SRC_UTILITIES_IINPUTGPIOINTERFACE_H_
namespace FlippR_Driver
{
namespace utility
{
class IInputGPIOInterface
{
public:
virtual ~IInputGPIOInterface(){};
virtual ~IInputGPIOInterface()
{};
virtual bool read_data(char pin) = 0;
};
}
}
#endif

View File

@@ -5,8 +5,16 @@
#ifndef FLIPPR_DRIVER_IOUTPUTGPIOINTERFACE_H
#define FLIPPR_DRIVER_IOUTPUTGPIOINTERFACE_H
namespace FlippR_Driver
{
namespace utility
{
class IOutputGPIOInterface
{
};
}
}
#endif //FLIPPR_DRIVER_IOUTPUTGPIOINTERFACE_H

View File

@@ -13,6 +13,11 @@
#include "easylogging/easylogging++.h"
#include "config.h"
#include <string>
namespace FlippR_Driver
{
namespace utility
{
using namespace nlohmann;
bool InputGPIOInterface::read_data(char pin)
@@ -41,8 +46,7 @@ void InputGPIOInterface::write_col(char data)
write_pin(this->col_address_C, data & 0b100);
}
InputGPIOInterface::InputGPIOInterface(std::istream& input_config_stream)
InputGPIOInterface::InputGPIOInterface(std::istream &input_config_stream)
{
init_members(input_config_stream);
@@ -54,23 +58,28 @@ 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>();
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>();
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();
}
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()
@@ -85,3 +94,6 @@ void InputGPIOInterface::init_pins()
initialize_input_pin(data_address);
}
}
}

View File

@@ -12,14 +12,19 @@
#include "IInputGPIOInterface.h"
#include "GPIOInterface.h"
namespace FlippR_Driver
{
namespace utility
{
class InputGPIOInterface : public IInputGPIOInterface, GPIOInterface
{
public:
InputGPIOInterface(std::istream& input_config_stream);
InputGPIOInterface(std::istream &input_config_stream);
bool read_data(char pin);
private:
void init_members(std::istream& input_config_stream);
void init_members(std::istream &input_config_stream);
void init_pins();
void write_row(char data);
void write_col(char data);
@@ -34,4 +39,6 @@ private:
char data_address;
};
}
}
#endif

View File

@@ -10,6 +10,11 @@
#define EASYLOGGING_IS_INITIALIZED
INITIALIZE_EASYLOGGINGPP
#endif
namespace FlippR_Driver
{
namespace utility
{
namespace LoggerFactory
{
@@ -60,3 +65,6 @@ void CreateOutputLogger(el::Level level)
}
};
}
}

View File

@@ -10,13 +10,20 @@
#include "config.h"
namespace FlippR_Driver
{
namespace utility
{
namespace LoggerFactory
{
void CreateInputTestLogger(el::Level level = el::Level::Global);
void CreateInputTestLogger(el::Level level = el::Level::Global);
void CreateInputLogger(el::Level level = el::Level::Info);
void CreateInputLogger(el::Level level = el::Level::Info);
void CreateOutputLogger(el::Level level = el::Level::Info);
void CreateOutputLogger(el::Level level = el::Level::Info);
};
}
}
#endif

View File

@@ -11,6 +11,10 @@
#include "GPIOInterface.h"
#include <mcp23017.h>
namespace FlippR_Driver
{
namespace utility
{
class OutputGPIOInterface : GPIOInterface
{
@@ -21,9 +25,10 @@ public:
private:
void
private:
IBlockingQueue<CabinetItem> event_queue;
IBlockingQueue <CabinetItem> event_queue;
};
}
}
#endif