1 
2 #if LV_BUILD_TEST
3 #include "lv_test_init.h"
4 #include "lv_test_indev.h"
5 #include <sys/time.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #define HOR_RES 800
10 #define VER_RES 480
11 
12 static void hal_init(void);
13 static void dummy_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p);
14 
15 lv_indev_t * lv_test_mouse_indev;
16 lv_indev_t * lv_test_keypad_indev;
17 lv_indev_t * lv_test_encoder_indev;
18 
19 lv_color_t test_fb[HOR_RES * VER_RES];
20 static lv_color_t disp_buf1[HOR_RES * VER_RES];
21 
lv_test_init(void)22 void lv_test_init(void)
23 {
24     lv_init();
25     hal_init();
26 }
27 
lv_test_deinit(void)28 void lv_test_deinit(void)
29 {
30     lv_mem_deinit();
31 }
32 
open_cb(lv_fs_drv_t * drv,const char * path,lv_fs_mode_t mode)33 static void * open_cb(lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
34 {
35     (void) drv;
36     (void) mode;
37 
38     FILE * fp = fopen(path, "rb"); // only reading is supported
39 
40     return fp;
41 }
42 
close_cb(lv_fs_drv_t * drv,void * file_p)43 static lv_fs_res_t close_cb(lv_fs_drv_t * drv, void * file_p)
44 {
45     (void) drv;
46 
47     fclose(file_p);
48     return LV_FS_RES_OK;
49 }
50 
read_cb(lv_fs_drv_t * drv,void * file_p,void * buf,uint32_t btr,uint32_t * br)51 static lv_fs_res_t read_cb(lv_fs_drv_t * drv, void * file_p, void * buf, uint32_t btr, uint32_t * br)
52 {
53     (void) drv;
54 
55     *br = fread(buf, 1, btr, file_p);
56     return (*br <= 0) ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
57 }
58 
seek_cb(lv_fs_drv_t * drv,void * file_p,uint32_t pos,lv_fs_whence_t w)59 static lv_fs_res_t seek_cb(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t w)
60 {
61     (void) drv;
62 
63     uint32_t w2;
64     switch(w) {
65     case LV_FS_SEEK_SET:
66         w2 = SEEK_SET;
67         break;
68     case LV_FS_SEEK_CUR:
69         w2 = SEEK_CUR;
70         break;
71     case LV_FS_SEEK_END:
72         w2 = SEEK_END;
73         break;
74     default:
75         w2 = SEEK_SET;
76     }
77 
78     fseek (file_p, pos, w2);
79 
80     return LV_FS_RES_OK;
81 }
82 
tell_cb(lv_fs_drv_t * drv,void * file_p,uint32_t * pos_p)83 static lv_fs_res_t tell_cb(lv_fs_drv_t * drv, void * file_p, uint32_t * pos_p)
84 {
85     (void) drv;
86 
87     *pos_p = ftell(file_p);
88 
89     return LV_FS_RES_OK;
90 }
91 
hal_init(void)92 static void hal_init(void)
93 {
94     static lv_disp_draw_buf_t draw_buf;
95 
96     lv_disp_draw_buf_init(&draw_buf, disp_buf1, NULL, HOR_RES * VER_RES);
97 
98     static lv_disp_drv_t disp_drv;
99     lv_disp_drv_init(&disp_drv);
100     disp_drv.draw_buf = &draw_buf;
101     disp_drv.flush_cb = dummy_flush_cb;
102     disp_drv.hor_res = HOR_RES;
103     disp_drv.ver_res = VER_RES;
104     lv_disp_drv_register(&disp_drv);
105 
106     static lv_indev_drv_t indev_mouse_drv;
107     lv_indev_drv_init(&indev_mouse_drv);
108     indev_mouse_drv.type = LV_INDEV_TYPE_POINTER;
109     indev_mouse_drv.read_cb = lv_test_mouse_read_cb;
110     lv_test_mouse_indev = lv_indev_drv_register(&indev_mouse_drv);
111 
112     static lv_indev_drv_t indev_keypad_drv;
113     lv_indev_drv_init(&indev_keypad_drv);
114     indev_keypad_drv.type = LV_INDEV_TYPE_KEYPAD;
115     indev_keypad_drv.read_cb = lv_test_keypad_read_cb;
116     lv_test_keypad_indev = lv_indev_drv_register(&indev_keypad_drv);
117 
118     static lv_indev_drv_t indev_encoder_drv;
119     lv_indev_drv_init(&indev_encoder_drv);
120     indev_encoder_drv.type = LV_INDEV_TYPE_ENCODER;
121     indev_encoder_drv.read_cb = lv_test_encoder_read_cb;
122     lv_test_encoder_indev = lv_indev_drv_register(&indev_encoder_drv);
123 
124 
125     static lv_fs_drv_t drv;
126     lv_fs_drv_init(&drv);                     /*Basic initialization*/
127 
128     drv.letter = 'F';                         /*An uppercase letter to identify the drive*/
129     drv.open_cb = open_cb;                 /*Callback to open a file*/
130     drv.close_cb = close_cb;               /*Callback to close a file*/
131     drv.read_cb = read_cb;                 /*Callback to read a file*/
132     drv.seek_cb = seek_cb;                 /*Callback to seek in a file (Move cursor)*/
133     drv.tell_cb = tell_cb;                 /*Callback to tell the cursor position*/
134 
135     lv_fs_drv_register(&drv);                 /*Finally register the drive*/
136 }
137 
dummy_flush_cb(lv_disp_drv_t * disp_drv,const lv_area_t * area,lv_color_t * color_p)138 static void dummy_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
139 {
140     LV_UNUSED(area);
141     LV_UNUSED(color_p);
142 
143     memcpy(test_fb, color_p, lv_area_get_size(area) * sizeof(lv_color_t));
144 
145     lv_disp_flush_ready(disp_drv);
146 }
147 
custom_tick_get(void)148 uint32_t custom_tick_get(void)
149 {
150     static uint64_t start_ms = 0;
151     if(start_ms == 0) {
152         struct timeval tv_start;
153         gettimeofday(&tv_start, NULL);
154         start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
155     }
156 
157     struct timeval tv_now;
158     gettimeofday(&tv_now, NULL);
159     uint64_t now_ms;
160     now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;
161 
162     uint32_t time_ms = now_ms - start_ms;
163     return time_ms;
164 }
165 
166 #endif
167