From f5ce925153aabc41f74efa2e41a309bd04b59261 Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Sat, 22 May 2021 20:41:51 +0200 Subject: [PATCH] implementation continues... --- libs/mare/mare.cpp | 62 +++++++++++++++- libs/mare/mare.h | 139 +++++++++++++++++++++++++++-------- libs/mare/mare_drawables.cpp | 14 +++- src/main.cpp | 41 +---------- 4 files changed, 184 insertions(+), 72 deletions(-) diff --git a/libs/mare/mare.cpp b/libs/mare/mare.cpp index 416cfd4..be2d289 100644 --- a/libs/mare/mare.cpp +++ b/libs/mare/mare.cpp @@ -5,8 +5,68 @@ namespace Render { - Mare::Mare(){ + Mare::Mare(dim_t size) : + _screenSize(size), + _screenBufferBackground(nullptr), + _screenBufferForeground(nullptr) + { + int rv; + _screenBufferBackground = (uint8_t*)malloc(sizeof(uint8_t)* (_screenSize.x * _screenSize.y)); + _screenBufferForeground = (uint8_t*)malloc(sizeof(uint8_t)* (_screenSize.x * _screenSize.y)); + clearBuffer(_screenBufferBackground, Color::White); + clearBuffer(_screenBufferBackground, Color::White); + // init display + DEV_Delay_ms(500); + if((rv=DEV_Module_Init())!=0){ + printf("Init Failed, %d",rv); + return; + } + EPD_2IN9_V2_Init(); + EPD_2IN9_V2_Clear(); + printf("Mare render engine created\n"); + } + + Mare::~Mare(){ + for (auto &d :_drawables) { + d.reset(); + } + for (auto &p : _pages){ + p.reset(); + } + free(_screenBufferBackground); + free(_screenBufferForeground); + } + + template + void Mare::addDrawable(T){ + static size_t lastDrawableIndex(0); + auto newD = std::make_shared(lastDrawableIndex++, ) + } + + + void Mare::render() { + clearBuffer(_screenBufferBackground, Color::White); + for (auto &d: _drawables) { + if (d != nullptr){ + d->render(); + } else { + printf("Drawable is expired"); + } + } + } + + void Mare::setPixel(uint8_t* img, uint16_t x, uint16_t y, bool value){ + assert(x <= _screenSize.x); + assert(y <= _screenSize.y); + img += (sizeof(uint8_t)*x*(_screenSize.y>>3)+ sizeof(uint8_t)*(y>>3)); + if (value) *img &= ~0x01 << (y%8); + else *img |= 0x01 << y%8; + } + + void clearBuffer(uint8_t* buffer, Color col){ + assert(buffer != nullptr); + std::memset(buffer, col == Color::White ? 0xff : 0x00, sizeof(buffer)); } } \ No newline at end of file diff --git a/libs/mare/mare.h b/libs/mare/mare.h index d1c306f..04a7bea 100644 --- a/libs/mare/mare.h +++ b/libs/mare/mare.h @@ -5,12 +5,14 @@ extern "C" { #include - #include + #include #include } #include #include +#include +#include namespace Render{ @@ -36,56 +38,64 @@ namespace Render{ Intersect }; + enum class Color { + White, + Black + }; + class Page; class Drawable; typedef pos_t dim_t; - typedef std::vector Pages; - typedef std::vector Drawables; + typedef std::vector> Pages; + typedef std::vector> Drawables; class Mare { - Mare(); - virtual ~Mare(); + friend class Drawable; + + Mare(dim_t size); + ~Mare(); // getters, setters public: - void setSize(const uint16_t x, const uint16_t y); + void setSize(const dim_t size); const dim_t getSize(); void setRotation(const ScreenRotation r); const ScreenRotation getRotation(); - - const Pages& getPages(); - const uint8_t getCurrentPage(); void setCurrentPage(const uint8_t p); - - const Drawables& getDrawables(); + const Page& getCurrentPage(); // drawables, pages public: - void addDrawable(); - void removeDrawable(size_t id); - void addPage(uint8_t num); - void removePage(uint8_t num); + template + void addDrawable(T); + void removeDrawable(const size_t id); + void addPage(); + void removePage(const uint8_t num); + const Drawables& getDrawables(); + const Pages& getPages(); - private: - void visitDrawables(Drawable* parent); + //render + private: void render(); - void setPixel(uint16_t x, uint16_t y, bool value); - + void clearBuffer(uint8_t* buffer, Color col); + void visitDrawables(Drawable* parent); + void setPixel(uint8_t* img, uint16_t x, uint16_t y, bool value); + // members private: - uint8_t* _screenBufferForeground; - uint8_t* _screenBufferBackgrund; dim_t _screenSize; Pages _pages; - Drawables _drawables; + uint8_t* _screenBufferForeground; + uint8_t* _screenBufferBackground; + Drawables _drawables; // background drawables for all the pages ScreenRotation _rotation; }; class Drawable { - - Drawable(const size_t id, const pos_t position, const Drawable* parent); - virtual ~Drawable(); + public: + Drawable(const size_t id, const Drawable* parent, const Mare* engine); + ~Drawable(); public: virtual void render(); @@ -100,27 +110,30 @@ namespace Render{ const BlendMode getBlendMode(); private: + const Drawable* _parent; + const Mare* _engine; bool _dirty; size_t _id; pos_t _origin; bbox2d_t _bbox; Drawables _children; - Drawable* _parent; - }; class DrawablePoint: public Drawable { - DrawablePoint(); + DrawablePoint(const size_t id, const Drawable* parent, const Mare* engine, dim_t size); ~DrawablePoint(); public: + void setSize(dim_t size); + const dim_t getSize(); private: void render(); // members private: + dim_t _size; }; class DrawableLine: public Drawable { @@ -129,12 +142,21 @@ namespace Render{ ~DrawableLine(); public: + void setThickness(uint16_t thickness); + const uint16_t getThickness(); + void setRotation(float rotation); + const float getRotation(); + void setLength(uint16_t length); + const uint16_t getLength(); private: - void render() {}; + void render(); // difficult business // members private: + uint16_t _thickness; + uint16_t _lenght; + float _rotation; }; class DrawableRectangle: public Drawable { @@ -151,6 +173,63 @@ namespace Render{ private: }; + class DrawableCircle: public Drawable { + + DrawableCircle(); + ~DrawableCircle(); + + public: + + private: + void render() {}; + + // members + private: + }; + + class DrawableChar: public Drawable { + + DrawableChar(); + ~DrawableChar(); + + public: + + private: + void render() {}; + + // members + private: + }; + + class DrawableString: public Drawable { + + DrawableString(); + ~DrawableString(); + + public: + + private: + void render() {}; + + // members + private: + }; + + class DrawableCustom: public Drawable { + + DrawableCustom(); + ~DrawableCustom(); + + public: + + private: + void render() {}; + + // members + private: + }; + + class Page { Page(); @@ -159,6 +238,8 @@ namespace Render{ public: private: + Drawables _backDrawables; + Drawables _frontDrawables; }; } \ No newline at end of file diff --git a/libs/mare/mare_drawables.cpp b/libs/mare/mare_drawables.cpp index 6fbb457..0f29d44 100644 --- a/libs/mare/mare_drawables.cpp +++ b/libs/mare/mare_drawables.cpp @@ -2,12 +2,22 @@ namespace Render { - Drawable::Drawable(const size_t id, const pos_t position, const Drawable* parent) { - + Drawable::Drawable(const size_t id, const Drawable* parent, const Mare* engine): + _id(id), + _parent(parent), + _engine(engine) + { + printf("Created Drawable id: %d\n", _id); } //--------+--------+--------+--------+--------+--------+--------+--------+--------+--------// //Point + DrawablePoint::DrawablePoint(const size_t id, const Drawable* parent, const Mare* engine, dim_t size): + Drawable(id, parent,engine), + _size(size) + { + + } //--------+--------+--------+--------+--------+--------+--------+--------+--------+--------// diff --git a/src/main.cpp b/src/main.cpp index ab91cfb..b937962 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,48 +31,9 @@ int main(){ gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); bool s=false; - uint32_t i=0; - uint8_t img[EPD_2IN9_V2_HEIGHT/8*EPD_2IN9_V2_WIDTH]; + uint i(0); - for (int x=0; x < EPD_2IN9_V2_HEIGHT/8*EPD_2IN9_V2_WIDTH; ++x){ - img[x] = 0xff; - } - - int rv; - DEV_Delay_ms(500); - if((rv=DEV_Module_Init())!=0){ - printf("Init Failed, %d",rv); - return -1; - } - EPD_2IN9_V2_Init(); - EPD_2IN9_V2_Clear(); - - //clear delta memory - uint8_t delta[EPD_2IN9_V2_HEIGHT][EPD_2IN9_V2_WIDTH/8]; - for(auto i(0); i < EPD_2IN9B_V3_HEIGHT; ++i){ - for (auto j(0); j< EPD_2IN9B_V3_WIDTH/8; ++j){ - delta[i][j]=0xff; - } - } - uint8_t dot(8); - bool col(false); while(true){ - EPD_2IN9_V2_Display_Base((uint8_t*)img); - for(auto k(0); k<2; ++k){ - col = col ? false : true; - for (auto x(0); x <= EPD_2IN9_V2_WIDTH-dot;x+=dot){ - for(auto y(0); y <= EPD_2IN9_V2_HEIGHT-dot;y+=dot){ - drawSquare((uint8_t*)delta,y,EPD_2IN9_V2_WIDTH-x-dot,dot,dot,col); - EPD_2IN9_V2_Display_Partial((uint8_t*)delta); - } - } - } - - for(auto i(0); i < EPD_2IN9B_V3_HEIGHT; ++i){ - for (auto j(0); j< EPD_2IN9B_V3_WIDTH/8; ++j){ - delta[i][j]=0xff; - } - } printf("[%u] Hello, world!\n",i++); gpio_put(LED_PIN,s); s = s ? false : true;