Merge branch 'master' of https://github.com/swinginbird/flippr-code
This commit is contained in:
1
FlippR-Driver/.gitignore
vendored
1
FlippR-Driver/.gitignore
vendored
@@ -11,3 +11,4 @@ bin
|
||||
Makefile
|
||||
.project
|
||||
.cproject
|
||||
.idea
|
||||
|
||||
@@ -8,7 +8,7 @@ set(OUTPUT_PATH bin)
|
||||
set(LIB_DIR lib)
|
||||
set(DEFAULT_BUILD_TYPE DEBUG)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
# Boost configuration
|
||||
set(BOOST_COMPONENTS program_options thread timer chrono)
|
||||
@@ -110,14 +110,17 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/include)
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/${LIB_DIR})
|
||||
|
||||
##################### WIRING_PI ##########################
|
||||
find_library(WIRING_PI NAMES libwiringPi.so.2.44 HINTS ${CMAKE_SOURCE_DIR}/lib)
|
||||
|
||||
if(NOT WIRING_PI)
|
||||
find_library(wiringPi_LIB wiringPi)
|
||||
if(NOT wiringPi_LIB)
|
||||
message(FATAL_ERROR "Could not find wiringPi library")
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC ${wiringPi_LIB})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC ${WIRING_PI})
|
||||
|
||||
find_library(crypt_LIB crypt)
|
||||
if(NOT crypt_LIB)
|
||||
message(FATAL_ERROR "Could not find crypt library")
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC ${crypt_LIB})
|
||||
## EASYLOGGING
|
||||
set(EASYLOGGING_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${LIB_DIR}/easylogging)
|
||||
add_library(Easylogging STATIC ${EASYLOGGING_INCLUDE_DIR}/easylogging++.cc)
|
||||
@@ -132,6 +135,9 @@ target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
|
||||
|
||||
## THREAD
|
||||
find_package(Threads REQUIRED)
|
||||
if(NOT CMAKE_THREAD_LIBS_INIT)
|
||||
message(FATAL_ERROR, "Could not find libthread")
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE ${Threads_LIBRARIES})
|
||||
|
||||
if(ENABLE_TESTING)
|
||||
|
||||
Binary file not shown.
@@ -55,7 +55,7 @@ bool Detector::check_inputs(char& address)
|
||||
{
|
||||
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;
|
||||
return true;
|
||||
|
||||
@@ -7,17 +7,33 @@
|
||||
|
||||
#include "GPIOInterface.h"
|
||||
|
||||
//#include "lib/wiringPi/wiringPi.h"
|
||||
#include "wiringPi/wiringPi.h"
|
||||
#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)
|
||||
{
|
||||
//digitalWrite(address, data);
|
||||
digitalWrite(address, data);
|
||||
}
|
||||
|
||||
|
||||
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_
|
||||
#define SRC_UTILITIES_GPIOINTERFACE_H_
|
||||
|
||||
#include <mutex>
|
||||
#include <fstream>
|
||||
#include "config.h"
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ class IInputGPIOInterface
|
||||
public:
|
||||
virtual ~IInputGPIOInterface(){};
|
||||
|
||||
virtual bool read_input_data(char pin) = 0;
|
||||
virtual bool read_data(char pin) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -15,56 +15,75 @@
|
||||
#include <string>
|
||||
using namespace nlohmann;
|
||||
|
||||
bool InputGPIOInterface::read_input_data(char pin)
|
||||
bool InputGPIOInterface::read_data(char pin)
|
||||
{
|
||||
// setting address to read
|
||||
write_input_row(pin / INPUT_MATRIX_SIZE);
|
||||
write_input_col(pin % INPUT_MATRIX_SIZE);
|
||||
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->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->input_row_address_B, data & 0b010);
|
||||
write_pin(this->input_row_address_C, data & 0b100);
|
||||
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_input_col(char data)
|
||||
void InputGPIOInterface::write_col(char data)
|
||||
{
|
||||
write_pin(this->input_col_address_A, data & 0b001);
|
||||
write_pin(this->input_col_address_B, data & 0b010);
|
||||
write_pin(this->input_col_address_C, data & 0b100);
|
||||
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& matrix_config_stream)
|
||||
InputGPIOInterface::InputGPIOInterface(std::istream& input_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");
|
||||
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_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 matrix_config_input = input_config.at("input");
|
||||
|
||||
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<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);
|
||||
}
|
||||
|
||||
@@ -15,21 +15,23 @@
|
||||
class InputGPIOInterface : public IInputGPIOInterface, GPIOInterface
|
||||
{
|
||||
public:
|
||||
InputGPIOInterface(std::istream& matrix_config_stream);
|
||||
bool read_input_data(char pin);
|
||||
InputGPIOInterface(std::istream& input_config_stream);
|
||||
bool read_data(char pin);
|
||||
|
||||
private:
|
||||
void write_input_row(char data);
|
||||
void write_input_col(char data);
|
||||
void init_members(std::istream& input_config_stream);
|
||||
void init_pins();
|
||||
void write_row(char data);
|
||||
void write_col(char data);
|
||||
|
||||
private:
|
||||
char input_row_address_A;
|
||||
char input_row_address_B;
|
||||
char input_row_address_C;
|
||||
char input_col_address_A;
|
||||
char input_col_address_B;
|
||||
char input_col_address_C;
|
||||
char input_data_address;
|
||||
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 /* SRC_UTILITIES_INPUTGPIOINTERFACE_H_ */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
###################### START_CMAKE #######################
|
||||
cmake_minimum_required(VERSION 3.9.1)
|
||||
cmake_minimum_required(VERSION 3.6.2)
|
||||
project(FlippR-Driver-Tests VERSION 0.1.0 LANGUAGES CXX)
|
||||
|
||||
# Compile tests to output_path/tests
|
||||
|
||||
@@ -36,7 +36,7 @@ SCENARIO("Creating a Detector object", "")
|
||||
Mock<IInputGPIOInterface> 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));
|
||||
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
||||
@@ -64,7 +64,7 @@ SCENARIO("There are events at the input", "")
|
||||
Mock<IInputGPIOInterface> 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));
|
||||
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;
|
||||
|
||||
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));
|
||||
When(Method(event_notifier_mock, distribute_event)).AlwaysReturn();
|
||||
|
||||
Reference in New Issue
Block a user