From 629bae5fd536f4804233702f7e3903cea671224c Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 28 Nov 2023 21:30:37 +0100 Subject: [PATCH] More generic Approach for TextBox, same for Components that will be containg Boxes --- embedded/lib/Component/Screen.cpp | 58 ++++++------------ embedded/lib/Component/Screen.hpp | 4 +- embedded/lib/Component/ScreenComponents.cpp | 67 ++++++++++++--------- embedded/lib/Component/ScreenComponents.hpp | 15 +++-- 4 files changed, 67 insertions(+), 77 deletions(-) diff --git a/embedded/lib/Component/Screen.cpp b/embedded/lib/Component/Screen.cpp index 24d06d6..bb54f30 100644 --- a/embedded/lib/Component/Screen.cpp +++ b/embedded/lib/Component/Screen.cpp @@ -15,30 +15,21 @@ Screen::Screen() } Screen::~Screen() -{ -} +{} void Screen::Setup(uint8_t *font) { _font = font; _screen->setFont(_font); + headerSetup = TextBox("Clover Setup", StyleWidth::LEFT, StyleHeight::TOP, U8G2_BTN_INV); // Static Components - connectingWindow.Add(TextBox("connect", StyleWidth::CENTERED, U8G2_BTN_BW0)); - connectedWindow.Add({TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, U8G2_BTN_BW0), - TextBox("IP address: ", StyleWidth::LEFT, U8G2_BTN_BW0), - TextBox("addr", StyleWidth::CENTERED, U8G2_BTN_BW0)}); - loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, U8G2_BTN_BW1)); -} - -uint16_t Screen::setupHeader(const uint16_t w_padding, const uint16_t h_padding) -{ - _screen->setFont(u8g2_font_helvB08_tr); - // calculating y position - const uint16_t y = FONT_SIZE + ((w_padding + 1) * 2); - _screen->drawButtonUTF8(w_padding, y, U8G2_BTN_INV, _screen->getDisplayWidth(), h_padding, w_padding, "Clover Setup"); - _screen->setFont(_font); - return y; + connectingWindow.Add({headerSetup, TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)}); + connectedWindow.Add({headerSetup, + TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0), + TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0), + TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)}); + loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1)); } void Screen::connecting(uint8_t state) @@ -49,7 +40,6 @@ void Screen::connecting(uint8_t state) // Connecting dot dot dot (depending on state) strncpy(connectText, "Connecting", connectSize); size_t currentLength = strlen(connectText); - for (uint8_t i = 0; i < state; i++) { // Checking space @@ -60,35 +50,28 @@ void Screen::connecting(uint8_t state) } } - // Displaying connecting text and setup bar. _screen->clearBuffer(); - const auto setupPadding = setupHeader(); - - // Reactive Component - connectingWindow.Update(0, connectText); + // Component + connectingWindow.Update(1, connectText); + connectingWindow.Display(); // Displaying - connectingWindow.Display(StyleHeight::CENTERED, setupPadding); _screen->sendBuffer(); } void Screen::connected(const char *ipaddress, uint8_t timing) { - - // Displaying connecting text and setup bar. _screen->clearBuffer(); - const auto setupPadding = setupHeader(); + // Component + connectedWindow.Update(3, ipaddress); - // Reactive Component - connectedWindow.Update(2, ipaddress); // Displaying - connectedWindow.Display(StyleHeight::CENTERED, setupPadding, 4); - - // Creating loading timing - if(timing !=0) + connectedWindow.Display(); + // Creating loading bar + if (timing != 0) { _screen->setFont(u8g2_font_3x3basic_tr); _loading.concat(" "); - _screen->drawButtonUTF8(0, _screen->getDisplayHeight()-5, U8G2_BTN_INV, _screen->getStrWidth(_loading.c_str()), 0, 0, _loading.c_str()); + _screen->drawButtonUTF8(0, _screen->getDisplayHeight() - 5, U8G2_BTN_INV, _screen->getStrWidth(_loading.c_str()), 0, 0, _loading.c_str()); _screen->setFont(_font); } _screen->sendBuffer(); @@ -97,10 +80,9 @@ void Screen::connected(const char *ipaddress, uint8_t timing) void Screen::loop() { _screen->clearBuffer(); - _screen->setFont(_font); - - loopWindow.Display(StyleHeight::CENTERED); - + // Component + loopWindow.Display(); + // Displaying _screen->sendBuffer(); } diff --git a/embedded/lib/Component/Screen.hpp b/embedded/lib/Component/Screen.hpp index f060c20..cb5fcc8 100644 --- a/embedded/lib/Component/Screen.hpp +++ b/embedded/lib/Component/Screen.hpp @@ -34,9 +34,6 @@ namespace Display ~Screen(); Screen(const Screen &) = delete; Screen &operator=(const Screen &) = delete; - - // Fonctions - uint16_t setupHeader(const uint16_t w_padding = 2, const uint16_t h_padding = 5); // Variables U8G2_SSD1306_128X64_NONAME_F_HW_I2C *_screen; @@ -46,6 +43,7 @@ namespace Display String _loading; // Static Components + TextBox headerSetup; Components connectingWindow; Components connectedWindow; Components loopWindow; diff --git a/embedded/lib/Component/ScreenComponents.cpp b/embedded/lib/Component/ScreenComponents.cpp index c1cd4d6..6bbd5a0 100644 --- a/embedded/lib/Component/ScreenComponents.cpp +++ b/embedded/lib/Component/ScreenComponents.cpp @@ -23,37 +23,38 @@ void Components::Add(std::vector boxes) void Components::Update(size_t index, String text) { - _boxes[index].updateString(text); + _boxes[index].Update(text); } -void Components::Display(StyleHeight sh, uint16_t offset, uint16_t padding) +void Components::Display() { - const auto totalTextSize = _boxes.size() * FONT_SIZE; - const auto centeredOffset = (Screen::GetInstance().getHeight() - totalTextSize) - offset; - for (size_t i = 0; i < _boxes.size(); i++) + const auto size_boxes = _boxes.size(); + size_t i(0); + for(auto it = _boxes.begin() ; it!= _boxes.end() ;it++) { - const auto x = (_boxes[i].getStyleWidth() == StyleWidth::CENTERED) ? _boxes[i].getX() : _boxes[i].getX()+padding*i; - switch (sh) - { - case StyleHeight::TOP: - Screen::GetInstance().getScreen().drawButtonUTF8(x, (i * FONT_SIZE)+offset+padding*i, _boxes[i].getStyle(), _boxes[i].getTextWidth(), padding, padding, _boxes[i].getString()); - break; - case StyleHeight::CENTERED: - Screen::GetInstance().getScreen().drawButtonUTF8(x, ((centeredOffset / 2)+i*FONT_SIZE)+offset+padding*i, _boxes[i].getStyle(), _boxes[i].getTextWidth(), padding, padding, _boxes[i].getString()); - break; - case StyleHeight::BOTTOM: - Screen::GetInstance().getScreen().drawButtonUTF8(x, (centeredOffset + i * FONT_SIZE)+offset+padding*i, _boxes[i].getStyle(), _boxes[i].getTextWidth(), padding, padding, _boxes[i].getString()); - break; - } + it->Display(size_boxes,i); + if (it+1 != _boxes.end() && ((it+1)->getStyleHeight() == it->getStyleHeight())) + i++; + else + i=0; } } -TextBox::TextBox(String str, StyleWidth sw, u8g2_uint_t style) - : _text(str), _style(style), _styleWidth(sw) +TextBox::TextBox() +{} + +TextBox::TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style) + : _text(str), _style(style), _styleWidth(sw), _styleHeight(sh) { Calculate(); } +void TextBox::Update(String str) +{ + _text = str; + Calculate(); +} + void TextBox::Calculate() { const auto width = Screen::GetInstance().getWidth(); @@ -72,13 +73,23 @@ void TextBox::Calculate() } } -uint16_t TextBox::getX() { return _x; } -const char *TextBox::getString() { return _text.c_str(); } -void TextBox::updateString(String str) +void TextBox::Display(size_t size, size_t position) { - _text = str; - Calculate(); + const auto centeredOffset = (Screen::GetInstance().getHeight() - (size * FONT_SIZE)); + // Later will be used with padding/margin + const auto x = (_styleWidth == StyleWidth::CENTERED) ? _x : _x; + switch (_styleHeight) + { + case StyleHeight::TOP: + Screen::GetInstance().getScreen().drawButtonUTF8(x, (position+1)*FONT_SIZE, _style, _textWidth, 0, 0, _text.c_str()); + break; + case StyleHeight::CENTERED: + Screen::GetInstance().getScreen().drawButtonUTF8(x, static_cast((centeredOffset/2)) + (position+1)*FONT_SIZE, _style, _textWidth, 0, 0, _text.c_str()); + break; + case StyleHeight::BOTTOM: + Screen::GetInstance().getScreen().drawButtonUTF8(x, centeredOffset + (position+1)*FONT_SIZE, _style, _textWidth, 0, 0, _text.c_str()); + break; + } } -u8g2_uint_t TextBox::getStyle() { return _style; } -uint16_t TextBox::getTextWidth() { return _textWidth; } -StyleWidth TextBox::getStyleWidth() { return _styleWidth; } \ No newline at end of file + +StyleHeight TextBox::getStyleHeight() { return _styleHeight; } \ No newline at end of file diff --git a/embedded/lib/Component/ScreenComponents.hpp b/embedded/lib/Component/ScreenComponents.hpp index 1a606eb..51b707d 100644 --- a/embedded/lib/Component/ScreenComponents.hpp +++ b/embedded/lib/Component/ScreenComponents.hpp @@ -25,19 +25,18 @@ namespace Display class TextBox { public: - TextBox(String str, StyleWidth sw, u8g2_uint_t style); - uint16_t getX(); - const char * getString(); - void updateString(String str); - u8g2_uint_t getStyle(); - uint16_t getTextWidth(); - StyleWidth getStyleWidth(); + TextBox(); + TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style); + void Display(size_t size, size_t position); + void Update(String str); + StyleHeight getStyleHeight(); private: void Calculate(); String _text; uint8_t *_font; u8g2_uint_t _style; StyleWidth _styleWidth; + StyleHeight _styleHeight; uint16_t _x; uint16_t _textWidth; }; @@ -50,7 +49,7 @@ namespace Display void Add(TextBox box); void Add(std::vector boxes); void Update(size_t index, String text); - void Display(StyleHeight sh=StyleHeight::TOP, uint16_t offset=0, uint16_t padding=0); + void Display(); private: // Boxes std::vector _boxes;