xref: /OK3568_Linux_fs/app/lvgl_demo/rk_demo/main.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2023 Rockchip, Inc. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16 
17 #include <lvgl/lvgl.h>
18 #include <lvgl/lv_conf.h>
19 
20 #include "main.h"
21 #include "hal_sdl.h"
22 #include "hal_drm.h"
23 
24 #include "ui_resource.h"
25 
26 lv_ft_info_t ttf_main_s;
27 lv_ft_info_t ttf_main_m;
28 lv_ft_info_t ttf_main_l;
29 
30 lv_style_t style_txt_s;
31 lv_style_t style_txt_m;
32 lv_style_t style_txt_l;
33 
34 static int g_indev_rotation = 0;
35 static int g_disp_rotation = LV_DISP_ROT_NONE;
36 
37 static int quit = 0;
38 
39 extern void rk_demo_init(void);
40 
sigterm_handler(int sig)41 static void sigterm_handler(int sig) {
42     fprintf(stderr, "signal %d\n", sig);
43     quit = 1;
44 }
45 
app_disp_rotation(void)46 int app_disp_rotation(void)
47 {
48     return g_disp_rotation;
49 }
50 
lvgl_init(void)51 static void lvgl_init(void)
52 {
53     lv_init();
54 
55 #ifdef USE_SDL_GPU
56     hal_sdl_init(0, 0, g_disp_rotation);
57 #else
58     hal_drm_init(0, 0, g_disp_rotation);
59 #endif
60     lv_port_fs_init();
61     lv_port_indev_init(g_indev_rotation);
62 }
63 
font_init(void)64 static void font_init(void)
65 {
66     lv_freetype_init(64, 1, 0);
67 
68     ttf_main_s.name = MAIN_FONT;
69     ttf_main_s.weight = 26;
70     ttf_main_s.style = FT_FONT_STYLE_NORMAL;
71     lv_ft_font_init(&ttf_main_s);
72 
73     ttf_main_m.name = MAIN_FONT;
74     ttf_main_m.weight = 36;
75     ttf_main_m.style = FT_FONT_STYLE_NORMAL;
76     lv_ft_font_init(&ttf_main_m);
77 
78     ttf_main_l.name = MAIN_FONT;
79     ttf_main_l.weight = 140;
80     ttf_main_l.style = FT_FONT_STYLE_NORMAL;
81     lv_ft_font_init(&ttf_main_l);
82 }
83 
style_init(void)84 static void style_init(void)
85 {
86     lv_style_init(&style_txt_s);
87     lv_style_set_text_font(&style_txt_s, ttf_main_s.font);
88     lv_style_set_text_color(&style_txt_s, lv_color_black());
89 
90     lv_style_init(&style_txt_m);
91     lv_style_set_text_font(&style_txt_m, ttf_main_m.font);
92     lv_style_set_text_color(&style_txt_m, lv_color_black());
93 
94     lv_style_init(&style_txt_l);
95     lv_style_set_text_font(&style_txt_l, ttf_main_l.font);
96     lv_style_set_text_color(&style_txt_l, lv_color_black());
97 }
98 
app_init(void)99 void app_init(void)
100 {
101     font_init();
102     style_init();
103 }
104 
main(int argc,char ** argv)105 int main(int argc, char **argv)
106 {
107 #define FPS     0
108 #if FPS
109     float maxfps = 0.0, minfps = 1000.0;
110     float fps;
111     float fps0 = 0, fps1 = 0;
112     uint32_t st, et;
113     uint32_t st0 = 0, et0;
114 #endif
115     signal(SIGINT, sigterm_handler);
116     lvgl_init();
117 
118     app_init();
119 
120     rk_demo_init();
121 
122     while(!quit) {
123 #if FPS
124         st = clock_ms();
125 #endif
126         lv_task_handler();
127 #if FPS
128         et = clock_ms();
129         fps = 1000 / (et - st);
130         if (fps != 0.0 && fps < minfps) {
131             minfps = fps;
132             printf("Update minfps %f\n", minfps);
133         }
134         if (fps < 60 && fps > maxfps) {
135             maxfps = fps;
136             printf("Update maxfps %f\n", maxfps);
137         }
138         if (fps > 0.0 && fps < 60) {
139             fps0 = (fps0 + fps) / 2;
140             fps1 = (fps0 + fps1) / 2;
141         }
142         et0 = clock_ms();
143         if ((et0 - st0) > 1000) {
144             printf("avg:%f\n", fps1);
145             st0 = et0;
146         }
147 #endif
148         usleep(100);
149     }
150 
151     return 0;
152 }
153