/
application_manager.c
306 lines (259 loc) · 9.77 KB
/
application_manager.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <applications/application_manager.h>
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(APP_MANAGER, LOG_LEVEL_DBG);
#define MAX_APPS 10
#define INVALID_APP_ID 0xFF
static void draw_application_picker(void);
static void app_clicked(lv_event_t *e);
static void async_app_start(lv_timer_t *timer);
static void async_app_close(lv_timer_t *timer);
static application_t *apps[MAX_APPS];
static uint8_t num_apps;
static uint8_t current_app;
static lv_obj_t *root_obj;
static lv_group_t *group_obj;
static on_application_manager_cb_fn close_cb_func;
static lv_obj_t *grid;
static uint8_t last_index;
static bool app_launch_only;
static void delete_application_picker(void)
{
if (grid != NULL) {
lv_obj_del(grid);
grid = NULL;
}
}
static void row_focused(lv_event_t *e)
{
lv_obj_t *row = lv_event_get_target(e);
int app_id = (int)lv_event_get_user_data(e);
if (row && lv_obj_get_child_cnt(row) > 0) {
// Don't show close button as last focused row
if (app_id != num_apps) {
last_index = app_id;
}
lv_obj_t *title_label = lv_obj_get_user_data(row);
if (title_label) {
lv_obj_set_style_text_color(title_label, lv_color_white(), LV_PART_MAIN);
}
}
}
static void row_unfocused(lv_event_t *e)
{
lv_obj_t *row = lv_event_get_target(e);
// Need to check child count as during delete we get this callback called, but without children.
// The image is deleted already.
if (row && lv_obj_get_child_cnt(row) > 0) {
lv_obj_t *title_label = lv_obj_get_user_data(row);
if (title_label) {
lv_obj_set_style_text_color(title_label, lv_palette_main(LV_PALETTE_GREY), LV_PART_MAIN);
}
}
}
static void app_clicked(lv_event_t *e)
{
int app_id = (int)lv_event_get_user_data(e);
current_app = app_id;
last_index = app_id;
// This function may be called within a lvgl callback such
// as a button click. If we create a new ui in this callback
// which registers a button press callback then that callback
// may get called, but we don't want that. So delay the opening
// of the new application some time.
lv_timer_t *timer = lv_timer_create(async_app_start, 500, NULL);
lv_timer_set_repeat_count(timer, 1);
}
static void async_app_start(lv_timer_t *timer)
{
LOG_DBG("Start %d", current_app);
delete_application_picker();
apps[current_app]->start_func(root_obj, group_obj);
}
static void async_app_close(lv_timer_t *timer)
{
if (current_app < num_apps) {
LOG_DBG("Stop %d", current_app);
apps[current_app]->stop_func();
current_app = INVALID_APP_ID;
if (app_launch_only) {
application_manager_delete();
close_cb_func();
} else {
draw_application_picker();
}
} else {
// No app running, then close whole application_manager
application_manager_delete();
close_cb_func();
}
}
static void async_app_manager_close(lv_timer_t *timer)
{
LOG_DBG("Close app manager");
close_cb_func();
}
static void app_manager_close_button_pressed(lv_event_t *e)
{
lv_timer_t *timer = lv_timer_create(async_app_manager_close, 500, NULL);
lv_timer_set_repeat_count(timer, 1);
// Next time we open focus on first app and not close button.
last_index = 0;
}
static void scroll_event_cb(lv_event_t *e)
{
lv_obj_t *cont = lv_event_get_target(e);
lv_area_t cont_a;
lv_obj_get_coords(cont, &cont_a);
lv_coord_t cont_y_center = cont_a.y1 + lv_area_get_height(&cont_a) / 2;
lv_coord_t r = lv_obj_get_height(cont) * 5 / 10;
uint32_t i;
uint32_t child_cnt = lv_obj_get_child_cnt(cont);
for (i = 0; i < child_cnt; i++) {
lv_obj_t *child = lv_obj_get_child(cont, i);
lv_area_t child_a;
lv_obj_get_coords(child, &child_a);
lv_coord_t child_y_center = child_a.y1 + lv_area_get_height(&child_a) / 2;
lv_coord_t diff_y = child_y_center - cont_y_center;
diff_y = LV_ABS(diff_y);
/* Get the x of diff_y on a circle. */
lv_coord_t x;
/* If diff_y is out of the circle use the last point of the circle (the radius) */
if (diff_y >= r) {
x = r;
} else {
/* Use Pythagoras theorem to get x from radius and y */
uint32_t x_sqr = r * r - diff_y * diff_y;
lv_sqrt_res_t res;
lv_sqrt(x_sqr, &res, 0x8000); /* Use lvgl's built in sqrt root function */
x = r - res.i - 20; /* Added - 20 here to pull all a bit more to the left side */
}
/* Translate the item by the calculated X coordinate */
lv_obj_set_style_translate_x(child, x, 0);
lv_obj_set_style_translate_y(child, -13, 0);
/* Uncomment if to use some opacity with larger translations */
//lv_opa_t opa = lv_map(x, 0, r, LV_OPA_TRANSP, LV_OPA_COVER);
//lv_obj_set_style_opa(child, LV_OPA_COVER - opa, 0);
}
}
static lv_obj_t *create_application_list_entry(lv_obj_t *grid, const lv_img_dsc_t *icon, const char *name, int app_id)
{
lv_obj_t *cont = lv_obj_create(grid);
lv_obj_center(cont);
lv_obj_set_style_border_side(cont, LV_BORDER_SIDE_NONE, 0);
lv_obj_set_scrollbar_mode(cont, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_size(cont, LV_SIZE_CONTENT, icon->header.h);
lv_obj_clear_flag(cont,
LV_OBJ_FLAG_SCROLLABLE); // Needed, otherwise indev will first focus on this cont before it's contents.
lv_obj_add_event_cb(cont, row_focused, LV_EVENT_FOCUSED, (void *)app_id);
lv_obj_add_event_cb(cont, row_unfocused, LV_EVENT_DEFOCUSED, (void *)app_id);
lv_group_add_obj(group_obj, cont);
lv_obj_add_flag(cont, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
lv_obj_t *img_icon = lv_img_create(cont);
lv_img_set_src(img_icon, icon);
lv_obj_set_size(img_icon, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_align(img_icon, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_t *title = lv_label_create(cont);
lv_label_set_text(title, name);
lv_obj_set_size(title, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_align_to(title, img_icon, LV_ALIGN_OUT_RIGHT_MID, 15, 0);
lv_obj_set_style_text_color(title, lv_palette_main(LV_PALETTE_GREY), LV_PART_MAIN);
lv_obj_set_style_text_align(title, LV_ALIGN_OUT_LEFT_MID, LV_PART_MAIN);
lv_obj_set_user_data(cont, title);
return cont;
}
static void draw_application_picker(void)
{
lv_obj_t *entry;
static lv_style_t style;
lv_style_init(&style);
lv_style_set_flex_flow(&style, LV_FLEX_FLOW_ROW_WRAP);
lv_style_set_flex_main_place(&style, LV_FLEX_ALIGN_SPACE_EVENLY);
lv_style_set_layout(&style, LV_LAYOUT_FLEX);
lv_obj_set_scrollbar_mode(lv_scr_act(), LV_SCROLLBAR_MODE_OFF);
assert(grid == NULL);
grid = lv_obj_create(root_obj);
lv_obj_add_style(grid, &style, 0);
lv_obj_set_scrollbar_mode(root_obj, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_style_border_side(grid, LV_BORDER_SIDE_NONE, 0);
lv_obj_set_size(grid, LV_PCT(100), LV_PCT(100));
lv_obj_center(grid);
lv_obj_set_flex_flow(grid, LV_FLEX_FLOW_COLUMN);
lv_obj_set_scroll_dir(grid, LV_DIR_VER);
lv_obj_set_scroll_snap_y(grid, LV_SCROLL_SNAP_CENTER);
lv_obj_set_scrollbar_mode(grid, LV_SCROLLBAR_MODE_OFF);
lv_obj_add_event_cb(grid, scroll_event_cb, LV_EVENT_SCROLL, NULL);
for (int i = 0; i < num_apps; i++) {
LOG_DBG("Apps[%d]: %s", i, apps[i]->name);
entry = create_application_list_entry(grid, apps[i]->icon, apps[i]->name, i);
lv_obj_add_event_cb(entry, app_clicked, LV_EVENT_CLICKED, (void *)i);
}
LV_IMG_DECLARE(close_icon);
entry = create_application_list_entry(grid, &close_icon, "Close", num_apps);
lv_obj_add_event_cb(entry, app_manager_close_button_pressed, LV_EVENT_CLICKED, NULL);
lv_group_focus_obj(lv_obj_get_child(grid, last_index));
/* Update the notifications position manually firt time */
lv_event_send(grid, LV_EVENT_SCROLL, NULL);
/* Be sure the fist notification is in the middle */
lv_obj_scroll_to_view(lv_obj_get_child(grid, last_index), LV_ANIM_OFF);
}
int application_manager_show(on_application_manager_cb_fn close_cb, lv_obj_t *root, lv_group_t *group, char *app_name)
{
int err = 0;
bool app_found;
close_cb_func = close_cb;
root_obj = root;
group_obj = group;
app_launch_only = false;
if (app_name == NULL) {
draw_application_picker();
} else {
app_found = false;
for (int i = 0; i < num_apps; i++) {
if (strcmp(apps[i]->name, app_name) == 0) {
app_launch_only = true;
current_app = i;
last_index = i;
lv_timer_t *timer = lv_timer_create(async_app_start, 1, NULL);
lv_timer_set_repeat_count(timer, 1);
}
}
}
if (app_name != NULL && !app_found) {
err = -ENOENT;
}
return err;
}
void application_manager_delete(void)
{
if (current_app < num_apps) {
LOG_DBG("Stop force %d", current_app);
apps[current_app]->stop_func();
}
delete_application_picker();
}
void application_manager_add_application(application_t *app)
{
__ASSERT_NO_MSG(num_apps < MAX_APPS);
apps[num_apps] = app;
num_apps++;
}
void application_manager_exit_app(void)
{
lv_timer_t *timer = lv_timer_create(async_app_close, 500, NULL);
lv_timer_set_repeat_count(timer, 1);
}
void application_manager_app_close_request(application_t *app)
{
LOG_DBG("application_manager_app_close_request");
application_manager_exit_app();
}
static int application_manager_init(const struct device *arg)
{
memset(apps, 0, sizeof(apps));
num_apps = 0;
current_app = INVALID_APP_ID;
return 0;
}
SYS_INIT(application_manager_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);