slint/api/cpp/esp-idf/slint
2024-02-20 17:33:11 +00:00
..
include Rename WindowRotation to RenderingRotation in the software renderer (#4181) 2023-12-19 08:47:55 +01:00
LICENSES reuse: remove glob for markdown files 2023-08-17 08:55:28 +02:00
src Rename WindowRotation to RenderingRotation in the software renderer (#4181) 2023-12-19 08:47:55 +01:00
CMakeLists.txt Bump version number to 1.5.0 2024-02-20 17:33:11 +00:00
idf_component.yml Bump version number to 1.5.0 2024-02-20 17:33:11 +00:00
LICENSE Add missing license symlinks 2023-08-14 10:23:47 +02:00
README.md fix typo in esp-idf README 2023-09-22 09:07:25 +02:00

Slint

Component Registry

Slint is a declarative GUI toolkit to build native user interfaces for desktop and embedded applications written in Rust, C++, or JavaScript.

This component provides the C++ version of Slint for the Espressif IoT Development Framework.

It has been tested on ESP32-S3 devices.

Screenshot

Getting Started

Prerequisites

Hello World

The following steps will guide from the a bare-bones esp-idf "hello_world" to a GUI with Slint.

  1. Start by creating a new project:
idf.py create-project slint-hello-world
cd slint-hello-world
  1. Select your chipset with idf.py set-target, for example if you're using an ESP32S3 chipset, run
idf.py set-target esp32s3
  1. Add a Board Support Package that matches your device as a dependency. For example, if you're using an ESP-BOX, run
idf.py add-dependency esp-box
  1. Add Slint as a dependency:
idf.py add-dependency slint/slint
  1. Ensure that Espressif's Rust toolchain is selected for building. Either set the RUSTUP_TOOLCHAIN environment variable to the value esp or create a file called rust-toolchain.toml in your project directory with the following contents:
[toolchain]
channel = "esp"
  1. Remove main/slint-hello-world.c.
  2. Create a new file main/slint-hello-world.cpp with the following contents:
#include <stdio.h>
#include <esp_err.h>
#include <bsp/display.h>
#include <bsp/esp-bsp.h>
#include <slint-esp.h>

#if defined(BSP_LCD_DRAW_BUFF_SIZE)
#    define DRAW_BUF_SIZE BSP_LCD_DRAW_BUFF_SIZE
#else
#    define DRAW_BUF_SIZE (BSP_LCD_H_RES * CONFIG_BSP_LCD_DRAW_BUF_HEIGHT)
#endif

#include "appwindow.h"

extern "C" void app_main(void)
{
    /* Initialize display  */
    esp_lcd_panel_io_handle_t io_handle = NULL;
    esp_lcd_panel_handle_t panel_handle = NULL;
    const bsp_display_config_t bsp_disp_cfg = {
        .max_transfer_sz = DRAW_BUF_SIZE * sizeof(uint16_t),
    };
    bsp_display_new(&bsp_disp_cfg, &panel_handle, &io_handle);

     /* Set display brightness to 100% */
    bsp_display_backlight_on();

    std::optional<esp_lcd_touch_handle_t> touch_handle;

    /* Allocate a drawing buffer */
    static std::vector<slint::platform::Rgb565Pixel> buffer(BSP_LCD_H_RES * BSP_LCD_V_RES);

    /* Initialize Slint's ESP platform support*/
    slint_esp_init(slint::PhysicalSize({ BSP_LCD_H_RES, BSP_LCD_V_RES }), panel_handle,
                                       touch_handle, buffer);
    /* Instantiate the UI */
    auto ui = AppWindow::create();
    /* Show it on the screen and run the event loop */
    ui->run();
}
  1. Create main/appwindow.slint with the following contents:
import { VerticalBox, AboutSlint } from "std-widgets.slint";
export component AppWindow {
    VerticalBox {
        AboutSlint {}
        Text {
            text: "Hello World";
            font-size: 18px;
            horizontal-alignment: center;
        }
    }
}
  1. Edit main/CMakeLists.txt to adjust for the new slint-hello-world.cpp, add slint as required component, and instruction the build system to compile appwindow.slint to appwindow.h. The file should look like this:
idf_component_register(SRCS "slint-hello-world.cpp" INCLUDE_DIRS "." REQUIRES slint)
slint_target_sources(${COMPONENT_LIB} appwindow.slint)
  1. Open the configuration editor with idf.py menuconfig:

    • Change the stack size under Component config --> ESP System Settings --> Main task stack size to at least 8192. You may need to tweak this value in the future if you run into stack overflows.
    • You may need additional device-specific settings. For example if your device has external SPI RAM, you may need to enable that. For details for ESP32-S3 based devices see how to Configure the PSRAM.
    • Quit the editor with Q and save the configuration.

    Alternatively, check in a default sdkconfig tweaked from your board that adds the right amount of ram, flash, and use CONFIG_MAIN_TASK_STACK_SIZE=8192

  2. Build the project with idf.py build.

  3. Connect your device, then flash and run it with idf.py flash monitor.

  4. Observe Slint rendering "Hello World" on the screen 🎉.

Congratulations, you're all set up to develop with Slint. For more information, check out our online documentation.

If you have feedback or questions, feel free to reach out to the Slint community:

Troubleshooting

You may run into compile or run-time issues due to Slint's requirements. The following sections track issues we're aware of and how to solve them.

Rust Compilation Error During Slint Build

You see the following error:

error: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel

Solution: You need to configure your Rust toolchain to use the esp channel. Either set the RUSTUP_TOOLCHAIN environment variable to the value esp or create a file called rust-toolchain.toml in your project directory with the following contents:

[toolchain]
channel = "esp"

Error compiling Slint for ESP-IDF on Windows

You see errors about slint-compiler:

Error copying file (if different) from "C:/path/build/./cargo/build/x86_64-pc-windows-msvc/release/slint-compiler" to "C:/path/build/_deps/slint-build".

Solution: At the moment development development on Windows while targeting ESP-IDF is not supported. We're working on a fix and in the meantime we recommend using Windows Subsystem for Linux.

The device crashes at boot or enter a boot loop

One reason could be that you don't have enough ram for the heap or the stack. Make sure that the stack is big enough (~8KiB), and that all the RAM was made available for the heap allocator.

License

You can use Slint under any of the following licenses, at your choice:

  1. GNU GPLv3,
  2. Paid license.

See also the Licensing FAQ.

Slint is also available with a third license (Royalty Free) for desktop applications.

Website · GitHub · Docs