luat_lib_lvgl7.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "luat_base.h"
  2. #include "luat_lvgl.h"
  3. #include "luat_malloc.h"
  4. #include "luat_zbuff.h"
  5. #include "luat_lcd.h"
  6. static lv_disp_t* my_disp = NULL;
  7. static lv_disp_buf_t my_disp_buff = {0};
  8. //static lv_disp_drv_t my_disp_drv;
  9. luat_lcd_conf_t* conf;
  10. static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p) {
  11. //-----
  12. if (conf != NULL) {
  13. conf->opts->draw(conf, area->x1, area->y1, area->x2, area->y2, color_p);
  14. }
  15. // LLOGD("CALL disp_flush (%d, %d, %d, %d)", area->x1, area->y1, area->x2, area->y2);
  16. lv_disp_flush_ready(disp_drv);
  17. }
  18. #ifdef LUA_USE_WINDOWS
  19. #include <windows.h>
  20. extern uint32_t WINDOW_HOR_RES;
  21. extern uint32_t WINDOW_VER_RES;
  22. #endif
  23. int luat_lv_init(lua_State *L) {
  24. if (my_disp != NULL) {
  25. lua_pushboolean(L, 0);
  26. return 1;
  27. }
  28. #ifdef LUA_USE_WINDOWS
  29. if (lua_isnumber(L, 1) && lua_isnumber(L, 2)) {
  30. WINDOW_HOR_RES= luaL_checkinteger(L, 1);
  31. WINDOW_VER_RES = luaL_checkinteger(L, 2);
  32. }
  33. HWND windrv_init(void);
  34. windrv_init();
  35. lua_pushboolean(L, 1);
  36. return 1;
  37. #elif defined(LUA_USE_LINUX)
  38. lvgl_linux_init();
  39. lua_pushboolean(L, 1);
  40. return 1;
  41. #else
  42. conf = luat_lcd_get_default();
  43. lv_color_t *fbuffer = NULL;
  44. size_t fbuff_size = 0;
  45. int w = luaL_optinteger(L, 1, conf->w);
  46. int h = luaL_optinteger(L, 2, conf->h);
  47. fbuff_size = w * h / 10;
  48. LLOGD("w %d h %d buff %d", w, h, fbuff_size);
  49. if (lua_isuserdata(L, 3)) {
  50. luat_zbuff *zbuff = tozbuff(L);
  51. fbuffer = (lv_color_t *)zbuff->addr;
  52. fbuff_size = zbuff->len / sizeof(lv_color_t);
  53. }
  54. else {
  55. fbuffer = luat_heap_malloc(fbuff_size * sizeof(lv_color_t));
  56. }
  57. lv_disp_buf_init(&my_disp_buff, fbuffer, NULL, fbuff_size);
  58. lv_disp_drv_t my_disp_drv;
  59. lv_disp_drv_init(&my_disp_drv);
  60. my_disp_drv.flush_cb = disp_flush;
  61. my_disp_drv.hor_res = w;
  62. my_disp_drv.ver_res = h;
  63. my_disp_drv.buffer = &my_disp_buff;
  64. //LLOGD(">>%s %d", __func__, __LINE__);
  65. my_disp = lv_disp_drv_register(&my_disp_drv);
  66. //LLOGD(">>%s %d", __func__, __LINE__);
  67. lua_pushboolean(L, my_disp != NULL ? 1 : 0);
  68. //LLOGD(">>%s %d", __func__, __LINE__);
  69. return 1;
  70. #endif
  71. }