acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
lv_example_msg_3.c (4790B)
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 */ 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 73 /* As there are new UI elements that don't know the system's state 74 * send an UPDATE REQUEST message which will trigger an UPDATE message with the current value*/ 75 lv_msg_send(MSG_UPDATE_REQUEST, NULL); 76 } 77 78 79 static void value_handler(void * s, lv_msg_t * m) 80 { 81 LV_UNUSED(s); 82 83 static int32_t value = 10; 84 int32_t old_value = value; 85 switch(lv_msg_get_id(m)) { 86 case MSG_INC: 87 if(value < 100) value++; 88 break; 89 case MSG_DEC: 90 if(value > 0) value--; 91 break; 92 case MSG_SET: { 93 const int32_t * new_value = lv_msg_get_payload(m); 94 value = *new_value; 95 } 96 break; 97 case MSG_UPDATE_REQUEST: 98 lv_msg_send(MSG_UPDATE, &value); 99 break; 100 default: 101 break; 102 } 103 104 if(value != old_value) { 105 lv_msg_send(MSG_UPDATE, &value); 106 } 107 } 108 109 110 static void btn_event_cb(lv_event_t * e) 111 { 112 lv_obj_t * btn = lv_event_get_target(e); 113 lv_event_code_t code = lv_event_get_code(e); 114 if(code == LV_EVENT_CLICKED || code == LV_EVENT_LONG_PRESSED_REPEAT) { 115 if(lv_obj_get_index(btn) == 0) { /*First object is the dec. button*/ 116 lv_msg_send(MSG_DEC, NULL); 117 } 118 else { 119 lv_msg_send(MSG_INC, NULL); 120 } 121 } 122 } 123 124 static void label_event_cb(lv_event_t * e) 125 { 126 lv_obj_t * label = lv_event_get_target(e); 127 lv_event_code_t code = lv_event_get_code(e); 128 if(code == LV_EVENT_MSG_RECEIVED) { 129 lv_msg_t * m = lv_event_get_msg(e); 130 if(lv_msg_get_id(m) == MSG_UPDATE) { 131 const int32_t * v = lv_msg_get_payload(m); 132 lv_label_set_text_fmt(label, "%d %%", *v); 133 } 134 } 135 } 136 137 static void slider_event_cb(lv_event_t * e) 138 { 139 lv_obj_t * slider = lv_event_get_target(e); 140 lv_event_code_t code = lv_event_get_code(e); 141 if(code == LV_EVENT_VALUE_CHANGED) { 142 int32_t v = lv_slider_get_value(slider); 143 lv_msg_send(MSG_SET, &v); 144 } 145 else if(code == LV_EVENT_MSG_RECEIVED) { 146 lv_msg_t * m = lv_event_get_msg(e); 147 if(lv_msg_get_id(m) == MSG_UPDATE) { 148 const int32_t * v = lv_msg_get_payload(m); 149 lv_slider_set_value(slider, *v, LV_ANIM_OFF); 150 } 151 } 152 } 153 154 #endif