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_INC 1
6 #define MSG_DEC 2
7 #define MSG_SET 3
8 #define MSG_UPDATE 4
9 #define MSG_UPDATE_REQUEST 5
10
11 static void value_handler(void * s, lv_msg_t * m);
12 static void value_handler(void * s, lv_msg_t * m);
13 static void btn_event_cb(lv_event_t * e);
14 static void label_event_cb(lv_event_t * e);
15 static void slider_event_cb(lv_event_t * e);
16
17 /**
18 * Show how an increment button, a decrement button, as slider can set a value
19 * and a label display it.
20 * The current value (i.e. the system's state) is stored only in one static variable in a function
21 * and no global variables are required.
22 */
lv_example_msg_3(void)23 void lv_example_msg_3(void)
24 {
25
26 lv_msg_subsribe(MSG_INC, value_handler, NULL);
27 lv_msg_subsribe(MSG_DEC, value_handler, NULL);
28 lv_msg_subsribe(MSG_SET, value_handler, NULL);
29 lv_msg_subsribe(MSG_UPDATE, value_handler, NULL);
30 lv_msg_subsribe(MSG_UPDATE_REQUEST, value_handler, NULL);
31
32 lv_obj_t * panel = lv_obj_create(lv_scr_act());
33 lv_obj_set_size(panel, 250, LV_SIZE_CONTENT);
34 lv_obj_center(panel);
35 lv_obj_set_flex_flow(panel, LV_FLEX_FLOW_ROW);
36 lv_obj_set_flex_align(panel, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
37
38 lv_obj_t * btn;
39 lv_obj_t * label;
40
41 /*Up button*/
42 btn = lv_btn_create(panel);
43 lv_obj_set_flex_grow(btn, 1);
44 lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);
45 label = lv_label_create(btn);
46 lv_label_set_text(label, LV_SYMBOL_LEFT);
47 lv_obj_center(label);
48
49 /*Current value*/
50 label = lv_label_create(panel);
51 lv_obj_set_flex_grow(label, 2);
52 lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
53 lv_label_set_text(label, "?");
54 lv_msg_subsribe_obj(MSG_UPDATE, label, NULL);
55 lv_obj_add_event_cb(label, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
56
57 /*Down button*/
58 btn = lv_btn_create(panel);
59 lv_obj_set_flex_grow(btn, 1);
60 lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);
61 label = lv_label_create(btn);
62 lv_label_set_text(label, LV_SYMBOL_RIGHT);
63 lv_obj_center(label);
64
65 /*Slider*/
66 lv_obj_t * slider = lv_slider_create(panel);
67 lv_obj_set_flex_grow(slider, 1);
68 lv_obj_add_flag(slider, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
69 lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_ALL, NULL);
70 lv_msg_subsribe_obj(MSG_UPDATE, slider, NULL);
71
72 /* As there are new UI elements that don't know the system's state
73 * send an UPDATE REQUEST message which will trigger an UPDATE message with the current value*/
74 lv_msg_send(MSG_UPDATE_REQUEST, NULL);
75 }
76
value_handler(void * s,lv_msg_t * m)77 static void value_handler(void * s, lv_msg_t * m)
78 {
79 LV_UNUSED(s);
80
81 static int32_t value = 10;
82 int32_t old_value = value;
83 switch(lv_msg_get_id(m)) {
84 case MSG_INC:
85 if(value < 100) value++;
86 break;
87 case MSG_DEC:
88 if(value > 0) value--;
89 break;
90 case MSG_SET: {
91 const int32_t * new_value = lv_msg_get_payload(m);
92 value = *new_value;
93 }
94 break;
95 case MSG_UPDATE_REQUEST:
96 lv_msg_send(MSG_UPDATE, &value);
97 break;
98 default:
99 break;
100 }
101
102 if(value != old_value) {
103 lv_msg_send(MSG_UPDATE, &value);
104 }
105 }
106
btn_event_cb(lv_event_t * e)107 static void btn_event_cb(lv_event_t * e)
108 {
109 lv_obj_t * btn = lv_event_get_target(e);
110 lv_event_code_t code = lv_event_get_code(e);
111 if(code == LV_EVENT_CLICKED || code == LV_EVENT_LONG_PRESSED_REPEAT) {
112 if(lv_obj_get_index(btn) == 0) { /*First object is the dec. button*/
113 lv_msg_send(MSG_DEC, NULL);
114 }
115 else {
116 lv_msg_send(MSG_INC, NULL);
117 }
118 }
119 }
120
label_event_cb(lv_event_t * e)121 static void label_event_cb(lv_event_t * e)
122 {
123 lv_obj_t * label = lv_event_get_target(e);
124 lv_event_code_t code = lv_event_get_code(e);
125 if(code == LV_EVENT_MSG_RECEIVED) {
126 lv_msg_t * m = lv_event_get_msg(e);
127 if(lv_msg_get_id(m) == MSG_UPDATE) {
128 const int32_t * v = lv_msg_get_payload(m);
129 lv_label_set_text_fmt(label, "%d %%", *v);
130 }
131 }
132 }
133
slider_event_cb(lv_event_t * e)134 static void slider_event_cb(lv_event_t * e)
135 {
136 lv_obj_t * slider = lv_event_get_target(e);
137 lv_event_code_t code = lv_event_get_code(e);
138 if(code == LV_EVENT_VALUE_CHANGED) {
139 int32_t v = lv_slider_get_value(slider);
140 lv_msg_send(MSG_SET, &v);
141 }
142 else if(code == LV_EVENT_MSG_RECEIVED) {
143 lv_msg_t * m = lv_event_get_msg(e);
144 if(lv_msg_get_id(m) == MSG_UPDATE) {
145 const int32_t * v = lv_msg_get_payload(m);
146 lv_slider_set_value(slider, *v, LV_ANIM_OFF);
147 }
148 }
149 }
150
151 #endif
152