-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvgltest.c
318 lines (244 loc) · 9.45 KB
/
lvgltest.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
307
308
309
310
311
312
313
314
315
316
317
318
/****************************************************************************
* apps/examples/lvgltest/lvgltest.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#if defined(__NuttX__) && defined(__clang__) // Workaround for NuttX with zig cc
#include <arch/types.h>
#include "../../nuttx/include/limits.h"
#define FAR
#endif // defined(__NuttX__) && defined(__clang__)
#include <nuttx/config.h>
#include <sys/boardctl.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <debug.h>
#include <lvgl/lvgl.h>
#include "fbdev.h"
#include "lcddev.h"
#if defined(CONFIG_INPUT_TOUCHSCREEN) || defined(CONFIG_INPUT_MOUSE)
#include "tp.h"
#include "tp_cal.h"
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Should we perform board-specific driver initialization? There are two
* ways that board initialization can occur: 1) automatically via
* board_late_initialize() during bootupif CONFIG_BOARD_LATE_INITIALIZE
* or 2).
* via a call to boardctl() if the interface is enabled
* (CONFIG_BOARDCTL=y).
* If this task is running as an NSH built-in application, then that
* initialization has probably already been performed otherwise we do it
* here.
*/
#undef NEED_BOARDINIT
#if defined(CONFIG_BOARDCTL) && !defined(CONFIG_NSH_ARCHINIT)
# define NEED_BOARDINIT 1
#endif
/* LVGL Canvas Size */
#define CANVAS_WIDTH 50
#define CANVAS_HEIGHT 50
/****************************************************************************
* Public Functions Prototypes
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: monitor_cb
*
* Description:
* Monitoring callback from lvgl every time the screen is flushed.
*
****************************************************************************/
static void monitor_cb(lv_disp_drv_t * disp_drv, uint32_t time, uint32_t px)
{
#ifndef __clang__ // Doesn't compile with zig cc
ginfo("%" PRIu32 " px refreshed in %" PRIu32 " ms\n", px, time);
#endif // __clang__
}
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: create_widgets
*
* Description:
* Create the LVGL Widgets that will be rendered on the display. Based on
* https://docs.lvgl.io/7.11/widgets/label.html#label-recoloring-and-scrolling
* https://docs.lvgl.io/7.11/widgets/canvas.html#drawing-on-the-canvas-and-rotate
* https://docs.lvgl.io/7.11/widgets/msgbox.html#simple-message-box
*
****************************************************************************/
static void create_widgets(void)
{
// Get the Active Screen
lv_obj_t *screen = lv_scr_act();
// Create a Label Widget
lv_obj_t *label = lv_label_create(screen, NULL);
// Wrap long lines in the label text
lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
// Interpret color codes in the label text
lv_label_set_recolor(label, true);
// Center align the label text
lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
// Set the label text and colors
lv_label_set_text(
label,
"#ff0000 HELLO# " // Red Text
"#00aa00 PINEDIO# " // Green Text
"#0000ff STACK!# " // Blue Text
);
// Set the label width
lv_obj_set_width(label, 200);
// Align the label to the center of the screen, shift 30 pixels up
lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, -30);
#if defined(CONFIG_USE_LV_CANVAS) && !defined(__clang__) // LVGL Canvas Demo doesn't work with zig cc because of `lv_color_t`
// Create the Canvas
lv_obj_t *canvas = lv_canvas_create(screen, NULL);
// Set the Canvas Buffer (Warning: Might take a lot of RAM!)
static lv_color_t cbuf[LV_CANVAS_BUF_SIZE_TRUE_COLOR(CANVAS_WIDTH, CANVAS_HEIGHT)];
lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_TRUE_COLOR);
// Align the canvas to the center of the screen, shift 50 pixels down
lv_obj_align(canvas, NULL, LV_ALIGN_CENTER, 0, 50);
// Fill the canvas with white
lv_canvas_fill_bg(canvas, LV_COLOR_WHITE, LV_OPA_TRANSP);
// Create a Rounded Rectangle
lv_draw_rect_dsc_t rect_dsc;
lv_draw_rect_dsc_init(&rect_dsc);
rect_dsc.radius = 10; // Corner Radius
rect_dsc.bg_opa = LV_OPA_COVER; // Opacity: Opaque
rect_dsc.bg_grad_dir = LV_GRAD_DIR_HOR; // Gradient Direction: Horizontal
rect_dsc.bg_color = LV_COLOR_BLUE; // From Blue
rect_dsc.bg_grad_color = LV_COLOR_GREEN; // To Green
rect_dsc.border_width = 2; // Border Width
rect_dsc.border_opa = LV_OPA_90; // Border Opacity: 90%
rect_dsc.border_color = LV_COLOR_SILVER; // Border Color
rect_dsc.shadow_width = 5; // Shadow Width
rect_dsc.shadow_ofs_x = 5; // Shadow Offset X
rect_dsc.shadow_ofs_y = 5; // Shadow Offset Y
// Draw the Rounded Rectangle to the canvas
lv_canvas_draw_rect(canvas, 0, 0, 45, 45, &rect_dsc);
#endif // defined(CONFIG_USE_LV_CANVAS) && !defined(__clang__)
#ifdef CONFIG_EXAMPLES_LVGLTEST_MESSAGEBOX // LVGL Message Box Demo
// Create a Message Box Widget
lv_obj_t *msgbox = lv_msgbox_create(screen, NULL);
// Set the Message Box Text
lv_msgbox_set_text(msgbox, "Hello PineDio Stack!");
// Define the Message Box Buttons
static const char *btns[] = {"Cancel", "OK", ""};
// Add the buttons to the Message Box
lv_msgbox_add_btns(msgbox, btns);
#endif // CONFIG_EXAMPLES_LVGLTEST_MESSAGEBOX
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: main or lvgltest_main
*
* Description:
*
* Input Parameters:
* Standard argc and argv
*
* Returned Value:
* Zero on success; a positive, non-zero value on failure.
*
****************************************************************************/
int main(int argc, FAR char *argv[])
{
lv_disp_drv_t *disp_drv = get_disp_drv();
lv_disp_buf_t *disp_buf = get_disp_buf();
#if defined(CONFIG_INPUT_TOUCHSCREEN) || defined(CONFIG_INPUT_MOUSE)
#ifndef CONFIG_EXAMPLES_LVGLTEST_CALIBRATE
lv_point_t p[4];
/* top left */
p[0].x = 0;
p[0].y = 0;
/* top right */
p[1].x = LV_HOR_RES;
p[1].y = 0;
/* bottom left */
p[2].x = 0;
p[2].y = LV_VER_RES;
/* bottom right */
p[3].x = LV_HOR_RES;
p[3].y = LV_VER_RES;
#endif
#endif
#ifdef NEED_BOARDINIT
/* Perform board-specific driver initialization */
boardctl(BOARDIOC_INIT, 0);
#ifdef CONFIG_BOARDCTL_FINALINIT
/* Perform architecture-specific final-initialization (if configured) */
boardctl(BOARDIOC_FINALINIT, 0);
#endif
#endif
/* LVGL initialization */
lv_init();
/* Basic LVGL display driver initialization */
init_disp_buf(disp_buf);
init_disp_drv(disp_drv, disp_buf, monitor_cb);
/* Display interface initialization */
if (lcddev_init(disp_drv) != EXIT_SUCCESS)
{
/* Failed to use lcd driver falling back to framebuffer */
if (fbdev_init(disp_drv) != EXIT_SUCCESS)
{
/* No possible drivers left, fail */
return EXIT_FAILURE;
}
}
lv_disp_drv_register(disp_drv);
#if defined(CONFIG_INPUT_TOUCHSCREEN) || defined(CONFIG_INPUT_MOUSE)
/* Touchpad Initialization */
tp_init();
lv_indev_drv_t *indev_drv = get_indev_drv();
/* tp_read will be called periodically (by the library) to get the
* mouse position and state.
*/
init_indev_drv(indev_drv, tp_read);
#endif
/* Create the widgets for display */
create_widgets();
#if defined(CONFIG_INPUT_TOUCHSCREEN) || defined(CONFIG_INPUT_MOUSE)
/* Start TP calibration */
#ifdef CONFIG_EXAMPLES_LVGLTEST_CALIBRATE
tp_cal_create();
#else
tp_set_cal_values(p, p + 1, p + 2, p + 3);
#endif
#endif
/* Handle LVGL tasks */
while (1)
{
lv_task_handler();
usleep(10000);
}
return EXIT_SUCCESS;
}