✏️ 修改(lvgl):修改页面元素

改为scroll滚动例程
结束
This commit is contained in:
Alvin Young 2023-08-05 00:47:02 +08:00
parent b119eeb61d
commit e618364302
2 changed files with 107 additions and 3 deletions

View File

@ -28,6 +28,8 @@
#include "CST816T.h"
#include "lv_conf.h"
#include "lvgl.h"
#include "driver/gpio.h"
#include <stdio.h>
#define LV_TICK_PERIOD_MS 1
@ -76,7 +78,7 @@ static void cst_scan_gesture()
if (index++ == 500) {
index = 0;
if (app_display_handle.back_light_auto_off) {
if (!app_display_handle.back_light_state) {
if (app_display_handle.back_light_state) {
lcdBacklightOff(&lcd_disp_dev);
app_display_handle.back_light_state = 0;
}
@ -84,6 +86,7 @@ static void cst_scan_gesture()
}
if (cst816t_available()) {
index = 0;
release_timeout = 5;
/*printf("gesture.id %u \n", data.gestureID);
printf("points %u \n", data.points);
@ -143,6 +146,93 @@ static void _display_thread_entry()
}
}
static void scroll_event_cb(lv_event_t * e)
{
lv_obj_t * cont = lv_event_get_target(e);
lv_area_t cont_a;
lv_obj_get_coords(cont, &cont_a);
lv_coord_t cont_y_center = cont_a.y1 + lv_area_get_height(&cont_a) / 2;
lv_coord_t r = lv_obj_get_height(cont) * 7 / 10;
uint32_t i;
uint32_t child_cnt = lv_obj_get_child_cnt(cont);
for(i = 0; i < child_cnt; i++) {
lv_obj_t * child = lv_obj_get_child(cont, i);
lv_area_t child_a;
lv_obj_get_coords(child, &child_a);
lv_coord_t child_y_center = child_a.y1 + lv_area_get_height(&child_a) / 2;
lv_coord_t diff_y = child_y_center - cont_y_center;
diff_y = LV_ABS(diff_y);
/*Get the x of diff_y on a circle.*/
lv_coord_t x;
/*If diff_y is out of the circle use the last point of the circle (the radius)*/
if(diff_y >= r) {
x = r;
} else {
/*Use Pythagoras theorem to get x from radius and y*/
uint32_t x_sqr = r * r - diff_y * diff_y;
lv_sqrt_res_t res;
lv_sqrt(x_sqr, &res, 0x8000); /*Use lvgl's built in sqrt root function*/
x = r - res.i;
}
/*Translate the item by the calculated X coordinate*/
lv_obj_set_style_translate_x(child, x, 0);
if (x <= 4) {
lv_obj_set_style_bg_color(child, lv_color_hex(0xff0000), LV_PART_MAIN);
}else {
lv_obj_set_style_bg_color(child, lv_color_hex(0x0000ff), LV_PART_MAIN);
// lv_obj_set_height(child, 10);
}
printf("x = %d\r\n", x);
/*Use some opacity with larger translations*/
lv_opa_t opa = lv_map(x, 0, r, LV_OPA_TRANSP, LV_OPA_COVER);
// lv_obj_set_style_opa(child, LV_OPA_COVER - opa, 0);
}
}
/**
* Translate the object as they scroll
*/
void lv_example_scroll_233(void)
{
lv_obj_t * cont = lv_obj_create(lv_scr_act());
lv_obj_set_size(cont, 240, 280);
lv_obj_center(cont);
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
lv_obj_add_event_cb(cont, scroll_event_cb, LV_EVENT_SCROLL, NULL);
lv_obj_set_style_radius(cont, 10, 0);
lv_obj_set_style_clip_corner(cont, true, 0);
lv_obj_set_scroll_dir(cont, LV_DIR_VER);
lv_obj_set_scroll_snap_y(cont, LV_SCROLL_SNAP_CENTER);
lv_obj_set_scrollbar_mode(cont, LV_SCROLLBAR_MODE_OFF);
uint32_t i;
for(i = 0; i < 20; i++) {
lv_obj_t * btn = lv_btn_create(cont);
lv_obj_set_width(btn, lv_pct(100));
lv_obj_set_height(btn, 50);
lv_obj_set_style_radius(btn, 25, LV_PART_MAIN);
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text_fmt(label, "Button %"LV_PRIu32, i);
}
/*Update the buttons position manually for first*/
lv_event_send(cont, LV_EVENT_SCROLL, NULL);
/*Be sure the fist button is in the middle*/
lv_obj_scroll_to_view(lv_obj_get_child(cont, 0), LV_ANIM_OFF);
}
static void event_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
@ -227,11 +317,25 @@ static void _lvgl_thread_entry()
lv_port_indev_init();
lcdFillScreen(&lcd_disp_dev, WHITE);
lv_example_btn_1233();
lv_example_scroll_233();
gpio_set_direction(GPIO_NUM_0, GPIO_MODE_INPUT);
while (1) {
lv_task_handler();
vTaskDelay(pdMS_TO_TICKS(10));
if (!gpio_get_level(GPIO_NUM_0)) {
vTaskDelay(pdMS_TO_TICKS(100));
if (!gpio_get_level(GPIO_NUM_0)) {
if (app_display_handle.back_light_state) {
lcdBacklightOff(&lcd_disp_dev);
vTaskDelay(pdMS_TO_TICKS(100));
}else {
lcdBacklightOn(&lcd_disp_dev);
}
app_display_handle.back_light_state = !app_display_handle.back_light_state;
}
}
}
}

View File

@ -27,7 +27,7 @@ static lv_disp_draw_buf_t disp_buf;
static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
lv_indev_t* touch_indev;
#define DISP_BUF_SIZE (LV_HOR_RES_MAX * LV_VER_RES_MAX / 4)
#define DISP_BUF_SIZE (LV_HOR_RES_MAX * LV_VER_RES_MAX / 8)
static lv_disp_drv_t g_disp_drv;