From 5ce47045e75d967fa0f7bddb0c62eaddece9694d Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Wed, 20 Aug 2025 12:51:14 -0500 Subject: [PATCH] Add SDL option to BaseUI on Native (#7568) * Add SDL option to BaseUI on Native * Update to latest LovyanGFX PR and use LGFX_SDL define * Move SDL backend to native-sdl target --- src/graphics/TFTDisplay.cpp | 62 +++++++++++++++++++++++- src/graphics/TFTDisplay.h | 1 + src/main.cpp | 8 ++- variants/native/portduino/platformio.ini | 20 +++++++- 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/graphics/TFTDisplay.cpp b/src/graphics/TFTDisplay.cpp index 24ea6c47a..f8787612f 100644 --- a/src/graphics/TFTDisplay.cpp +++ b/src/graphics/TFTDisplay.cpp @@ -667,15 +667,19 @@ static LGFX *tft = nullptr; static TFT_eSPI *tft = nullptr; // Invoke library, pins defined in User_Setup.h #elif ARCH_PORTDUINO #include // Graphics and font library for ST7735 driver chip +#if defined(LGFX_SDL) +#include +#endif class LGFX : public lgfx::LGFX_Device { - lgfx::Panel_Device *_panel_instance; lgfx::Bus_SPI _bus_instance; lgfx::ITouch *_touch_instance; public: + lgfx::Panel_Device *_panel_instance; + LGFX(void) { if (settingsMap[displayPanel] == st7789) @@ -694,6 +698,11 @@ class LGFX : public lgfx::LGFX_Device _panel_instance = new lgfx::Panel_ILI9488; else if (settingsMap[displayPanel] == hx8357d) _panel_instance = new lgfx::Panel_HX8357D; +#if defined(LGFX_SDL) + else if (settingsMap[displayPanel] == x11) { + _panel_instance = new lgfx::Panel_sdl; + } +#endif else { _panel_instance = new lgfx::Panel_NULL; LOG_ERROR("Unknown display panel configured!"); @@ -754,7 +763,13 @@ class LGFX : public lgfx::LGFX_Device _touch_instance->config(touch_cfg); _panel_instance->setTouch(_touch_instance); } - +#if defined(LGFX_SDL) + if (settingsMap[displayPanel] == x11) { + lgfx::Panel_sdl *sdl_panel_ = (lgfx::Panel_sdl *)_panel_instance; + sdl_panel_->setup(); + sdl_panel_->addKeyCodeMapping(SDLK_RETURN, SDL_SCANCODE_KP_ENTER); + } +#endif setPanel(_panel_instance); // Sets the panel to use. } }; @@ -1060,6 +1075,49 @@ void TFTDisplay::display(bool fromBlank) } } +void TFTDisplay::sdlLoop() +{ +#if defined(LGFX_SDL) + static int lastPressed = 0; + static int shuttingDown = false; + if (settingsMap[displayPanel] == x11) { + lgfx::Panel_sdl *sdl_panel_ = (lgfx::Panel_sdl *)tft->_panel_instance; + if (sdl_panel_->loop() && !shuttingDown) { + LOG_WARN("Window Closed!"); + InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_SHUTDOWN, .kbchar = 0, .touchX = 0, .touchY = 0}; + inputBroker->injectInputEvent(&event); + } + + // debounce + if (lastPressed != 0 && !lgfx::v1::gpio_in(lastPressed)) + return; + if (!lgfx::v1::gpio_in(37)) { + lastPressed = 37; + InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_RIGHT, .kbchar = 0, .touchX = 0, .touchY = 0}; + inputBroker->injectInputEvent(&event); + } else if (!lgfx::v1::gpio_in(36)) { + lastPressed = 36; + InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_UP, .kbchar = 0, .touchX = 0, .touchY = 0}; + inputBroker->injectInputEvent(&event); + } else if (!lgfx::v1::gpio_in(38)) { + lastPressed = 38; + InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_DOWN, .kbchar = 0, .touchX = 0, .touchY = 0}; + inputBroker->injectInputEvent(&event); + } else if (!lgfx::v1::gpio_in(39)) { + lastPressed = 39; + InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_LEFT, .kbchar = 0, .touchX = 0, .touchY = 0}; + inputBroker->injectInputEvent(&event); + } else if (!lgfx::v1::gpio_in(SDL_SCANCODE_KP_ENTER)) { + lastPressed = SDL_SCANCODE_KP_ENTER; + InputEvent event = {.inputEvent = (input_broker_event)INPUT_BROKER_SELECT, .kbchar = 0, .touchX = 0, .touchY = 0}; + inputBroker->injectInputEvent(&event); + } else { + lastPressed = 0; + } + } +#endif +} + // Send a command to the display (low level function) void TFTDisplay::sendCommand(uint8_t com) { diff --git a/src/graphics/TFTDisplay.h b/src/graphics/TFTDisplay.h index 38cd53ebb..60adfdf7c 100644 --- a/src/graphics/TFTDisplay.h +++ b/src/graphics/TFTDisplay.h @@ -23,6 +23,7 @@ class TFTDisplay : public OLEDDisplay // Write the buffer to the display memory virtual void display() override { display(false); }; virtual void display(bool fromBlank); + void sdlLoop(); // Turn the display upside down virtual void flipScreenVertically(); diff --git a/src/main.cpp b/src/main.cpp index c53877e37..ef5f5a721 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1562,7 +1562,13 @@ void loop() #endif service->loop(); - +#if defined(LGFX_SDL) + if (screen) { + auto dispdev = screen->getDisplayDevice(); + if (dispdev) + static_cast(dispdev)->sdlLoop(); + } +#endif long delayMsec = mainController.runOrDelay(); // We want to sleep as long as possible here - because it saves power diff --git a/variants/native/portduino/platformio.ini b/variants/native/portduino/platformio.ini index b8452bb48..62942a80e 100644 --- a/variants/native/portduino/platformio.ini +++ b/variants/native/portduino/platformio.ini @@ -25,7 +25,6 @@ build_flags = ${native_base.build_flags} -Os -lX11 -linput -lxkbcommon -ffunctio -D USE_X11=1 -D HAS_TFT=1 -D HAS_SCREEN=1 - -D LV_CACHE_DEF_SIZE=6291456 -D LV_BUILD_TEST=0 -D LV_USE_LIBINPUT=1 @@ -41,6 +40,25 @@ build_flags = ${native_base.build_flags} -Os -lX11 -linput -lxkbcommon -ffunctio build_src_filter = ${native_base.build_src_filter} +[env:native-sdl] +extends = native_base +build_type = release +lib_deps = + ${env.lib_deps} + ${networking_base.lib_deps} + ${radiolib_base.lib_deps} + ${environmental_base.lib_deps} + # renovate: datasource=custom.pio depName=rweather/Crypto packageName=rweather/library/Crypto + rweather/Crypto@0.4.0 + # renovate: datasource=git-refs depName=libch341-spi-userspace packageName=https://github.com/pine64/libch341-spi-userspace gitBranch=main + https://github.com/pine64/libch341-spi-userspace/archive/af9bc27c9c30fa90772279925b7c5913dff789b4.zip + # renovate: datasource=custom.pio depName=adafruit/Adafruit seesaw Library packageName=adafruit/library/Adafruit seesaw Library + adafruit/Adafruit seesaw Library@1.7.9 + https://github.com/jp-bennett/LovyanGFX/archive/7458f84a126c1f8fdc7b038074f71be903f6e4c0.zip +build_flags = ${native_base.build_flags} + !pkg-config --cflags --libs sdl2 --silence-errors || : + -D LGFX_SDL=1 + [env:native-fb] extends = native_base build_type = release