luat_lib_lvgl_qrcode.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "lvgl.h"
  10. #include "../exts/lv_qrcode/lv_qrcode.h"
  11. /**
  12. 创建qrcode组件
  13. @api lvgl.qrcode_create(parent, size, dark_color, light_color)
  14. @userdata 父组件
  15. @int 长度,因为qrcode是正方形
  16. @int 二维码中数据点的颜色, RGB颜色, 默认 0x3333ff
  17. @int 二维码中背景点的颜色, RGB颜色, 默认 0xeeeeff
  18. @return userdata qrcode组件
  19. @usage
  20. -- 创建并显示qrcode
  21. local qrcode = lvgl.qrcode_create(scr, 100)
  22. lvgl.qrcode_update(qrcode, "https://luatos.com")
  23. lvgl.obj_align(qrcode, lvgl.scr_act(), lvgl.ALIGN_CENTER, -100, -100)
  24. */
  25. int luat_lv_qrcode_create(lua_State *L) {
  26. lv_obj_t * parent = lua_touserdata(L, 1);
  27. lv_coord_t size = luaL_checkinteger(L, 2);
  28. int32_t dark_color = luaL_optinteger(L, 3, 0x3333ff);
  29. int32_t light_color = luaL_optinteger(L, 4, 0xeeeeff);
  30. lv_obj_t * qrcode = lv_qrcode_create(parent, size, lv_color_hex(dark_color), lv_color_hex(light_color));
  31. lua_pushlightuserdata(L, qrcode);
  32. return 1;
  33. }
  34. /**
  35. 设置qrcode组件的二维码内容,配合qrcode_create使用
  36. @api lvgl.qrcode_update(qrcode, cnt)
  37. @userdata qrcode组件,由qrcode_create创建
  38. @string 二维码的内容数据
  39. @return bool 更新成功返回true,否则返回false. 通常只有数据太长无法容纳才会返回false
  40. */
  41. int luat_lv_qrcode_update(lua_State *L) {
  42. lv_obj_t * qrcode = lua_touserdata(L, 1);
  43. size_t len = 0;
  44. const char* data = luaL_checklstring(L, 2, &len);
  45. lv_res_t ret = lv_qrcode_update(qrcode, data, len);
  46. lua_pushboolean(L, ret == LV_RES_OK ? 1 : 0);
  47. return 1;
  48. }
  49. /**
  50. 删除qrcode组件
  51. @api lvgl.qrcode_delete(qrcode)
  52. @userdata qrcode组件,由qrcode_create创建
  53. @return nil 无返回值
  54. */
  55. int luat_lv_qrcode_delete(lua_State *L) {
  56. lv_obj_t * qrcode = lua_touserdata(L, 1);
  57. lv_qrcode_delete(qrcode);
  58. return 0;
  59. }