main.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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") 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 (GPIO9) (PB05/SPI0_MOSI) (PC13/HSPI_MOSI)
  22. RES (GPIO1) (PB03/GPIO19) (PC12/HSPI_MISO)
  23. DC (GPIO10) (PB01/GPIO17) (PE08)
  24. CS (GPIO8) (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 rtos_bsp = rtos.bsp()
  38. -- spi_id,pin_reset,pin_dc,pin_cs,bl
  39. function lcd_pin()
  40. if rtos_bsp == "AIR101" then
  41. return 0,pin.PB03,pin.PB01,pin.PB04,pin.PB00
  42. elseif rtos_bsp == "AIR103" then
  43. return 0,pin.PB03,pin.PB01,pin.PB04,pin.PB00
  44. elseif rtos_bsp == "AIR105" then
  45. return 5,pin.PC12,pin.PE08,pin.PC14,pin.PE09
  46. elseif rtos_bsp == "ESP32C3" then
  47. return 2,10,6,7,11
  48. elseif rtos_bsp == "ESP32S3" then
  49. return 2,16,15,14,13
  50. elseif rtos_bsp == "EC618" then
  51. return 0,1,10,8,18
  52. else
  53. log.info("main", "bsp not support")
  54. return
  55. end
  56. end
  57. local spi_id,pin_reset,pin_dc,pin_cs,bl = lcd_pin()
  58. -- v0006及以后版本可用pin方式, 请升级到最新固件 https://gitee.com/openLuat/LuatOS/releases
  59. spi_lcd = spi.deviceSetup(spi_id,pin_cs,0,0,8,20*1000*1000,spi.MSB,1,0)
  60. --[[ 此为合宙售卖的2.4寸TFT LCD 分辨率:240X320 屏幕ic:GC9306 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.39.6c2275a1Pa8F9o&id=655959696358]]
  61. -- lcd.init("gc9a01",{port = "device",pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 240,h = 320,xoffset = 0,yoffset = 0},spi_lcd)
  62. --[[ 此为合宙售卖的1.8寸TFT LCD LCD 分辨率:128X160 屏幕ic:st7735 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.19.6c2275a1Pa8F9o&id=560176729178]]
  63. lcd.init("st7735",{port = "device",pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 128,h = 160,xoffset = 0,yoffset = 0},spi_lcd)
  64. --[[ 此为合宙售卖的1.54寸TFT LCD LCD 分辨率:240X240 屏幕ic:st7789 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.20.391445d5Ql4uJl&id=659456700222]]
  65. -- lcd.init("st7789",{port = "device",pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 0,w = 240,h = 240,xoffset = 0,yoffset = 0},spi_lcd)
  66. --[[ 此为合宙售卖的0.96寸TFT LCD LCD 分辨率:160X80 屏幕ic:st7735s 购买地址:https://item.taobao.com/item.htm?id=661054472686]]
  67. --lcd.init("st7735v",{port = "device",pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 1,w = 160,h = 80,xoffset = 0,yoffset = 24},spi_lcd)
  68. --如果显示颜色相反,请解开下面一行的注释,关闭反色
  69. --lcd.invoff()
  70. --如果显示依旧不正常,可以尝试老版本的板子的驱动
  71. --lcd.init("st7735s",{port = "device",pin_dc = pin_dc, pin_pwr = bl, pin_rst = pin_reset,direction = 2,w = 160,h = 80,xoffset = 0,yoffset = 0},spi_lcd)
  72. --[[ 此为合宙售卖的2.4寸TFT LCD 分辨率:240X320 屏幕ic:GC9306 购买地址:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-24045920841.39.6c2275a1Pa8F9o&id=655959696358]]
  73. -- lcd.init("gc9306",{port = "device",pin_dc = pin_dc , pin_pwr = bl,pin_rst = pin_reset,direction = 0,w = 240,h = 320,xoffset = 0,yoffset = 0},spi_lcd)
  74. -- 不在上述内置驱动的, 看demo/lcd_custom
  75. sys.taskInit(function()
  76. while 1 do
  77. lcd.clear()
  78. log.info("wiki", "https://wiki.luatos.com/api/lcd.html")
  79. -- API 文档 https://wiki.luatos.com/api/lcd.html
  80. if lcd.showImage then
  81. -- 注意, jpg需要是常规格式, 不能是渐进式JPG
  82. -- 如果无法解码, 可以用画图工具另存为,新文件就能解码了
  83. lcd.showImage(40,0,"/luadb/logo.jpg")
  84. sys.wait(100)
  85. end
  86. log.info("lcd.drawLine", lcd.drawLine(20,20,150,20,0x001F))
  87. log.info("lcd.drawRectangle", lcd.drawRectangle(20,40,120,70,0xF800))
  88. log.info("lcd.drawCircle", lcd.drawCircle(50,50,20,0x0CE0))
  89. sys.wait(1000)
  90. end
  91. end)
  92. -- 用户代码已结束---------------------------------------------
  93. -- 结尾总是这一句
  94. sys.run()
  95. -- sys.run()之后后面不要加任何语句!!!!!