refactored displays
This commit is contained in:
@@ -21,7 +21,7 @@ class Display
|
||||
public:
|
||||
virtual ~Display() = default;
|
||||
|
||||
virtual void write_score(uint score) = 0;
|
||||
virtual void write_score(unsigned int score) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "output/items/Display.h"
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
@@ -19,7 +19,9 @@ namespace items
|
||||
class EightDigitDisplay : public Display
|
||||
{
|
||||
public:
|
||||
virtual void write_content(std::array<char , 8> content) = 0;
|
||||
virtual ~EightDigitDisplay() = default;
|
||||
|
||||
virtual void write_content(std::string content) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef _SRC_OUTPUT_ILAMP_H_
|
||||
#define _SRC_OUTPUT_ILAMP_H_
|
||||
|
||||
#include "output/items/Item.h"
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace output
|
||||
@@ -19,8 +18,7 @@ namespace items
|
||||
class Lamp
|
||||
{
|
||||
public:
|
||||
~Lamp()
|
||||
{};
|
||||
virtual ~Lamp() = default;
|
||||
|
||||
virtual void activate() = 0;
|
||||
virtual void deactivate() = 0;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include "Display.h"
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
@@ -19,7 +19,9 @@ namespace items
|
||||
class SevenDigitDisplay : public Display
|
||||
{
|
||||
public:
|
||||
virtual void write_content(std::array<char, 7> content) = 0;
|
||||
virtual ~SevenDigitDisplay() = default;
|
||||
|
||||
virtual void write_content(std::string content) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#ifndef _SRC_OUTPUT_ISOLENOID_H_
|
||||
#define _SRC_OUTPUT_ISOLENOID_H_
|
||||
|
||||
#include "output/items/Item.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
@@ -20,8 +19,7 @@ namespace items
|
||||
class Solenoid
|
||||
{
|
||||
public:
|
||||
~Solenoid()
|
||||
{};
|
||||
virtual ~Solenoid() = default;
|
||||
|
||||
virtual void trigger() = 0;
|
||||
};
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
#ifndef _SRC_OUTPUT_ISOUND_H_
|
||||
#define _SRC_OUTPUT_ISOUND_H_
|
||||
|
||||
#include "output/items/Item.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace output
|
||||
@@ -20,8 +18,7 @@ namespace items
|
||||
class Sound
|
||||
{
|
||||
public:
|
||||
Sound();
|
||||
virtual ~Sound();
|
||||
virtual ~Sound() = default;
|
||||
|
||||
virtual void play() = 0;
|
||||
};
|
||||
|
||||
@@ -3,9 +3,6 @@
|
||||
*
|
||||
* Responsible for communicating with the actual GPIO hardware.
|
||||
*
|
||||
* Gets a JSON file with following style:
|
||||
* TODO
|
||||
*
|
||||
* Created on: May 6, 2018
|
||||
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
|
||||
*/
|
||||
|
||||
@@ -65,7 +65,7 @@ std::map<std::string, uint8_t> parse_pins_driver_board(json &driver_board_config
|
||||
|
||||
try
|
||||
{
|
||||
pins_driver_board["i2c_address"] = driver_board_config.at("i2c_address").get<uint8_t>(); // todo dont write into map
|
||||
pins_driver_board["i2c_address"] = driver_board_config.at("i2c_address").get<uint8_t>();
|
||||
pins_driver_board["pin_base"] = driver_board_config.at("pin_base").get<uint8_t>();
|
||||
pins_driver_board["data"] = driver_board_config.at("data").get<uint8_t>();
|
||||
pins_driver_board["CL"] = driver_board_config.at("CL").get<uint8_t>();
|
||||
@@ -95,7 +95,7 @@ std::map<std::string, uint8_t> parse_pins_sound_board(json &sound_board_config)
|
||||
std::map<std::string, uint8_t> pins_sound;
|
||||
try
|
||||
{
|
||||
pins_sound["i2c_address"] = sound_board_config.at("i2c_address").get<uint8_t>(); // todo dont write into map
|
||||
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>();
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ void DisplayBoardPinController::deactivate_displays() const
|
||||
|
||||
void DisplayBoardPinController::write_display(const items::OutputDisplay &display) const
|
||||
{
|
||||
std::vector<uint8_t> content = display.get_content();
|
||||
std::string content = display.get_content();
|
||||
|
||||
for(uint8_t i = 0; i < content.size(); i++)
|
||||
{
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <cstdint>
|
||||
#include <output/items/Display.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace output
|
||||
@@ -27,7 +29,7 @@ public:
|
||||
|
||||
virtual uint8_t get_address() const = 0;
|
||||
virtual uint8_t get_id() const = 0;
|
||||
virtual std::vector<uint8_t> get_content() const = 0;
|
||||
virtual std::string get_content() const = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
76
FlippR-Driver/src/output/items/detail/Display.cpp
Normal file
76
FlippR-Driver/src/output/items/detail/Display.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// Created by rhetenor on 10.10.18.
|
||||
//
|
||||
|
||||
#include "Display.h"
|
||||
#include "utility/config.h"
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace output
|
||||
{
|
||||
namespace items
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
Display::Display(uint8_t address, uint8_t id) :
|
||||
address(address),
|
||||
id(id)
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Created display with id " << id << " and address " << address << ".";
|
||||
}
|
||||
|
||||
uint8_t Display::get_id() const
|
||||
{
|
||||
return this->id;
|
||||
}
|
||||
|
||||
void Display::write_score(unsigned int score, unsigned int length)
|
||||
{
|
||||
auto score_string = std::to_string(score);
|
||||
|
||||
fit_score_string(score_string, length);
|
||||
|
||||
write_content(score_string, length);
|
||||
}
|
||||
|
||||
std::string Display::fit_score_string(std::string &score_string, unsigned int length)
|
||||
{
|
||||
auto score_length = score_string.length();
|
||||
|
||||
if (score_length > length)
|
||||
{
|
||||
CLOG(DEBUG, OUTPUT_LOGGER) << "Score too long for display";
|
||||
std::string full_display;
|
||||
// TODO mach mal schöner hier wird 9999 angezeigt wenn die zahl zu groß is
|
||||
return full_display.insert(0, length, '9');
|
||||
}
|
||||
|
||||
score_string.insert(0, length - score_length, '\0');
|
||||
return score_string;
|
||||
}
|
||||
|
||||
void Display::write_content(std::string content, unsigned int length)
|
||||
{
|
||||
if(content.size() > length)
|
||||
{
|
||||
CLOG(WARNING, OUTPUT_LOGGER) << "Cannot write more than " << length << " digits on " << length << "-Digit Display. Truncating!";
|
||||
content = content.substr(0, length);
|
||||
}
|
||||
|
||||
this->content = content;
|
||||
}
|
||||
|
||||
std::string Display::get_content() const
|
||||
{
|
||||
return this->content;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,25 +21,24 @@ namespace items
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<int DigitCount>
|
||||
class Display : public items::OutputDisplay
|
||||
{
|
||||
public:
|
||||
Display(uint8_t address, uint8_t id);
|
||||
virtual ~Display() = default;
|
||||
|
||||
void write_score(uint score) override;
|
||||
void write_content(std::array<char, DigitCount> content);
|
||||
void write_score(unsigned int score, unsigned int length);
|
||||
void write_content(std::string content, unsigned int length);
|
||||
|
||||
std::vector<uint8_t> get_content() const override;
|
||||
std::string get_content() const override;
|
||||
uint8_t get_address() const override;
|
||||
uint8_t get_id() const override;
|
||||
|
||||
private:
|
||||
std::string fit_string(std::string &score_string);
|
||||
std::string fit_score_string(std::string &score_string, unsigned int length);
|
||||
|
||||
public:
|
||||
std::array<char, DigitCount> content;
|
||||
std::string content;
|
||||
|
||||
private:
|
||||
const uint8_t id;
|
||||
@@ -52,7 +51,5 @@ private:
|
||||
} /* namespace output */
|
||||
}
|
||||
|
||||
#include "Display.hpp"
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
//
|
||||
// Created by rhetenor on 10.10.18.
|
||||
//
|
||||
|
||||
#include "utility/config.h"
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace output
|
||||
{
|
||||
namespace items
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template<int DigitCount>
|
||||
Display<DigitCount>::Display(uint8_t address, uint8_t id) :
|
||||
address(address),
|
||||
id(id)
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Created display with id " << id << " and address " << address << ".";
|
||||
}
|
||||
|
||||
template<int DigitCount>
|
||||
uint8_t Display<DigitCount>::get_id() const
|
||||
{
|
||||
return this->id;
|
||||
}
|
||||
|
||||
template<int DigitCount>
|
||||
void Display<DigitCount>::write_score(uint score)
|
||||
{
|
||||
auto score_string = std::to_string(score);
|
||||
|
||||
std::copy(score_string.begin(), score_string.end(), std::begin(this->content));
|
||||
}
|
||||
|
||||
template<int DigitCount>
|
||||
std::string Display<DigitCount>::fit_string(std::string& score_string)
|
||||
{
|
||||
auto score_length = score_string.length();
|
||||
|
||||
if (score_length > DigitCount)
|
||||
{
|
||||
CLOG(DEBUG, OUTPUT_LOGGER) << "Score too long for display";
|
||||
std::string full_display;
|
||||
// TODO mach mal schöner hier wird 9999 angezeigt wenn die zahl zu groß is
|
||||
return full_display.insert(0, DigitCount, '9');
|
||||
}
|
||||
|
||||
score_string.insert(0, DigitCount-score_length, '\0');
|
||||
return score_string;
|
||||
}
|
||||
|
||||
template<int DigitCount>
|
||||
void Display<DigitCount>::write_content( std::array<char, DigitCount> content)
|
||||
{
|
||||
this->content = content;
|
||||
}
|
||||
|
||||
template <int DigitCount>
|
||||
std::vector<uint8_t> Display<DigitCount>::get_content() const
|
||||
{
|
||||
// todo: expensive?
|
||||
return std::vector<uint8_t>(content.begin(), content.end());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "output/items/EightDigitDisplay.h"
|
||||
|
||||
#include "utility/config.h"
|
||||
|
||||
namespace flippR_driver
|
||||
{
|
||||
namespace output
|
||||
@@ -18,25 +20,26 @@ namespace items
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class EightDigitDisplay : public Display<8>, public items::EightDigitDisplay
|
||||
class EightDigitDisplay : public items::detail::Display, public items::EightDigitDisplay
|
||||
{
|
||||
public:
|
||||
EightDigitDisplay(uint8_t address, uint8_t id) :
|
||||
Display<8>(address, id) {}
|
||||
|
||||
void write_score(uint score) override
|
||||
{
|
||||
Display<8>::write_score(score);
|
||||
}
|
||||
|
||||
void write_content(std::array<char, 8> content) override
|
||||
{
|
||||
Display<8>::write_content(content);
|
||||
}
|
||||
|
||||
detail::Display(address, id) {}
|
||||
|
||||
~EightDigitDisplay() override = default;
|
||||
|
||||
void write_score(unsigned int score) override
|
||||
{
|
||||
detail::Display::write_score(score, 8);
|
||||
}
|
||||
|
||||
void write_content(std::string content) override
|
||||
{
|
||||
detail::Display::write_content(content, 8);
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -26,11 +26,13 @@ DriverBoardItem(std::move(pin_controller), address, std::move(name)), activated(
|
||||
|
||||
void Lamp::activate()
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Activate lamp " << name;
|
||||
pin_controller->activate(*this);
|
||||
}
|
||||
|
||||
void Lamp::deactivate()
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Deactivate lamp " << name;
|
||||
pin_controller->deactivate(*this);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,20 +18,19 @@ namespace items
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class SevenDigitDisplay : public Display<7>, public items::SevenDigitDisplay
|
||||
class SevenDigitDisplay : public items::detail::Display, public items::SevenDigitDisplay
|
||||
{
|
||||
public:
|
||||
SevenDigitDisplay(uint8_t address, uint8_t id) :
|
||||
Display<7>(address, id) {}
|
||||
detail::Display(address, id) {}
|
||||
|
||||
void write_score(uint score) override
|
||||
void write_score(unsigned int score) override
|
||||
{
|
||||
Display<7>::write_score(score);
|
||||
detail::Display::write_score(score, 7);
|
||||
}
|
||||
|
||||
void write_content(std::array<char, 7> content) override
|
||||
void write_content(std::string content) override
|
||||
{
|
||||
Display<7>::write_content(content);
|
||||
detail::Display::write_content(content, 7);
|
||||
}
|
||||
|
||||
~SevenDigitDisplay() override = default;
|
||||
|
||||
@@ -36,6 +36,7 @@ void Solenoid::triggerTask()
|
||||
|
||||
void Solenoid::trigger()
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Trigger Solenoid " << name << " for " << deactivation_time.count() << "ms";
|
||||
this->trigger_task = std::async(std::launch::async, &Solenoid::triggerTask, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ pin_controller(std::move(pin_controller)), Item(address, std::move(name)), deact
|
||||
|
||||
void Sound::play()
|
||||
{
|
||||
CLOG(INFO, OUTPUT_LOGGER) << "Play Sound " << id << " " << name;
|
||||
this->play_task = std::async(std::launch::async, &Sound::playTask, this);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user