From 38308f77f83378805dd184761deefe6a837aad16 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Wed, 29 Nov 2023 22:27:57 +0100 Subject: [PATCH] Updated clover.xbm. Modified Box to use std::any. Added SpriteBox. Showing the clover sprite at startup. --- embedded/lib/Display/Components/Box.hpp | 6 ++-- embedded/lib/Display/Components/SpriteBox.cpp | 27 ++++++++++++++ embedded/lib/Display/Components/SpriteBox.hpp | 36 +++++++++++++++++++ embedded/lib/Display/Components/TextBox.cpp | 4 +-- embedded/lib/Display/Components/TextBox.hpp | 8 ++++- embedded/lib/Display/Screen.cpp | 6 +++- embedded/lib/Pictures/clover.xbm | 6 ++-- 7 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 embedded/lib/Display/Components/SpriteBox.cpp create mode 100644 embedded/lib/Display/Components/SpriteBox.hpp diff --git a/embedded/lib/Display/Components/Box.hpp b/embedded/lib/Display/Components/Box.hpp index f127ad1..0f8d897 100644 --- a/embedded/lib/Display/Components/Box.hpp +++ b/embedded/lib/Display/Components/Box.hpp @@ -1,6 +1,7 @@ #ifndef _HEADER_DISPLAY_BOX #define _HEADER_DISPLAY_BOX #include +#include namespace Display { @@ -63,11 +64,10 @@ namespace Display /** * @brief Will update by recalculating the 'Box' constants * with the given argument. - * ! Will be modified in the near future to accept a template, or an std::any * - * @param str + * @param data the data to modify, depending on the Box type */ - virtual void Update(String str){}; + virtual void Update(std::any data){}; /** * @brief Get the Style Height, logic can be changed. diff --git a/embedded/lib/Display/Components/SpriteBox.cpp b/embedded/lib/Display/Components/SpriteBox.cpp new file mode 100644 index 0000000..08f892d --- /dev/null +++ b/embedded/lib/Display/Components/SpriteBox.cpp @@ -0,0 +1,27 @@ +#include "../Screen.hpp" +#include "SpriteBox.hpp" + +using namespace Display; + +SpriteBox::SpriteBox() + :Box(StyleHeight::UNDEFINED,0) +{} + +SpriteBox::SpriteBox(unsigned char *sprite, uint16_t width, uint16_t height, u8g2_uint_t x, u8g2_uint_t y) + :Box(StyleHeight::TOP,0) + , _sprite(sprite) + , _width(width) + , _height(height) + , _x(x) + , _y(y) +{} + +void SpriteBox::Update(std::any data) +{ + _sprite = std::any_cast(data); +} + +void SpriteBox::Display(size_t size, size_t position, u8g2_uint_t offsetY) +{ + Screen::GetInstance().getScreen().drawXBM(_x,_y,_width,_height,_sprite); +} \ No newline at end of file diff --git a/embedded/lib/Display/Components/SpriteBox.hpp b/embedded/lib/Display/Components/SpriteBox.hpp new file mode 100644 index 0000000..06dcae5 --- /dev/null +++ b/embedded/lib/Display/Components/SpriteBox.hpp @@ -0,0 +1,36 @@ +#ifndef _HEADER_DISPLAY_SPRITEBOX +#define _HEADER_DISPLAY_SPRITEBOX +#include "Box.hpp" + +namespace Display +{ + + class SpriteBox : public Box + { + public: + SpriteBox(); + /** + * @brief Construct a new Text Box object + * + * @param sprite array from an .xbm format + */ + SpriteBox(unsigned char *sprite, uint16_t width, uint16_t height, u8g2_uint_t x=0, u8g2_uint_t y=0); + void Display(size_t size, size_t position, u8g2_uint_t offsetY) override; + + /** + * @brief Updates sprite + * + * @param data unsigned char* + */ + void Update(std::any data) override; + + private: + unsigned char* _sprite; + uint16_t _width; + uint16_t _height; + u8g2_uint_t _x; + u8g2_uint_t _y; + }; +} + +#endif //_HEADER_DISPLAY_SPRITEBOX \ No newline at end of file diff --git a/embedded/lib/Display/Components/TextBox.cpp b/embedded/lib/Display/Components/TextBox.cpp index b40c0c1..03b5d7d 100644 --- a/embedded/lib/Display/Components/TextBox.cpp +++ b/embedded/lib/Display/Components/TextBox.cpp @@ -14,9 +14,9 @@ TextBox::TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style, u Calculate(); } -void TextBox::Update(String str) +void TextBox::Update(std::any data) { - _text = str; + _text = std::any_cast(data); Calculate(); } diff --git a/embedded/lib/Display/Components/TextBox.hpp b/embedded/lib/Display/Components/TextBox.hpp index 922dc1d..6bb6c41 100644 --- a/embedded/lib/Display/Components/TextBox.hpp +++ b/embedded/lib/Display/Components/TextBox.hpp @@ -22,7 +22,13 @@ namespace Display */ 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) override; - void Update(String str) override; + + /** + * @brief Updates String data + * + * @param data String + */ + void Update(std::any data) override; private: /** diff --git a/embedded/lib/Display/Screen.cpp b/embedded/lib/Display/Screen.cpp index 8e46855..5da8036 100644 --- a/embedded/lib/Display/Screen.cpp +++ b/embedded/lib/Display/Screen.cpp @@ -2,6 +2,10 @@ #include #include +// XBM Files +#include "Components/SpriteBox.hpp" +#include "../Pictures/clover.xbm" + using namespace Display; Screen::Screen() @@ -30,7 +34,7 @@ void Screen::Setup(uint8_t *font) 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))); + loopWindow.Add(std::make_shared(SpriteBox(clover_bits,clover_height,clover_width))); } void Screen::connecting(uint8_t state) diff --git a/embedded/lib/Pictures/clover.xbm b/embedded/lib/Pictures/clover.xbm index 4f520d9..3e0cf08 100644 --- a/embedded/lib/Pictures/clover.xbm +++ b/embedded/lib/Pictures/clover.xbm @@ -1,6 +1,6 @@ #define clover_width 16 #define clover_height 16 static unsigned char clover_bits[] = { - 0xff, 0xff, 0x7f, 0xfe, 0x7f, 0xfe, 0x3f, 0xfc, 0x3f, 0xfc, 0x3f, 0xfc, - 0x1f, 0xf8, 0x1f, 0xf8, 0x1c, 0x38, 0x30, 0x0c, 0x60, 0x06, 0xc1, 0x83, - 0xc1, 0x83, 0x83, 0xc1, 0x87, 0xe1, 0x3f, 0xfc }; + 0x00, 0x00, 0x80, 0x01, 0x80, 0x01, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, + 0xe0, 0x07, 0xe0, 0x07, 0xe3, 0xc7, 0xcf, 0xf3, 0x9f, 0xf9, 0x3e, 0x7c, + 0x3e, 0x7c, 0x7c, 0x3e, 0x78, 0x1e, 0xc0, 0x03 };