Loading...
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 | /* * Copyright (c) 2018-2019 Jan Van Winkel <jan.van_winkel@dxplore.eu> * * SPDX-License-Identifier: Apache-2.0 */ #include <init.h> #include <zephyr.h> #include <lvgl.h> #include "lvgl_display.h" #ifdef CONFIG_LVGL_FILESYSTEM #include "lvgl_fs.h" #endif #ifdef CONFIG_LVGL_POINTER_KSCAN #include <drivers/kscan.h> #endif #include LV_MEM_CUSTOM_INCLUDE #define LOG_LEVEL CONFIG_LVGL_LOG_LEVEL #include <logging/log.h> LOG_MODULE_REGISTER(lvgl); #ifdef CONFIG_LVGL_BUFFER_ALLOC_STATIC static lv_disp_buf_t disp_buf; #define BUFFER_SIZE (CONFIG_LVGL_BITS_PER_PIXEL * ((CONFIG_LVGL_VDB_SIZE * \ CONFIG_LVGL_HOR_RES * CONFIG_LVGL_VER_RES) / 100) / 8) #define NBR_PIXELS_IN_BUFFER (BUFFER_SIZE * 8 / CONFIG_LVGL_BITS_PER_PIXEL) static u8_t buf0[BUFFER_SIZE]; #ifdef CONFIG_LVGL_DOUBLE_VDB static u8_t buf1[BUFFER_SIZE]; #endif #endif /* CONFIG_LVGL_BUFFER_ALLOC_STATIC */ #if CONFIG_LVGL_LOG_LEVEL != 0 static void lvgl_log(lv_log_level_t level, const char *file, uint32_t line, const char *dsc) { /* Convert LVGL log level to Zephyr log level * * LVGL log level mapping: * * LV_LOG_LEVEL_TRACE 0 * * LV_LOG_LEVEL_INFO 1 * * LV_LOG_LEVEL_WARN 2 * * LV_LOG_LEVEL_ERROR 3 * * LV_LOG_LEVEL_NUM 4 * * Zephyr log level mapping: * * LOG_LEVEL_NONE 0 * * LOG_LEVEL_ERR 1 * * LOG_LEVEL_WRN 2 * * LOG_LEVEL_INF 3 * * LOG_LEVEL_DBG 4 */ u8_t zephyr_level = LOG_LEVEL_DBG - level; ARG_UNUSED(file); ARG_UNUSED(line); Z_LOG(zephyr_level, "%s", dsc); } #endif #ifdef CONFIG_LVGL_BUFFER_ALLOC_STATIC static int lvgl_allocate_rendering_buffers(lv_disp_drv_t *disp_drv) { disp_drv->buffer = &disp_buf; #ifdef CONFIG_LVGL_DOUBLE_VDB lv_disp_buf_init(disp_drv->buffer, &buf0, &buf1, NBR_PIXELS_IN_BUFFER); #else lv_disp_buf_init(disp_drv->buffer, &buf0, NULL, NBR_PIXELS_IN_BUFFER); #endif /* CONFIG_LVGL_DOUBLE_VDB */ return 0; } #else static int lvgl_allocate_rendering_buffers(lv_disp_drv_t *disp_drv) { void *buf0 = NULL; void *buf1 = NULL; u16_t buf_nbr_pixels; u32_t buf_size; struct display_capabilities cap; struct device *display_dev = (struct device *)disp_drv->user_data; display_get_capabilities(display_dev, &cap); disp_drv->hor_res = cap.x_resolution; disp_drv->ver_res = cap.y_resolution; buf_nbr_pixels = (CONFIG_LVGL_VDB_SIZE * disp_drv->hor_res * disp_drv->ver_res) / 100; /* one horizontal line is the minimum buffer requirement for lvgl */ if (buf_nbr_pixels < disp_drv->hor_res) { buf_nbr_pixels = disp_drv->hor_res; } switch (cap.current_pixel_format) { case PIXEL_FORMAT_ARGB_8888: buf_size = 4 * buf_nbr_pixels; break; case PIXEL_FORMAT_RGB_888: buf_size = 3 * buf_nbr_pixels; break; case PIXEL_FORMAT_RGB_565: buf_size = 2 * buf_nbr_pixels; break; case PIXEL_FORMAT_MONO01: case PIXEL_FORMAT_MONO10: buf_size = buf_nbr_pixels / 8; buf_size += (buf_nbr_pixels % 8) == 0 ? 0 : 1; break; default: return -ENOTSUP; } buf0 = LV_MEM_CUSTOM_ALLOC(buf_size); if (buf0 == NULL) { LOG_ERR("Failed to allocate memory for rendering buffer"); return -ENOMEM; } #ifdef CONFIG_LVGL_DOUBLE_VDB buf1 = LV_MEM_CUSTOM_ALLOC(buf_size); if (buf1 == NULL) { LV_MEM_CUSTOM_FREE(buf0); LOG_ERR("Failed to allocate memory for rendering buffer"); return -ENOMEM; } #endif disp_drv->buffer = LV_MEM_CUSTOM_ALLOC(sizeof(lv_disp_buf_t)); if (disp_drv->buffer == NULL) { LV_MEM_CUSTOM_FREE(buf0); LV_MEM_CUSTOM_FREE(buf1); LOG_ERR("Failed to allocate memory to store rendering buffers"); return -ENOMEM; } lv_disp_buf_init(disp_drv->buffer, buf0, buf1, buf_nbr_pixels); return 0; } #endif /* CONFIG_LVGL_BUFFER_ALLOC_STATIC */ #ifdef CONFIG_LVGL_POINTER_KSCAN K_MSGQ_DEFINE(kscan_msgq, sizeof(lv_indev_data_t), CONFIG_LVGL_POINTER_KSCAN_MSGQ_COUNT, 4); static void lvgl_pointer_kscan_callback(struct device *dev, u32_t row, u32_t col, bool pressed) { lv_indev_data_t data = { .point.x = col, .point.y = row, .state = pressed ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL, }; if (k_msgq_put(&kscan_msgq, &data, K_NO_WAIT) != 0) { LOG_ERR("Could put input data into queue"); } } static bool lvgl_pointer_kscan_read(lv_indev_drv_t *drv, lv_indev_data_t *data) { static lv_indev_data_t prev = { .point.x = 0, .point.y = 0, .state = LV_INDEV_STATE_REL, }; lv_indev_data_t curr; if (k_msgq_get(&kscan_msgq, &curr, K_NO_WAIT) == 0) { prev = curr; } *data = prev; return k_msgq_num_used_get(&kscan_msgq) > 0; } static int lvgl_pointer_kscan_init(void) { struct device *kscan_dev = device_get_binding(CONFIG_LVGL_POINTER_KSCAN_DEV_NAME); lv_indev_drv_t indev_drv; if (kscan_dev == NULL) { LOG_ERR("Keyboard scan device not found."); return -ENODEV; } if (kscan_config(kscan_dev, lvgl_pointer_kscan_callback) < 0) { LOG_ERR("Could not configure keyboard scan device."); return -ENODEV; } lv_indev_drv_init(&indev_drv); indev_drv.type = LV_INDEV_TYPE_POINTER; indev_drv.read_cb = lvgl_pointer_kscan_read; if (lv_indev_drv_register(&indev_drv) == NULL) { LOG_ERR("Failed to register input device."); return -EPERM; } kscan_enable_callback(kscan_dev); return 0; } #endif /* CONFIG_LVGL_POINTER_KSCAN */ static int lvgl_init(struct device *dev) { ARG_UNUSED(dev); struct device *display_dev = device_get_binding(CONFIG_LVGL_DISPLAY_DEV_NAME); lv_disp_drv_t disp_drv; if (display_dev == NULL) { LOG_ERR("Display device not found."); return -ENODEV; } #if CONFIG_LVGL_LOG_LEVEL != 0 lv_log_register_print_cb(lvgl_log); #endif lv_init(); #ifdef CONFIG_LVGL_FILESYSTEM lvgl_fs_init(); #endif lv_disp_drv_init(&disp_drv); disp_drv.user_data = (void *) display_dev; lvgl_allocate_rendering_buffers(&disp_drv); if (set_lvgl_rendering_cb(&disp_drv) != 0) { LOG_ERR("Display not supported."); return -ENOTSUP; } if (lv_disp_drv_register(&disp_drv) == NULL) { LOG_ERR("Failed to register display device."); return -EPERM; } #ifdef CONFIG_LVGL_POINTER_KSCAN lvgl_pointer_kscan_init(); #endif /* CONFIG_LVGL_POINTER_KSCAN */ return 0; } SYS_INIT(lvgl_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); |