main.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. --- 模块功能:airui demo
  2. -- @module airui
  3. -- @author ???
  4. -- @release 2025.05.24
  5. -- LuaTools需要PROJECT和VERSION这两个信息
  6. PROJECT = "airui_demo"
  7. VERSION = "1.0.0"
  8. log.info("main", PROJECT, VERSION)
  9. --加载sys库
  10. _G.sys = require("sys")
  11. local lcd_chip = "st7796" -- "st7796" or "jd9261t_inited"
  12. function lcd_init()
  13. sys.wait(500)
  14. gpio.setup(164, 1, gpio.PULLUP)
  15. gpio.setup(141, 1, gpio.PULLUP)
  16. sys.wait(1000)
  17. if lcd_chip == "st7796" then
  18. lcd.init("st7796", {
  19. port = lcd.HWID_0, -- 使用的spi id 号
  20. pin_dc = 0xff, -- 命令选择硬件,不设置
  21. pin_pwr = 9, -- 背光控制管脚,默认打开背光,不设置
  22. pin_rst = 2, -- 屏幕reset 管脚
  23. direction = 1, -- 屏幕方向 0: 0度 1:90度 2:180度 3:270度
  24. w = 480, -- 屏幕宽度
  25. h = 320, -- 屏幕高度
  26. xoffset = 0, -- X轴偏移像素
  27. yoffset = 0, -- Y轴偏移像素
  28. sleepcmd = 0x10, -- LCD睡眠命令
  29. wakecmd = 0x11, -- LCD唤醒命令
  30. })
  31. elseif lcd_chip == "jd9261t_inited" then
  32. lcd.init("jd9261t_inited", {
  33. port = lcd.HWID_0, -- 使用的spi id 号
  34. pin_pwr = 160, -- 背光控制管脚
  35. pin_rst = 36, -- 屏幕reset 管脚
  36. direction = 0, -- 屏幕方向 0: 0度 1:90度 2:180度 3:270度
  37. w = 480, -- 屏幕宽度
  38. h = 480, -- 屏幕高度
  39. xoffset = 0, -- X轴偏移像素
  40. yoffset = 0, -- Y轴偏移像素
  41. interface_mode = lcd.QSPI_MODE, -- 接口模式 qspi
  42. bus_speed = 70000000, -- SPI总线速度
  43. flush_rate = 658, -- 刷新率
  44. vbp = 19, vfp = 108, vs = 2 -- VBP:垂直后沿, VFP:垂直前沿, VS:垂直同步
  45. })
  46. end
  47. gpio.setup(160, 1, gpio.PULLUP) -- 单独拉高背光
  48. if lcd_chip == "jd9261t_inited" then
  49. lcd.setupBuff(nil, true) -- 开启buff缓存,更适合lvgl场景
  50. lcd.autoFlush(false)
  51. end
  52. end
  53. function tp_init()
  54. if tp then
  55. local function tp_callBack(tp_device, tp_data)
  56. log.info("TP", tp_data[1].x, tp_data[1].y, tp_data[1].event)
  57. sys.publish("TP", tp_device, tp_data)
  58. end
  59. local i2c_id = 0
  60. i2c.setup(i2c_id)
  61. if lcd_chip == "st7796" then
  62. tp_device = tp.init("gt911",{port=i2c_id, pin_int = gpio.WAKEUP0},tp_callBack)
  63. elseif lcd_chip == "jd9261t_inited" then
  64. tp_device = tp.init("jd9261t_inited", {port = i2c_id, pin_int = gpio.WAKEUP0}, tp_callBack)
  65. end
  66. lvgl.indev_drv_register("pointer", "touch", tp_device) -- 注册触摸屏驱动
  67. else
  68. log.info("TP", "not found")
  69. end
  70. end
  71. local num = 10
  72. local username = "张三"
  73. -- 用户数据,可以自定义,要和airui.json中的控件设置文本中格式匹配
  74. -- 例如当前的例子:label的文本中就使用为 {{person.name}} 和 {{person.age}}
  75. local appdata = {
  76. person = {
  77. name = username,
  78. age = num
  79. }
  80. }
  81. -- 自定义的回调函数
  82. local function event_handler(obj, event)
  83. if (event == lvgl.EVENT_CLICKED) then
  84. appdata.person.age = appdata.person.age + 1
  85. airui.refresh_text("text_area_2", appdata)
  86. log.info("button clicked age=", appdata.person.age)
  87. end
  88. end
  89. -- 用户自定义函数,要和airui.json中的控件下的events字段对应
  90. local appfuncs = {
  91. update_age = event_handler
  92. }
  93. sys.taskInit(function()
  94. -- local uiJson = io.open("/luadb/ui.json")
  95. -- local ui_path = "/luadb/ui.json"
  96. local ui_path = "/luadb/demo_2.json"
  97. local uiJson = io.open(ui_path)
  98. local ui = json.decode(uiJson:read("*a"))
  99. log.info("ui", ui, ui.pages[1].children[1].name)
  100. lcd_init()
  101. log.info("初始化lvgl", lvgl.init(ui.project_settings.resolution.width, ui.project_settings.resolution.height))
  102. tp_init()
  103. airui.init(ui_path, {data = appdata, funcs = appfuncs})
  104. local button = airui.get_widget("button_2") -- 获得名称为 button_2 的按钮控件对象
  105. -- sys.wait(5000) -- 等待5s
  106. -- airui.del_widget(button) -- 删除按钮
  107. end)
  108. sys.run()