moved gpio interface pin parsing to factory
This commit is contained in:
@@ -22,12 +22,12 @@ GPIOInterface::GPIOInterface()
|
|||||||
std::call_once(GPIO_LIB_INITIALIZED, wiringPiSetup);
|
std::call_once(GPIO_LIB_INITIALIZED, wiringPiSetup);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPIOInterface::initialize_input_pin(char address)
|
void GPIOInterface::initialize_input_pin(const char address)
|
||||||
{
|
{
|
||||||
pinMode(address, INPUT);
|
pinMode(address, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPIOInterface::initialize_output_pin(char address)
|
void GPIOInterface::initialize_output_pin(const char address)
|
||||||
{
|
{
|
||||||
pinMode(address, OUTPUT);
|
pinMode(address, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,13 +25,13 @@ public:
|
|||||||
virtual ~GPIOInterface() = default;
|
virtual ~GPIOInterface() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void initialize_input_pin(char address);
|
static void initialize_input_pin(const char address);
|
||||||
|
|
||||||
static void initialize_output_pin(char address);
|
static void initialize_output_pin(const char address);
|
||||||
|
|
||||||
static void write_pin(char address, char data);
|
static void write_pin(const char address, const char data);
|
||||||
|
|
||||||
static bool read_pin(char address);
|
static bool read_pin(const char address);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::once_flag GPIO_LIB_INITIALIZED;
|
static std::once_flag GPIO_LIB_INITIALIZED;
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
flippR_driver::input::DistributingEvent::DistributingEvent(char address, int priority, std::string name,
|
flippR_driver::input::DistributingEvent::DistributingEvent(char address, int priority, std::string name,
|
||||||
std::chrono::milliseconds bounce_time, std::shared_ptr<IEventNotifier> event_notifier):
|
std::chrono::milliseconds bounce_time, std::shared_ptr<IEventNotifier> event_notifier):
|
||||||
Event(address, priority, name),
|
Event(address, priority, std::move(name)),
|
||||||
bounce_time(bounce_time),
|
bounce_time(bounce_time),
|
||||||
event_notifier(event_notifier),
|
event_notifier(std::move(event_notifier)),
|
||||||
activation_state(NOT_ACTIVATED)
|
activation_state(NOT_ACTIVATED)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace input
|
|||||||
{
|
{
|
||||||
|
|
||||||
EventHandler::EventHandler(std::shared_ptr<IInputDriver> input_driver) :
|
EventHandler::EventHandler(std::shared_ptr<IInputDriver> input_driver) :
|
||||||
input_driver(input_driver)
|
input_driver(std::move(input_driver))
|
||||||
{
|
{
|
||||||
this->input_driver->register_event_handler(this);
|
this->input_driver->register_event_handler(this);
|
||||||
|
|
||||||
|
|||||||
@@ -16,10 +16,9 @@ namespace input
|
|||||||
class IInputGPIOInterface
|
class IInputGPIOInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~IInputGPIOInterface()
|
virtual ~IInputGPIOInterface() = default;
|
||||||
= default;;
|
|
||||||
|
|
||||||
virtual bool read_data(char pin) = 0;
|
virtual bool read_data(char pin) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ namespace input
|
|||||||
|
|
||||||
InputDriver::InputDriver(std::shared_ptr<IEventNotifier> event_notifier, std::unique_ptr<IDetector> detector,
|
InputDriver::InputDriver(std::shared_ptr<IEventNotifier> event_notifier, std::unique_ptr<IDetector> detector,
|
||||||
std::map<std::string, std::shared_ptr<Event>> events) :
|
std::map<std::string, std::shared_ptr<Event>> events) :
|
||||||
event_notifier(event_notifier), detector(std::move(detector)), events(events)
|
event_notifier(std::move(event_notifier)), detector(std::move(detector)), events(std::move(events))
|
||||||
{
|
{
|
||||||
CLOG(INFO, INPUT_LOGGER) << "Created InputDriver";
|
CLOG(INFO, INPUT_LOGGER) << "Created InputDriver";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace InputDriverFactory
|
|||||||
using namespace nlohmann;
|
using namespace nlohmann;
|
||||||
using namespace flippR_driver::utility;
|
using namespace flippR_driver::utility;
|
||||||
|
|
||||||
std::shared_ptr<IInputDriver> get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream)
|
std::shared_ptr<IInputDriver> get_InputDriver(std::istream &input_pin_stream, std::istream &matrix_config_stream)
|
||||||
{
|
{
|
||||||
LoggerFactory::CreateInputLogger();
|
LoggerFactory::CreateInputLogger();
|
||||||
|
|
||||||
@@ -36,23 +36,24 @@ std::shared_ptr<IInputDriver> get_InputDriver(std::istream& input_config_stream,
|
|||||||
std::vector<std::shared_ptr<DistributingEvent>> events;
|
std::vector<std::shared_ptr<DistributingEvent>> events;
|
||||||
std::map<std::string, std::shared_ptr<Event>> name_event_map;
|
std::map<std::string, std::shared_ptr<Event>> name_event_map;
|
||||||
|
|
||||||
json matrix_config;
|
create_events(matrix_config_stream, events, name_event_map, event_notifier);
|
||||||
matrix_config_stream >> matrix_config;
|
|
||||||
create_events(matrix_config, events, name_event_map, event_notifier);
|
|
||||||
|
|
||||||
std::unique_ptr<IInputGPIOInterface> input_gpio_interface(new InputGPIOInterface(input_config_stream));
|
std::unique_ptr<IInputGPIOInterface> input_gpio_interface(new InputGPIOInterface(create_pin_map(input_pin_stream)));
|
||||||
std::unique_ptr<IDetector> detector(new Detector(std::move(input_gpio_interface), events));
|
std::unique_ptr<IDetector> detector(new Detector(std::move(input_gpio_interface), events));
|
||||||
|
|
||||||
return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, std::move(detector), name_event_map));
|
return std::make_shared<InputDriver>(event_notifier, std::move(detector), name_event_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
void create_events(json matrix_config, std::vector<std::shared_ptr<DistributingEvent>> &events,
|
void create_events(std::istream &matrix_config_stream, std::vector<std::shared_ptr<DistributingEvent>> &events,
|
||||||
std::map<std::string, std::shared_ptr<Event>> &name_event_map,
|
std::map<std::string, std::shared_ptr<Event>> &name_event_map,
|
||||||
std::shared_ptr<IEventNotifier> event_notifier)
|
std::shared_ptr<IEventNotifier> event_notifier)
|
||||||
{
|
{
|
||||||
int global_bounce_time = matrix_config.at("global_bounce_time").get<json::number_integer_t>();
|
json matrix_config;
|
||||||
|
matrix_config_stream >> matrix_config;
|
||||||
|
|
||||||
|
int global_bounce_time = matrix_config.at("global_bounce_time").get<uint8_t>();
|
||||||
auto &json_events = matrix_config.at("input_matrix");
|
auto &json_events = matrix_config.at("input_matrix");
|
||||||
for(auto &json_event : json_events)
|
for(auto &json_event : json_events)
|
||||||
{
|
{
|
||||||
@@ -68,8 +69,8 @@ namespace
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::string name = json_event.at("name");
|
std::string name = json_event.at("name");
|
||||||
char address = json_event.at("address").get<json::number_integer_t>();
|
char address = json_event.at("address").get<uint8_t>();
|
||||||
int priority = json_event.at("priority").get<json::number_integer_t>();
|
int priority = json_event.at("priority").get<uint8_t>();
|
||||||
|
|
||||||
set_individual_bounce_time(json_event, bounce_time);
|
set_individual_bounce_time(json_event, bounce_time);
|
||||||
|
|
||||||
@@ -88,10 +89,45 @@ namespace
|
|||||||
|
|
||||||
if(it_bounce_time != json_event.end())
|
if(it_bounce_time != json_event.end())
|
||||||
{
|
{
|
||||||
bounce_time = it_bounce_time->get<json::number_integer_t>();
|
bounce_time = it_bounce_time->get<uint8_t>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::map<std::string, uint8_t> create_pin_map(std::istream &input_pin_stream)
|
||||||
|
{
|
||||||
|
std::map<std::string, uint8_t> input_pin_map;
|
||||||
|
|
||||||
|
json pin_config;
|
||||||
|
input_pin_stream >> pin_config;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
json row_json = pin_config.at("row");
|
||||||
|
input_pin_map["row_address_A"] = row_json.at("A").get<uint8_t>();
|
||||||
|
input_pin_map["row_address_B"] = row_json.at("B").get<uint8_t>();
|
||||||
|
input_pin_map["row_address_C"] = row_json.at("C").get<uint8_t>();
|
||||||
|
|
||||||
|
json col_json = pin_config.at("col");
|
||||||
|
input_pin_map["col_address_A"] = col_json.at("A").get<uint8_t>();
|
||||||
|
input_pin_map["col_address_B"] = col_json.at("B").get<uint8_t>();
|
||||||
|
input_pin_map["col_address_C"] = col_json.at("C").get<uint8_t>();
|
||||||
|
|
||||||
|
input_pin_map["data_address"] = pin_config.at("data").get<uint8_t>();
|
||||||
|
}
|
||||||
|
catch(json::type_error &e)
|
||||||
|
{
|
||||||
|
CLOG(ERROR, INPUT_LOGGER) << "Input json corrupted! " << e.what();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
catch(json::out_of_range &e)
|
||||||
|
{
|
||||||
|
CLOG(ERROR, INPUT_LOGGER) << "Input json corrupted! " << e.what();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return input_pin_map;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,17 +22,19 @@ namespace input
|
|||||||
{
|
{
|
||||||
namespace InputDriverFactory
|
namespace InputDriverFactory
|
||||||
{
|
{
|
||||||
std::shared_ptr<IInputDriver> get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream);
|
std::shared_ptr<IInputDriver> get_InputDriver(std::istream &input_pin_stream, std::istream &matrix_config_stream);
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
static void create_events(nlohmann::json matrix_config,
|
static void create_events(std::istream &matrix_config,
|
||||||
std::vector<std::shared_ptr<DistributingEvent>> &events,
|
std::vector<std::shared_ptr<DistributingEvent>> &events,
|
||||||
std::map<std::string, std::shared_ptr<Event>> &name_event_map,
|
std::map<std::string, std::shared_ptr<Event>> &name_event_map,
|
||||||
std::shared_ptr<IEventNotifier> event_notifier);
|
std::shared_ptr<IEventNotifier> event_notifier);
|
||||||
|
|
||||||
static std::shared_ptr<DistributingEvent> create_event(nlohmann::json &json_event, std::shared_ptr<IEventNotifier> event_notifier, int bounce_time);
|
static std::shared_ptr<DistributingEvent> create_event(nlohmann::json &json_event, std::shared_ptr<IEventNotifier> event_notifier, int bounce_time);
|
||||||
static void set_individual_bounce_time(nlohmann::json &json_event, int &bounce_time);
|
static void set_individual_bounce_time(nlohmann::json &json_event, int &bounce_time);
|
||||||
|
|
||||||
|
static std::map<std::string, uint8_t> create_pin_map(std::istream &input_pin_stream);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,14 @@
|
|||||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <fstream>
|
#include "InputGPIOInterface.h"
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "InputGPIOInterface.h"
|
|
||||||
#include "json/json.hpp"
|
#include "json/json.hpp"
|
||||||
#include "easylogging/easylogging++.h"
|
#include "easylogging/easylogging++.h"
|
||||||
#include "utility/config.h"
|
#include "utility/config.h"
|
||||||
#include <string>
|
|
||||||
namespace flippR_driver
|
namespace flippR_driver
|
||||||
{
|
{
|
||||||
namespace input
|
namespace input
|
||||||
@@ -20,14 +20,14 @@ namespace input
|
|||||||
|
|
||||||
using namespace nlohmann;
|
using namespace nlohmann;
|
||||||
|
|
||||||
InputGPIOInterface::InputGPIOInterface(std::istream &input_config)
|
|
||||||
{
|
|
||||||
init_members(input_config);
|
|
||||||
|
|
||||||
|
InputGPIOInterface::InputGPIOInterface(std::map<std::string, uint8_t> pins)
|
||||||
|
: pins(pins)
|
||||||
|
{
|
||||||
init_pins();
|
init_pins();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool InputGPIOInterface::read_data(char pin)
|
bool InputGPIOInterface::read_data(char pin) const
|
||||||
{
|
{
|
||||||
// setting address to read
|
// setting address to read
|
||||||
write_row(pin / INPUT_MATRIX_SIZE);
|
write_row(pin / INPUT_MATRIX_SIZE);
|
||||||
@@ -36,63 +36,36 @@ bool InputGPIOInterface::read_data(char pin)
|
|||||||
// wait for mux to set address
|
// wait for mux to set address
|
||||||
std::this_thread::sleep_for(std::chrono::nanoseconds(INPUT_SLEEP_DURATION_NANO));
|
std::this_thread::sleep_for(std::chrono::nanoseconds(INPUT_SLEEP_DURATION_NANO));
|
||||||
|
|
||||||
return read_pin(this->data_address);
|
return read_pin(this->pins.at("data_address"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputGPIOInterface::write_row(char data)
|
void InputGPIOInterface::write_row(char data) const
|
||||||
{
|
{
|
||||||
write_pin(this->row_address_A, data & 0b001);
|
write_pin(this->pins.at("row_address_A"), data & 0b001);
|
||||||
write_pin(this->row_address_B, data & 0b010);
|
write_pin(this->pins.at("row_address_B"), data & 0b010);
|
||||||
write_pin(this->row_address_C, data & 0b100);
|
write_pin(this->pins.at("row_address_C"), data & 0b100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputGPIOInterface::write_col(char data)
|
void InputGPIOInterface::write_col(char data) const
|
||||||
{
|
{
|
||||||
write_pin(this->col_address_A, data & 0b001);
|
write_pin(this->pins.at("col_address_A"), data & 0b001);
|
||||||
write_pin(this->col_address_B, data & 0b010);
|
write_pin(this->pins.at("col_address_B"), data & 0b010);
|
||||||
write_pin(this->col_address_C, data & 0b100);
|
write_pin(this->pins.at("col_address_C"), data & 0b100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputGPIOInterface::init_members(std::istream &input_config_stream)
|
|
||||||
|
|
||||||
|
void InputGPIOInterface::init_pins() const
|
||||||
{
|
{
|
||||||
json input_config;
|
initialize_output_pin(this->pins.at("col_address_A"));
|
||||||
input_config_stream >> input_config;
|
initialize_output_pin(this->pins.at("col_address_B"));
|
||||||
|
initialize_output_pin(this->pins.at("col_address_C"));
|
||||||
|
|
||||||
try
|
initialize_output_pin(this->pins.at("row_address_A"));
|
||||||
{
|
initialize_output_pin(this->pins.at("row_address_B"));
|
||||||
json row_json = input_config.at("row");
|
initialize_output_pin(this->pins.at("row_address_C"));
|
||||||
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");
|
initialize_input_pin(this->pins.at("data_address"));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,11 @@
|
|||||||
#ifndef SRC_UTILITIES_INPUTGPIOINTERFACE_H_
|
#ifndef SRC_UTILITIES_INPUTGPIOINTERFACE_H_
|
||||||
#define SRC_UTILITIES_INPUTGPIOINTERFACE_H_
|
#define SRC_UTILITIES_INPUTGPIOINTERFACE_H_
|
||||||
|
|
||||||
#include <exception>
|
|
||||||
#include "IInputGPIOInterface.h"
|
#include "IInputGPIOInterface.h"
|
||||||
|
|
||||||
|
#include <istream>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include "GPIOInterface.h"
|
#include "GPIOInterface.h"
|
||||||
|
|
||||||
namespace flippR_driver
|
namespace flippR_driver
|
||||||
@@ -20,23 +23,17 @@ namespace input
|
|||||||
class InputGPIOInterface : public IInputGPIOInterface, GPIOInterface
|
class InputGPIOInterface : public IInputGPIOInterface, GPIOInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit InputGPIOInterface(std::istream &input_config);
|
explicit InputGPIOInterface(std::map<std::string, uint8_t> pins);
|
||||||
bool read_data(char pin) override;
|
bool read_data(char pin) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init_members(std::istream &input_config_stream);
|
void init_pins() const;
|
||||||
void init_pins();
|
void write_row(char data) const;
|
||||||
void write_row(char data);
|
void write_col(char data) const;
|
||||||
void write_col(char data);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char row_address_A;
|
const std::map<std::string, uint8_t> pins;
|
||||||
char row_address_B;
|
|
||||||
char row_address_C;
|
|
||||||
char col_address_A;
|
|
||||||
char col_address_B;
|
|
||||||
char col_address_C;
|
|
||||||
char data_address;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace output
|
|||||||
{
|
{
|
||||||
|
|
||||||
DisplayController::DisplayController(std::vector<std::shared_ptr<items::IDisplay>> displays, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface)
|
DisplayController::DisplayController(std::vector<std::shared_ptr<items::IDisplay>> displays, std::shared_ptr<IOutputGPIOInterface> output_gpio_interface)
|
||||||
: displays(displays), output_gpio_interface(output_gpio_interface), is_running(true)
|
: displays(std::move(displays)), output_gpio_interface(std::move(output_gpio_interface)), is_running(true)
|
||||||
{
|
{
|
||||||
this->display_cycle_thread = std::thread(&DisplayController::cycle_displays, this);
|
this->display_cycle_thread = std::thread(&DisplayController::cycle_displays, this);
|
||||||
|
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ class IDisplayController
|
|||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IDisplayController ();
|
virtual ~IDisplayController () = default;
|
||||||
virtual ~IDisplayController ();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace output */
|
} /* namespace output */
|
||||||
|
|||||||
@@ -21,8 +21,7 @@ class IDisplay
|
|||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IDisplay();
|
virtual ~IDisplay() = default;
|
||||||
virtual ~IDisplay();
|
|
||||||
|
|
||||||
virtual std::vector<char> get_content() = 0;
|
virtual std::vector<char> get_content() = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace items
|
|||||||
class IItem
|
class IItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~IItem();
|
virtual ~IItem() = default;
|
||||||
|
|
||||||
virtual uint8_t get_address() = 0;
|
virtual uint8_t get_address() = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class Item : public IItem
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Item(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, uint8_t address, std::string name);
|
Item(std::shared_ptr<IOutputGPIOInterface> output_gpio_interface, uint8_t address, std::string name);
|
||||||
~Item() override;
|
virtual ~Item() = default;
|
||||||
|
|
||||||
uint8_t get_address() override;
|
uint8_t get_address() override;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user