1 #include "../../lv_examples.h"
2 #if LV_USE_MSG && LV_USE_SLIDER && LV_USE_LABEL && LV_BUILD_EXAMPLES
3 
4 /*Define a message ID*/
5 #define MSG_NEW_TEMPERATURE     1
6 
7 static void slider_event_cb(lv_event_t * e);
8 static void label_event_cb(lv_event_t * e);
9 
10 /**
11  * A slider sends a message on value change and a label display's that value
12  */
lv_example_msg_1(void)13 void lv_example_msg_1(void)
14 {
15     /*Create a slider in the center of the display*/
16     lv_obj_t * slider = lv_slider_create(lv_scr_act());
17     lv_obj_center(slider);
18     lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
19 
20     /*Create a label below the slider*/
21     lv_obj_t * label = lv_label_create(lv_scr_act());
22     lv_obj_add_event_cb(label, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
23     lv_label_set_text(label, "0%");
24     lv_obj_align(label, LV_ALIGN_CENTER, 0, 30);
25 
26     /*Subscribe the label to a message. Also use the user_data to set a format string here.*/
27     lv_msg_subsribe_obj(MSG_NEW_TEMPERATURE, label, "%d °C");
28 }
29 
slider_event_cb(lv_event_t * e)30 static void slider_event_cb(lv_event_t * e)
31 {
32     /*Notify all subscribers (only the label now) that the slider value has been changed*/
33     lv_obj_t * slider = lv_event_get_target(e);
34     int32_t v = lv_slider_get_value(slider);
35     lv_msg_send(MSG_NEW_TEMPERATURE, &v);
36 }
37 
label_event_cb(lv_event_t * e)38 static void label_event_cb(lv_event_t * e)
39 {
40     lv_obj_t * label = lv_event_get_target(e);
41     lv_msg_t * m = lv_event_get_msg(e);
42 
43     const char * fmt = lv_msg_get_user_data(m);
44     const int32_t * v = lv_msg_get_payload(m);
45 
46     lv_label_set_text_fmt(label, fmt, *v);
47 }
48 
49 #endif
50