Adapated sounds and ouput driver factory to new board layout

This commit is contained in:
Johannes Wendel
2019-07-16 15:13:31 +02:00
parent d113e206c0
commit de57b6590f
9 changed files with 297 additions and 432 deletions

View File

@@ -2,19 +2,16 @@
// Created by rhetenor on 04.10.18.
//
#include "OutputDriverFactory.h"
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/adaptor/map.hpp>
#include "utility/LoggerFactory.h"
#include "output/detail/OutputDriver.h"
#include "output/detail/DisplayController.h"
#include "output/detail/DisplayBoardPinController.h"
#include "output/detail/DriverBoardPinController.h"
#include "output/detail/SoundBoardPinController.h"
#include "output/items/detail/EightDigitDisplay.h"
@@ -29,14 +26,13 @@ namespace output
namespace OutputDriverFactory
{
using namespace nlohmann;
std::shared_ptr<OutputDriver> get_OutputDriver(std::istream& output_pin_config,
std::istream& solenoid_config,
std::istream& lamp_config,
std::istream& sound_config,
std::istream& display_config)
std::istream& solenoid_config,
std::istream& lamp_config,
std::istream& sound_config,
std::istream& display_config)
{
utility::LoggerFactory::CreateOutputLogger();
@@ -45,379 +41,235 @@ std::shared_ptr<OutputDriver> get_OutputDriver(std::istream& output_pin_config,
json output_config;
output_pin_config >> output_config;
json driver_board_config = output_config.at("driver_board");
std::shared_ptr<DriverBoardPinController> driver_board_pin_controller(new detail::DriverBoardPinController(output_pin_mutex));
std::shared_ptr<DriverBoardPinController> driver_board_pin_controller(new DriverBoardPinController(output_pin_mutex));
auto solenoids = create_solenoids(solenoid_config, driver_board_pin_controller);
//TODO create flippers
auto lamps = create_lamps(lamp_config, driver_board_pin_controller);
json sound_board_config = output_config.at("sound_board");
std::shared_ptr<SoundBoardPinController> sound_board_pin_controller(new detail::SoundBoardPinController(parse_pins_sound_board(sound_board_config), output_pin_mutex));
uint8_t sound_fire_address = get_sound_fire_address(sound_config);
std::shared_ptr<SoundBoardPinController> sound_board_pin_controller(new detail::SoundBoardPinController(output_pin_mutex, sound_fire_address));
auto sounds = create_sounds(sound_config, sound_board_pin_controller);
json display_board_config = output_config.at("display_board");
std::unique_ptr<DisplayBoardPinController> display_board_pin_controller(new detail::DisplayBoardPinController(parse_pins_display_board(display_board_config)));
auto displays = create_displays(display_config);
std::unique_ptr<DisplayController> display_controller(new detail::DisplayController(displays, std::move(display_board_pin_controller)));
auto display_map = map_displays(displays);
return std::make_shared<detail::OutputDriver>(std::move(display_controller), solenoids, lamps, sounds, display_map);
// json display_board_config = output_config.at("display_board");
// std::unique_ptr<DisplayBoardPinController> display_board_pin_controller(new detail::DisplayBoardPinController(parse_pins_display_board(display_board_config)));
// auto displays = create_displays(display_config);
//
// std::unique_ptr<DisplayController> display_controller(new detail::DisplayController(displays, std::move(display_board_pin_controller)));
//
// auto display_map = map_displays(displays);
//
// return std::make_shared<detail::OutputDriver>(std::move(display_controller), solenoids, lamps, sounds, display_map);
// return std::make_shared<detail::OutputDriver>(NULL, NULL, NULL, NULL, NULL);
}
std::map<std::string, uint8_t> parse_pins_sound_board(json &sound_board_config)
std::map<std::string, std::shared_ptr<items::Solenoid>> create_solenoids(std::istream & solenoid_config, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
std::map<std::string, uint8_t> pins_sound;
try
{
pins_sound["i2c_address"] = sound_board_config.at("i2c_address").get<uint8_t>();
pins_sound["pin_base"] = sound_board_config.at("pin_base").get<uint8_t>();
pins_sound["fire"] = sound_board_config.at("fire").get<uint8_t>();
json sound_address = sound_board_config.at("sound_address_select");
pins_sound["A"] = sound_address.at("A").get<uint8_t>();
pins_sound["B"] = sound_address.at("B").get<uint8_t>();
pins_sound["C"] = sound_address.at("C").get<uint8_t>();
pins_sound["D"] = sound_address.at("D").get<uint8_t>();
pins_sound["E"] = sound_address.at("E").get<uint8_t>();
pins_sound["F"] = sound_address.at("F").get<uint8_t>();
pins_sound["G"] = sound_address.at("G").get<uint8_t>();
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output pin config file at sound_board corrupted: " << e.what();
exit(EXIT_FAILURE);
}
return pins_sound;
}
std::map<std::string, uint8_t> parse_pins_display_board(json &display_board_config)
{
std::map<std::string, uint8_t> pins_display;
try
{
pins_display["run"] = display_board_config.at("run");
json display_select = display_board_config.at("display_select");
for(json::iterator it = display_select.begin(); it != display_select.end(); it++)
{
pins_display["display_select" + it.key()] = it.value().get<uint8_t>();
}
json segment_select = display_board_config.at("segment_select");
pins_display["segment_select_A"] = segment_select.at("A").get<uint8_t>();
pins_display["segment_select_B"] = segment_select.at("B").get<uint8_t>();
pins_display["segment_select_C"] = segment_select.at("C").get<uint8_t>();
json digit_select = display_board_config.at("digit_select");
pins_display["digit_select_A"] = digit_select.at("A").get<uint8_t>();
pins_display["digit_select_B"] = digit_select.at("B").get<uint8_t>();
pins_display["digit_select_C"] = digit_select.at("C").get<uint8_t>();
pins_display["digit_select_D"] = digit_select.at("D").get<uint8_t>();
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output pin config file at display_board corrupted: " << e.what();
exit(EXIT_FAILURE);
}
return pins_display;
}
std::map<std::string, std::shared_ptr<items::Solenoid>> create_solenoids(std::istream &solenoid_config, std::shared_ptr<DriverBoardPinController> &output_gpio_interface)
{
std::map<std::string, std::shared_ptr<items::Solenoid>> solenoids;
json solenoid_config_json;
solenoid_config >> solenoid_config_json;
auto deactivation_time = get_deactivation_time(solenoid_config_json);
json port_extenders = get_element("port_extenders", solenoid_config_json, "");
initialize_port_extenders(solenoid_config_json, output_gpio_interface);
initialize_port_extenders(port_extenders, pin_controller.get(), "solenoid_config.json");
json solenoids_json;
try
{
solenoids_json = solenoid_config_json.at("solenoids");
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output solenoids config file corrupted: Key \"solenoids\" of type array needed. \n" << e.what();
exit(EXIT_FAILURE);
}
json solenoids_json = get_element("solenoids", solenoid_config_json, "solenoid_config.json");
std::chrono::milliseconds deactivation_time{ get_value<int>("deactivation_time_milliseconds", solenoid_config_json, "solenoid_config.json") };
std::map<std::string, std::shared_ptr<items::Solenoid>> solenoids;
for(auto &solenoid_json : solenoids_json)
{
auto solenoid = create_solenoid(solenoid_json, output_gpio_interface, deactivation_time);
auto solenoid = create_solenoid(solenoid_json, port_extenders, pin_controller, deactivation_time);
solenoids.emplace(solenoid->get_name(), solenoid);
}
return solenoids;
}
std::map<std::string, std::shared_ptr<items::Flipper>> create_flippers(std::istream &flipper_config, std::shared_ptr<DriverBoardPinController> &output_gpio_interface)
std::shared_ptr<items::detail::Solenoid> create_solenoid(nlohmann::json &solenoid_json, nlohmann::json &port_extenders, std::shared_ptr<DriverBoardPinController> &pin_controller, std::chrono::milliseconds deactivation_time)
{
std::map<std::string, std::shared_ptr<items::Flipper>> flippers;
std::string config_file_name = "solenoid_config.json";
json flipper_config_json;
flipper_config >> flipper_config_json;
uint8_t pin_base = get_pin_base(solenoid_json, port_extenders, config_file_name);
json flippers_json;
try
{
flippers_json = flipper_config_json.at("flippers");
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output solenoids config file corrupted: Key \"flippers\" of type array needed. \n" << e.what();
exit(EXIT_FAILURE);
}
for(auto &flipper_json : flippers_json)
{
auto flipper = create_flipper(flipper_json, output_gpio_interface);
flippers.emplace(flipper->get_name(), flipper);
}
return flippers;
}
std::shared_ptr<items::detail::Flipper> create_flipper(nlohmann::json &flipper_json, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
try
{
std::string name = flipper_json.at("name");
auto address = flipper_json.at("address").get<uint8_t>();
return std::make_shared<items::detail::Flipper>(pin_controller, address, name);
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output solenoid config file corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
std::map<std::string, std::shared_ptr<items::Lamp>> create_lamps(std::istream &lamp_config, std::shared_ptr<DriverBoardPinController> &output_gpio_interface)
{
//TODO initialize portextender
std::map<std::string, std::shared_ptr<items::Lamp>> lamps;
json lamp_config_json;
lamp_config >> lamp_config_json;
json lamps_json;
try
{
lamps_json = lamp_config_json.at("lamps");
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output lamp config file corrupted: Key \"lamps\" of type array needed. \n" << e.what();
exit(EXIT_FAILURE);
}
for(auto &lamp_json : lamps_json)
{
auto lamp = create_lamp(lamp_json, output_gpio_interface);
lamps.emplace(lamp->get_name(), lamp);
}
return lamps;
}
std::map<std::string, std::shared_ptr<items::Sound>> create_sounds(std::istream &sound_config, std::shared_ptr<SoundBoardPinController> &output_gpio_interface)
{
std::map<std::string, std::shared_ptr<items::Sound>> sounds;
json sounds_config_json;
sound_config >> sounds_config_json;
auto deactivation_time = get_deactivation_time(sounds_config_json);
json sounds_json;
try
{
sounds_json = sounds_config_json.at("sounds");
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output sound config file corrupted: Key \"sounds\" of type array needed. \n" << e.what();
exit(EXIT_FAILURE);
}
for(auto &sound_json : sounds_json)
{
auto sound = create_sound(sound_json, output_gpio_interface, deactivation_time);
sounds.emplace(sound->Item::get_name(), sound);
}
return sounds;
}
void initialize_port_extenders(nlohmann::json &json_stream, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
json port_extenders;
try
{
port_extenders = json_stream.at("port_extenders");
}
catch (json::exception & e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output solenoids config file corrupted: Key \"port_extenders\" of type array needed. \n" << e.what();
exit(EXIT_FAILURE);
}
for (auto & extender_json : port_extenders)
{
pin_controller->initialize_port_expander(extender_json.at("i2c_address").get<uint8_t>(), extender_json.at("pin_base").get<uint8_t>());
}
}
std::chrono::milliseconds get_deactivation_time(nlohmann::json &json)
{
try
{
auto deactivation_time = json.at("deactivation_time_milliseconds").get<uint>();
return std::chrono::milliseconds(deactivation_time);
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output config file at deactivation_time_milliseconds corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
std::shared_ptr<items::detail::Solenoid> create_solenoid(nlohmann::json &solenoid_json, std::shared_ptr<DriverBoardPinController> &pin_controller, std::chrono::milliseconds &deactivation_time)
{
try
{
uint8_t address = 0;
if(solenoid_json.find("extender") != solenoid_json.end())
{
address += solenoid_json.at("extender").get<uint8_t>();
}
std::string name = solenoid_json.at("name");
address += solenoid_json.at("address").get<uint8_t>();
auto name = get_value<std::string>("name", solenoid_json, config_file_name);
auto address = get_value<uint8_t>("address", solenoid_json, config_file_name);
if(solenoid_json.find("deactivation_time_milliseconds") != solenoid_json.end())
{
deactivation_time = get_deactivation_time(solenoid_json);
deactivation_time = std::chrono::milliseconds(get_value<uint8_t>("deactivation_time_milliseconds", solenoid_json, config_file_name));
}
return std::make_shared<items::detail::Solenoid>(pin_controller, address, name, deactivation_time);
}
catch(json::exception &e)
return std::make_shared<items::detail::Solenoid>(pin_controller, address, pin_base, name, deactivation_time);
}
std::map<std::string, std::shared_ptr<items::Flipper>> create_flippers(std::istream &solenoid_config, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
json solenoid_config_json;
solenoid_config >> solenoid_config_json;
json port_extenders = get_element("port_extenders", solenoid_config_json, "solenoid_config.json");
json flippers_json = get_element("flippers", solenoid_config_json, "solenoid_config.json");
std::chrono::milliseconds deactivation_time{ get_value<int>("deactivation_time_milliseconds", solenoid_config_json, "solenoid_config.json") };
std::map<std::string, std::shared_ptr<items::Flipper>> flippers;
for(auto &flipper_json : flippers_json)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output solenoid config file corrupted: " << e.what();
exit(EXIT_FAILURE);
auto flipper = create_flipper(flipper_json, port_extenders, pin_controller);
flippers.emplace(flipper->get_name(), flipper);
}
return flippers;
}
std::shared_ptr<items::detail::Flipper> create_flipper(nlohmann::json &flipper_json, json &port_extenders, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
std::string config_file_name = "solenoid_config.json";
uint8_t pin_base = get_pin_base(flipper_json, port_extenders, config_file_name);
auto address = get_value<uint8_t>("address", flipper_json, config_file_name);
auto name = get_value<std::string>("name", flipper_json, config_file_name);
return std::make_shared<items::detail::Flipper>(pin_controller, address, pin_base, name);
}
std::map<std::string, std::shared_ptr<items::Lamp>> create_lamps(std::istream &lamp_config, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
json lamp_config_json;
lamp_config >> lamp_config_json;
json port_extenders = get_element("port_extenders", lamp_config_json, "lamp_config.json");
initialize_port_extenders(port_extenders, pin_controller.get(), "lamp_config.json");
json solenoids_json = get_element("lamps", lamp_config_json, "lamp_config.json");
std::map<std::string, std::shared_ptr<items::Lamp>> lamps;
for(auto &solenoid_json : solenoids_json)
{
auto solenoid = create_lamp(solenoid_json, port_extenders, pin_controller);
lamps.emplace(solenoid->get_name(), solenoid);
}
return lamps;
}
std::shared_ptr<items::detail::Lamp> create_lamp(json &lamp_json, json & port_extenders, std::shared_ptr<DriverBoardPinController> &pin_controller)
{
std::string config_file_name = "lamps_config.json";
uint8_t pin_base = get_pin_base(lamp_json, port_extenders, config_file_name);
auto address = get_value<uint8_t>("address", lamp_json, config_file_name);
auto name = get_value<std::string>("name", lamp_json, config_file_name);
return std::make_shared<items::detail::Lamp>(pin_controller, address, pin_base, name);
}
std::map<std::string, std::shared_ptr<items::Sound>> create_sounds(std::istream &sound_config, std::shared_ptr<SoundBoardPinController> &pin_controller)
{
json sound_config_json;
sound_config >> sound_config_json;
json port_extenders = get_element("port_extenders", sound_config_json, "sound_config.json");
initialize_port_extenders(port_extenders, pin_controller.get(), "sound_config.json");
std::chrono::milliseconds deactivation_time{ get_value<int>("deactivation_time_milliseconds", sound_config_json, "solenoid_config.json") };
json solenoids_json = get_element("sounds", sound_config_json, "sound_config.json");
std::map<std::string, std::shared_ptr<items::Sound>> sounds;
for(auto &solenoid_json : solenoids_json)
{
auto solenoid = create_sound(solenoid_json, port_extenders, pin_controller, deactivation_time);
sounds.emplace(solenoid->get_name(), solenoid);
}
return sounds;
}
std::shared_ptr<items::detail::Sound> create_sound(json &sound_json, json &port_extenders, std::shared_ptr<SoundBoardPinController> &pin_controller, std::chrono::milliseconds &deactivation_time)
{
std::string config_file_name = "sound_config.json";
uint8_t pin_base = get_pin_base(sound_json, port_extenders, config_file_name);
auto address = get_value<uint8_t>("address", sound_json, config_file_name);
auto name = get_value<std::string>("name", sound_json, config_file_name);
auto id = get_value<uint>("id", sound_json, config_file_name);
return std::make_shared<items::detail::Sound>(pin_controller, address, pin_base, name, deactivation_time, id);
}
uint8_t get_sound_fire_address(std::istream &sound_config)
{
json sound_config_json;
sound_config >> sound_config_json;
json port_extenders = get_element("port_extenders", sound_config_json, "sound_config.json");
json fire_pin = get_element("fire_pin", sound_config_json, "sound_config.json");
auto pin_base = get_pin_base(fire_pin, port_extenders, "sound_config.json");
auto address = get_value<uint8_t>("address", fire_pin, "sound_config.json");
return pin_base + address;
}
void initialize_port_extenders(nlohmann::json &port_extenders, PinController * pin_controller, const std::string & file_name)
{
for (auto & extender_json : port_extenders)
{
auto i2c_address = get_value<uint8_t>("i2c_address", extender_json, file_name);
auto pin_base = get_value<uint8_t>("pin_base", extender_json, file_name);
pin_controller->initialize_port_expander(i2c_address, pin_base);
}
}
std::shared_ptr<items::detail::Lamp> create_lamp(nlohmann::json &lamp_json, std::shared_ptr<DriverBoardPinController> &pin_controller)
uint8_t get_pin_base(json & object, json & port_extenders, const std::string & config_file_name)
{
try
uint8_t pin_base{ 0 };
if(object.find("extender") != object.end())
{
std::string name = lamp_json.at("name");
auto address = lamp_json.at("address").get<uint8_t>();
return std::make_shared<items::detail::Lamp>(pin_controller, address, name);
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output lamp config file corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
std::shared_ptr<items::detail::Sound> create_sound(nlohmann::json &sound_json, std::shared_ptr<SoundBoardPinController> &pin_controller, std::chrono::milliseconds &deactivation_time)
{
try
{
auto id = sound_json.at("id").get<uint>();
std::string description = sound_json.at("description");
auto address = sound_json.at("address").get<uint8_t>();
return std::make_shared<items::detail::Sound>(pin_controller, address, description, deactivation_time, id);
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output sound config file corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
std::vector<std::shared_ptr<items::OutputDisplay>> create_displays(std::istream &display_config)
{
std::vector<std::shared_ptr<items::OutputDisplay>> displays;
json display_config_json;
display_config >> display_config_json;
json displays_json;
try
{
displays_json = display_config_json.at("displays");
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output display config file corrupted: Key \"displays\" of type array needed. \n" << e.what();
exit(EXIT_FAILURE);
}
for(json &display_json : displays_json)
{
auto display = create_display(display_json);
displays.push_back(display);
}
return displays;
}
std::shared_ptr<items::OutputDisplay> create_display(nlohmann::json &display_json)
{
try
{
auto id = display_json.at("id").get<uint8_t>();
auto address = display_json.at("address").get<uint8_t>();
auto digits = display_json.at("digits").get<uint8_t>();
if(digits == 8)
auto extender_name = get_value<std::string>("extender", object, config_file_name);
for (auto & extender : port_extenders)
{
return std::make_shared<items::detail::EightDigitDisplay>(address, id);
}
else if(digits == 7)
{
return std::make_shared<items::detail::SevenDigitDisplay>(address, id);
}
else
{
throw new std::logic_error("Display digits can either be 7 or 8");
auto actual_extender = get_value<std::string>("name", extender, config_file_name);
if (actual_extender == extender_name)
{
pin_base = get_value<uint8_t>("pin_base", extender, config_file_name);
}
}
}
return pin_base;
}
json get_element(const std::string & name, json & object, const std::string & file_name)
{
json sub_object;
try
{
sub_object = object.at(name);
return sub_object;
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "Output display config file corrupted: " << e.what();
CLOG(ERROR, OUTPUT_LOGGER) << "File " << file_name << " seems to be corrupted at " << name << ": " << e.what();
exit(EXIT_FAILURE);
}
}
std::map<uint8_t, std::shared_ptr<items::Display>> map_displays(const std::vector<std::shared_ptr<items::OutputDisplay>> &displays)
template<typename type>
type get_value(const std::string & name, json & object, const std::string & file_name)
{
std::map<uint8_t, std::shared_ptr<items::Display>> display_map;
for(auto &display : displays)
try
{
display_map.emplace(display->get_address(), display);
type element = object.at(name).get<type>();
return element;
}
catch(json::exception &e)
{
CLOG(ERROR, OUTPUT_LOGGER) << "File " << file_name << " seems to be corrupted at " << name << ": " << e.what();
exit(EXIT_FAILURE);
}
return display_map;
}
}