mirror of
https://github.com/Lemonochrme/clover.git
synced 2025-06-08 08:40:50 +02:00
Updated Display to Screen. Added ScreenComponents to simplify the use of the U8g2lib. Now showing IP on the screen.
This commit is contained in:
parent
c49073ece1
commit
b5c01dc017
8 changed files with 289 additions and 35 deletions
|
@ -1,16 +0,0 @@
|
|||
#include "Display.hpp"
|
||||
|
||||
Display::Display()
|
||||
{
|
||||
_display = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0,U8X8_PIN_NONE,SCL,SDA);
|
||||
_display->begin();
|
||||
}
|
||||
|
||||
void Display::loop()
|
||||
{
|
||||
_display->clearBuffer(); // clear the internal memory
|
||||
_display->setFont(u8g2_font_ncenB08_tr); // choose a suitable font
|
||||
_display->drawStr(0,10,"Hello World!"); // write something to the internal memory
|
||||
_display->sendBuffer(); // transfer internal memory to the display
|
||||
delay(1000);
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
#include <U8g2lib.h>
|
||||
#include <Wire.h>
|
||||
|
||||
class Display {
|
||||
public:
|
||||
Display();
|
||||
void loop();
|
||||
private:
|
||||
U8G2_SSD1306_128X64_NONAME_F_HW_I2C* _display;
|
||||
};
|
86
embedded/lib/Component/Screen.cpp
Normal file
86
embedded/lib/Component/Screen.cpp
Normal file
|
@ -0,0 +1,86 @@
|
|||
#include "Screen.hpp"
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
|
||||
using namespace Display;
|
||||
|
||||
Screen::Screen()
|
||||
{
|
||||
_screen = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE, SCL, SDA);
|
||||
_screen->begin();
|
||||
_width = _screen->getDisplayWidth();
|
||||
_height = _screen->getDisplayHeight();
|
||||
}
|
||||
|
||||
Screen::~Screen()
|
||||
{
|
||||
}
|
||||
|
||||
void Screen::Setup(uint8_t *font)
|
||||
{
|
||||
_font = font;
|
||||
_screen->setFont(_font);
|
||||
|
||||
// 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)});
|
||||
}
|
||||
|
||||
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");
|
||||
return y;
|
||||
}
|
||||
|
||||
void Screen::connecting(uint8_t state)
|
||||
{
|
||||
const size_t connectSize(16);
|
||||
char connectText[connectSize];
|
||||
|
||||
// Connecting dot dot dot (depending on state)
|
||||
strncpy(connectText, "Connecting", connectSize);
|
||||
for (uint8_t i = 0; i < state; i++)
|
||||
strncat(connectText, ".", connectSize);
|
||||
|
||||
// Displaying connecting text and setup bar.
|
||||
_screen->clearBuffer();
|
||||
const auto setupPadding = setupHeader();
|
||||
|
||||
// Reactive Component
|
||||
connectingWindow.Update(0,connectText);
|
||||
// Displaying
|
||||
connectingWindow.Display(StyleHeight::CENTERED, setupPadding);
|
||||
_screen->sendBuffer();
|
||||
}
|
||||
|
||||
void Screen::connected(const char *ipaddress)
|
||||
{
|
||||
// Displaying connecting text and setup bar.
|
||||
_screen->clearBuffer();
|
||||
const auto setupPadding = setupHeader();
|
||||
|
||||
// Reactive Component
|
||||
connectedWindow.Update(2,ipaddress);
|
||||
// Displaying
|
||||
connectedWindow.Display(StyleHeight::CENTERED, setupPadding+5);
|
||||
_screen->sendBuffer();
|
||||
}
|
||||
|
||||
void Screen::loop()
|
||||
{
|
||||
_screen->clearBuffer(); // clear the internal memory
|
||||
_screen->setFont(_font);
|
||||
_screen->drawStr(0, 10, "Hello World!"); // write something to the internal memory
|
||||
_screen->sendBuffer(); // transfer internal memory to the display
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
uint16_t Screen::getHeight() { return _height; }
|
||||
uint16_t Screen::getWidth() { return _width; }
|
||||
U8G2_SSD1306_128X64_NONAME_F_HW_I2C &Screen::getScreen() { return *_screen; }
|
||||
uint16_t Screen::getTextWidth(const char *str) { return _screen->getStrWidth(str); }
|
53
embedded/lib/Component/Screen.hpp
Normal file
53
embedded/lib/Component/Screen.hpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#ifndef _HEADER_SCREEN
|
||||
#define _HEADER_SCREEN
|
||||
#include <U8g2lib.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#include "ScreenComponents.hpp"
|
||||
|
||||
namespace Display
|
||||
{
|
||||
constexpr uint8_t FONT_SIZE=8;
|
||||
|
||||
class Screen
|
||||
{
|
||||
public:
|
||||
// Singleton
|
||||
static Screen &GetInstance()
|
||||
{
|
||||
static Screen instance;
|
||||
return instance;
|
||||
}
|
||||
// Public functions
|
||||
void Setup(uint8_t *font);
|
||||
void connecting(uint8_t state = 0);
|
||||
void connected(const char *ipaddress);
|
||||
void loop();
|
||||
// Getters
|
||||
uint16_t getHeight();
|
||||
uint16_t getWidth();
|
||||
U8G2_SSD1306_128X64_NONAME_F_HW_I2C& getScreen();
|
||||
uint16_t getTextWidth(const char * str);
|
||||
private:
|
||||
// Singleton
|
||||
Screen();
|
||||
~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;
|
||||
uint8_t *_font;
|
||||
uint16_t _width;
|
||||
uint16_t _height;
|
||||
|
||||
// Static Components
|
||||
Components connectingWindow;
|
||||
Components connectedWindow;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //_HEADER_SCREEN
|
82
embedded/lib/Component/ScreenComponents.cpp
Normal file
82
embedded/lib/Component/ScreenComponents.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
#include "ScreenComponents.hpp"
|
||||
#include "Screen.hpp"
|
||||
#include <U8g2lib.h>
|
||||
|
||||
using namespace Display;
|
||||
|
||||
Components::Components()
|
||||
{
|
||||
}
|
||||
|
||||
void Components::Add(TextBox box)
|
||||
{
|
||||
_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].updateString(text);
|
||||
}
|
||||
|
||||
void Components::Display(StyleHeight sh, uint16_t offset)
|
||||
{
|
||||
const auto totalTextSize = _boxes.size() * FONT_SIZE;
|
||||
const auto centeredOffset = (Screen::GetInstance().getHeight() - totalTextSize) - offset;
|
||||
for (size_t i = 0; i < _boxes.size(); i++)
|
||||
{
|
||||
switch (sh)
|
||||
{
|
||||
case StyleHeight::TOP:
|
||||
Screen::GetInstance().getScreen().drawButtonUTF8(_boxes[i].getX(), (i * FONT_SIZE) + offset, _boxes[i].getStyle(), _boxes[i].getTextWidth(), 0, 0, _boxes[i].getString());
|
||||
break;
|
||||
case StyleHeight::CENTERED:
|
||||
Screen::GetInstance().getScreen().drawButtonUTF8(_boxes[i].getX(), ((centeredOffset / 2) + i * FONT_SIZE) + offset, _boxes[i].getStyle(), _boxes[i].getTextWidth(), 0, 0, _boxes[i].getString());
|
||||
break;
|
||||
case StyleHeight::BOTTOM:
|
||||
Screen::GetInstance().getScreen().drawButtonUTF8(_boxes[i].getX(), (centeredOffset + i * FONT_SIZE) + offset, _boxes[i].getStyle(), _boxes[i].getTextWidth(), 0, 0, _boxes[i].getString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TextBox::TextBox(String str, StyleWidth sw, u8g2_uint_t style)
|
||||
: _text(str), _style(style), _styleWidth(sw)
|
||||
{
|
||||
Calculate();
|
||||
}
|
||||
|
||||
void TextBox::Calculate()
|
||||
{
|
||||
const auto width = Screen::GetInstance().getWidth();
|
||||
_textWidth = Screen::GetInstance().getTextWidth(_text.c_str());
|
||||
switch (_styleWidth)
|
||||
{
|
||||
case StyleWidth::LEFT:
|
||||
_x = 0;
|
||||
break;
|
||||
case StyleWidth::CENTERED:
|
||||
_x = (width - _textWidth) / 2;
|
||||
break;
|
||||
case StyleWidth::RIGHT:
|
||||
_x = width - _textWidth;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t TextBox::getX() { return _x; }
|
||||
const char *TextBox::getString() { return _text.c_str(); }
|
||||
void TextBox::updateString(String str)
|
||||
{
|
||||
_text = str;
|
||||
Calculate();
|
||||
}
|
||||
u8g2_uint_t TextBox::getStyle() { return _style; }
|
||||
uint16_t TextBox::getTextWidth() { return _textWidth; }
|
59
embedded/lib/Component/ScreenComponents.hpp
Normal file
59
embedded/lib/Component/ScreenComponents.hpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#ifndef _HEADER_SCREEN_COMPONENTS
|
||||
#define _HEADER_SCREEN_COMPONENTS
|
||||
#include <WString.h>
|
||||
#include <vector>
|
||||
#include <U8g2lib.h>
|
||||
|
||||
namespace Display
|
||||
{
|
||||
// Enums
|
||||
enum class StyleWidth
|
||||
{
|
||||
LEFT,
|
||||
CENTERED,
|
||||
RIGHT
|
||||
};
|
||||
|
||||
enum class StyleHeight
|
||||
{
|
||||
TOP,
|
||||
CENTERED,
|
||||
BOTTOM
|
||||
};
|
||||
|
||||
// TextBox which inherits from Box (Single Component)
|
||||
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();
|
||||
private:
|
||||
void Calculate();
|
||||
String _text;
|
||||
uint8_t *_font;
|
||||
u8g2_uint_t _style;
|
||||
StyleWidth _styleWidth;
|
||||
uint16_t _x;
|
||||
uint16_t _textWidth;
|
||||
};
|
||||
|
||||
// Components which contains every type
|
||||
class Components
|
||||
{
|
||||
public:
|
||||
Components();
|
||||
void Add(TextBox box);
|
||||
void Add(std::vector<TextBox> boxes);
|
||||
void Update(size_t index, String text);
|
||||
void Display(StyleHeight sh=StyleHeight::TOP, uint16_t offset=0);
|
||||
private:
|
||||
// Boxes
|
||||
std::vector<TextBox> _boxes;
|
||||
};
|
||||
};
|
||||
|
||||
#endif //_HEADER_SCREEN_COMPONENTS
|
|
@ -1,4 +1,5 @@
|
|||
#include "ServerHandler.hpp"
|
||||
#include "Screen.hpp"
|
||||
|
||||
ServerHandler::ServerHandler() : server(80) {
|
||||
}
|
||||
|
@ -6,17 +7,18 @@ ServerHandler::ServerHandler() : server(80) {
|
|||
ServerHandler::~ServerHandler() {}
|
||||
|
||||
void ServerHandler::setup(const char* ssid, const char* password) { // On utilise les scope resolution operator pour définir les méthodes la classe ServerHandle qui elle est dans hpp
|
||||
uint8_t state(0);
|
||||
Serial.begin(9600);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
Display::Screen::GetInstance().connecting(state);
|
||||
state >= 3 ? state = 0: state++;
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("Connected to Wi-Fi. IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
auto ip = WiFi.localIP().toString();
|
||||
Display::Screen::GetInstance().connected(ip.c_str());
|
||||
|
||||
server.begin();
|
||||
server.on("/", [this]() { this->handleRoot(); }); // fonction lamda pour gérer les requettes get
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
#include "ServerHandler.hpp"
|
||||
#include "Component.hpp"
|
||||
#include "Display.hpp"
|
||||
#include "Screen.hpp"
|
||||
|
||||
#ifdef SSID_CLOVER
|
||||
const char* ssid = SSID_CLOVER;
|
||||
|
@ -14,13 +14,12 @@
|
|||
#endif
|
||||
|
||||
Component humidity(ComponentType::Analog, PIN_A0);
|
||||
Display screen;
|
||||
|
||||
void setup()
|
||||
{
|
||||
auto& serverHandler = ServerHandler::GetInstance();
|
||||
Serial.begin(9600);
|
||||
serverHandler.setup(ssid, pswd);
|
||||
Display::Screen::GetInstance().Setup(const_cast<uint8_t*>(u8g2_font_helvB08_tr));
|
||||
ServerHandler::GetInstance().setup(ssid, pswd);
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
@ -28,7 +27,6 @@ void loop()
|
|||
auto& serverHandler = ServerHandler::GetInstance();
|
||||
auto& dataHandler = DataHandler::GetInstance();
|
||||
|
||||
screen.loop();
|
||||
dataHandler.updateTemperatureData(random(1800, 2200) / 100.0);
|
||||
// 0 -> air(0), 0-300 -> dry(20), 300-700 -> humid (580), 700-950 -> water(940)
|
||||
dataHandler.updateHumidityData(static_cast<float>(std::any_cast<int>(humidity.getValue())));
|
||||
|
|
Loading…
Add table
Reference in a new issue