finishing inputgpiointerface
This commit is contained in:
@@ -55,7 +55,7 @@ bool Detector::check_inputs(char& address)
|
|||||||
{
|
{
|
||||||
for(char pin = 0; pin < (INPUT_MATRIX_SIZE * INPUT_MATRIX_SIZE); pin++)
|
for(char pin = 0; pin < (INPUT_MATRIX_SIZE * INPUT_MATRIX_SIZE); pin++)
|
||||||
{
|
{
|
||||||
if(input_gpio_interface->read_input_data(pin))
|
if(input_gpio_interface->read_data(pin))
|
||||||
{
|
{
|
||||||
address = pin;
|
address = pin;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -10,6 +10,12 @@
|
|||||||
#include "wiringPi/wiringPi.h"
|
#include "wiringPi/wiringPi.h"
|
||||||
#include "json/json.hpp"
|
#include "json/json.hpp"
|
||||||
|
|
||||||
|
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)
|
void GPIOInterface::write_pin(char address, char data)
|
||||||
{
|
{
|
||||||
@@ -21,3 +27,13 @@ bool GPIOInterface::read_pin(char address)
|
|||||||
{
|
{
|
||||||
return digitalRead(address);
|
return digitalRead(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GPIOInterface::initialize_output_pin(char address)
|
||||||
|
{
|
||||||
|
pinMode(address, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
GPIOInterface::GPIOInterface()
|
||||||
|
{
|
||||||
|
std::call_once(GPIO_LIB_INITIALIZED, wiringPiSetup);
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,17 +13,26 @@
|
|||||||
#ifndef SRC_UTILITIES_GPIOINTERFACE_H_
|
#ifndef SRC_UTILITIES_GPIOINTERFACE_H_
|
||||||
#define SRC_UTILITIES_GPIOINTERFACE_H_
|
#define SRC_UTILITIES_GPIOINTERFACE_H_
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
class GPIOInterface
|
class GPIOInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
GPIOInterface();
|
||||||
|
|
||||||
virtual ~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 void write_pin(char address, char data);
|
||||||
|
|
||||||
static bool read_pin(char address);
|
static bool read_pin(char address);
|
||||||
|
|
||||||
|
public:
|
||||||
|
static std::once_flag GPIO_LIB_INITIALIZED;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class IInputGPIOInterface
|
|||||||
public:
|
public:
|
||||||
virtual ~IInputGPIOInterface(){};
|
virtual ~IInputGPIOInterface(){};
|
||||||
|
|
||||||
virtual bool read_input_data(char pin) = 0;
|
virtual bool read_data(char pin) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,56 +15,75 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
using namespace nlohmann;
|
using namespace nlohmann;
|
||||||
|
|
||||||
bool InputGPIOInterface::read_input_data(char pin)
|
bool InputGPIOInterface::read_data(char pin)
|
||||||
{
|
{
|
||||||
// setting address to read
|
// setting address to read
|
||||||
write_input_row(pin / INPUT_MATRIX_SIZE);
|
write_row(pin / INPUT_MATRIX_SIZE);
|
||||||
write_input_col(pin % INPUT_MATRIX_SIZE);
|
write_col(pin % INPUT_MATRIX_SIZE);
|
||||||
|
|
||||||
// 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->input_data_address);
|
return read_pin(this->data_address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputGPIOInterface::write_input_row(char data)
|
void InputGPIOInterface::write_row(char data)
|
||||||
{
|
{
|
||||||
write_pin(this->input_row_address_A, data & 0b001);
|
write_pin(this->row_address_A, data & 0b001);
|
||||||
write_pin(this->input_row_address_B, data & 0b010);
|
write_pin(this->row_address_B, data & 0b010);
|
||||||
write_pin(this->input_row_address_C, data & 0b100);
|
write_pin(this->row_address_C, data & 0b100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputGPIOInterface::write_input_col(char data)
|
void InputGPIOInterface::write_col(char data)
|
||||||
{
|
{
|
||||||
write_pin(this->input_col_address_A, data & 0b001);
|
write_pin(this->col_address_A, data & 0b001);
|
||||||
write_pin(this->input_col_address_B, data & 0b010);
|
write_pin(this->col_address_B, data & 0b010);
|
||||||
write_pin(this->input_col_address_C, data & 0b100);
|
write_pin(this->col_address_C, data & 0b100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
InputGPIOInterface::InputGPIOInterface(std::istream& matrix_config_stream)
|
InputGPIOInterface::InputGPIOInterface(std::istream& matrix_config_stream)
|
||||||
{
|
{
|
||||||
json matrix_config;
|
init_members(matrix_config_stream);
|
||||||
matrix_config_stream >> matrix_config;
|
|
||||||
|
|
||||||
try {
|
|
||||||
json matrix_config_input = matrix_config.at("input");
|
|
||||||
|
|
||||||
json row_json = matrix_config.at("row");
|
|
||||||
input_row_address_A = row_json.at("A").get<json::number_integer_t>();
|
|
||||||
input_row_address_B = row_json.at("B").get<json::number_integer_t>();
|
|
||||||
input_row_address_C = row_json.at("C").get<json::number_integer_t>();
|
|
||||||
|
|
||||||
json col_json = matrix_config.at("col");
|
|
||||||
input_col_address_A = col_json.at("A").get<json::number_integer_t>();
|
|
||||||
input_col_address_B = col_json.at("B").get<json::number_integer_t>();
|
|
||||||
input_col_address_C = col_json.at("C").get<json::number_integer_t>();
|
|
||||||
|
|
||||||
input_data_address = matrix_config.at("data").get<nlohmann::json::number_integer_t>();
|
|
||||||
} catch (json::type_error& e) {
|
|
||||||
CLOG(ERROR, INPUT_LOGGER) << "ERROR";
|
|
||||||
} catch (json::out_of_range& e) {
|
|
||||||
CLOG(ERROR, INPUT_LOGGER) << "ANOTHER ERROR!";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
init_pins();
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputGPIOInterface::init_members(std::istream &matrix_config_stream)
|
||||||
|
{
|
||||||
|
json matrix_config;
|
||||||
|
matrix_config_stream >> matrix_config;
|
||||||
|
|
||||||
|
try {
|
||||||
|
json matrix_config_input = matrix_config.at("input");
|
||||||
|
|
||||||
|
json row_json = matrix_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 = matrix_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 = matrix_config.at("data").get<nlohmann::json::number_integer_t>();
|
||||||
|
} catch (json::type_error& e) {
|
||||||
|
CLOG(ERROR, INPUT_LOGGER) << "ERROR";
|
||||||
|
} catch (json::out_of_range& e) {
|
||||||
|
CLOG(ERROR, INPUT_LOGGER) << "ANOTHER ERROR!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,20 +16,22 @@ class InputGPIOInterface : public IInputGPIOInterface, GPIOInterface
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InputGPIOInterface(std::istream& matrix_config_stream);
|
InputGPIOInterface(std::istream& matrix_config_stream);
|
||||||
bool read_input_data(char pin);
|
bool read_data(char pin);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void write_input_row(char data);
|
void init_members(std::istream& matrix_config_stream);
|
||||||
void write_input_col(char data);
|
void init_pins();
|
||||||
|
void write_row(char data);
|
||||||
|
void write_col(char data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char input_row_address_A;
|
char row_address_A;
|
||||||
char input_row_address_B;
|
char row_address_B;
|
||||||
char input_row_address_C;
|
char row_address_C;
|
||||||
char input_col_address_A;
|
char col_address_A;
|
||||||
char input_col_address_B;
|
char col_address_B;
|
||||||
char input_col_address_C;
|
char col_address_C;
|
||||||
char input_data_address;
|
char data_address;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SRC_UTILITIES_INPUTGPIOINTERFACE_H_ */
|
#endif /* SRC_UTILITIES_INPUTGPIOINTERFACE_H_ */
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ SCENARIO("Creating a Detector object", "")
|
|||||||
Mock<IInputGPIOInterface> gpio_interface_mock;
|
Mock<IInputGPIOInterface> gpio_interface_mock;
|
||||||
|
|
||||||
Fake(Dtor(gpio_interface_mock));
|
Fake(Dtor(gpio_interface_mock));
|
||||||
When(Method(gpio_interface_mock, read_input_data)).AlwaysReturn(false);
|
When(Method(gpio_interface_mock, read_data)).AlwaysReturn(false);
|
||||||
|
|
||||||
Fake(Dtor(event_notifier_mock));
|
Fake(Dtor(event_notifier_mock));
|
||||||
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
||||||
@@ -64,7 +64,7 @@ SCENARIO("There are events at the input", "")
|
|||||||
Mock<IInputGPIOInterface> gpio_interface_mock;
|
Mock<IInputGPIOInterface> gpio_interface_mock;
|
||||||
|
|
||||||
Fake(Dtor(gpio_interface_mock));
|
Fake(Dtor(gpio_interface_mock));
|
||||||
When(Method(gpio_interface_mock, read_input_data)).AlwaysDo([](char c){return c==2;});
|
When(Method(gpio_interface_mock, read_data)).AlwaysDo([](char c){return c==2;});
|
||||||
|
|
||||||
Fake(Dtor(event_notifier_mock));
|
Fake(Dtor(event_notifier_mock));
|
||||||
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
||||||
@@ -104,7 +104,7 @@ SCENARIO("There are events at the input but no suitable event in map", "")
|
|||||||
Mock<IInputGPIOInterface> gpio_interface_mock;
|
Mock<IInputGPIOInterface> gpio_interface_mock;
|
||||||
|
|
||||||
Fake(Dtor(gpio_interface_mock));
|
Fake(Dtor(gpio_interface_mock));
|
||||||
When(Method(gpio_interface_mock, read_input_data)).AlwaysDo([](char c){return c==4;});
|
When(Method(gpio_interface_mock, read_data)).AlwaysDo([](char c){return c==4;});
|
||||||
|
|
||||||
Fake(Dtor(event_notifier_mock));
|
Fake(Dtor(event_notifier_mock));
|
||||||
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
||||||
|
|||||||
Reference in New Issue
Block a user