lvgl/README.md

281 lines
14 KiB
Markdown
Raw Normal View History

2020-05-25 12:41:06 +08:00
<h1 align="center"> LVGL - Little and Versatile Graphics Library</h1>
2019-02-01 02:13:26 +08:00
<p align="center">
2020-05-25 11:54:38 +08:00
<a href="https://github.com/lvgl/lvgl/blob/master/LICENCE.txt"><img src="https://img.shields.io/badge/licence-MIT-blue.svg"></a>
<a href="https://github.com/lvgl/lvgl/releases/tag/v7.0.0"><img src="https://img.shields.io/badge/version-7.0.0-blue.svg"></a>
2019-08-07 15:39:35 +08:00
</p>
2019-08-07 15:41:01 +08:00
<p align="center">
2020-05-19 17:35:18 +08:00
<img src="https://lvgl.io/assets/images/img_1.png">
2019-02-01 02:13:26 +08:00
</p>
2019-02-01 04:23:56 +08:00
<p align="center">
2020-05-15 20:43:45 +08:00
LVGL provides everything you need to create embedded GUI with easy-to-use graphical elements, beautiful visual effects and low memory footprint.
2019-02-01 04:23:56 +08:00
</p>
2017-11-29 18:41:34 +08:00
2019-02-01 02:13:26 +08:00
<h4 align="center">
2020-05-15 20:43:45 +08:00
<a href="https://lvgl.io">Website </a> &middot;
<a href="https://lvgl.io/demos">Live demo</a> &middot;
2020-05-23 20:14:02 +08:00
<a href="https://docs.lvgl.io/v7/en/html/get-started/pc-simulator.html">Simulator</a> &middot;
2020-05-15 20:43:45 +08:00
<a href="https://forum.lvgl.io">Forum</a> &middot;
<a href="https://docs.lvgl.io/">Docs</a> &middot;
<a href="https://blog.lvgl.io/">Blog</a>
2019-02-01 02:13:50 +08:00
</h4>
2019-01-31 21:01:56 +08:00
2019-07-19 14:07:53 +08:00
---
- [Features](#features)
- [Supported devices](#supported-devices)
- [Quick start in a simulator](#quick-start-in-a-simulator)
2020-05-25 11:54:38 +08:00
- [Add LVGL to your project](#add-lvgl-to-your-project)
2019-07-19 14:07:53 +08:00
- [Learn the basics](#learn-the-basics)
- [Examples](#examples)
- [Contributing](#contributing)
## Features
2018-09-24 17:42:13 +08:00
* **Powerful building blocks** buttons, charts, lists, sliders, images, etc.
* **Advanced graphics** with animations, anti-aliasing, opacity, smooth scrolling
2019-06-30 21:31:01 +08:00
* **Simultaneously use various input devices** touchscreen, mouse, keyboard, encoder, buttons, etc.
* **Simultaneously use multiple displays** i.e. monochrome and color display
2020-05-25 14:29:57 +08:00
* **Multi-language support** with UTF-8 encoding, Bidirectional support, and Arabic text handling
2018-09-24 17:42:13 +08:00
* **Fully customizable** graphical elements
* **Hardware independent** to use with any microcontroller or display
2019-07-03 06:46:53 +08:00
* **Scalable** to operate with little memory (64 kB Flash, 10 kB RAM)
2018-09-24 17:42:13 +08:00
* **OS, External memory and GPU** supported but not required
* **Single frame buffer** operation even with advances graphical effects
2020-05-15 20:43:45 +08:00
* **Written in C** for maximal compatibility (C++ compatible)
* **Micropython Binding** exposes [LVGL API in Micropython](https://blog.lvgl.io/2019-02-20/micropython-bindings)
2018-09-24 17:42:13 +08:00
* **Simulator** to develop on PC without embedded hardware
* **Tutorials, examples, themes** for rapid development
2019-07-19 17:08:08 +08:00
* **Documentation** and API references
2016-06-15 17:40:04 +08:00
2019-07-19 14:07:53 +08:00
## Supported devices
2020-05-25 14:29:57 +08:00
Basically, every modern controller (which is able to drive a display( is suitable to run LVGL. The minimal requirements are:
2019-07-19 14:07:53 +08:00
- 16, 32 or 64 bit microcontroller or processor
- &gt; 16 MHz clock speed is recommended
- Flash/ROM: &gt; 64 kB for the very essential components (&gt; 180 kB is recommended)
- RAM:
2020-05-25 14:29:57 +08:00
- Static RAM usage: ~2 kB depending on the used features and objects types
- Stack: &gt; 2kB (&gt; 8 kB is recommended)
- Dynamic data (heap): &gt; 2 KB (&gt; 16 kB is recommended if using several objects).
2019-07-19 14:07:53 +08:00
Set by `LV_MEM_SIZE` in *lv_conf.h*.
- Display buffer: &gt; *"Horizontal resolution"* pixels (&gt; 10 &times; *"Horizontal resolution"* is recommended)
2020-05-25 15:29:17 +08:00
- C99 or newer compiler
2019-07-19 14:07:53 +08:00
*Note that the memory usage might vary depending on the architecture, compiler and build options.*
2019-01-29 22:41:07 +08:00
2019-01-30 03:57:46 +08:00
Just to mention some **platforms**:
2020-05-25 11:54:38 +08:00
- STM32F1, STM32F3, [STM32F4](https://blog.lvgl.io/2017-07-15/stm32f429_disco_port), [STM32F7](https://github.com/lvgl/stm32f746_disco_no_os_sw4stm32)
2019-01-29 22:41:07 +08:00
- Microchip dsPIC33, PIC24, PIC32MX, PIC32MZ
- NXP Kinetis, LPC, iMX
2020-05-15 20:43:45 +08:00
- [Linux frame buffer](https://blog.lvgl.io/2018-01-03/linux_fb) (/dev/fb)
2019-01-29 22:41:07 +08:00
- [Raspberry PI](http://www.vk3erw.com/index.php/16-software/63-raspberry-pi-official-7-touchscreen-and-littlevgl)
2020-05-15 20:43:45 +08:00
- [Espressif ESP32](https://github.com/lvgl/lv_port_esp32)
2019-01-29 22:41:07 +08:00
- Nordic nrf52
- Quectell M66
2019-07-19 14:07:53 +08:00
## Quick start in a simulator
2020-05-25 11:54:38 +08:00
The easiest way to get started with LVGL is to run it in a simulator on your PC without any embedded hardware.
2019-01-31 17:25:08 +08:00
Choose a project with your favourite IDE:
2019-01-29 22:41:07 +08:00
2019-02-19 02:05:58 +08:00
| Eclipse | CodeBlocks | Visual Studio | PlatformIO | Qt Creator |
|-------------|-------------|---------------|-----------|------------|
2020-05-23 20:41:11 +08:00
| [![Eclipse](https://raw.githubusercontent.com/lvgl/docs/master/v7/misc//eclipse.jpg)](https://github.com/lvgl/pc_simulator_sdl_eclipse) | [![CodeBlocks](https://raw.githubusercontent.com/lvgl/docs/master/v7/misc//codeblocks.jpg)](https://github.com/lvgl/pc_simulator_win_codeblocks) | [![VisualStudio](https://raw.githubusercontent.com/lvgl/docs/master/v7/misc//visualstudio.jpg)](https://github.com/lvgl/visual_studio_2017_sdl_x64) | [![PlatformIO](https://raw.githubusercontent.com/lvgl/docs/master/v7/misc//platformio.jpg)](https://github.com/lvgl/pc_simulator_sdl_platformio) | [![QtCreator](https://raw.githubusercontent.com/lvgl/docs/master/v7/misc//qtcreator.jpg)](https://blog.lvgl.io/2019-01-03/qt-creator) |
2019-11-12 22:25:52 +08:00
| Cross-platform<br>with SDL<br>(Recommended on<br>Linux and Mac) | Native Windows | Windows<br>with SDL | Cross-platform<br>with SDL | Cross-platform<br>with SDL |
2019-01-29 22:41:07 +08:00
2019-07-19 14:07:53 +08:00
2020-05-25 11:54:38 +08:00
## Add LVGL to your project
2019-07-19 14:07:53 +08:00
2020-05-15 20:43:45 +08:00
The steps below show how to setup LVGL on an embedded system with a display and a touchpad.
You can use the [Simulators](https://docs.lvgl.io/v7/en/html/get-started/pc-simulator) to get ready to use projects which can be run on your PC.
2019-07-19 14:07:53 +08:00
2020-05-15 20:43:45 +08:00
1. [Download](https://lvgl.com/developers) or [Clone](https://github.com/lvgl/lvgl) the library
2019-07-19 14:07:53 +08:00
2. Copy the `lvgl` folder into your project
3. Copy `lvgl/lv_conf_template.h` as `lv_conf.h` next to the `lvgl` folder and set at least `LV_HOR_RES_MAX`, `LV_VER_RES_MAX` and `LV_COLOR_DEPTH`. Don't forget to **change the `#if 0` statement near the top of the file to `#if 1`**, otherwise you will get compilation errors.
2020-05-15 20:43:45 +08:00
4. Include `lvgl/lvgl.h` where you need to use LVGL related functions.
5. Call `lv_tick_inc(x)` every `x` milliseconds **in a Timer or Task** (`x` should be between 1 and 10). It is required for the internal timing of LVGL.
2019-07-19 14:07:53 +08:00
6. Call `lv_init()`
2020-05-15 20:43:45 +08:00
7. Create a display buffer for LVGL
2019-02-21 06:58:13 +08:00
```c
static lv_disp_buf_t disp_buf;
static lv_color_t buf[LV_HOR_RES_MAX * 10]; /*Declare a buffer for 10 lines*/
2019-04-03 00:12:00 +08:00
lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10); /*Initialize the display buffer*/
2019-02-21 06:58:13 +08:00
```
2019-07-19 14:07:53 +08:00
8. Implement and register a function which can **copy a pixel array** to an area of your display:
2019-01-31 05:17:04 +08:00
```c
2019-01-31 06:05:21 +08:00
lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
2019-02-21 06:58:13 +08:00
disp_drv.flush_cb = my_disp_flush; /*Set your driver function*/
2019-07-09 05:34:46 +08:00
disp_drv.buffer = &disp_buf; /*Assign the buffer to the display*/
2019-01-31 06:05:21 +08:00
lv_disp_drv_register(&disp_drv); /*Finally register the driver*/
2019-01-31 05:17:04 +08:00
2019-02-21 06:58:13 +08:00
void my_disp_flush(lv_disp_t * disp, const lv_area_t * area, lv_color_t * color_p)
2019-01-31 05:17:04 +08:00
{
int32_t x, y;
2019-02-21 06:58:13 +08:00
for(y = area->y1; y <= area->y2; y++) {
for(x = area->x1; x <= area->x2; x++) {
set_pixel(x, y, *color_p); /* Put a pixel to the display.*/
2019-01-31 05:17:04 +08:00
color_p++;
}
}
2019-07-19 14:07:53 +08:00
lv_disp_flush_ready(disp); /* Indicate you are ready with the flushing*/
2019-01-31 05:17:04 +08:00
}
```
2019-07-19 14:07:53 +08:00
9. Implement and register a function which can **read an input device**. E.g. for a touch pad:
2019-01-31 05:17:04 +08:00
```c
2019-01-31 06:05:21 +08:00
lv_indev_drv_init(&indev_drv); /*Descriptor of a input device driver*/
indev_drv.type = LV_INDEV_TYPE_POINTER; /*Touch pad is a pointer-like device*/
2019-02-21 07:50:06 +08:00
indev_drv.read_cb = my_touchpad_read; /*Set your driver function*/
2019-01-31 06:05:21 +08:00
lv_indev_drv_register(&indev_drv); /*Finally register the driver*/
2019-01-31 05:17:04 +08:00
bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data)
2019-01-31 05:17:04 +08:00
{
static lv_coord_t last_x = 0;
static lv_coord_t last_y = 0;
2019-02-02 05:27:57 +08:00
/*Save the state and save the pressed coordinate*/
2019-01-31 05:17:04 +08:00
data->state = touchpad_is_pressed() ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
if(data->state == LV_INDEV_STATE_PR) touchpad_get_xy(&last_x, &last_y);
2019-02-02 09:26:49 +08:00
/*Set the coordinates (if released use the last pressed coordinates)*/
2019-01-31 05:17:04 +08:00
data->point.x = last_x;
data->point.y = last_y;
return false; /*Return `false` because we are not buffering and no more data to read*/
}
```
2019-07-19 14:07:53 +08:00
10. Call `lv_task_handler()` periodically every few milliseconds in the main `while(1)` loop, in Timer interrupt or in an Operation system task.
It will redraw the screen if required, handle input devices etc.
## Learn the basics
2020-05-25 11:54:38 +08:00
In this section you can ready the very basics. For a more detailed guide check the [Quick overview](https://docs.lvgl.io/v7/en/html/get-started/quick-overview.html#learn-the-basics) in the documentation.
2019-07-19 14:07:53 +08:00
2020-05-15 20:43:45 +08:00
### Widgets (Objects)
2020-05-25 11:54:38 +08:00
The graphical elements like Buttons, Labels, Sliders, Charts etc are called objects in LittelvGL. Go to [Object types](https://docs.lvgl.io/v7/en/html/widgets/index) to see the full list of available types.
2019-07-19 14:07:53 +08:00
Every object has a parent object. The child object moves with the parent and if you delete the parent the children will be deleted too. Children can be visible only on their parent.
The *screen* are the "root" parents. To get the current screen call `lv_scr_act()`.
You can create a new object with `lv_<type>_create(parent, obj_to_copy)`. It will return an `lv_obj_t *` variable which should be used as a reference to the object to set its parameters.
2020-05-15 20:43:45 +08:00
The first parameter is the desired *parent*, the second parameters can be an object to copy (`NULL` if unused).
2019-07-19 14:07:53 +08:00
For example:
```c
lv_obj_t * slider1 = lv_slider_create(lv_scr_act(), NULL);
```
To set some basic attribute `lv_obj_set_<paramters_name>(obj, <value>)` function can be used. For example:
```c
lv_obj_set_x(btn1, 30);
lv_obj_set_y(btn1, 10);
lv_obj_set_size(btn1, 200, 50);
```
The objects has type specific parameters too which can be set by `lv_<type>_set_<paramters_name>(obj, <value>)` functions. For example:
```c
lv_slider_set_value(slider1, 70, LV_ANIM_ON);
```
To see the full API visit the documentation of the object types or the related header file (e.g. `lvgl/src/lv_objx/lv_slider.h`).
### Styles
2020-05-15 20:43:45 +08:00
Widgets are created with a default appearance but it can be changed by adding new styles to the object. A new style can be created like this:
2019-07-19 14:07:53 +08:00
```c
2020-05-15 20:43:45 +08:00
static lv_style_t style1; /*Should be static, global or dynamically allocated*/
lv_style_init(&style1);
lv_style_set_bg_color(&style1, LV_STATE_DEFAULT, LV_COLOR_RED); /*Default background color*/
lv_style_set_bg_color(&style1, LV_STATE_PRESSED, LV_COLOR_BLUE); /*Pressed background color*/
2019-07-19 14:07:53 +08:00
```
2020-05-15 20:43:45 +08:00
The wigedt have *parts* which can be referenced via `LV_<TYPE>_PART_<PART_NAME>`. E.g. `LV_BRN_PART_MAIN` or `LV_SLIDER_PART_KNOB`. See the documentation of the widgets to see the exisitng parts.
To add the style to a button:
```
lv_obj_add_style(btn1, LV_BTN_PART_MAIN, &style1);
2019-07-19 14:07:53 +08:00
```
2020-05-15 20:43:45 +08:00
To remove all styles from an object use:
```c
lv_obj_reset_style_list(obj, LV_OBJ_PART_MAIN);
```
2019-07-19 14:07:53 +08:00
2020-05-15 20:43:45 +08:00
Learn more in [Style overview](https://docs.lvgl.io/v7/en/html/overview/style) section.
2019-07-19 14:07:53 +08:00
### Events
Events are used to inform the user if something has happened with an object. You can assign a callback to an object which will be called if the object is clicked, released, dragged, being deleted etc. It should look like this:
```c
lv_obj_set_event_cb(btn, btn_event_cb); /*Assign a callback to the button*/
...
void btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
if(event == LV_EVENT_CLICKED) {
printf("Clicked\n");
}
}
```
2019-01-29 22:50:29 +08:00
2020-05-25 11:54:38 +08:00
Learn more about the events in the [Event overview](https://docs.lvgl.io/v7/en/html/overview/event) section.
2019-07-19 14:07:53 +08:00
## Examples
### Button with label
2019-01-31 06:05:21 +08:00
```c
lv_obj_t * btn = lv_btn_create(lv_scr_act(), NULL); /*Add a button the current screen*/
lv_obj_set_pos(btn, 10, 10); /*Set its position*/
lv_obj_set_size(btn, 100, 50); /*Set its size*/
2019-07-05 07:05:03 +08:00
lv_obj_set_event_cb(btn, btn_event_cb); /*Assign a callback to the button*/
2019-07-19 14:07:53 +08:00
2019-01-31 06:05:21 +08:00
lv_obj_t * label = lv_label_create(btn, NULL); /*Add a label to the button*/
lv_label_set_text(label, "Button"); /*Set the labels text*/
2019-07-19 14:07:53 +08:00
...
2019-07-05 07:05:03 +08:00
void btn_event_cb(lv_obj_t * btn, lv_event_t event)
2019-01-31 06:05:21 +08:00
{
2019-07-05 07:05:03 +08:00
if(event == LV_EVENT_CLICKED) {
printf("Clicked\n");
}
2019-01-31 06:05:21 +08:00
}
```
2020-05-15 20:43:45 +08:00
![LVGL button with label example](https://docs.lvgl.io/v7/en/misc/simple_button_example.gif)
2019-01-31 06:05:21 +08:00
2020-05-15 20:43:45 +08:00
### Use LVGL from Micropython
Learn more about [Micropython](https://docs.lvgl.io/en/html/get-started/micropython).
```python
# Create a Button and a Label
scr = lv.obj()
btn = lv.btn(scr)
btn.align(lv.scr_act(), lv.ALIGN.CENTER, 0, 0)
label = lv.label(btn)
label.set_text("Button")
# Load the screen
lv.scr_load(scr)
```
2019-07-19 14:07:53 +08:00
## Contributing
2020-05-15 20:43:45 +08:00
To ask questions please use the [Forum](https://forum.lvgl.io).
For development-related things (bug reports, feature suggestions) use [GitHub's Issue tracker](https://github.com/lvgl/lvgl/issues).
2019-07-19 14:07:53 +08:00
2020-05-15 20:43:45 +08:00
If you are interested in contributing to LVGL you can
- **Help others** in the [Forum](https://forum.lvgl.io).
- **Inspire people** by speaking about your project in [My project](https://forum.lvgl.io/c/my-projects) category in the Forum.
- **Improve and/or translate the documentation.** Go to the [Documentation](https://github.com/lvgl/docs) repository to learn more
2020-05-25 11:54:38 +08:00
- **Write a blog post** about your experiences. See how to do it in the [Blog](https://github.com/lvgl/blog) repository
2020-05-15 20:43:45 +08:00
- **Report and/or fix bugs** in [GitHub's issue tracker](https://github.com/lvgl/lvgl/issues)
- **Help in the developement**. Check the [Open issues](https://github.com/lvgl/lvgl/issues) especially the ones with [Help wanted](https://github.com/lvgl/lvgl/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) label and tell your ideas about a topic or implement a feature.
2017-05-15 20:11:12 +08:00
2020-05-15 20:43:45 +08:00
Before sending Pull requests, please read the following guides:
2020-05-25 11:54:38 +08:00
- [Contributing guide](https://github.com/lvgl/lvgl/blob/master/docs/CONTRIBUTING.md)
2020-05-15 20:43:45 +08:00
- [Coding style guide](https://github.com/lvgl/lvgl/blob/master/docs/CODING_STYLE.md)