luat_lib_lvgl7.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. @module lvgl
  3. @summary LVGL图像库
  4. @version 1.0
  5. @date 2021.06.01
  6. */
  7. #include "luat_base.h"
  8. #include "luat_lvgl.h"
  9. #include "luat_malloc.h"
  10. #include "luat_zbuff.h"
  11. typedef struct luat_lv {
  12. lv_disp_t* disp;
  13. lv_disp_buf_t disp_buf;
  14. int buff_ref;
  15. int buff2_ref;
  16. }luat_lv_t;
  17. static luat_lv_t LV = {0};
  18. //static lv_disp_drv_t my_disp_drv;
  19. #if !defined (LUA_USE_WINDOWS) && !defined (LUA_USE_LINUX)
  20. #include "luat_lcd.h"
  21. static luat_lcd_conf_t* lcd_conf;
  22. LUAT_WEAK void luat_lv_disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p) {
  23. //-----
  24. if (lcd_conf != NULL) {
  25. #ifdef LV_NO_BLOCK_FLUSH
  26. luat_lcd_draw_no_block(lcd_conf, area->x1, area->y1, area->x2, area->y2, color_p, disp_drv->buffer->flushing_last);
  27. #else
  28. luat_lcd_draw(lcd_conf, area->x1, area->y1, area->x2, area->y2, color_p);
  29. #endif
  30. }
  31. // LLOGD("CALL disp_flush (%d, %d, %d, %d)", area->x1, area->y1, area->x2, area->y2);
  32. lv_disp_flush_ready(disp_drv);
  33. }
  34. #endif
  35. #ifdef LUA_USE_WINDOWS
  36. #include <windows.h>
  37. extern uint32_t WINDOW_HOR_RES;
  38. extern uint32_t WINDOW_VER_RES;
  39. #endif
  40. /**
  41. 初始化LVGL
  42. @api lvgl.init(w, h, lcd, buff_size, buff_mode)
  43. @int 屏幕宽,可选,默认从lcd取
  44. @int 屏幕高,可选,默认从lcd取
  45. @userdata lcd指针,可选,lcd初始化后有默认值,预留的多屏入口
  46. @int 缓冲区大小,默认宽*10, 不含色深.
  47. @int 缓冲模式,默认0, 单buff模式, 可选1,双buff模式
  48. @return bool 成功返回true,否则返回false
  49. */
  50. int luat_lv_init(lua_State *L) {
  51. if (LV.disp != NULL) {
  52. lua_pushboolean(L, 0);
  53. return 1;
  54. }
  55. #ifdef LUA_USE_WINDOWS
  56. if (lua_isnumber(L, 1) && lua_isnumber(L, 2)) {
  57. WINDOW_HOR_RES= luaL_checkinteger(L, 1);
  58. WINDOW_VER_RES = luaL_checkinteger(L, 2);
  59. }
  60. HWND windrv_init(void);
  61. windrv_init();
  62. lua_pushboolean(L, 1);
  63. return 1;
  64. #elif defined(LUA_USE_LINUX)
  65. //lvgl_linux_init();
  66. lua_pushboolean(L, 1);
  67. return 1;
  68. #else
  69. lv_color_t *fbuffer = NULL;
  70. lv_color_t *fbuffer2 = NULL;
  71. size_t fbuff_size = 0;
  72. size_t buffmode = 0;
  73. if (lua_isuserdata(L, 3)) {
  74. lcd_conf = lua_touserdata(L, 3);
  75. }
  76. else {
  77. lcd_conf = luat_lcd_get_default();
  78. }
  79. if (lcd_conf == NULL) {
  80. LLOGE("setup lcd first!!");
  81. return 0;
  82. }
  83. int w = lcd_conf->w;
  84. int h = lcd_conf->h;
  85. if (lua_isinteger(L, 1))
  86. w = luaL_checkinteger(L, 1);
  87. if (lua_isinteger(L, 2))
  88. h = luaL_checkinteger(L, 2);
  89. if (lua_isinteger(L, 4)) {
  90. fbuff_size = luaL_checkinteger(L, 4);
  91. if (fbuff_size < w*10)
  92. fbuff_size = w * 10;
  93. }
  94. else {
  95. fbuff_size = w * 10;
  96. }
  97. if (lua_isinteger(L, 5)) {
  98. buffmode = luaL_checkinteger(L, 5);
  99. }
  100. LLOGD("w %d h %d buff %d mode %d", w, h, fbuff_size, buffmode);
  101. if (buffmode & 0x02) {
  102. fbuffer = luat_heap_malloc(fbuff_size * sizeof(lv_color_t));
  103. if (fbuffer == NULL) {
  104. LLOGD("not enough memory");
  105. return 0;
  106. }
  107. if (buffmode & 0x01) {
  108. fbuffer2 = luat_heap_malloc(fbuff_size * sizeof(lv_color_t));
  109. if (fbuffer2 == NULL) {
  110. luat_heap_free(fbuffer);
  111. LLOGD("not enough memory");
  112. return 0;
  113. }
  114. }
  115. }
  116. else {
  117. fbuffer = lua_newuserdata(L, fbuff_size * sizeof(lv_color_t));
  118. if (fbuffer == NULL) {
  119. LLOGD("not enough memory");
  120. return 0;
  121. }
  122. if (buffmode & 0x01) {
  123. fbuffer2 = lua_newuserdata(L, fbuff_size * sizeof(lv_color_t));
  124. if (fbuffer2 == NULL) {
  125. LLOGD("not enough memory");
  126. return 0;
  127. }
  128. }
  129. // 引用即弹出
  130. if (fbuffer2)
  131. LV.buff2_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  132. LV.buff_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  133. }
  134. lv_disp_buf_init(&LV.disp_buf, fbuffer, fbuffer2, fbuff_size);
  135. lv_disp_drv_t my_disp_drv;
  136. lv_disp_drv_init(&my_disp_drv);
  137. my_disp_drv.flush_cb = luat_lv_disp_flush;
  138. my_disp_drv.hor_res = w;
  139. my_disp_drv.ver_res = h;
  140. my_disp_drv.buffer = &LV.disp_buf;
  141. //LLOGD(">>%s %d", __func__, __LINE__);
  142. LV.disp = lv_disp_drv_register(&my_disp_drv);
  143. //LLOGD(">>%s %d", __func__, __LINE__);
  144. lua_pushboolean(L, LV.disp != NULL ? 1 : 0);
  145. //LLOGD(">>%s %d", __func__, __LINE__);
  146. #ifdef __LVGL_SLEEP_ENABLE__
  147. luat_lvgl_tick_sleep(0);
  148. #endif
  149. return 1;
  150. #endif
  151. }