diff options
Diffstat (limited to 'docs/porting')
-rw-r--r-- | docs/porting/display.md | 134 | ||||
-rw-r--r-- | docs/porting/indev.md | 94 |
2 files changed, 124 insertions, 104 deletions
diff --git a/docs/porting/display.md b/docs/porting/display.md index 87d9d62ab..c75d2cf6a 100644 --- a/docs/porting/display.md +++ b/docs/porting/display.md @@ -4,86 +4,104 @@ ``` # Display interface -To set up a display an `lv_disp_buf_t` and an `lv_disp_drv_t` variables have to be initialized. -- **lv_disp_buf_t** contains internal graphic buffer(s). -- **lv_disp_drv_t** contains callback functions to interact with the display and manipulate drawing related things. +To set up a display an `lv_disp_draw_buf_t` and an `lv_disp_drv_t` variables have to be initialized. +- `lv_disp_draw_buf_t` contains internal graphic buffer(s), called draw buffer(s). +- `lv_disp_drv_t` contains callback functions to interact with the display and manipulate drawing related things. +## Draw buffer -## Display buffer +Draw buffer(s) are simple array(s) that LVGL uses to render the content of the screen. +Once rendering is ready the content of the draw buffer is send to display using the `flush_cb` set in the display driver (see below). -`lv_disp_buf_t` can be initialized like this: +A draw draw buffer can be initialized via a `lv_disp_draw_buf_t` variable like this: ```c /*A static or global variable to store the buffers*/ - static lv_disp_buf_t disp_buf; + static lv_disp_draw_buf_t disp_buf; /*Static or global buffer(s). The second buffer is optional*/ static lv_color_t buf_1[MY_DISP_HOR_RES * 10]; static lv_color_t buf_2[MY_DISP_HOR_RES * 10]; /*Initialize `disp_buf` with the buffer(s) */ - lv_disp_buf_init(&disp_buf, buf_1, buf_2, MY_DISP_HOR_RES*10); + lv_disp_draw_buf_init(&disp_buf, buf_1, buf_2, MY_DISP_HOR_RES*10); ``` -There are 3 possible configurations regarding the buffer size: -1. **One buffer** LVGL draws the content of the screen into a buffer and sends it to the display. -The buffer can be smaller than the screen. In this case, the larger areas will be redrawn in multiple parts. -If only small areas changes (e.g. button press) then only those areas will be refreshed. -2. **Two non-screen-sized buffers** having two buffers LVGL can draw into one buffer while the content of the other buffer is sent to display in the background. +Note that `lv_disp_draw_buf_t` needs to be static, global or dynamically allocated and not a local variable destroyed if goes out of the scope. + + +As you can see the draw buffer can be smaller than the screen. In this case, the larger areas will be redrawn in smaller parts that fit into the draw buffer(s). +If only a small area changes (e.g. a button is pressed) then only that area will be refreshed. + +A larger buffer results in better performance but above 1/10 screen sized buffer(s) there is no significant performance improvement. +Therefore it's recommended to choose the size of the draw buffer(s) to at least 1/10 screen sized. + +If only **one buffer** is used LVGL draws the content of the screen into that draw buffer and sends it to the display. + +If **two buffers** are used LVGL can draw into one buffer while the content of the other buffer is sent to display in the background. DMA or other hardware should be used to transfer the data to the display to let the CPU draw meanwhile. -This way the rendering and refreshing of the display become parallel. -Similarly to the *One buffer*, LVGL will draw the display's content in chunks if the buffer is smaller than the area to refresh. -3. **Two screen-sized buffers**. -In contrast to *Two non-screen-sized buffers* LVGL will always provide the whole screen's content not only chunks. -This way the driver can simply change the address of the frame buffer to the buffer received from LVGL. -Therefore this method works the best when the MCU has an LCD/TFT interface and the frame buffer is just a location in the RAM. +This way, the rendering and refreshing of the display become parallel. -You can measure the performance of your display configuration using the [benchmark example](https://github.com/lvgl/lv_examples/tree/master/src/lv_demo_benchmark). +In the display driver (`lv_disp_drv_t`) the `full_refresh` bit can be enabled to force LVGL always redraw the whole screen. It works in both *one buffer* and *two buffers* modes. + +If `full_refresh` is enabled and 2 screen sized draw buffers are provided, LVGL work as "traditional" double buffering. +It means in `flush_cb` only the address of the frame buffer needs to be changed to provided pointer (`color_p` parameter). +This configuration should be used if the MCU has LCD controller periphery and not with an external display controller (e.g. ILI9341 or SSD1963). + +You can measure the performance of different draw buffer configurations using the [benchmark example](https://github.com/lvgl/lv_examples/tree/master/src/lv_demo_benchmark). ## Display driver -Once the buffer initialization is ready the display drivers need to be initialized. In the most simple case only the following two fields of `lv_disp_drv_t` needs to be set: -- **buffer** pointer to an initialized `lv_disp_buf_t` variable. -- **flush_cb** a callback function to copy a buffer's content to a specific area of the display. `lv_disp_flush_ready()` needs to be called when flushing is ready. LVGL might render the screen in multiple chunks and therefore call `flush_cb` multiple times. To see which is the last chunk of rendering use `lv_disp_flush_is_last()`. +Once the buffer initialization is ready a `lv_disp_drv_t` display drivers need to be +1. initialized with `lv_disp_drv_init(&disp_drv)` +2. its fields needs to be set and +3. registered in LVGL with `lv_disp_drv_register(&disp_drv)` -There are some optional data fields: -- **hor_res** horizontal resolution of the display. (`LV_HOR_RES_MAX` by default from *lv_conf.h*). -- **ver_res** vertical resolution of the display. (`LV_VER_RES_MAX` by default from *lv_conf.h*). -- **color_chroma_key** a color which will be drawn as transparent on chrome keyed images. `LV_COLOR_TRANSP` by default from *lv_conf.h*). -- **user_data** custom user data for the driver. Its type can be modified in lv_conf.h. -- **anti-aliasing** use anti-aliasing (edge smoothing). `LV_ANTIALIAS` by default from *lv_conf.h*. -- **rotated** and **sw_rotate** See the [rotation](#rotation) section below. -- **screen_transp** if `1` the screen can have transparent or opaque style. `LV_COLOR_SCREEN_TRANSP` needs to enabled in *lv_conf.h*. +Note that `lv_disp_drv_t` needs to be static, global or dynamically allocated and not a local variable destroyed if goes out of the scope. -To use a GPU the following callbacks can be used: -- **gpu_fill_cb** fill an area in memory with colors. -- **gpu_blend_cb** blend two memory buffers using opacity. -- **gpu_wait_cb** if any GPU function return, while the GPU is still working LVGL, will use this function when required the be sure GPU rendering is ready. +### Mandatory fields +In the most simple case only the following fields of `lv_disp_drv_t` needs to be set: +- `draw_buf` pointer to an initialized `lv_disp_draw_buf_t` variable. +- `flush_cb` a callback function to copy a buffer's content to a specific area of the display. +`lv_disp_flush_ready(&disp_drv)` needs to be called when flushing is ready. +LVGL might render the screen in multiple chunks and therefore call `flush_cb` multiple times. To see which is the last chunk of rendering use `lv_disp_flush_is_last(&disp_drv)`. +- `hor_res` horizontal resolution of the display in pixels. +- `ver_res` vertical resolution of the display in pixels. -Note that, these functions need to draw to the memory (RAM) and not your display directly. +### Optional fields +There are some optional data fields: +- `color_chroma_key` A color which will be drawn as transparent on chrome keyed images. Set to `LV_COLOR_CHROMA_KEY` by default from `lv_conf.h`. +- `user_data` A custom `void `user data for the driver.. +- `anti_aliasing` use anti-aliasing (edge smoothing). Enabled by default if `LV_COLOR_DEPTH` is set to at least 16 in `lv_conf.h`. +- `rotated` and `sw_rotate` See the [rotation](#rotation) section below. +- `screen_transp` if `1` the screen itself can have transparency as well. `LV_COLOR_SCREEN_TRANSP` needs to enabled in `lv_conf.h` and requires `LV_COLOR_DEPTH 32`. Some other optional callbacks to make easier and more optimal to work with monochrome, grayscale or other non-standard RGB displays: -- **rounder_cb** round the coordinates of areas to redraw. E.g. a 2x2 px can be converted to 2x8. +- `rounder_cb` Round the coordinates of areas to redraw. E.g. a 2x2 px can be converted to 2x8. It can be used if the display controller can refresh only areas with specific height or width (usually 8 px height with monochrome displays). -- **set_px_cb** a custom function to write the *display buffer*. -It can be used to store the pixels more compactly if the display has a special color format. (e.g. 1-bit monochrome, 2-bit grayscale etc.) -This way the buffers used in `lv_disp_buf_t` can be smaller to hold only the required number of bits for the given area size. `set_px_cb` is not working with `Two screen-sized buffers` display buffer configuration. -- **monitor_cb** a callback function tells how many pixels were refreshed in how much time. -- **clean_dcache_cb** a callback for cleaning any caches related to the display +- `set_px_cb` a custom function to write the draw buffer. It can be used to store the pixels more compactly i nthe draw buffer if the display has a special color format. (e.g. 1-bit monochrome, 2-bit grayscale etc.) +This way the buffers used in `lv_disp_draw_buf_t` can be smaller to hold only the required number of bits for the given area size. Rendering with `set_px_cb` is slower than normal rendering. +- `monitor_cb` A callback function that tells how many pixels were refreshed in how much time. +- `clean_dcache_cb` A callback for cleaning any caches related to the display. -To set the fields of *lv_disp_drv_t* variable it needs to be initialized with `lv_disp_drv_init(&disp_drv)`. -And finally to register a display for LVGL `lv_disp_drv_register(&disp_drv)` needs to be called. +To use a GPU the following callbacks can be used: +- `gpu_fill_cb` fill an area in the memory with a color. +- `gpu_wait_cb` if any GPU function return, while the GPU is still working, LVGL will use this function when required the be sure GPU rendering is ready. +### Examples All together it looks like this: ```c - lv_disp_drv_t disp_drv; /*A variable to hold the drivers. Can be local variable*/ + static lv_disp_drv_t disp_drv; /*A variable to hold the drivers. Must be static or global.*/ lv_disp_drv_init(&disp_drv); /*Basic initialization*/ - disp_drv.buffer = &disp_buf; /*Set an initialized buffer*/ + disp_drv.draw_buf = &disp_buf; /*Set an initialized buffer*/ disp_drv.flush_cb = my_flush_cb; /*Set a flush callback to draw to the display*/ + disp_drv.hor_res = 320; /*Set the horizontal resolution in pixels*/ + disp_drv.ver_res = 240; /*Set the vertical resolution in pixels*/ + lv_disp_t * disp; disp = lv_disp_drv_register(&disp_drv); /*Register the driver and save the created display objects*/ ``` -Here some simple examples of the callbacks: +Here are some simple examples of the callbacks: ```c void my_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p) { @@ -115,19 +133,11 @@ void my_gpu_fill_cb(lv_disp_drv_t * disp_drv, lv_color_t * dest_buf, const lv_ar } } -void my_gpu_blend_cb(lv_disp_drv_t * disp_drv, lv_color_t * dest, const lv_color_t * src, uint32_t length, lv_opa_t opa) -{ - /*It's an example code which should be done by your GPU*/ - uint32_t i; - for(i = 0; i < length; i++) { - dest[i] = lv_color_mix(dest[i], src[i], opa); - } -} void my_rounder_cb(lv_disp_drv_t * disp_drv, lv_area_t * area) { - /* Update the areas as needed. Can be only larger. - * For example to always have lines 8 px height:*/ + /* Update the areas as needed. + * For example make the area to start only on 8th rows and have Nx8 pixel height:*/ area->y1 = area->y1 & 0x07; area->y2 = (area->y2 & 0x07) + 8; } @@ -136,9 +146,9 @@ void my_set_px_cb(lv_disp_drv_t * disp_drv, uint8_t * buf, lv_coord_t buf_w, lv_ { /* Write to the buffer as required for the display. * Write only 1-bit for monochrome displays mapped vertically:*/ - buf += buf_w * (y >> 3) + x; - if(lv_color_brightness(color) > 128) (*buf) |= (1 << (y % 8)); - else (*buf) &= ~(1 << (y % 8)); + buf += buf_w * (y >> 3) + x; + if(lv_color_brightness(color) > 128) (*buf) |= (1 << (y % 8)); + else (*buf) &= ~(1 << (y % 8)); } void my_monitor_cb(lv_disp_drv_t * disp_drv, uint32_t time, uint32_t px) @@ -169,6 +179,12 @@ Display rotation can also be changed at runtime using the `lv_disp_set_rotation( Support for software rotation is a new feature, so there may be some glitches/bugs depending on your configuration. If you encounter a problem please open an issue on [GitHub](https://github.com/lvgl/lvgl/issues). +## Further reading + +See [lv_port_disp_template.c](https://github.com/lvgl/lvgl/blob/master/examples/porting/lv_port_disp_template.c) for a template for your own driver. + +Check out the [Drawing](/overview/drawing) section to learn more about how rendering works in LVGL. + ## API ```eval_rst diff --git a/docs/porting/indev.md b/docs/porting/indev.md index c90dbbc43..fbe8ffc43 100644 --- a/docs/porting/indev.md +++ b/docs/porting/indev.md @@ -17,21 +17,18 @@ indev_drv.read_cb =... /*See below.*/ lv_indev_t * my_indev = lv_indev_drv_register(&indev_drv); ``` -**type** can be -- **LV_INDEV_TYPE_POINTER** touchpad or mouse -- **LV_INDEV_TYPE_KEYPAD** keyboard or keypad -- **LV_INDEV_TYPE_ENCODER** encoder with left, right, push options -- **LV_INDEV_TYPE_BUTTON** external buttons pressing the screen - -**read_cb** is a function pointer which will be called periodically to report the current state of an input device. -It can also buffer data and return `false` when no more data to be read or `true` when the buffer is not empty. +`type` can be +- `LV_INDEV_TYPE_POINTER` touchpad or mouse +- `LV_INDEV_TYPE_KEYPAD` keyboard or keypad +- `LV_INDEV_TYPE_ENCODER` encoder with left/right turn and push options +- `LV_INDEV_TYPE_BUTTON` external buttons virtually pressing the screen +`read_cb` is a function pointer which will be called periodically to report the current state of an input device. Visit [Input devices](/overview/indev) to learn more about input devices in general. - ### Touchpad, mouse or any pointer -Input devices which can click points of the screen belong to this category. +Input devices that can click points of the screen belong to this category. ```c indev_drv.type = LV_INDEV_TYPE_POINTER; @@ -39,12 +36,15 @@ indev_drv.read_cb = my_input_read; ... -bool my_input_read(lv_indev_drv_t * drv, lv_indev_data_t*data) +void my_input_read(lv_indev_drv_t * drv, lv_indev_data_t*data) { + if(touchpad_pressed) { data->point.x = touchpad_x; data->point.y = touchpad_y; - data->state = LV_INDEV_STATE_PR or LV_INDEV_STATE_REL; - return false; /*No buffering now so no more data read*/ + data->state = LV_INDEV_STATE_PRESSED; + } else { + data->state = LV_INDEV_STATE_RELEASED; + } } ``` @@ -59,7 +59,6 @@ Full keyboards with all the letters or simple keypads with a few navigation butt To use a keyboard/keypad: - Register a `read_cb` function with `LV_INDEV_TYPE_KEYPAD` type. -- Enable `LV_USE_GROUP` in *lv_conf.h* - An object group has to be created: `lv_group_t * g = lv_group_create()` and objects have to be added to it with `lv_group_add_obj(g, obj)` - The created group has to be assigned to an input device: `lv_indev_set_group(my_indev, g)` (`my_indev` is the return value of `lv_indev_drv_register`) - Use `LV_KEY_...` to navigate among the objects in the group. See `lv_core/lv_group.h` for the available keys. @@ -70,13 +69,11 @@ indev_drv.read_cb = keyboard_read; ... -bool keyboard_read(lv_indev_drv_t * drv, lv_indev_data_t*data){ +void keyboard_read(lv_indev_drv_t * drv, lv_indev_data_t*data){ data->key = last_key(); /*Get the last pressed or released key*/ - if(key_pressed()) data->state = LV_INDEV_STATE_PR; - else data->state = LV_INDEV_STATE_REL; - - return false; /*No buffering now so no more data read*/ + if(key_pressed()) data->state = LV_INDEV_STATE_PRESSED; + else data->state = LV_INDEV_STATE_RELEASED; } ``` @@ -103,23 +100,22 @@ indev_drv.read_cb = encoder_read; ... -bool encoder_read(lv_indev_drv_t * drv, lv_indev_data_t*data){ +void encoder_read(lv_indev_drv_t * drv, lv_indev_data_t*data){ data->enc_diff = enc_get_new_moves(); - if(enc_pressed()) data->state = LV_INDEV_STATE_PR; - else data->state = LV_INDEV_STATE_REL; - - return false; /*No buffering now so no more data read*/ + if(enc_pressed()) data->state = LV_INDEV_STATE_PRESSED; + else data->state = LV_INDEV_STATE_RELEASED; } ``` + #### Using buttons with Encoder logic -In addition to standard encoder behavior, you can also utilise its logic to navigate(focus) and edit widgets using buttons. -This is especially handy if you have only few buttons avalible, or you want to use other buttons in addition to encoder wheel. +In addition to standard encoder behavior, you can also utilize its logic to navigate(focus) and edit widgets using buttons. +This is especially handy if you have only few buttons available, or you want to use other buttons in addition to encoder wheel. -You need to have 3 buttons avalible: -- **LV_KEY_ENTER** will simulate press or pushing of the encoder button -- **LV_KEY_LEFT** will simulate turnuing encoder left -- **LV_KEY_RIGHT** will simulate turnuing encoder right +You need to have 3 buttons available: +- `LV_KEY_ENTER` will simulate press or pushing of the encoder button +- `LV_KEY_LEFT` will simulate turning encoder left +- `LV_KEY_RIGHT` will simulate turning encoder right - other keys will be passed to the focused widget If you hold the keys it will simulate encoder click with period specified in `indev_drv.long_press_rep_time`. @@ -133,9 +129,9 @@ indev_drv.read_cb = encoder_with_keys_read; bool encoder_with_keys_read(lv_indev_drv_t * drv, lv_indev_data_t*data){ data->key = last_key(); /*Get the last pressed or released key*/ /* use LV_KEY_ENTER for encoder press */ - if(key_pressed()) data->state = LV_INDEV_STATE_PR; + if(key_pressed()) data->state = LV_INDEV_STATE_PRESSED; else { - data->state = LV_INDEV_STATE_REL; + data->state = LV_INDEV_STATE_RELEASED; /* Optionally you can also use enc_diff, if you have encoder*/ data->enc_diff = enc_get_new_moves(); } @@ -160,38 +156,46 @@ indev_drv.read_cb = button_read; ... -bool button_read(lv_indev_drv_t * drv, lv_indev_data_t*data){ +void button_read(lv_indev_drv_t * drv, lv_indev_data_t*data){ static uint32_t last_btn = 0; /*Store the last pressed button*/ int btn_pr = my_btn_read(); /*Get the ID (0,1,2...) of the pressed button*/ if(btn_pr >= 0) { /*Is there a button press? (E.g. -1 indicated no button was pressed)*/ last_btn = btn_pr; /*Save the ID of the pressed button*/ - data->state = LV_INDEV_STATE_PR; /*Set the pressed state*/ + data->state = LV_INDEV_STATE_PRESSED; /*Set the pressed state*/ } else { - data->state = LV_INDEV_STATE_REL; /*Set the released state*/ + data->state = LV_INDEV_STATE_RELEASED; /*Set the released state*/ } data->btn = last_btn; /*Save the last button*/ - - return false; /*No buffering now so no more data read*/ } ``` ## Other features -Besides `read_cb` a `feedback_cb` callback can be also specified in `lv_indev_drv_t`. -`feedback_cb` is called when any type of event is sent by the input devices. (independently from its type). It allows making feedback for the user e.g. to play a sound on `LV_EVENT_CLICK`. +### Parameters -The default value of the following parameters can be set in *lv_conf.h* but the default value can be overwritten in `lv_indev_drv_t`: -- **drag_limit** Number of pixels to slide before actually drag the object -- **drag_throw** Drag throw slow-down in [%]. Greater value means faster slow-down -- **long_press_time** Press time to send `LV_EVENT_LONG_PRESSED` (in milliseconds) -- **long_press_rep_time** Interval of sending `LV_EVENT_LONG_PRESSED_REPEAT` (in milliseconds) -- **read_task** pointer to the `lv_task` which reads the input device. Its parameters can be changed by `lv_task_...()` functions +The default value of the following parameters can changed in `lv_indev_drv_t`: +- `scroll_limit` Number of pixels to slide before actually scrolling the object. +- `scroll_throw` Scroll throw (momentum) slow-down in [%]. Greater value means faster slow-down. +- `long_press_time` Press time to send `LV_EVENT_LONG_PRESSED` (in milliseconds) +- `long_press_rep_time` Interval of sending `LV_EVENT_LONG_PRESSED_REPEAT` (in milliseconds) +- `read_timer` pointer to the `lv_rimer` which reads the input device. Its parameters can be changed by `lv_timer_...()` functions. `LV_INDEV_DEF_READ_PERIOD` in `lv_conf.h` sets the default read period. +### Feedback +Besides `read_cb` a `feedback_cb` callback can be also specified in `lv_indev_drv_t`. +`feedback_cb` is called when any type of event is sent by the input devices. (independently from its type). It allows making feedback for the user e.g. to play a sound on `LV_EVENT_CLICKED`. + + +### Associating with a display Every Input device is associated with a display. By default, a new input device is added to the lastly created or the explicitly selected (using `lv_disp_set_default()`) display. The associated display is stored and can be changed in `disp` field of the driver. +### Event driven reading +By default LVGL calls `read_cb` periodically. This way there is a chance that some user gestures are missed. + +To solve this you write an event driven driver for your input device that buffers measured data. In `read_cb` you can set the buffered data instead of reading the input device. +You can set the `data->continue_reding` flag to LVGL there is more data to read and call `read_cb` again. ## API |