mirror of
https://github.com/Lemonochrme/clover.git
synced 2025-06-08 16:50:50 +02:00
Moved all the files from Screen. Components now uses shared ptr objects of Box. TextBox inherits from Box.
This commit is contained in:
parent
f2153f53af
commit
a16ee2b01f
8 changed files with 150 additions and 114 deletions
39
embedded/lib/Display/Components/Box.hpp
Normal file
39
embedded/lib/Display/Components/Box.hpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#ifndef _HEADER_DISPLAY_BOX
|
||||||
|
#define _HEADER_DISPLAY_BOX
|
||||||
|
#include <U8g2lib.h>
|
||||||
|
|
||||||
|
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
|
57
embedded/lib/Display/Components/Components.cpp
Normal file
57
embedded/lib/Display/Components/Components.cpp
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
#include "Components.hpp"
|
||||||
|
|
||||||
|
using namespace Display;
|
||||||
|
|
||||||
|
Components::Components()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Components::Add(std::shared_ptr<Box> box)
|
||||||
|
{
|
||||||
|
// When Added, boxes should be reordered by style.
|
||||||
|
_boxes.push_back(box);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Components::Add(std::vector<std::shared_ptr<Box>> 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;
|
||||||
|
}
|
24
embedded/lib/Display/Components/Components.hpp
Normal file
24
embedded/lib/Display/Components/Components.hpp
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef _HEADER_DISPLAY_COMPONENTS
|
||||||
|
#define _HEADER_DISPLAY_COMPONENTS
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
#include "Box.hpp"
|
||||||
|
|
||||||
|
namespace Display
|
||||||
|
{
|
||||||
|
class Components
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Components();
|
||||||
|
void Add(std::shared_ptr<Box> box);
|
||||||
|
void Add(std::vector<std::shared_ptr<Box>> boxes);
|
||||||
|
void Update(size_t index, String text);
|
||||||
|
void Display();
|
||||||
|
private:
|
||||||
|
size_t GetSize(StyleHeight sh);
|
||||||
|
// Boxes
|
||||||
|
std::vector<std::shared_ptr<Box>> _boxes;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //_HEADER_DISPLAY_COMPONENTS
|
|
@ -1,63 +1,8 @@
|
||||||
#include "ScreenComponents.hpp"
|
#include "../Screen.hpp"
|
||||||
#include "Screen.hpp"
|
#include "TextBox.hpp"
|
||||||
#include <U8g2lib.h>
|
|
||||||
|
|
||||||
using namespace Display;
|
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<TextBox> 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()
|
TextBox::TextBox()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -89,6 +34,8 @@ void TextBox::Calculate()
|
||||||
case StyleWidth::RIGHT:
|
case StyleWidth::RIGHT:
|
||||||
_x = width - _textWidth;
|
_x = width - _textWidth;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
_x=0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,6 +55,8 @@ void TextBox::Display(size_t size, size_t position, u8g2_uint_t offsetY)
|
||||||
case StyleHeight::BOTTOM:
|
case StyleHeight::BOTTOM:
|
||||||
Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, centeredOffset + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str());
|
Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, centeredOffset + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str());
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,11 @@
|
||||||
#ifndef _HEADER_SCREEN_COMPONENTS
|
#ifndef _HEADER_DISPLAY_TEXTBOX
|
||||||
#define _HEADER_SCREEN_COMPONENTS
|
#define _HEADER_DISPLAY_TEXTBOX
|
||||||
#include <WString.h>
|
#include "Box.hpp"
|
||||||
#include <vector>
|
|
||||||
#include <U8g2lib.h>
|
|
||||||
|
|
||||||
namespace Display
|
namespace Display
|
||||||
{
|
{
|
||||||
// Enums
|
|
||||||
enum class StyleWidth
|
|
||||||
{
|
|
||||||
LEFT,
|
|
||||||
CENTERED,
|
|
||||||
RIGHT
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class StyleHeight
|
class TextBox : public Box
|
||||||
{
|
|
||||||
TOP,
|
|
||||||
CENTERED,
|
|
||||||
BOTTOM
|
|
||||||
};
|
|
||||||
|
|
||||||
// TextBox which inherits from Box (Single Component)
|
|
||||||
class TextBox
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TextBox();
|
TextBox();
|
||||||
|
@ -38,11 +21,10 @@ namespace Display
|
||||||
* @param takeWholeLine if true, the button takes the whole line
|
* @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);
|
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 Display(size_t size, size_t position, u8g2_uint_t offsetY) override;
|
||||||
void Update(String str);
|
void Update(String str) override;
|
||||||
StyleHeight getStyleHeight();
|
StyleHeight getStyleHeight() override;
|
||||||
// Only vertical/height padding
|
u8g2_uint_t getPadding() override;
|
||||||
u8g2_uint_t getPadding();
|
|
||||||
private:
|
private:
|
||||||
void Calculate();
|
void Calculate();
|
||||||
String _text;
|
String _text;
|
||||||
|
@ -56,21 +38,6 @@ namespace Display
|
||||||
uint16_t _textWidth;
|
uint16_t _textWidth;
|
||||||
bool _takeWholeLine;
|
bool _takeWholeLine;
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Components which contains every type
|
#endif //_HEADER_DISPLAY_TEXTBOX
|
||||||
class Components
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Components();
|
|
||||||
void Add(TextBox box);
|
|
||||||
void Add(std::vector<TextBox> boxes);
|
|
||||||
void Update(size_t index, String text);
|
|
||||||
void Display();
|
|
||||||
private:
|
|
||||||
size_t GetSize(StyleHeight sh);
|
|
||||||
// Boxes
|
|
||||||
std::vector<TextBox> _boxes;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //_HEADER_SCREEN_COMPONENTS
|
|
|
@ -1,7 +1,6 @@
|
||||||
#include "Screen.hpp"
|
#include "Screen.hpp"
|
||||||
#include <string.h>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <memory>
|
||||||
|
|
||||||
using namespace Display;
|
using namespace Display;
|
||||||
|
|
||||||
|
@ -15,21 +14,23 @@ Screen::Screen()
|
||||||
}
|
}
|
||||||
|
|
||||||
Screen::~Screen()
|
Screen::~Screen()
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void Screen::Setup(uint8_t *font)
|
void Screen::Setup(uint8_t *font)
|
||||||
{
|
{
|
||||||
_font = font;
|
_font = font;
|
||||||
_screen->setFont(_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
|
// Static Components
|
||||||
connectingWindow.Add({headerSetup, TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)});
|
connectingWindow.Add({std::make_shared<TextBox>(headerSetup),
|
||||||
connectedWindow.Add({headerSetup,
|
std::make_shared<TextBox>(TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0))});
|
||||||
TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2),
|
connectedWindow.Add({std::make_shared<TextBox>(headerSetup),
|
||||||
TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2),
|
std::make_shared<TextBox>(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
|
||||||
TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2)});
|
std::make_shared<TextBox>(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
|
||||||
loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1));
|
std::make_shared<TextBox>(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))});
|
||||||
|
loopWindow.Add(std::make_shared<TextBox>(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Screen::connecting(uint8_t state)
|
void Screen::connecting(uint8_t state)
|
|
@ -3,12 +3,11 @@
|
||||||
#include <U8g2lib.h>
|
#include <U8g2lib.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
|
|
||||||
#include "ScreenComponents.hpp"
|
#include "Components/Components.hpp"
|
||||||
|
#include "Components/TextBox.hpp"
|
||||||
|
|
||||||
namespace Display
|
namespace Display
|
||||||
{
|
{
|
||||||
constexpr uint8_t FONT_SIZE=8;
|
|
||||||
|
|
||||||
class Screen
|
class Screen
|
||||||
{
|
{
|
||||||
public:
|
public:
|
|
@ -1,5 +1,5 @@
|
||||||
#include "ServerHandler.hpp"
|
#include "ServerHandler.hpp"
|
||||||
#include "Screen.hpp"
|
#include "../Display/Screen.hpp"
|
||||||
|
|
||||||
ServerHandler::ServerHandler() : server(80), display_time(0) {
|
ServerHandler::ServerHandler() : server(80), display_time(0) {
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue