refactored displays

This commit is contained in:
Jonas Zeunert
2018-12-20 00:21:02 +01:00
parent bc35bcbea6
commit 120d3693f2
18 changed files with 125 additions and 124 deletions

View File

@@ -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;
};
}

View 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;
}
}
}
}
}

View File

@@ -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

View File

@@ -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());
}
}
}
}
}

View File

@@ -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);
}
};
}

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);
}