refactored displays
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user