main.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. --- 模块功能:lcddemo
  2. -- @module lcd
  3. -- @author Dozingfiretruck
  4. -- @release 2021.01.25
  5. -- LuaTools需要PROJECT和VERSION这两个信息
  6. PROJECT = "lcddemo"
  7. VERSION = "1.0.0"
  8. log.info("main", PROJECT, VERSION)
  9. -- sys库是标配
  10. _G.sys = require("sys")
  11. -- UI带屏的项目一般不需要低功耗了吧, Air101/Air103设置到最高性能
  12. if mcu and (rtos.bsp() == "AIR101" or rtos.bsp() == "AIR103" or rtos.bsp() == "AIR601" ) then
  13. mcu.setClk(240)
  14. end
  15. --[[
  16. -- LCD接法示例
  17. LCD管脚 Air780E管脚 Air101/Air103管脚 Air105管脚
  18. GND GND GND GND
  19. VCC 3.3V 3.3V 3.3V
  20. SCL (GPIO11) (PB02/SPI0_SCK) (PC15/HSPI_SCK)
  21. SDA (GPIO09) (PB05/SPI0_MOSI) (PC13/HSPI_MOSI)
  22. RES (GPIO01) (PB03/GPIO19) (PC12/HSPI_MISO)
  23. DC (GPIO10) (PB01/GPIO17) (PE08)
  24. CS (GPIO08) (PB04/GPIO20) (PC14/HSPI_CS)
  25. BL(可以不接) (GPIO22) (PB00/GPIO16) (PE09)
  26. 提示:
  27. 1. 只使用SPI的时钟线(SCK)和数据输出线(MOSI), 其他均为GPIO脚
  28. 2. 数据输入(MISO)和片选(CS), 虽然是SPI, 但已复用为GPIO, 并非固定,是可以自由修改成其他脚
  29. 3. 若使用多个SPI设备, 那么RES/CS请选用非SPI功能脚
  30. 4. BL可以不接的, 若使用Air10x屏幕扩展板,对准排针插上即可
  31. ]]
  32. --添加硬狗防止程序卡死
  33. if wdt then
  34. wdt.init(9000)--初始化watchdog设置为9s
  35. sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
  36. end
  37. local lcd_use_buff = false -- 是否使用缓冲模式, 提升绘图效率,占用更大内存
  38. local rtos_bsp = rtos.bsp()
  39. local chip_type = hmeta.chip()
  40. -- 根据不同的BSP返回不同的值
  41. -- spi_id,pin_reset,pin_dc,pin_cs,bl
  42. function lcd_pin()
  43. if rtos_bsp == "AIR101" then
  44. return 0,pin.PB03,pin.PB01,pin.PB04,pin.PB00
  45. elseif rtos_bsp == "AIR103" then
  46. return 0,pin.PB03,pin.PB01,pin.PB04,pin.PB00
  47. elseif rtos_bsp == "AIR105" then
  48. return 5,pin.PC12,pin.PE08,pin.PC14,pin.PE09
  49. elseif rtos_bsp == "ESP32C3" then
  50. return 2,10,6,7,11
  51. elseif rtos_bsp == "ESP32S3" then
  52. return 2,16,15,14,13
  53. elseif rtos_bsp == "EC618" then
  54. return 0,1,10,8,22
  55. elseif string.find(rtos_bsp,"EC718") or string.find(chip_type,"EC718") then
  56. return lcd.HWID_0,36,0xff,0xff,25 -- 注意:EC718P有硬件lcd驱动接口, 无需使用spi,当然spi驱动也支持
  57. elseif string.find(rtos_bsp,"Air8101") then
  58. lcd_use_buff = true -- RGB仅支持buff缓冲模式
  59. return lcd.RGB,36,0xff,0xff,25
  60. else
  61. log.info("main", "bsp not support")
  62. return
  63. end
  64. end
  65. local spi_id,pin_reset,pin_dc,pin_cs,bl = lcd_pin()
  66. if spi_id ~= lcd.HWID_0 and spi_id ~= lcd.RGB then
  67. spi_lcd = spi.deviceSetup(spi_id,pin_cs,0,0,8,20*1000*1000,spi.MSB,1,0)
  68. port = "device"
  69. else
  70. port = spi_id
  71. end
  72. if spi_id == lcd.RGB then
  73. lcd.init("h050iwv",
  74. {port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,
  75. direction = 0,w = 800,h = 480})
  76. -- lcd.init("hx8282",
  77. -- {port = port,pin_pwr = bl, pin_rst = pin_reset,
  78. -- direction = 0,w = 1024,h = 600})
  79. -- lcd.init("nv3052c",
  80. -- {port = port,pin_pwr = bl, pin_rst = pin_reset,
  81. -- direction = 0,w = 720,h = 1280})
  82. -- lcd.init("st7701sn",
  83. -- {port = port,pin_pwr = bl, pin_rst = pin_reset,
  84. -- direction = 0,w = 480,h = 854})
  85. -- lcd.init("st7701s",
  86. -- {port = port,pin_pwr = bl, pin_rst = pin_reset,
  87. -- direction = 0,w = 480,h = 480})
  88. -- lcd.init("custom",
  89. -- {port = port,hbp = 46, hspw = 2, hfp = 48,vbp = 24, vspw = 2, vfp = 24,
  90. -- bus_speed = 60*1000*1000,direction = 0,w = 800,h = 480})
  91. -- "jd9261t"
  92. -- lcd.init("custom",{port = port,
  93. -- hbp = 180, hspw = 2, hfp = 48,vbp =24, vspw = 2, vfp = 158,
  94. -- bus_speed = 60*1000*1000,direction = 0,w =720,h = 720})
  95. else
  96. --[[ 此为合宙售卖的1.8寸TFT LCD LCD 分辨率:128X160 屏幕ic:st7735 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.19.6c2275a1Pa8F9o&id=560176729178]]
  97. lcd.init("st7735",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 128,h = 160,xoffset = 0,yoffset = 0},spi_lcd)
  98. -- [[ 此为合宙售卖的0.96寸TFT LCD LCD 分辨率:160X80 屏幕ic:st7735s 购买地址:https://item.taobao.com/item.htm?id=661054472686]]
  99. -- lcd.init("st7735v",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 1,w = 160,h = 80,xoffset = 0,yoffset = 24},spi_lcd)
  100. -- [[ 此为合宙售卖的ec718系列专用硬件双data驱动TFT LCD LCD 分辨率:320x480 屏幕ic:nv3037 购买地址:https://item.taobao.com/item.htm?id=764253232987&skuId=5258482696347&spm=a1z10.1-c-s.w4004-24087038454.8.64961170w5EdoA]]
  101. -- lcd.init("nv3037",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 320,h = 480,xoffset = 0,yoffset = 0,interface_mode=lcd.DATA_2_LANE},spi_lcd)
  102. -- lcd.init("st7789",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 240,h = 320,xoffset = 0,yoffset = 0},spi_lcd)
  103. -- [[ QSPI接口无RAM屏幕,必须开启lcd_use_buff ]]
  104. -- lcd.init("jd9261t_inited",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 480,h = 480,xoffset = 0,yoffset = 0,interface_mode=lcd.QSPI_MODE,bus_speed=60000000,flush_rate=658,vbp=19,vfp=108,vs=2},spi_lcd)
  105. -- lcd.init("jd9261t_inited",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 540,h = 540,xoffset = 0,yoffset = 0,interface_mode=lcd.QSPI_MODE,bus_speed=60000000,flush_rate=400,vbp=10,vfp=108,vs=2},spi_lcd)
  106. -- lcd.init("jd9261t_inited",{port = port,pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 720,h = 720,xoffset = 0,yoffset = 0,interface_mode=lcd.QSPI_MODE,bus_speed=60000000,flush_rate=300,vbp=10,vfp=160,vs=2},spi_lcd)
  107. -- lcd_use_buff = true
  108. end
  109. --如果显示颜色相反,请解开下面一行的注释,关闭反色
  110. --lcd.invoff()
  111. -- 不在内置驱动的, 看demo/lcd_custom
  112. sys.taskInit(function()
  113. -- 开启缓冲区, 刷屏速度回加快, 但也消耗2倍屏幕分辨率的内存
  114. if lcd_use_buff then
  115. lcd.setupBuff(nil, true) -- 使用sys内存, 只需要选一种
  116. -- lcd.setupBuff() -- 使用lua内存, 只需要选一种
  117. lcd.autoFlush(false)
  118. end
  119. while 1 do
  120. lcd.clear()
  121. log.info("wiki", "https://wiki.luatos.com/api/lcd.html")
  122. -- API 文档 https://wiki.luatos.com/api/lcd.html
  123. if lcd.showImage then
  124. -- 注意, jpg需要是常规格式, 不能是渐进式JPG
  125. -- 如果无法解码, 可以用画图工具另存为,新文件就能解码了
  126. lcd.showImage(40,0,"/luadb/logo.jpg")
  127. sys.wait(100)
  128. end
  129. log.info("lcd.drawLine", lcd.drawLine(20,20,150,20,0x001F))
  130. log.info("lcd.drawRectangle", lcd.drawRectangle(20,40,120,70,0xF800))
  131. log.info("lcd.drawCircle", lcd.drawCircle(50,50,20,0x0CE0))
  132. if lcd_use_buff then
  133. lcd.flush()
  134. end
  135. sys.wait(5000)
  136. end
  137. end)
  138. if tp then
  139. local function tp_callBack(tp_device,tp_data)
  140. sys.publish("TP",tp_device,tp_data)
  141. end
  142. -- 根据具体设计修改配置
  143. -- 硬件i2c参考
  144. -- local i2c_id = 0
  145. -- i2c.setup(i2c_id)
  146. -- tp_device = tp.init("gt911",{port=i2c_id0,pin_rst = 22,pin_int = 23},tp_callBack)
  147. -- 软件i2c 参考, 由于软件模拟,所以i2c速率需自己测试填写延时进行控制!不同bsp速度不同需自行调整!!!!
  148. -- "jd9261t"
  149. -- softI2C = i2c.createSoft(8, 5, 25)
  150. -- tp_device = tp.init("jd9261t",{port=softI2C,pin_rst = 9,pin_int = 6},tp_callBack)
  151. -- "gt911"
  152. softI2C = i2c.createSoft(8, 5)
  153. tp_device = tp.init("gt911",{port=softI2C,pin_rst = 9,pin_int = 6},tp_callBack)
  154. if tp_device then
  155. print(tp_device)
  156. sys.taskInit(function()
  157. while 1 do
  158. local result, tp_device, tp_data = sys.waitUntil("TP")
  159. if result then
  160. if tp_data[1].event == tp.EVENT_DOWN or tp_data[1].event == tp.EVENT_MOVE then
  161. lcd.drawPoint(tp_data[1].x, tp_data[1].y, 0xF800)
  162. if lcd_use_buff then
  163. lcd.flush()
  164. end
  165. end
  166. end
  167. end
  168. end)
  169. end
  170. end
  171. -- 用户代码已结束---------------------------------------------
  172. -- 结尾总是这一句
  173. sys.run()
  174. -- sys.run()之后后面不要加任何语句!!!!!