init lvgl code
This commit is contained in:
19
LVGL.Simulator/lvgl/examples/event/index.rst
Normal file
19
LVGL.Simulator/lvgl/examples/event/index.rst
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
Button click event
|
||||
"""""""""""""""""""
|
||||
|
||||
.. lv_example:: event/lv_example_event_1
|
||||
:language: c
|
||||
|
||||
Handle multiple events
|
||||
""""""""""""""""""""""""
|
||||
.. lv_example:: event/lv_example_event_2
|
||||
:language: c
|
||||
|
||||
|
||||
Event bubbling
|
||||
""""""""""""""""""""""""
|
||||
.. lv_example:: event/lv_example_event_3
|
||||
:language: c
|
||||
|
||||
|
||||
41
LVGL.Simulator/lvgl/examples/event/lv_example_event.h
Normal file
41
LVGL.Simulator/lvgl/examples/event/lv_example_event.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file lv_example_event.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_EXAMPLE_EVENT_H
|
||||
#define LV_EXAMPLE_EVENT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
void lv_example_event_1(void);
|
||||
void lv_example_event_2(void);
|
||||
void lv_example_event_3(void);
|
||||
void lv_example_event_4(void);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /*LV_EXAMPLE_EVENT_H*/
|
||||
30
LVGL.Simulator/lvgl/examples/event/lv_example_event_1.c
Normal file
30
LVGL.Simulator/lvgl/examples/event/lv_example_event_1.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "../lv_examples.h"
|
||||
#if LV_BUILD_EXAMPLES && LV_USE_SWITCH
|
||||
|
||||
static void event_cb(lv_event_t * e)
|
||||
{
|
||||
LV_LOG_USER("Clicked");
|
||||
|
||||
static uint32_t cnt = 1;
|
||||
lv_obj_t * btn = lv_event_get_target(e);
|
||||
lv_obj_t * label = lv_obj_get_child(btn, 0);
|
||||
lv_label_set_text_fmt(label, "%"LV_PRIu32, cnt);
|
||||
cnt++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add click event to a button
|
||||
*/
|
||||
void lv_example_event_1(void)
|
||||
{
|
||||
lv_obj_t * btn = lv_btn_create(lv_scr_act());
|
||||
lv_obj_set_size(btn, 100, 50);
|
||||
lv_obj_center(btn);
|
||||
lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
lv_obj_t * label = lv_label_create(btn);
|
||||
lv_label_set_text(label, "Click me!");
|
||||
lv_obj_center(label);
|
||||
}
|
||||
|
||||
#endif
|
||||
25
LVGL.Simulator/lvgl/examples/event/lv_example_event_1.py
Normal file
25
LVGL.Simulator/lvgl/examples/event/lv_example_event_1.py
Normal file
@@ -0,0 +1,25 @@
|
||||
class Event_1():
|
||||
def __init__(self):
|
||||
self.cnt = 1
|
||||
#
|
||||
# Add click event to a button
|
||||
#
|
||||
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.set_size(100, 50)
|
||||
btn.center()
|
||||
btn.add_event_cb(self.event_cb, lv.EVENT.CLICKED, None)
|
||||
|
||||
label = lv.label(btn)
|
||||
label.set_text("Click me!")
|
||||
label.center()
|
||||
|
||||
def event_cb(self,e):
|
||||
print("Clicked")
|
||||
|
||||
btn = e.get_target()
|
||||
label = btn.get_child(0)
|
||||
label.set_text(str(self.cnt))
|
||||
self.cnt += 1
|
||||
|
||||
evt1 = Event_1()
|
||||
46
LVGL.Simulator/lvgl/examples/event/lv_example_event_2.c
Normal file
46
LVGL.Simulator/lvgl/examples/event/lv_example_event_2.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "../lv_examples.h"
|
||||
#if LV_BUILD_EXAMPLES && LV_USE_SWITCH
|
||||
|
||||
static void event_cb(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * label = lv_event_get_user_data(e);
|
||||
|
||||
switch(code) {
|
||||
case LV_EVENT_PRESSED:
|
||||
lv_label_set_text(label, "The last button event:\nLV_EVENT_PRESSED");
|
||||
break;
|
||||
case LV_EVENT_CLICKED:
|
||||
lv_label_set_text(label, "The last button event:\nLV_EVENT_CLICKED");
|
||||
break;
|
||||
case LV_EVENT_LONG_PRESSED:
|
||||
lv_label_set_text(label, "The last button event:\nLV_EVENT_LONG_PRESSED");
|
||||
break;
|
||||
case LV_EVENT_LONG_PRESSED_REPEAT:
|
||||
lv_label_set_text(label, "The last button event:\nLV_EVENT_LONG_PRESSED_REPEAT");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle multiple events
|
||||
*/
|
||||
void lv_example_event_2(void)
|
||||
{
|
||||
lv_obj_t * btn = lv_btn_create(lv_scr_act());
|
||||
lv_obj_set_size(btn, 100, 50);
|
||||
lv_obj_center(btn);
|
||||
|
||||
lv_obj_t * btn_label = lv_label_create(btn);
|
||||
lv_label_set_text(btn_label, "Click me!");
|
||||
lv_obj_center(btn_label);
|
||||
|
||||
lv_obj_t * info_label = lv_label_create(lv_scr_act());
|
||||
lv_label_set_text(info_label, "The last button event:\nNone");
|
||||
|
||||
lv_obj_add_event_cb(btn, event_cb, LV_EVENT_ALL, info_label);
|
||||
}
|
||||
|
||||
#endif
|
||||
22
LVGL.Simulator/lvgl/examples/event/lv_example_event_2.py
Normal file
22
LVGL.Simulator/lvgl/examples/event/lv_example_event_2.py
Normal file
@@ -0,0 +1,22 @@
|
||||
def event_cb(e,label):
|
||||
code = e.get_code()
|
||||
if code == lv.EVENT.PRESSED:
|
||||
label.set_text("The last button event:\nLV_EVENT_PRESSED")
|
||||
elif code == lv.EVENT.CLICKED:
|
||||
label.set_text("The last button event:\nLV_EVENT_CLICKED")
|
||||
elif code == lv.EVENT.LONG_PRESSED:
|
||||
label.set_text("The last button event:\nLV_EVENT_LONG_PRESSED")
|
||||
elif code == lv.EVENT.LONG_PRESSED_REPEAT:
|
||||
label.set_text("The last button event:\nLV_EVENT_LONG_PRESSED_REPEAT")
|
||||
btn = lv.btn(lv.scr_act())
|
||||
btn.set_size(100, 50)
|
||||
btn.center()
|
||||
|
||||
btn_label = lv.label(btn)
|
||||
btn_label.set_text("Click me!")
|
||||
btn_label.center()
|
||||
|
||||
info_label = lv.label(lv.scr_act())
|
||||
info_label.set_text("The last button event:\nNone")
|
||||
|
||||
btn.add_event_cb(lambda e: event_cb(e,info_label), lv.EVENT.ALL, None)
|
||||
44
LVGL.Simulator/lvgl/examples/event/lv_example_event_3.c
Normal file
44
LVGL.Simulator/lvgl/examples/event/lv_example_event_3.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "../lv_examples.h"
|
||||
#if LV_BUILD_EXAMPLES && LV_USE_FLEX
|
||||
|
||||
static void event_cb(lv_event_t * e)
|
||||
{
|
||||
/*The original target of the event. Can be the buttons or the container*/
|
||||
lv_obj_t * target = lv_event_get_target(e);
|
||||
|
||||
/*The current target is always the container as the event is added to it*/
|
||||
lv_obj_t * cont = lv_event_get_current_target(e);
|
||||
|
||||
/*If container was clicked do nothing*/
|
||||
if(target == cont) return;
|
||||
|
||||
/*Make the clicked buttons red*/
|
||||
lv_obj_set_style_bg_color(target, lv_palette_main(LV_PALETTE_RED), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrate event bubbling
|
||||
*/
|
||||
void lv_example_event_3(void)
|
||||
{
|
||||
|
||||
lv_obj_t * cont = lv_obj_create(lv_scr_act());
|
||||
lv_obj_set_size(cont, 290, 200);
|
||||
lv_obj_center(cont);
|
||||
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_ROW_WRAP);
|
||||
|
||||
uint32_t i;
|
||||
for(i = 0; i < 30; i++) {
|
||||
lv_obj_t * btn = lv_btn_create(cont);
|
||||
lv_obj_set_size(btn, 80, 50);
|
||||
lv_obj_add_flag(btn, LV_OBJ_FLAG_EVENT_BUBBLE);
|
||||
|
||||
lv_obj_t * label = lv_label_create(btn);
|
||||
lv_label_set_text_fmt(label, "%"LV_PRIu32, i);
|
||||
lv_obj_center(label);
|
||||
}
|
||||
|
||||
lv_obj_add_event_cb(cont, event_cb, LV_EVENT_CLICKED, NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
32
LVGL.Simulator/lvgl/examples/event/lv_example_event_3.py
Normal file
32
LVGL.Simulator/lvgl/examples/event/lv_example_event_3.py
Normal file
@@ -0,0 +1,32 @@
|
||||
def event_cb(e):
|
||||
|
||||
# The original target of the event. Can be the buttons or the container
|
||||
target = e.get_target()
|
||||
# print(type(target))
|
||||
|
||||
# If container was clicked do nothing
|
||||
if type(target) != type(lv.btn()):
|
||||
return
|
||||
|
||||
# Make the clicked buttons red
|
||||
target.set_style_bg_color(lv.palette_main(lv.PALETTE.RED), 0)
|
||||
|
||||
#
|
||||
# Demonstrate event bubbling
|
||||
#
|
||||
|
||||
cont = lv.obj(lv.scr_act())
|
||||
cont.set_size(320, 200)
|
||||
cont.center()
|
||||
cont.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
|
||||
|
||||
for i in range(30):
|
||||
btn = lv.btn(cont)
|
||||
btn.set_size(80, 50)
|
||||
btn.add_flag(lv.obj.FLAG.EVENT_BUBBLE)
|
||||
|
||||
label = lv.label(btn)
|
||||
label.set_text(str(i))
|
||||
label.center()
|
||||
|
||||
cont.add_event_cb(event_cb, lv.EVENT.CLICKED, None)
|
||||
66
LVGL.Simulator/lvgl/examples/event/lv_example_event_4.c
Normal file
66
LVGL.Simulator/lvgl/examples/event/lv_example_event_4.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "../lv_examples.h"
|
||||
|
||||
#if LV_BUILD_EXAMPLES
|
||||
|
||||
static int n = 3;
|
||||
static lv_obj_t * label = NULL;
|
||||
|
||||
static void timer_cb(lv_timer_t * timer)
|
||||
{
|
||||
if(n < 3 || n > 32) {
|
||||
n = 3;
|
||||
}
|
||||
else {
|
||||
static uint32_t old_tick = 0;
|
||||
uint32_t tick = lv_tick_get();
|
||||
if(!old_tick) {
|
||||
old_tick = tick;
|
||||
}
|
||||
if(tick - old_tick > 3000) {
|
||||
n++;
|
||||
lv_label_set_text_fmt(label, "%d sides", n);
|
||||
old_tick = tick;
|
||||
}
|
||||
}
|
||||
lv_obj_invalidate(timer->user_data);
|
||||
}
|
||||
|
||||
static void event_cb(lv_event_t * e)
|
||||
{
|
||||
/*The original target of the event. Can be the buttons or the container*/
|
||||
lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
|
||||
lv_draw_rect_dsc_t draw_dsc;
|
||||
lv_draw_rect_dsc_init(&draw_dsc);
|
||||
draw_dsc.bg_color = lv_palette_main(LV_PALETTE_LIGHT_GREEN);
|
||||
draw_dsc.bg_opa = LV_OPA_COVER;
|
||||
lv_point_t points[32];
|
||||
int i, r = 150;
|
||||
uint32_t tick = lv_tick_get();
|
||||
for(i = 0; i < n; i++) {
|
||||
int angle = i * 360 / n + ((tick % 36000) / 100);
|
||||
lv_coord_t x = 150 + (r * lv_trigo_cos(angle) >> LV_TRIGO_SHIFT), y =
|
||||
150 + (r * lv_trigo_sin(angle) >> LV_TRIGO_SHIFT);
|
||||
points[i].x = x;
|
||||
points[i].y = y;
|
||||
}
|
||||
lv_draw_polygon(draw_ctx, &draw_dsc, points, n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrate event bubbling
|
||||
*/
|
||||
void lv_example_event_4(void)
|
||||
{
|
||||
|
||||
lv_obj_t * cont = lv_obj_create(lv_scr_act());
|
||||
lv_obj_remove_style_all(cont);
|
||||
lv_obj_set_size(cont, 300, 300);
|
||||
label = lv_label_create(cont);
|
||||
lv_label_set_text_fmt(label, "%d sides", n);
|
||||
lv_obj_center(label);
|
||||
|
||||
lv_obj_add_event_cb(cont, event_cb, LV_EVENT_DRAW_MAIN, NULL);
|
||||
lv_timer_create(timer_cb, 17, cont);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user