1 #include "../../lv_examples.h"
2 #if LV_USE_OBSERVER && LV_USE_SLIDER && LV_USE_LABEL && LV_BUILD_EXAMPLES
3 
4 /*This the only interface between the UI and the application*/
5 static lv_subject_t engine_subject;
6 
7 static void app_init(void);
8 static void ui_init(void);
9 
10 /**
11  * Simple PIN login screen to start an engine.
12  * The only interface between the UI and the application is a single "subject".
13  */
lv_example_observer_2(void)14 void lv_example_observer_2(void)
15 {
16     lv_subject_init_int(&engine_subject, 0);
17     app_init();
18     ui_init();
19 }
20 
21 /*--------------------------------------------------
22  * APPLICATION
23  *
24  * This part contains a demo application logic.
25  * It doesn't know anything about the internals of the UI
26  * and uses any the `engine_subject` as an interface.
27  * -------------------------------------------------*/
engine_state_observer_cb(lv_observer_t * observer,lv_subject_t * subject)28 static void engine_state_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
29 {
30     LV_UNUSED(observer);
31 
32     int32_t v = lv_subject_get_int(subject);
33     LV_UNUSED(v);
34     /*In a real application set/clear a pin here*/
35     LV_LOG_USER("Engine state: %" LV_PRId32, v);
36 }
37 
app_init(void)38 static void app_init(void)
39 {
40     lv_subject_add_observer(&engine_subject, engine_state_observer_cb, NULL);
41 }
42 
43 /*--------------------------------------------------
44  * USER INTERFACE
45  *
46  * This part contains only UI related code and data.
47  * In a project it would a separate file and the
48  * application couldn't see its internals
49  * -------------------------------------------------*/
50 
51 typedef enum {
52     LOGGED_OUT,
53     LOGGED_IN,
54     AUTH_FAILED,
55 } auth_state_t;
56 
57 static lv_subject_t auth_state_subject;
58 
textarea_event_cb(lv_event_t * e)59 static void textarea_event_cb(lv_event_t * e)
60 {
61     lv_obj_t * ta = lv_event_get_target(e);
62     if(lv_strcmp(lv_textarea_get_text(ta), "hello") == 0) {
63         lv_subject_set_int(&auth_state_subject, LOGGED_IN);
64     }
65     else {
66         lv_subject_set_int(&auth_state_subject, AUTH_FAILED);
67     }
68 }
69 
info_label_observer_cb(lv_observer_t * observer,lv_subject_t * subject)70 static void info_label_observer_cb(lv_observer_t * observer, lv_subject_t * subject)
71 {
72     lv_obj_t * label = lv_observer_get_target(observer);
73     switch(lv_subject_get_int(subject)) {
74         case LOGGED_IN:
75             lv_label_set_text(label, "Login successful");
76             break;
77         case LOGGED_OUT:
78             lv_label_set_text(label, "Logged out");
79             break;
80         case AUTH_FAILED:
81             lv_label_set_text(label, "Login failed");
82             break;
83     }
84 }
85 
log_out_click_event_cb(lv_event_t * e)86 static void log_out_click_event_cb(lv_event_t * e)
87 {
88     LV_UNUSED(e);
89     lv_subject_set_int(&auth_state_subject, LOGGED_OUT);
90 }
91 
ui_init(void)92 static void ui_init(void)
93 {
94     lv_subject_init_int(&auth_state_subject, LOGGED_OUT);
95 
96     /*Create a slider in the center of the display*/
97     lv_obj_t * ta = lv_textarea_create(lv_screen_active());
98     lv_obj_set_pos(ta, 10, 10);
99     lv_obj_set_width(ta, 200);
100     lv_textarea_set_one_line(ta, true);
101     lv_textarea_set_password_mode(ta, true);
102     lv_textarea_set_placeholder_text(ta, "The password is: hello");
103     lv_obj_add_event_cb(ta, textarea_event_cb, LV_EVENT_READY, NULL);
104     lv_obj_bind_state_if_eq(ta, &auth_state_subject, LV_STATE_DISABLED, LOGGED_IN);
105 
106     lv_obj_t * kb = lv_keyboard_create(lv_screen_active());
107     lv_keyboard_set_textarea(kb, ta);
108 
109     lv_obj_t * btn;
110     lv_obj_t * label;
111 
112     /*Create a log out button which will be active only when logged in*/
113     btn = lv_button_create(lv_screen_active());
114     lv_obj_set_pos(btn, 220, 10);
115     lv_obj_add_event_cb(btn, log_out_click_event_cb, LV_EVENT_CLICKED, NULL);
116     lv_obj_bind_state_if_not_eq(btn, &auth_state_subject, LV_STATE_DISABLED, LOGGED_IN);
117 
118     label = lv_label_create(btn);
119     lv_label_set_text(label, "LOG OUT");
120 
121     /*Create a label to show info*/
122     label = lv_label_create(lv_screen_active());
123     lv_obj_set_pos(label, 10, 60);
124     lv_subject_add_observer_obj(&auth_state_subject, info_label_observer_cb, label, NULL);
125 
126     /*Create button which will be active only when logged in*/
127     btn = lv_button_create(lv_screen_active());
128     lv_obj_set_pos(btn, 10, 80);
129     lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
130     lv_obj_bind_state_if_not_eq(btn, &auth_state_subject, LV_STATE_DISABLED, LOGGED_IN);
131     lv_button_bind_checked(btn, &engine_subject);
132     label = lv_label_create(btn);
133     lv_label_set_text(label, "START ENGINE");
134 }
135 
136 #endif
137