exlcd.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. -- exlcd.lua
  2. --[[
  3. @module exlcd
  4. @summary LCD显示拓展库
  5. @version 1.0.4
  6. @date 2025.11.11
  7. @author 江访
  8. @usage
  9. 本文件为LCD显示拓展库,核心业务逻辑为:
  10. 1、初始化LCD显示屏,支持多种显示芯片
  11. 2、管理屏幕背光亮度及开关状态
  12. 3、提供屏幕状态管理功能
  13. 4、支持根据lcd_model自动配置参数
  14. 本文件的对外接口有6个:
  15. 1、exlcd.init(param):LCD初始化函数
  16. 2、exlcd.set_bl(level):设置背光亮度接口,level为亮度级别(0-100)
  17. 3、exlcd.get_bl():当前设置背光亮度级别查询
  18. 4、exlcd.sleep():屏幕休眠
  19. 5、exlcd.wakeup():屏幕唤醒
  20. 6、exlcd.get_sleep():休眠状态查询
  21. ]]
  22. local exlcd = {}
  23. -- 屏幕状态管理表
  24. local screen_state = {
  25. last_brightness = 100, -- 默认亮度100%
  26. backlight_on = true, -- 背光默认开启
  27. lcd_config = nil -- 存储LCD配置
  28. }
  29. -- 预定义屏幕配置表
  30. local predefined_configs = {
  31. Air780EHM_LCD_4 = {
  32. lcd_model = "Air780EHM_LCD_4",
  33. pin_vcc = 24,
  34. pin_rst = 36,
  35. pin_pwr = 25,
  36. pin_pwm = 2,
  37. port = lcd.HWID_0,
  38. direction = 3,
  39. w = 480,
  40. h = 320,
  41. xoffset = 0,
  42. yoffset = 0,
  43. sleepcmd = 0X10,
  44. wakecmd = 0X11,
  45. },
  46. AirLCD_1000 = {
  47. lcd_model = "AirLCD_1000",
  48. pin_vcc = 29,
  49. pin_rst = 36,
  50. pin_pwr = 30,
  51. pin_pwm = 1,
  52. port = lcd.HWID_0,
  53. direction = 0,
  54. w = 320,
  55. h = 480,
  56. xoffset = 0,
  57. yoffset = 0,
  58. sleepcmd = 0X10,
  59. wakecmd = 0X11,
  60. },
  61. AirLCD_1010 = {
  62. lcd_model = "AirLCD_1010",
  63. pin_vcc = 141,
  64. pin_rst = 36,
  65. pin_pwr = 1,
  66. pin_pwm = 0,
  67. port = lcd.HWID_0,
  68. direction = 0,
  69. w = 320,
  70. h = 480,
  71. xoffset = 0,
  72. yoffset = 0,
  73. sleepcmd = 0X10,
  74. wakecmd = 0X11,
  75. },
  76. AirLCD_1020 = {
  77. lcd_model = "AirLCD_1020",
  78. pin_pwr = 8,
  79. pin_pwm = 0,
  80. port = lcd.RGB,
  81. direction = 0,
  82. w = 800,
  83. h = 480,
  84. xoffset = 0,
  85. yoffset = 0,
  86. bus_speed = 30 * 1000 * 1000,
  87. hbp = 8,
  88. hspw = 4,
  89. hfp = 8,
  90. vbp = 16,
  91. vspw = 4,
  92. vfp = 16,
  93. }
  94. }
  95. --[[
  96. 初始化LCD显示屏
  97. @api exlcd.init(param)
  98. @table param LCD配置参数,参考库的说明及demo用法
  99. @return bool 初始化成功返回true,失败返回false
  100. @usage
  101. -- 使用预定义配置初始化
  102. exlcd.init({lcd_model = "Air780EHM_LCD_4"})
  103. -- 自定义参数初始化
  104. exlcd.init({
  105. lcd_model = "st7796",
  106. port = lcd.HWID_0,
  107. pin_rst = 36,
  108. pin_pwr = 25,
  109. pin_pwm = 2,
  110. w = 480,
  111. h = 320,
  112. direction = 0
  113. })
  114. ]]
  115. function exlcd.init(param)
  116. if type(param) ~= "table" then
  117. log.error("exlcd", "参数必须为表")
  118. return false
  119. end
  120. -- 检查必要参数
  121. if not param.lcd_model then
  122. log.error("exlcd", "缺少必要参数: lcd_model")
  123. return false
  124. end
  125. local config = {}
  126. -- 根据lcd_model选择配置策略
  127. if param.lcd_model == "Air780EHM_LCD_4" then
  128. -- Air780EHM_LCD_4: 只使用lcd_model,其他参数固定
  129. config = predefined_configs.Air780EHM_LCD_4
  130. log.info("exlcd", "使用Air780EHM_LCD_4固定配置")
  131. elseif predefined_configs[param.lcd_model] then
  132. -- 其他预定义型号: 使用预定义配置作为基础,传入参数覆盖预定义配置
  133. config = {}
  134. -- 复制预定义配置
  135. for k, v in pairs(predefined_configs[param.lcd_model]) do
  136. config[k] = v
  137. end
  138. -- 用传入参数覆盖预定义配置
  139. for k, v in pairs(param) do
  140. if k ~= "lcd_model" or v ~= param.lcd_model then -- 避免重复设置lcd_model
  141. config[k] = v
  142. end
  143. end
  144. log.info("exlcd", "使用" .. param.lcd_model .. "基础配置,传入参数已覆盖")
  145. else
  146. -- 未知型号: 直接使用传入参数
  147. config = param
  148. log.info("exlcd", "使用传入参数配置")
  149. end
  150. -- LCD型号映射表
  151. local lcd_models = {
  152. AirLCD_1000 = "st7796",
  153. Air780EHM_LCD_4 = "st7796",
  154. AirLCD_1010 = "st7796",
  155. AirLCD_1020 = "nv3052c"
  156. }
  157. -- 确定LCD型号
  158. local lcd_model = lcd_models[config.lcd_model] or config.lcd_model
  159. -- 存储LCD配置供其他函数使用
  160. screen_state.lcd_config = {
  161. pin_pwr = config.pin_pwr,
  162. pin_pwm = config.pin_pwm,
  163. model = lcd_model,
  164. lcd_model = config.lcd_model
  165. }
  166. -- 设置电源引脚 (可选)
  167. if config.pin_vcc then
  168. gpio.setup(config.pin_vcc, 1, gpio.PULLUP)
  169. gpio.set(config.pin_vcc, 1)
  170. end
  171. -- 设置背光电源引脚 (可选)
  172. if config.pin_pwr then
  173. gpio.setup(config.pin_pwr, 1, gpio.PULLUP)
  174. gpio.set(config.pin_pwr, 1) -- 默认开启背光
  175. end
  176. -- 设置PWM背光引脚 (可选)
  177. if config.pin_pwm then
  178. pwm.setup(config.pin_pwm, 1000, screen_state.last_brightness)
  179. pwm.open(config.pin_pwm, 1000, screen_state.last_brightness)
  180. end
  181. -- 屏幕初始化 (spi_dev和init_in_service为可选参数)
  182. local lcd_init = lcd.init(
  183. lcd_model,
  184. config,
  185. config.spi_dev and config.spi_dev or nil,
  186. config.init_in_service and config.init_in_service or nil
  187. )
  188. log.info("exlcd", "LCD初始化", lcd_init)
  189. -- 自定义初始化完成确认
  190. if lcd_model == "custom" then
  191. lcd.user_done()
  192. end
  193. return lcd_init
  194. end
  195. --[[
  196. 设置背光亮度
  197. @api exlcd.set_bl(level)
  198. @number level 亮度级别,0-100,0表示关闭背光
  199. @return bool 设置成功返回true,失败返回false
  200. @usage
  201. -- 设置50%亮度
  202. exlcd.set_bl(50)
  203. -- 关闭背光
  204. exlcd.set_bl(0)
  205. ]]
  206. function exlcd.set_bl(level)
  207. -- 检查PWM配置
  208. if not screen_state.lcd_config.pin_pwm then
  209. log.error("exlcd", "PWM配置不存在,无法调节背光")
  210. return false
  211. end
  212. -- 确保GPIO已关闭
  213. if screen_state.lcd_config.pin_pwr then
  214. gpio.close(screen_state.lcd_config.pin_pwr)
  215. end
  216. -- 设置并开启PWM
  217. pwm.stop(screen_state.lcd_config.pin_pwm)
  218. pwm.close(screen_state.lcd_config.pin_pwm)
  219. pwm.setup(screen_state.lcd_config.pin_pwm, 1000, 100)
  220. pwm.open(screen_state.lcd_config.pin_pwm, 1000, level)
  221. screen_state.last_brightness = level
  222. screen_state.backlight_on = (level > 0)
  223. log.info("exlcd", "背光设置为", level, "%")
  224. return true
  225. end
  226. --[[
  227. 获取当前背光亮度
  228. @api exlcd.get_bl()
  229. @return number 当前背光亮度级别(0-100)
  230. @usage
  231. local brightness = exlcd.get_bl()
  232. log.info("当前背光亮度", brightness)
  233. ]]
  234. function exlcd.get_bl()
  235. return screen_state.last_brightness
  236. end
  237. --[[
  238. 屏幕进入休眠状态
  239. @api exlcd.sleep()
  240. @usage
  241. exlcd.sleep()
  242. ]]
  243. function exlcd.sleep()
  244. if not screen_state.is_sleeping then
  245. -- 关闭PWM背光 (如果配置了)
  246. if screen_state.lcd_config and screen_state.lcd_config.pin_pwm then
  247. pwm.close(screen_state.lcd_config.pin_pwm)
  248. end
  249. -- 关闭背光电源 (如果配置了)
  250. if screen_state.lcd_config and screen_state.lcd_config.pin_pwr then
  251. gpio.setup(screen_state.lcd_config.pin_pwr, 1, gpio.PULLUP)
  252. gpio.set(screen_state.lcd_config.pin_pwr, 0)
  253. end
  254. -- 执行LCD睡眠
  255. lcd.sleep()
  256. screen_state.is_sleeping = true
  257. log.info("exlcd", "LCD进入休眠状态")
  258. end
  259. end
  260. --[[
  261. 屏幕从休眠状态唤醒
  262. @api exlcd.wakeup()
  263. @usage
  264. exlcd.wakeup()
  265. ]]
  266. function exlcd.wakeup()
  267. if screen_state.is_sleeping then
  268. -- 开启背光电源 (如果配置了)
  269. if screen_state.lcd_config and screen_state.lcd_config.pin_pwr then
  270. gpio.set(screen_state.lcd_config.pin_pwr, 1)
  271. end
  272. -- 唤醒LCD
  273. lcd.wakeup()
  274. sys.wait(100) -- 等待100ms稳定
  275. -- 恢复背光设置 (如果配置了PWM引脚)
  276. if screen_state.lcd_config and screen_state.lcd_config.pin_pwm then
  277. pwm.setup(screen_state.lcd_config.pin_pwm, 1000, screen_state.last_brightness)
  278. pwm.open(screen_state.lcd_config.pin_pwm, 1000, screen_state.last_brightness)
  279. end
  280. screen_state.is_sleeping = false
  281. log.info("exlcd", "LCD唤醒")
  282. end
  283. end
  284. --[[
  285. 获取屏幕休眠状态
  286. @api exlcd.get_sleep()
  287. @return bool true表示屏幕处于休眠状态,false表示屏幕处于工作状态
  288. @usage
  289. if exlcd.get_sleep() then
  290. log.info("屏幕处于休眠状态")
  291. end
  292. ]]
  293. function exlcd.get_sleep()
  294. return screen_state.is_sleeping
  295. end
  296. return exlcd