Refactores item factories
This commit is contained in:
@@ -16,7 +16,7 @@ namespace output
|
||||
namespace items
|
||||
{
|
||||
|
||||
class EightDigitDisplay : public Display
|
||||
class EightDigitDisplay : public virtual Display
|
||||
{
|
||||
public:
|
||||
virtual ~EightDigitDisplay() = default;
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace output
|
||||
namespace items
|
||||
{
|
||||
|
||||
class SevenDigitDisplay : public Display
|
||||
class SevenDigitDisplay : public virtual Display
|
||||
{
|
||||
public:
|
||||
virtual ~SevenDigitDisplay() = default;
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
#include "output/detail/DisplayBoardPinController.h"
|
||||
#include "output/detail/DriverBoardPinController.h"
|
||||
|
||||
#include "output/items/detail/EightDigitDisplay.h"
|
||||
#include "output/items/detail/SevenDigitDisplay.h"
|
||||
#include "output/items/Flipper.h"
|
||||
|
||||
namespace flippR_driver
|
||||
@@ -39,23 +37,23 @@ std::shared_ptr<OutputDriver> get_OutputDriver(const std::string & solenoid_conf
|
||||
auto output_pin_mutex = std::make_shared<std::mutex>();
|
||||
std::shared_ptr<DriverBoardPinController> driver_board_pin_controller(new detail::DriverBoardPinController(output_pin_mutex));
|
||||
|
||||
auto flippers = get_items<FlipperFactory, detail::DriverBoardPinController>(solenoid_config_path, driver_board_pin_controller);
|
||||
auto solenoids = get_items<SolenoidFactory, detail::DriverBoardPinController>(solenoid_config_path, driver_board_pin_controller);
|
||||
auto lamps = get_items<LampFactory, detail::DriverBoardPinController>(lamp_config_path, driver_board_pin_controller);
|
||||
auto flippers = get_items<FlipperFactory, detail::DriverBoardPinController, items::Flipper>(solenoid_config_path, driver_board_pin_controller);
|
||||
auto solenoids = get_items<SolenoidFactory, detail::DriverBoardPinController, items::Solenoid>(solenoid_config_path, driver_board_pin_controller);
|
||||
auto lamps = get_items<LampFactory, detail::DriverBoardPinController, items::Lamp>(lamp_config_path, driver_board_pin_controller);
|
||||
|
||||
auto sound_board_pin_controller = std::make_shared<detail::SoundBoardPinController>(output_pin_mutex);
|
||||
auto sounds = get_items<SoundFactory, detail::SoundBoardPinController>(sound_config_path, sound_board_pin_controller);
|
||||
auto sounds = get_items<SoundFactory, detail::SoundBoardPinController, items::Sound>(sound_config_path, sound_board_pin_controller);
|
||||
|
||||
auto display_board_pin_controller = std::make_shared<detail::DisplayBoardPinController>();
|
||||
auto displays = get_items<DisplayFactory, detail::DisplayBoardPinController>(display_config_path, display_board_pin_controller);
|
||||
auto displays = get_items<DisplayFactory, detail::DisplayBoardPinController, items::Display>(display_config_path, display_board_pin_controller);
|
||||
|
||||
auto display_controller = std::make_unique<detail::DisplayController>(displays, display_board_pin_controller);
|
||||
|
||||
//return std::make_shared<OutputDriver>(std::move(display_controller), solenoids, lamps, sounds, flippers, displays);
|
||||
return std::make_shared<OutputDriver>(std::move(display_controller), solenoids, lamps, sounds, flippers, displays);
|
||||
}
|
||||
|
||||
template<typename FactoryType, typename ControllerType>
|
||||
std::map<std::string, std::shared_ptr<items::Item>> get_items(const std::string &config_path, std::shared_ptr<PinController> pin_controller)
|
||||
template<typename FactoryType, typename ControllerType, typename ItemType>
|
||||
std::map<std::string, std::shared_ptr<ItemType>> get_items(const std::string &config_path, std::shared_ptr<PinController> pin_controller)
|
||||
{
|
||||
std::ifstream config_stream{config_path};
|
||||
nlohmann::json config_json;
|
||||
@@ -63,7 +61,7 @@ std::map<std::string, std::shared_ptr<items::Item>> get_items(const std::string
|
||||
|
||||
FactoryType factory{config_json, std::static_pointer_cast<ControllerType>(pin_controller)};
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Item>> map;
|
||||
std::map<std::string, std::shared_ptr<ItemType>> map;
|
||||
try{
|
||||
map = factory.getItemMap();
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ std::shared_ptr<OutputDriver> get_OutputDriver(const std::string & solenoid_conf
|
||||
const std::string & sound_config_path,
|
||||
const std::string & display_config_path);
|
||||
|
||||
template<typename FactoryType, typename ControllerType>
|
||||
std::map<std::string, std::shared_ptr<items::Item>> get_items(const std::string & config_path, std::shared_ptr<PinController> pin_controller);
|
||||
template<typename FactoryType, typename ControllerType, typename ItemType>
|
||||
std::map<std::string, std::shared_ptr<ItemType>> get_items(const std::string & config_path, std::shared_ptr<PinController> pin_controller);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace output
|
||||
namespace detail
|
||||
{
|
||||
|
||||
DisplayController::DisplayController(std::map<std::string, std::shared_ptr<items::Item>> & displays,
|
||||
DisplayController::DisplayController(std::map<std::string, std::shared_ptr<items::Display>> & displays,
|
||||
std::shared_ptr<DisplayBoardPinController> pin_controller
|
||||
)
|
||||
: pin_controller{pin_controller}, is_running(true)
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace detail
|
||||
class DisplayController : public output::DisplayController
|
||||
{
|
||||
public:
|
||||
explicit DisplayController(std::map<std::string, std::shared_ptr<items::Item>> &displays, std::shared_ptr<DisplayBoardPinController> pin_controller);
|
||||
explicit DisplayController(std::map<std::string, std::shared_ptr<items::Display>> &displays, std::shared_ptr<DisplayBoardPinController> pin_controller);
|
||||
~DisplayController() override;
|
||||
|
||||
void activate_displays() const override;
|
||||
|
||||
@@ -23,9 +23,9 @@ DisplayFactory::DisplayFactory(nlohmann::json &object, std::shared_ptr<DisplayBo
|
||||
pin_controller->set_pin_map(this->pin_map);
|
||||
}
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Item>> DisplayFactory::getItemMap()
|
||||
std::map<std::string, std::shared_ptr<items::Display>> DisplayFactory::getItemMap()
|
||||
{
|
||||
std::map<std::string, std::shared_ptr<items::Item>> display_map;
|
||||
std::map<std::string, std::shared_ptr<items::Display>> display_map;
|
||||
auto displays = this->object.at(config_path::display_list);
|
||||
for (auto & display : displays)
|
||||
{
|
||||
@@ -33,9 +33,9 @@ std::map<std::string, std::shared_ptr<items::Item>> DisplayFactory::getItemMap()
|
||||
auto address = display.at(config_path::item_address).get<uint8_t>();
|
||||
auto digits = display.at(config_path::display_digits).get<uint8_t>();
|
||||
if (digits == 8)
|
||||
display_map.emplace(std::string{static_cast<char>(id)}, std::make_shared<items::detail::EightDigitDisplay>(address, id));
|
||||
display_map.emplace(std::string{static_cast<char>(id)}, std::dynamic_pointer_cast<items::Display>(std::make_shared<items::detail::EightDigitDisplay>(address, id)));
|
||||
else if (digits == 7)
|
||||
display_map.emplace(std::string{static_cast<char>(id)}, std::make_shared<items::detail::SevenDigitDisplay>(address, id));
|
||||
display_map.emplace(std::string{static_cast<char>(id)}, std::dynamic_pointer_cast<items::Display>(std::make_shared<items::detail::SevenDigitDisplay>(address, id)));
|
||||
else
|
||||
throw new std::logic_error{"Display digits can either be 7 or 8"};
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@ class DisplayFactory : ItemFactory
|
||||
{
|
||||
public:
|
||||
explicit DisplayFactory(nlohmann::json & object, std::shared_ptr<DisplayBoardPinController> pin_controller);
|
||||
std::map<std::string, std::shared_ptr<items::Item>> getItemMap() override;
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Display>> getItemMap();
|
||||
|
||||
private:
|
||||
void create_pin_map();
|
||||
|
||||
@@ -18,10 +18,10 @@ FlipperFactory::FlipperFactory(nlohmann::json &object, std::shared_ptr<DriverBoa
|
||||
ItemFactory{object, pin_controller}
|
||||
{}
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Item>> FlipperFactory::getItemMap()
|
||||
std::map<std::string, std::shared_ptr<items::Flipper>> FlipperFactory::getItemMap()
|
||||
{
|
||||
auto flippers = this->object.at(config_path::flipper_path);
|
||||
std::map<std::string, std::shared_ptr<items::Item>> flipper_map;
|
||||
std::map<std::string, std::shared_ptr<items::Flipper>> flipper_map;
|
||||
for (auto flipper : flippers)
|
||||
{
|
||||
auto name = flipper.at(config_path::item_name).get<std::string>();
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#define FLIPPR_DRIVER_FLIPPERFACTORY_H
|
||||
|
||||
#include "ItemFactory.h"
|
||||
#include "output/items/Flipper.h"
|
||||
#include <string>
|
||||
|
||||
namespace flippR_driver
|
||||
@@ -20,7 +21,7 @@ class FlipperFactory : ItemFactory
|
||||
{
|
||||
public:
|
||||
explicit FlipperFactory(nlohmann::json & object, std::shared_ptr<DriverBoardPinController> pin_controller);
|
||||
std::map <std::string, std::shared_ptr<items::Item>> getItemMap() override;
|
||||
std::map <std::string, std::shared_ptr<items::Flipper>> getItemMap();
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace config_path
|
||||
|
||||
const char solenoid_path[] = "solenoids";
|
||||
|
||||
const char lamps_path[] = "lamps";
|
||||
|
||||
const char display_board[] = "display_board";
|
||||
const char run_pin[] = "run";
|
||||
const char display_select[] = "display_select";
|
||||
@@ -50,7 +52,7 @@ class ItemFactory
|
||||
public:
|
||||
ItemFactory(nlohmann::json &object, std::shared_ptr<PinController> pin_controller);
|
||||
|
||||
virtual std::map<std::string, std::shared_ptr<items::Item>> getItemMap() = 0;
|
||||
//virtual std::map<std::string, std::shared_ptr<items::Item>> getItemMap() = 0;
|
||||
|
||||
protected:
|
||||
void initialize_port_extender(nlohmann::json & extender);
|
||||
|
||||
@@ -18,15 +18,15 @@ LampFactory::LampFactory(nlohmann::json &object, std::shared_ptr<DriverBoardPinC
|
||||
ItemFactory{object, pin_controller}
|
||||
{}
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Item>> LampFactory::getItemMap()
|
||||
std::map<std::string, std::shared_ptr<items::Lamp>> LampFactory::getItemMap()
|
||||
{
|
||||
auto lamps = this->object.at("lamps");
|
||||
std::map<std::string, std::shared_ptr<items::Item>> lamp_map;
|
||||
auto lamps = this->object.at(config_path::lamps_path);
|
||||
std::map<std::string, std::shared_ptr<items::Lamp>> lamp_map;
|
||||
for (auto lamp : lamps)
|
||||
{
|
||||
auto name = lamp.at("name").get<std::string>();
|
||||
auto address = lamp.at("address").get<uint8_t >();
|
||||
auto extender = lamp.at("extender").get<std::string>();
|
||||
auto name = lamp.at(config_path::item_name).get<std::string>();
|
||||
auto address = lamp.at(config_path::item_address).get<uint8_t >();
|
||||
auto extender = lamp.at(config_path::item_extender).get<std::string>();
|
||||
auto pin_base = this->get_extender_pin_base(extender);
|
||||
auto lamp_item = std::make_shared<items::detail::Lamp>(std::static_pointer_cast<DriverBoardPinController>(this->pin_controller), address, pin_base, name);
|
||||
lamp_map.emplace(name, lamp_item);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "output/DriverBoardPinController.h"
|
||||
#include "ItemFactory.h"
|
||||
#include "output/items/Lamp.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
@@ -20,7 +21,7 @@ class LampFactory : ItemFactory
|
||||
{
|
||||
public:
|
||||
explicit LampFactory(nlohmann::json & object, std::shared_ptr<DriverBoardPinController> pin_controller);
|
||||
std::map<std::string, std::shared_ptr<items::Item>> getItemMap() override;
|
||||
std::map<std::string, std::shared_ptr<items::Lamp>> getItemMap();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -22,10 +22,10 @@ SolenoidFactory::SolenoidFactory(nlohmann::json &object, std::shared_ptr<DriverB
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Item>> SolenoidFactory::getItemMap()
|
||||
std::map<std::string, std::shared_ptr<items::Solenoid>> SolenoidFactory::getItemMap()
|
||||
{
|
||||
auto solenoids = this->object.at(config_path::solenoid_path);
|
||||
std::map<std::string, std::shared_ptr<items::Item>> solenoid_map;
|
||||
std::map<std::string, std::shared_ptr<items::Solenoid>> solenoid_map;
|
||||
|
||||
for (auto solenoid : solenoids)
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "output/DriverBoardPinController.h"
|
||||
#include "ItemFactory.h"
|
||||
#include "output/items/Solenoid.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
@@ -21,7 +22,7 @@ class SolenoidFactory : public ItemFactory
|
||||
public:
|
||||
explicit SolenoidFactory(nlohmann::json &object, std::shared_ptr<DriverBoardPinController> pin_controller);
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Item>> getItemMap() override;
|
||||
std::map<std::string, std::shared_ptr<items::Solenoid>> getItemMap();
|
||||
|
||||
private:
|
||||
uint8_t deactivation_time;
|
||||
|
||||
@@ -25,10 +25,10 @@ SoundFactory::SoundFactory(nlohmann::json &object, std::shared_ptr<SoundBoardPin
|
||||
this->set_address_pins();
|
||||
}
|
||||
|
||||
std::map<std::string, std::shared_ptr<items::Item>> SoundFactory::getItemMap()
|
||||
std::map<std::string, std::shared_ptr<items::Sound>> SoundFactory::getItemMap()
|
||||
{
|
||||
auto sounds = this->object.at(config_path::sound_path);
|
||||
std::map<std::string, std::shared_ptr<items::Item>> sound_map;
|
||||
std::map<std::string, std::shared_ptr<items::Sound>> sound_map;
|
||||
for (auto sound : sounds)
|
||||
{
|
||||
auto name = sound.at(config_path::item_name).get<std::string>();
|
||||
|
||||
@@ -21,7 +21,7 @@ class SoundFactory : ItemFactory
|
||||
{
|
||||
public:
|
||||
explicit SoundFactory(nlohmann::json & object, std::shared_ptr<SoundBoardPinController> pin_controller);
|
||||
std::map<std::string, std::shared_ptr<items::Item>> getItemMap() override;
|
||||
std::map<std::string, std::shared_ptr<items::Sound>> getItemMap();
|
||||
|
||||
private:
|
||||
void set_fire_pin();
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace output
|
||||
namespace items
|
||||
{
|
||||
|
||||
class OutputDisplay : public Display
|
||||
class OutputDisplay : public virtual Display
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace items
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class Display : public items::OutputDisplay
|
||||
class Display : public virtual items::OutputDisplay
|
||||
{
|
||||
public:
|
||||
Display(const uint8_t & address, const uint8_t & id);
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace items
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class EightDigitDisplay : public items::detail::Display, public items::EightDigitDisplay
|
||||
class EightDigitDisplay : public virtual items::detail::Display, public virtual items::EightDigitDisplay
|
||||
{
|
||||
public:
|
||||
EightDigitDisplay(uint8_t address, uint8_t id) :
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace detail
|
||||
Flipper::Flipper(std::shared_ptr<DriverBoardPinController> pin_controller, const uint8_t & address, const uint8_t & pin_base, const std::string & name) :
|
||||
Item(std::move(name)), DriverBoardItem(pin_controller, address, pin_base), pin_controller(std::move(pin_controller))
|
||||
{
|
||||
CLOG(INFO , OUTPUT_LOGGER) << "Created flipper \"" << name << "\" with pin-base " << int(pin_base) << " and address " << int(address);
|
||||
//CLOG(INFO , OUTPUT_LOGGER) << "Created flipper \"" << name << "\" with pin-base " << int(pin_base) << " and address " << int(address);
|
||||
}
|
||||
|
||||
Flipper::~Flipper()
|
||||
@@ -33,13 +33,13 @@ Flipper::~Flipper()
|
||||
|
||||
void Flipper::activate()
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Flipper " << name << "activated";
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Flipper " << name << " activated";
|
||||
this->pin_controller->activate(*this);
|
||||
}
|
||||
|
||||
void Flipper::deactivate()
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Flipper " << name << "deactivated";
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Flipper " << name << " deactivated";
|
||||
this->pin_controller->deactivate(*this);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace items
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class SevenDigitDisplay : public items::detail::Display, public items::SevenDigitDisplay
|
||||
class SevenDigitDisplay : public virtual items::detail::Display, public virtual items::SevenDigitDisplay
|
||||
{
|
||||
public:
|
||||
SevenDigitDisplay(uint8_t address, uint8_t id) :
|
||||
|
||||
Reference in New Issue
Block a user