implementing displays and refactor fail

This commit is contained in:
Jonas Zeunert
2018-12-14 14:20:11 +01:00
parent f9edf2f8cc
commit 49cd625808
20 changed files with 100 additions and 48 deletions

View File

@@ -0,0 +1,10 @@
{
"displays" :
[
{
"digits" : 8,
"id" : 1,
"address" : 1
}
]
}

View File

@@ -10,7 +10,7 @@
#include "output/items/impl/Lamp.h" #include "output/items/impl/Lamp.h"
#include "output/items/Solenoid.h" #include "output/items/Solenoid.h"
#include "output/items/Display.h" #include "output/items/OutputDisplay.h"
#include "output/items/Sound.h" #include "output/items/Sound.h"
#include <vector> #include <vector>

View File

@@ -15,12 +15,11 @@ namespace output
namespace items namespace items
{ {
class IOutputDisplay class Display
{ {
public: public:
virtual ~IOutputDisplay() virtual ~IOutputDisplay() = default;
{};
virtual void write_score(int score) = 0; virtual void write_score(int score) = 0;
}; };

View File

@@ -5,7 +5,7 @@
#ifndef FLIPPR_DRIVER_IEIGHTDIGITDISPLAY_H #ifndef FLIPPR_DRIVER_IEIGHTDIGITDISPLAY_H
#define FLIPPR_DRIVER_IEIGHTDIGITDISPLAY_H #define FLIPPR_DRIVER_IEIGHTDIGITDISPLAY_H
#include "OutputDisplay.h" #include "Display.h"
#include <array> #include <array>
@@ -16,7 +16,7 @@ namespace output
namespace items namespace items
{ {
class IEightDigitDisplay : public IOutputDisplay class EightDigitDisplay : public Display
{ {
public: public:
virtual void write_content(std::array<char, 8> content) = 0; virtual void write_content(std::array<char, 8> content) = 0;

View File

@@ -5,7 +5,7 @@
#ifndef FLIPPR_DRIVER_ISEVENDIGITDISPLAY_H #ifndef FLIPPR_DRIVER_ISEVENDIGITDISPLAY_H
#define FLIPPR_DRIVER_ISEVENDIGITDISPLAY_H #define FLIPPR_DRIVER_ISEVENDIGITDISPLAY_H
#include "OutputDisplay.h" #include "Display.h"
#include <array> #include <array>
@@ -16,7 +16,7 @@ namespace output
namespace items namespace items
{ {
class ISevenDigitDisplay : public IOutputDisplay class SevenDigitDisplay : public Display
{ {
public: public:
virtual void write_content(std::array<char, 7> content) = 0; virtual void write_content(std::array<char, 7> content) = 0;

View File

@@ -5,7 +5,7 @@
#ifndef FLIPPR_DRIVER_DISPLAYPINCONTROLLER_H #ifndef FLIPPR_DRIVER_DISPLAYPINCONTROLLER_H
#define FLIPPR_DRIVER_DISPLAYPINCONTROLLER_H #define FLIPPR_DRIVER_DISPLAYPINCONTROLLER_H
#include "output/items/Display.h" #include "output/items/OutputDisplay.h"
namespace flippR_driver namespace flippR_driver
{ {

View File

@@ -135,11 +135,12 @@ std::map<std::string, std::shared_ptr<items::Solenoid>> create_solenoids(std::is
{ {
std::map<std::string, std::shared_ptr<items::Solenoid>> solenoids; std::map<std::string, std::shared_ptr<items::Solenoid>> solenoids;
json solenoids_json; json solenoid_config_json;
solenoid_config >> solenoids_json; solenoid_config >> solenoid_config_json;
auto deactivation_time = get_deactivation_time(solenoids_json); auto deactivation_time = get_deactivation_time(solenoid_config_json);
json solenoids_json = solenoid_config_json.at("solenoids");
for(auto &solenoid_json : solenoids_json) for(auto &solenoid_json : solenoids_json)
{ {
auto solenoid = create_solenoid(solenoid_json, output_gpio_interface, deactivation_time); auto solenoid = create_solenoid(solenoid_json, output_gpio_interface, deactivation_time);
@@ -153,9 +154,10 @@ std::map<std::string, std::shared_ptr<items::Lamp>> create_lamps(std::istream &l
{ {
std::map<std::string, std::shared_ptr<items::Lamp>> lamps; std::map<std::string, std::shared_ptr<items::Lamp>> lamps;
json lamps_json; json lamp_config_json;
lamp_config >> lamps_json; lamp_config >> lamp_config_json;
json lamps_json = lamp_config_json.at("lamps");
for(auto &lamp_json : lamps_json) for(auto &lamp_json : lamps_json)
{ {
auto lamp = create_lamp(lamp_json, output_gpio_interface); auto lamp = create_lamp(lamp_json, output_gpio_interface);
@@ -169,11 +171,12 @@ std::map<std::string, std::shared_ptr<items::Sound>> create_sounds(std::istream
{ {
std::map<std::string, std::shared_ptr<items::Sound>> sounds; std::map<std::string, std::shared_ptr<items::Sound>> sounds;
json sounds_json; json sounds_config_json;
sound_config >> sounds_json; sound_config >> sounds_config_json;
auto deactivation_time = get_deactivation_time(sounds_json); auto deactivation_time = get_deactivation_time(sounds_config_json);
json sounds_json = sounds_config_json.at("sounds"); // todo catch
for(auto &sound_json : sounds_json) for(auto &sound_json : sounds_json)
{ {
auto sound = create_sound(sound_json, output_gpio_interface, deactivation_time); auto sound = create_sound(sound_json, output_gpio_interface, deactivation_time);
@@ -190,7 +193,7 @@ std::chrono::milliseconds get_deactivation_time(nlohmann::json &json)
auto deactivation_time = json.at("deactivation_time_milliseconds").get<uint>(); auto deactivation_time = json.at("deactivation_time_milliseconds").get<uint>();
return std::chrono::milliseconds(deactivation_time); return std::chrono::milliseconds(deactivation_time);
} }
catch(json::type_error &e) catch(json::exception &e)
{ {
// todo log and exit // todo log and exit
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -211,7 +214,7 @@ std::shared_ptr<items::impl::Solenoid> create_solenoid(nlohmann::json &solenoid_
return std::make_shared<items::impl::Solenoid>(pin_controller, address, name, deactivation_time); return std::make_shared<items::impl::Solenoid>(pin_controller, address, name, deactivation_time);
} }
catch(json::type_error &e) catch(json::exception &e)
{ {
// todo log and exit // todo log and exit
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -226,7 +229,7 @@ std::shared_ptr<items::impl::Lamp> create_lamp(nlohmann::json &lamp_json, std::s
auto address = lamp_json.at("address").get<uint8_t>(); auto address = lamp_json.at("address").get<uint8_t>();
return std::make_shared<items::impl::Lamp>(pin_controller, address, name); return std::make_shared<items::impl::Lamp>(pin_controller, address, name);
} }
catch(json::type_error &e) catch(json::exception &e)
{ {
// todo log and exit // todo log and exit
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -242,7 +245,7 @@ std::shared_ptr<items::impl::Sound> create_sound(nlohmann::json &sound_json, std
auto address = sound_json.at("address").get<uint8_t>(); auto address = sound_json.at("address").get<uint8_t>();
return std::make_shared<items::impl::Sound>(pin_controller, address, description, deactivation_time, id); return std::make_shared<items::impl::Sound>(pin_controller, address, description, deactivation_time, id);
} }
catch(json::type_error &e) catch(json::exception &e)
{ {
// todo log and exit // todo log and exit
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -253,9 +256,39 @@ std::map<uint8_t, std::shared_ptr<items::Display>> create_displays(std::istream
{ {
std::map<uint8_t, std::shared_ptr<items::Display>> displays; std::map<uint8_t, std::shared_ptr<items::Display>> displays;
json display_config_json;
display_config >> display_config_json;
json displays_json = display_config_json.at('displays');
for(json &display_json : displays_json)
{
auto display = create_display(display_json);
}
return displays; return displays;
} }
std::shared_ptr<items::Display> 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)
{
return std::make_shared<items::impl::EightDigitDisplay>(address, id);
}
}
catch(json::exception &e)
{
exit(EXIT_FAILURE);
}
return std::shared_ptr<items::Display>();
}
} }
} }
} }

View File

@@ -37,6 +37,7 @@ namespace OutputDriverFactory
std::chrono::milliseconds get_deactivation_time(nlohmann::json &json); std::chrono::milliseconds get_deactivation_time(nlohmann::json &json);
std::map<uint8_t, std::shared_ptr<items::Display>> create_displays(std::istream &display_config); std::map<uint8_t, std::shared_ptr<items::Display>> create_displays(std::istream &display_config);
std::shared_ptr<items::Display> create_display(nlohmann::json &display_json);
std::map<std::string, uint8_t> parse_pins_driver_board(nlohmann::json &driver_board_config); std::map<std::string, uint8_t> parse_pins_driver_board(nlohmann::json &driver_board_config);
std::map<std::string, uint8_t> parse_pins_sound_board(nlohmann::json &sound_board_config); std::map<std::string, uint8_t> parse_pins_sound_board(nlohmann::json &sound_board_config);

View File

@@ -13,7 +13,7 @@
#include <vector> #include <vector>
#include <thread> #include <thread>
#include "output/items/Display.h" #include "output/items/OutputDisplay.h"
#include "output/DisplayBoardPinController.h" #include "output/DisplayBoardPinController.h"
namespace flippR_driver namespace flippR_driver

View File

@@ -8,7 +8,7 @@
#ifndef _SRC_OUTPUT_DISPLAY_H_ #ifndef _SRC_OUTPUT_DISPLAY_H_
#define _SRC_OUTPUT_DISPLAY_H_ #define _SRC_OUTPUT_DISPLAY_H_
#include "output/items/Display.h" #include "output/items/OutputDisplay.h"
#include <array> #include <array>
@@ -25,7 +25,7 @@ template<int DigitCount>
class Display : public items::Display class Display : public items::Display
{ {
public: public:
Display(int address, int id); Display(uint8_t address, uint8_t id);
virtual ~Display() = default; virtual ~Display() = default;
virtual void write_score(int score); virtual void write_score(int score);
@@ -38,7 +38,8 @@ public:
std::array<uint8_t, DigitCount> content; std::array<uint8_t, DigitCount> content;
private: private:
int address; const uint8_t id;
const uint8_t address;
std::string fit_string(std::string &score_string); std::string fit_string(std::string &score_string);
}; };

View File

@@ -17,7 +17,7 @@ namespace impl
{ {
template<int DigitCount> template<int DigitCount>
Display<DigitCount>::Display(int address, int id) : Display<DigitCount>::Display(uint8_t address, uint8_t id) :
address(address), address(address),
id(id) id(id)
{ {

View File

@@ -2,8 +2,8 @@
// Created by rhetenor on 20.11.18. // Created by rhetenor on 20.11.18.
// //
#ifndef FLIPPR_DRIVER_EIGHTDIGITDISPLAY_H #ifndef FLIPPR_DRIVER_OUTPUT_ITEMS_IMPL_EIGHTDIGITDISPLAY_H
#define FLIPPR_DRIVER_EIGHTDIGITDISPLAY_H #define FLIPPR_DRIVER_OUTPUT_ITEMS_IMPL_EIGHTDIGITDISPLAY_H
#include "output/items/EightDigitDisplay.h" #include "output/items/EightDigitDisplay.h"
@@ -16,7 +16,15 @@ namespace items
namespace impl namespace impl
{ {
class EightDigitDisplay : public Display<8>, IEightDigitDisplay; class EightDigitDisplay : public Display<8>, public EightDigitDisplay
{
public:
EightDigitDisplay(uint8_t address, uint8_t id) :
Display<8>(address, id) {}
~EightDigitDisplay() override = default;
};
} }
} }