1 /**
2 * @file lv_example_osal.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "../../lv_examples.h"
10
11 #if LV_BUILD_EXAMPLES
12 #include "../../../lvgl_private.h"
13
14 /*********************
15 * DEFINES
16 *********************/
17
18 /**********************
19 * TYPEDEFS
20 **********************/
21
22 /**********************
23 * STATIC PROTOTYPES
24 **********************/
25 static void counter_button_event_cb(lv_event_t * e);
26 static void increment_thread_entry(void * user_data);
27
28 /**********************
29 * STATIC VARIABLES
30 **********************/
31 static lv_thread_sync_t press_sync;
32 static lv_thread_t increment_thread;
33
34 /**********************
35 * MACROS
36 **********************/
37
38 /**********************
39 * GLOBAL FUNCTIONS
40 **********************/
41
lv_example_osal(void)42 void lv_example_osal(void)
43 {
44 lv_obj_t * counter_button;
45
46 counter_button = lv_button_create(lv_screen_active());
47 lv_obj_align(counter_button, LV_ALIGN_CENTER, 0, -15);
48 lv_obj_add_event_cb(counter_button, counter_button_event_cb, LV_EVENT_CLICKED, NULL);
49
50 if(lv_thread_sync_init(&press_sync) != LV_RESULT_OK) {
51 LV_LOG_ERROR("Error initializing thread sync");
52 }
53
54 if(lv_thread_init(&increment_thread, "inc_th", LV_THREAD_PRIO_MID, increment_thread_entry, 2048,
55 NULL) != LV_RESULT_OK) {
56 LV_LOG_ERROR("Error initializing thread");
57 }
58 }
59
60 /**********************
61 * STATIC FUNCTIONS
62 **********************/
63
counter_button_event_cb(lv_event_t * e)64 static void counter_button_event_cb(lv_event_t * e)
65 {
66 LV_UNUSED(e);
67 if(lv_thread_sync_signal(&press_sync) != LV_RESULT_OK) {
68 LV_LOG_ERROR("Error signaling thread sync");
69 }
70 }
71
increment_thread_entry(void * user_data)72 static void increment_thread_entry(void * user_data)
73 {
74 LV_UNUSED(user_data);
75 lv_obj_t * counter_label;
76 uint32_t press_count = 0;
77
78 lv_lock();
79 counter_label = lv_label_create(lv_scr_act());
80 lv_obj_align(counter_label, LV_ALIGN_CENTER, 0, 0);
81 lv_label_set_text_fmt(counter_label, "Pressed %" LV_PRIu32 " times", press_count);
82 lv_unlock();
83
84 while(true) {
85 if(lv_thread_sync_wait(&press_sync) != LV_RESULT_OK) {
86 LV_LOG_ERROR("Error awaiting thread sync");
87 }
88 press_count += 1;
89
90 lv_lock();
91 lv_label_set_text_fmt(counter_label, "Pressed %" LV_PRIu32 " times", press_count);
92 lv_unlock();
93 }
94 }
95
96
97 #endif
98