diff --git a/embedded/lib/Display/Components/Box.hpp b/embedded/lib/Display/Components/Box.hpp new file mode 100644 index 0000000..8a11bab --- /dev/null +++ b/embedded/lib/Display/Components/Box.hpp @@ -0,0 +1,39 @@ +#ifndef _HEADER_DISPLAY_BOX +#define _HEADER_DISPLAY_BOX +#include + +namespace Display +{ + // Global Constants + constexpr uint8_t FONT_SIZE = 8; + + // Enums + enum class StyleWidth + { + UNDEFINED, + LEFT, + CENTERED, + RIGHT + }; + + enum class StyleHeight + { + UNDEFINED, + TOP, + CENTERED, + BOTTOM + }; + + class Box + { + public: + Box() {}; + virtual void Display(size_t size, size_t position, u8g2_uint_t offsetY) {}; + virtual void Update(String str) {}; + virtual StyleHeight getStyleHeight() { return StyleHeight::UNDEFINED; }; + // Only vertical/height padding + virtual u8g2_uint_t getPadding() { return 0; }; + }; +} + +#endif //_HEADER_DISPLAY_BOX \ No newline at end of file diff --git a/embedded/lib/Display/Components/Components.cpp b/embedded/lib/Display/Components/Components.cpp new file mode 100644 index 0000000..8aae7c8 --- /dev/null +++ b/embedded/lib/Display/Components/Components.cpp @@ -0,0 +1,57 @@ +#include "Components.hpp" + +using namespace Display; + +Components::Components() +{ +} + +void Components::Add(std::shared_ptr box) +{ + // When Added, boxes should be reordered by style. + _boxes.push_back(box); +} + +void Components::Add(std::vector> boxes) +{ + for (auto box : boxes) + { + _boxes.push_back(box); + } +} + +void Components::Update(size_t index, String text) +{ + _boxes[index]->Update(text); +} + +void Components::Display() +{ + size_t i(0); + u8g2_uint_t verticalPadding(0); + for (auto it = _boxes.begin(); it != _boxes.end(); it++) + { + const auto size_boxes = GetSize((*it)->getStyleHeight()); + (*it)->Display(size_boxes, i, verticalPadding); + // Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.) + if (it + 1 != _boxes.end() && ((*(it+1))->getStyleHeight() == (*it)->getStyleHeight())) + { + verticalPadding += (*it)->getPadding(); + i++; + } + else + { + verticalPadding = 0; + i = 0; + } + } +} + +size_t Components::GetSize(StyleHeight sh) +{ + size_t returnSize(0); + // returnSize is equal to FONT_SIZE + vertical padding * boxes with style sh + for(auto& box : _boxes) + returnSize += (box->getStyleHeight() == sh ? (FONT_SIZE+box->getPadding()) : 0); + return returnSize; +} \ No newline at end of file diff --git a/embedded/lib/Display/Components/Components.hpp b/embedded/lib/Display/Components/Components.hpp new file mode 100644 index 0000000..0f1ef8c --- /dev/null +++ b/embedded/lib/Display/Components/Components.hpp @@ -0,0 +1,24 @@ +#ifndef _HEADER_DISPLAY_COMPONENTS +#define _HEADER_DISPLAY_COMPONENTS +#include +#include +#include "Box.hpp" + +namespace Display +{ + class Components + { + public: + Components(); + void Add(std::shared_ptr box); + void Add(std::vector> boxes); + void Update(size_t index, String text); + void Display(); + private: + size_t GetSize(StyleHeight sh); + // Boxes + std::vector> _boxes; + }; +}; + +#endif //_HEADER_DISPLAY_COMPONENTS \ No newline at end of file diff --git a/embedded/lib/Component/ScreenComponents.cpp b/embedded/lib/Display/Components/TextBox.cpp similarity index 59% rename from embedded/lib/Component/ScreenComponents.cpp rename to embedded/lib/Display/Components/TextBox.cpp index abb5c07..ccadce3 100644 --- a/embedded/lib/Component/ScreenComponents.cpp +++ b/embedded/lib/Display/Components/TextBox.cpp @@ -1,63 +1,8 @@ -#include "ScreenComponents.hpp" -#include "Screen.hpp" -#include +#include "../Screen.hpp" +#include "TextBox.hpp" using namespace Display; -Components::Components() -{ -} - -void Components::Add(TextBox box) -{ - // When Added, boxes should be reordered by style. - _boxes.push_back(box); -} - -void Components::Add(std::vector boxes) -{ - for (auto box : boxes) - { - _boxes.push_back(box); - } -} - -void Components::Update(size_t index, String text) -{ - _boxes[index].Update(text); -} - -void Components::Display() -{ - size_t i(0); - u8g2_uint_t verticalPadding(0); - for (auto it = _boxes.begin(); it != _boxes.end(); it++) - { - const auto size_boxes = GetSize(it->getStyleHeight()); - it->Display(size_boxes, i, verticalPadding); - // Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.) - if (it + 1 != _boxes.end() && ((it + 1)->getStyleHeight() == it->getStyleHeight())) - { - verticalPadding += it->getPadding(); - i++; - } - else - { - verticalPadding = 0; - i = 0; - } - } -} - -size_t Components::GetSize(StyleHeight sh) -{ - size_t returnSize(0); - // returnSize is equal to FONT_SIZE + vertical padding * boxes with style sh - for(auto& box : _boxes) - returnSize += (box.getStyleHeight() == sh ? (FONT_SIZE+box.getPadding()) : 0); - return returnSize; -} - TextBox::TextBox() { } @@ -89,6 +34,8 @@ void TextBox::Calculate() case StyleWidth::RIGHT: _x = width - _textWidth; break; + default: + _x=0; } } @@ -108,6 +55,8 @@ void TextBox::Display(size_t size, size_t position, u8g2_uint_t offsetY) case StyleHeight::BOTTOM: Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, centeredOffset + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str()); break; + default: + break; } } diff --git a/embedded/lib/Component/ScreenComponents.hpp b/embedded/lib/Display/Components/TextBox.hpp similarity index 53% rename from embedded/lib/Component/ScreenComponents.hpp rename to embedded/lib/Display/Components/TextBox.hpp index 816490f..4bef776 100644 --- a/embedded/lib/Component/ScreenComponents.hpp +++ b/embedded/lib/Display/Components/TextBox.hpp @@ -1,28 +1,11 @@ -#ifndef _HEADER_SCREEN_COMPONENTS -#define _HEADER_SCREEN_COMPONENTS -#include -#include -#include +#ifndef _HEADER_DISPLAY_TEXTBOX +#define _HEADER_DISPLAY_TEXTBOX +#include "Box.hpp" namespace Display { - // Enums - enum class StyleWidth - { - LEFT, - CENTERED, - RIGHT - }; - enum class StyleHeight - { - TOP, - CENTERED, - BOTTOM - }; - - // TextBox which inherits from Box (Single Component) - class TextBox + class TextBox : public Box { public: TextBox(); @@ -38,11 +21,10 @@ namespace Display * @param takeWholeLine if true, the button takes the whole line */ TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style, u8g2_uint_t w_padding=0, u8g2_uint_t h_padding=0, bool takeWholeLine=false); - void Display(size_t size, size_t position, u8g2_uint_t offsetY); - void Update(String str); - StyleHeight getStyleHeight(); - // Only vertical/height padding - u8g2_uint_t getPadding(); + void Display(size_t size, size_t position, u8g2_uint_t offsetY) override; + void Update(String str) override; + StyleHeight getStyleHeight() override; + u8g2_uint_t getPadding() override; private: void Calculate(); String _text; @@ -56,21 +38,6 @@ namespace Display uint16_t _textWidth; bool _takeWholeLine; }; +} - // Components which contains every type - class Components - { - public: - Components(); - void Add(TextBox box); - void Add(std::vector boxes); - void Update(size_t index, String text); - void Display(); - private: - size_t GetSize(StyleHeight sh); - // Boxes - std::vector _boxes; - }; -}; - -#endif //_HEADER_SCREEN_COMPONENTS \ No newline at end of file +#endif //_HEADER_DISPLAY_TEXTBOX \ No newline at end of file diff --git a/embedded/lib/Component/Screen.cpp b/embedded/lib/Display/Screen.cpp similarity index 69% rename from embedded/lib/Component/Screen.cpp rename to embedded/lib/Display/Screen.cpp index 7084fec..8e46855 100644 --- a/embedded/lib/Component/Screen.cpp +++ b/embedded/lib/Display/Screen.cpp @@ -1,7 +1,6 @@ #include "Screen.hpp" -#include #include -#include +#include using namespace Display; @@ -15,21 +14,23 @@ 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,2,5, true); + headerSetup = TextBox("Clover Setup", StyleWidth::LEFT, StyleHeight::TOP, U8G2_BTN_INV, 2, 5, true); // Static Components - 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,0,2), - TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2), - TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2)}); - loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1)); + connectingWindow.Add({std::make_shared(headerSetup), + std::make_shared(TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0))}); + connectedWindow.Add({std::make_shared(headerSetup), + std::make_shared(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), + std::make_shared(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), + std::make_shared(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))}); + loopWindow.Add(std::make_shared(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1))); } void Screen::connecting(uint8_t state) diff --git a/embedded/lib/Component/Screen.hpp b/embedded/lib/Display/Screen.hpp similarity index 94% rename from embedded/lib/Component/Screen.hpp rename to embedded/lib/Display/Screen.hpp index cb5fcc8..9f26ff0 100644 --- a/embedded/lib/Component/Screen.hpp +++ b/embedded/lib/Display/Screen.hpp @@ -3,12 +3,11 @@ #include #include -#include "ScreenComponents.hpp" +#include "Components/Components.hpp" +#include "Components/TextBox.hpp" namespace Display { - constexpr uint8_t FONT_SIZE=8; - class Screen { public: diff --git a/embedded/lib/ServerHandler/ServerHandler.cpp b/embedded/lib/ServerHandler/ServerHandler.cpp index 6983c88..fee89d0 100644 --- a/embedded/lib/ServerHandler/ServerHandler.cpp +++ b/embedded/lib/ServerHandler/ServerHandler.cpp @@ -1,5 +1,5 @@ #include "ServerHandler.hpp" -#include "Screen.hpp" +#include "../Display/Screen.hpp" ServerHandler::ServerHandler() : server(80), display_time(0) { }