Files
ESP8266/src/Display.cpp
2025-11-27 00:32:15 +01:00

59 lines
1.1 KiB
C++

#include <Display.h>
#include <algorithm>
Display::Display(uint8_t clockPin, uint8_t dataPin)
: u8g2(U8G2_R0, clockPin, dataPin, U8X8_PIN_NONE)
{
}
Display::~Display()
{
}
void Display::init()
{
u8g2.begin();
u8g2.setFont(u8g2_font_7x14B_tr);
}
void Display::drawText(const std::string text)
{
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_7x14B_tr);
uint8_t charHeight = u8g2.getMaxCharHeight();
std::vector<std::string> textGrid;
splitText(text, &textGrid);
for (uint i = 0; i < textGrid.size(); i++)
{
if (textGrid.at(i).empty())
continue;
u8g2.drawStr(0, (i + 1) * charHeight, textGrid.at(i).c_str());
}
u8g2.sendBuffer();
}
void Display::splitText(const std::string text, std::vector<std::string> *out)
{
if (out->empty())
out->push_back("");
uint8_t row = 0;
for (uint i = 0; i < text.length(); i++)
{
std::string added = out->at(row) + text[i];
if (u8g2.getStrWidth(added.c_str()) > u8g2.getWidth())
{
row++;
out->push_back("");
added = text[i];
}
out->at(row) = added;
}
}