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