main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /**
  6. * @file main
  7. *
  8. */
  9. /*********************
  10. * INCLUDES
  11. *********************/
  12. #include <stdlib.h>
  13. #include <inttypes.h>
  14. #include "lvgl/lvgl.h"
  15. #include "display_indev.h"
  16. #include <unistd.h>
  17. /*********************
  18. * DEFINES
  19. *********************/
  20. /**********************
  21. * TYPEDEFS
  22. **********************/
  23. /**********************
  24. * STATIC PROTOTYPES
  25. **********************/
  26. static void
  27. hal_init(void);
  28. // static int tick_thread(void * data);
  29. // static void memory_monitor(void * param);
  30. /**********************
  31. * STATIC VARIABLES
  32. **********************/
  33. /**********************
  34. * MACROS
  35. **********************/
  36. /**********************
  37. * GLOBAL FUNCTIONS
  38. **********************/
  39. uint32_t count = 0;
  40. char count_str[11] = { 0 };
  41. lv_obj_t *hello_world_label;
  42. lv_obj_t *count_label;
  43. lv_obj_t *btn1;
  44. lv_obj_t *label_count1;
  45. int label_count1_value = 0;
  46. char label_count1_str[11] = { 0 };
  47. static lv_res_t
  48. btn_rel_action(lv_obj_t *btn)
  49. {
  50. label_count1_value++;
  51. snprintf(label_count1_str, sizeof(label_count1_str), "%d",
  52. label_count1_value);
  53. lv_label_set_text(label_count1, label_count1_str);
  54. return LV_RES_OK;
  55. }
  56. int
  57. main()
  58. {
  59. void display_SDL_init();
  60. display_SDL_init();
  61. /*Initialize LittlevGL*/
  62. lv_init();
  63. /*Initialize the HAL (display, input devices, tick) for LittlevGL*/
  64. hal_init();
  65. hello_world_label = lv_label_create(lv_scr_act(), NULL);
  66. lv_label_set_text(hello_world_label, "Hello world!");
  67. lv_obj_align(hello_world_label, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
  68. count_label = lv_label_create(lv_scr_act(), NULL);
  69. lv_obj_align(count_label, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);
  70. btn1 = lv_btn_create(
  71. lv_scr_act(), NULL); /*Create a button on the currently loaded screen*/
  72. lv_btn_set_action(btn1, LV_BTN_ACTION_CLICK,
  73. btn_rel_action); /*Set function to be called when the
  74. button is released*/
  75. lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, 20); /*Align below the label*/
  76. /*Create a label on the button*/
  77. lv_obj_t *btn_label = lv_label_create(btn1, NULL);
  78. lv_label_set_text(btn_label, "Click ++");
  79. label_count1 = lv_label_create(lv_scr_act(), NULL);
  80. lv_label_set_text(label_count1, "0");
  81. lv_obj_align(label_count1, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
  82. while (1) {
  83. /* Periodically call the lv_task handler.
  84. * It could be done in a timer interrupt or an OS task too.*/
  85. if ((count % 100) == 0) {
  86. snprintf(count_str, sizeof(count_str), "%d", count / 100);
  87. lv_label_set_text(count_label, count_str);
  88. }
  89. lv_task_handler();
  90. ++count;
  91. usleep(10 * 1000); /*Just to let the system breath*/
  92. }
  93. return 0;
  94. }
  95. /**********************
  96. * STATIC FUNCTIONS
  97. **********************/
  98. /**
  99. * Initialize the Hardware Abstraction Layer (HAL) for the Littlev graphics
  100. * library
  101. */
  102. void
  103. display_flush_wrapper(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
  104. const lv_color_t *color_p)
  105. {
  106. display_flush(x1, y1, x2, y2, color_p);
  107. lv_flush_ready();
  108. }
  109. void
  110. display_vdb_write_wrapper(uint8_t *buf, lv_coord_t buf_w, lv_coord_t x,
  111. lv_coord_t y, lv_color_t color, lv_opa_t opa)
  112. {
  113. display_vdb_write(buf, buf_w, x, y, &color, opa);
  114. }
  115. extern void
  116. display_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
  117. lv_color_t color_p);
  118. extern void
  119. display_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2,
  120. const lv_color_t *color_p);
  121. static void
  122. hal_init(void)
  123. {
  124. /* Add a display*/
  125. lv_disp_drv_t disp_drv;
  126. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  127. disp_drv.disp_flush =
  128. display_flush_wrapper; /*Used when `LV_VDB_SIZE != 0` in lv_conf.h
  129. (buffered drawing)*/
  130. disp_drv.disp_fill = display_fill; /*Used when `LV_VDB_SIZE == 0` in
  131. lv_conf.h (unbuffered drawing)*/
  132. disp_drv.disp_map = display_map; /*Used when `LV_VDB_SIZE == 0` in lv_conf.h
  133. (unbuffered drawing)*/
  134. #if LV_VDB_SIZE != 0
  135. disp_drv.vdb_wr = display_vdb_write_wrapper;
  136. #endif
  137. lv_disp_drv_register(&disp_drv);
  138. /* Add the mouse as input device
  139. * Use the 'mouse' driver which reads the PC's mouse*/
  140. // mouse_init();
  141. lv_indev_drv_t indev_drv;
  142. lv_indev_drv_init(&indev_drv); /*Basic initialization*/
  143. indev_drv.type = LV_INDEV_TYPE_POINTER;
  144. indev_drv.read =
  145. display_input_read; /*This function will be called periodically (by the
  146. library) to get the mouse position and state*/
  147. lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv);
  148. }