From 7a9dc1d93dcc58a2ae040f8a33d1043cdd7e396a Mon Sep 17 00:00:00 2001 From: Matthew Colvin Date: Mon, 14 Aug 2023 15:41:01 -0500 Subject: [PATCH] Implement Simulator Display Abstract without public function support. --- .../Targets/Simulator/HardwareSimulator.cpp | 40 ++----------------- .../Targets/Simulator/HardwareSimulator.hpp | 4 +- .../HAL/Targets/Simulator/SDLDisplay.hpp | 27 +++++++++++++ 3 files changed, 33 insertions(+), 38 deletions(-) create mode 100644 Platformio/HAL/Targets/Simulator/SDLDisplay.hpp diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp index 3e9ea0e..38c3996 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.cpp @@ -7,6 +7,9 @@ #include "indev/keyboard.h" #include "sdl/sdl.h" +#include "SDLDisplay.hpp" + + /** * A task to measure the elapsed time for LittlevGL * @param data unused @@ -24,42 +27,7 @@ static int tick_thread(void * data) return 0; } - -void HardwareSimulator::init() { - lv_init(); - // Workaround for sdl2 `-m32` crash - // https://bugs.launchpad.net/ubuntu/+source/libsdl2/+bug/1775067/comments/7 - #ifndef WIN32 - setenv("DBUS_FATAL_WARNINGS", "0", 1); - #endif - - /* Add a display - * Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/ - - static lv_disp_draw_buf_t disp_buf; - static lv_color_t buf[SDL_HOR_RES * 10]; /*Declare a buffer for 10 lines*/ - lv_disp_draw_buf_init(&disp_buf, buf, NULL, SDL_HOR_RES * 10); /*Initialize the display buffer*/ - - static lv_disp_drv_t disp_drv; - lv_disp_drv_init(&disp_drv); /*Basic initialization*/ - disp_drv.flush_cb = sdl_display_flush; /*Used when `LV_VDB_SIZE != 0` in lv_conf.h (buffered drawing)*/ - disp_drv.draw_buf = &disp_buf; - disp_drv.hor_res = SDL_HOR_RES; - disp_drv.ver_res = SDL_VER_RES; - //disp_drv.disp_fill = monitor_fill; /*Used when `LV_VDB_SIZE == 0` in lv_conf.h (unbuffered drawing)*/ - //disp_drv.disp_map = monitor_map; /*Used when `LV_VDB_SIZE == 0` in lv_conf.h (unbuffered drawing)*/ - lv_disp_drv_register(&disp_drv); - - /* Add the mouse as input device - * Use the 'mouse' driver which reads the PC's mouse*/ - static lv_indev_drv_t indev_drv; - lv_indev_drv_init(&indev_drv); /*Basic initialization*/ - indev_drv.type = LV_INDEV_TYPE_POINTER; - indev_drv.read_cb = sdl_mouse_read; /*This function will be called periodically (by the library) to get the mouse position and state*/ - lv_indev_drv_register(&indev_drv); - - sdl_init(); - +HardwareSimulator::HardwareSimulator(): HardwareAbstract(SDLDisplay::getInstance()){ /* Tick init. * You have to call 'lv_tick_inc()' in periodically to inform LittelvGL about how much time were elapsed * Create an SDL thread to do this*/ diff --git a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp index 6fc67d5..5837fe3 100644 --- a/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp +++ b/Platformio/HAL/Targets/Simulator/HardwareSimulator.hpp @@ -5,13 +5,13 @@ class HardwareSimulator : public HardwareAbstract { public: - HardwareSimulator() : HardwareAbstract(){}; + HardwareSimulator(); virtual void debugPrint(std::string message) override { std::cout << message; } - virtual void init() override; + virtual void init() override {}; virtual std::optional getBatteryStatus() override { HardwareAbstract::batteryStatus fakeStatus; diff --git a/Platformio/HAL/Targets/Simulator/SDLDisplay.hpp b/Platformio/HAL/Targets/Simulator/SDLDisplay.hpp new file mode 100644 index 0000000..977ca94 --- /dev/null +++ b/Platformio/HAL/Targets/Simulator/SDLDisplay.hpp @@ -0,0 +1,27 @@ +#include "DisplayAbstract.h" + +class SDLDisplay : public DisplayAbstract{ + +public: + static std::shared_ptr getInstance(){ + if (!DisplayAbstract::mInstance){ + DisplayAbstract::mInstance = std::shared_ptr(new SDLDisplay()); + } + return std::static_pointer_cast(mInstance); + } + + virtual void setBrightness(uint8_t brightness) override {}; + virtual void turnOff() override {}; + +protected: + virtual void flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) override + { sdl_display_flush(disp,area,color_p); }; + virtual void screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) override + { sdl_mouse_read(indev_driver,data);} + +private: + SDLDisplay(): DisplayAbstract(){ + sdl_init(); + } + +}; \ No newline at end of file