exlcd.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. -- exlcd.lua
  2. --[[
  3. @module exlcd
  4. @summary LCD显示拓展库
  5. @version 1.0.5
  6. @date 2025.12.23
  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. }
  87. }
  88. --[[
  89. 初始化LCD显示屏
  90. @api exlcd.init(param)
  91. @table param LCD配置参数,参考库的说明及demo用法
  92. @return bool 初始化成功返回true,失败返回false
  93. @usage
  94. -- 使用预定义配置初始化
  95. exlcd.init({lcd_model = "Air780EHM_LCD_4"})
  96. -- 自定义参数初始化
  97. exlcd.init({
  98. lcd_model = "st7796",
  99. port = lcd.HWID_0,
  100. pin_rst = 36,
  101. pin_pwr = 25,
  102. pin_pwm = 2,
  103. w = 480,
  104. h = 320,
  105. direction = 0
  106. })
  107. ]]
  108. function exlcd.init(param)
  109. if type(param) ~= "table" then
  110. log.error("exlcd", "参数必须为表")
  111. return false
  112. end
  113. -- 检查必要参数
  114. if not param.lcd_model then
  115. log.error("exlcd", "缺少必要参数: lcd_model")
  116. return false
  117. end
  118. local config = {}
  119. -- 根据lcd_model选择配置策略
  120. if param.lcd_model == "Air780EHM_LCD_4" then
  121. -- Air780EHM_LCD_4: 只使用lcd_model,其他参数固定
  122. config = predefined_configs.Air780EHM_LCD_4
  123. log.info("exlcd", "使用Air780EHM_LCD_4固定配置")
  124. elseif predefined_configs[param.lcd_model] then
  125. -- 其他预定义型号: 使用预定义配置作为基础,传入参数覆盖预定义配置
  126. config = {}
  127. -- 复制预定义配置
  128. for k, v in pairs(predefined_configs[param.lcd_model]) do
  129. config[k] = v
  130. end
  131. -- 用传入参数覆盖预定义配置
  132. for k, v in pairs(param) do
  133. if k ~= "lcd_model" or v ~= param.lcd_model then -- 避免重复设置lcd_model
  134. config[k] = v
  135. end
  136. end
  137. log.info("exlcd", "使用" .. param.lcd_model .. "基础配置,传入参数已覆盖")
  138. else
  139. -- 未知型号: 直接使用传入参数
  140. config = param
  141. log.info("exlcd", "使用传入参数配置")
  142. end
  143. -- LCD型号映射表
  144. local lcd_models = {
  145. AirLCD_1000 = "st7796",
  146. Air780EHM_LCD_4 = "st7796",
  147. AirLCD_1010 = "st7796",
  148. AirLCD_1020 = "h050iwv"
  149. }
  150. -- 确定LCD型号
  151. local lcd_model = lcd_models[config.lcd_model] or config.lcd_model
  152. -- 存储LCD配置供其他函数使用
  153. screen_state.lcd_config = {
  154. pin_pwr = config.pin_pwr,
  155. pin_pwm = config.pin_pwm,
  156. model = lcd_model,
  157. lcd_model = config.lcd_model
  158. }
  159. -- 设置电源引脚 (可选)
  160. if config.pin_vcc then
  161. gpio.setup(config.pin_vcc, 1, gpio.PULLUP)
  162. gpio.set(config.pin_vcc, 1)
  163. end
  164. -- 设置背光电源引脚 (可选)
  165. if config.pin_pwr then
  166. gpio.setup(config.pin_pwr, 1, gpio.PULLUP)
  167. gpio.set(config.pin_pwr, 1) -- 默认开启背光
  168. end
  169. -- 设置PWM背光引脚 (可选)
  170. if config.pin_pwm then
  171. pwm.setup(config.pin_pwm, 1000, screen_state.last_brightness)
  172. pwm.open(config.pin_pwm, 1000, screen_state.last_brightness)
  173. end
  174. -- 屏幕初始化 (spi_dev和init_in_service为可选参数)
  175. local lcd_init = lcd.init(
  176. lcd_model,
  177. config,
  178. config.spi_dev and config.spi_dev or nil,
  179. config.init_in_service and config.init_in_service or nil
  180. )
  181. log.info("exlcd", "LCD初始化", lcd_init)
  182. -- 自定义初始化完成确认
  183. if lcd_model == "custom" then
  184. lcd.user_done()
  185. end
  186. return lcd_init
  187. end
  188. --[[
  189. 设置背光亮度
  190. @api exlcd.set_bl(level)
  191. @number level 亮度级别,0-100,0表示关闭背光
  192. @return bool 设置成功返回true,失败返回false
  193. @usage
  194. -- 设置50%亮度
  195. exlcd.set_bl(50)
  196. -- 关闭背光
  197. exlcd.set_bl(0)
  198. ]]
  199. function exlcd.set_bl(level)
  200. -- 检查PWM配置
  201. if not screen_state.lcd_config.pin_pwm then
  202. log.error("exlcd", "PWM配置不存在,无法调节背光")
  203. return false
  204. end
  205. -- 确保GPIO已关闭
  206. if screen_state.lcd_config.pin_pwr then
  207. gpio.close(screen_state.lcd_config.pin_pwr)
  208. end
  209. -- 设置并开启PWM
  210. pwm.stop(screen_state.lcd_config.pin_pwm)
  211. pwm.close(screen_state.lcd_config.pin_pwm)
  212. pwm.setup(screen_state.lcd_config.pin_pwm, 1000, 100)
  213. pwm.open(screen_state.lcd_config.pin_pwm, 1000, level)
  214. screen_state.last_brightness = level
  215. screen_state.backlight_on = (level > 0)
  216. log.info("exlcd", "背光设置为", level, "%")
  217. return true
  218. end
  219. --[[
  220. 获取当前背光亮度
  221. @api exlcd.get_bl()
  222. @return number 当前背光亮度级别(0-100)
  223. @usage
  224. local brightness = exlcd.get_bl()
  225. log.info("当前背光亮度", brightness)
  226. ]]
  227. function exlcd.get_bl()
  228. return screen_state.last_brightness
  229. end
  230. --[[
  231. 屏幕进入休眠状态
  232. @api exlcd.sleep()
  233. @usage
  234. exlcd.sleep()
  235. ]]
  236. function exlcd.sleep()
  237. if not screen_state.is_sleeping then
  238. -- 关闭PWM背光 (如果配置了)
  239. if screen_state.lcd_config and screen_state.lcd_config.pin_pwm then
  240. pwm.close(screen_state.lcd_config.pin_pwm)
  241. end
  242. -- 关闭背光电源 (如果配置了)
  243. if screen_state.lcd_config and screen_state.lcd_config.pin_pwr then
  244. gpio.setup(screen_state.lcd_config.pin_pwr, 1, gpio.PULLUP)
  245. gpio.set(screen_state.lcd_config.pin_pwr, 0)
  246. end
  247. -- 执行LCD睡眠
  248. lcd.sleep()
  249. screen_state.is_sleeping = true
  250. log.info("exlcd", "LCD进入休眠状态")
  251. end
  252. end
  253. --[[
  254. 屏幕从休眠状态唤醒
  255. @api exlcd.wakeup()
  256. @usage
  257. exlcd.wakeup()
  258. ]]
  259. function exlcd.wakeup()
  260. if screen_state.is_sleeping then
  261. -- 开启背光电源 (如果配置了)
  262. if screen_state.lcd_config and screen_state.lcd_config.pin_pwr then
  263. gpio.set(screen_state.lcd_config.pin_pwr, 1)
  264. end
  265. -- 唤醒LCD
  266. lcd.wakeup()
  267. sys.wait(100) -- 等待100ms稳定
  268. -- 恢复背光设置 (如果配置了PWM引脚)
  269. if screen_state.lcd_config and screen_state.lcd_config.pin_pwm then
  270. pwm.setup(screen_state.lcd_config.pin_pwm, 1000, screen_state.last_brightness)
  271. pwm.open(screen_state.lcd_config.pin_pwm, 1000, screen_state.last_brightness)
  272. end
  273. screen_state.is_sleeping = false
  274. log.info("exlcd", "LCD唤醒")
  275. end
  276. end
  277. --[[
  278. 获取屏幕休眠状态
  279. @api exlcd.get_sleep()
  280. @return bool true表示屏幕处于休眠状态,false表示屏幕处于工作状态
  281. @usage
  282. if exlcd.get_sleep() then
  283. log.info("屏幕处于休眠状态")
  284. end
  285. ]]
  286. function exlcd.get_sleep()
  287. return screen_state.is_sleeping
  288. end
  289. return exlcd