extp.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. -- extp.lua - 触摸系统模块
  2. --[[
  3. @module extp
  4. @summary 触摸系统拓展库
  5. @version 1.0
  6. @date 2025.09.17
  7. @author 江访
  8. @usage
  9. 核心业务逻辑为:
  10. 1、初始化触摸设备,支持多种触摸芯片
  11. 2、处理原始触摸数据并解析为各种手势事件
  12. 3、通过统一消息接口发布触摸事件
  13. 4、提供消息发布控制功能
  14. 5、提供滑动和长按阈值配置功能
  15. 支持的触摸事件类型包括:
  16. 1、RAW_DATA - 原始触摸数据
  17. 2、TOUCH_DOWN - 按下事件
  18. 3、MOVE_X - 水平移动
  19. 4、MOVE_Y - 垂直移动
  20. 5、SWIPE_LEFT - 向左滑动
  21. 6、SWIPE_RIGHT - 向右滑动
  22. 7、SWIPE_UP - 向上滑动
  23. 8、SWIPE_DOWN - 向下滑动
  24. 9、SINGLE_TAP - 单击
  25. 10、LONG_PRESS - 长按
  26. 本文件的对外接口有5个:
  27. 1、extp.init(args) -- 触摸设备初始化函数
  28. 2、extp.setPublishEnabled(msg_type, enabled) -- 设置消息发布状态
  29. 3、extp.getPublishEnabled(msg_type) -- 获取消息发布状态
  30. 4、extp.setSlideThreshold(threshold) -- 设置滑动判定阈值
  31. 5、extp.setLongPressThreshold(threshold) -- 设置长按判定阈值
  32. 所有触摸事件均通过sys.publish("baseTouchEvent", event_type, ...)发布
  33. ]]
  34. local extp = {}
  35. -- 触摸状态变量
  36. local state = "IDLE" -- 当前状态:IDLE(空闲), DOWN(按下), MOVE(移动)
  37. local touch_down_x = 0 -- 按下时的X坐标
  38. local touch_down_y = 0 -- 按下时的Y坐标
  39. local touch_down_time = 0 -- 按下时的时间戳
  40. local slide_threshold = 45 -- 滑动判定阈值(像素)
  41. local long_press_threshold = 500 -- 长按判定阈值(毫秒)
  42. local slide_direction = nil -- 滑动方向(用于MOVE状态)
  43. -- 消息发布控制表,默认全部打开
  44. local publish_control = {
  45. RAW_DATA = true, -- 原始触摸数据
  46. TOUCH_DOWN = true, -- 按下事件
  47. MOVE_X = true, -- 水平移动
  48. MOVE_Y = true, -- 垂直移动
  49. SWIPE_LEFT = true, -- 向左滑动
  50. SWIPE_RIGHT = true, -- 向右滑动
  51. SWIPE_UP = true, -- 向上滑动
  52. SWIPE_DOWN = true, -- 向下滑动
  53. SINGLE_TAP = true, -- 单击
  54. LONG_PRESS = true -- 长按
  55. }
  56. -- 定义支持的触摸芯片配置
  57. local tp_configs = {
  58. cst820 = { i2c_speed = i2c.FAST, tp_model = "cst820" },
  59. cst9220 = { i2c_speed = i2c.SLOW, tp_model = "cst9220" },
  60. gt9157 = { i2c_speed = i2c.FAST, tp_model = "gt9157" },
  61. jd9261t = { i2c_speed = i2c.FAST, tp_model = "jd9261t" },
  62. AirLCD_1001 = { i2c_speed = i2c.SLOW, tp_model = "gt911" },
  63. Air780EHM_LCD_3 = { i2c_speed = i2c.SLOW, tp_model = "gt911" },
  64. Air780EHM_LCD_4 = { i2c_speed = i2c.SLOW, tp_model = "gt911" }
  65. }
  66. -- 设置消息发布状态
  67. -- @param msg_type 消息类型 ("RAW_DATA", "TOUCH_DOWN", "MOVE_X", "MOVE_Y", "SWIPE_LEFT", "SWIPE_RIGHT", "SWIPE_UP", "SWIPE_DOWN", "SINGLE_TAP", "LONG_PRESS", 或 "all")
  68. -- @param enabled 是否启用 (true/false)
  69. -- @return boolean 操作是否成功
  70. function extp.setPublishEnabled(msg_type, enabled)
  71. if msg_type == "all" then
  72. for k, _ in pairs(publish_control) do
  73. publish_control[k] = enabled
  74. end
  75. log.info("extp", "所有消息发布", enabled and "启用" or "禁用")
  76. return true
  77. elseif publish_control[msg_type] ~= nil then
  78. publish_control[msg_type] = enabled
  79. log.info("extp", msg_type, "消息发布", enabled and "启用" or "禁用")
  80. return true
  81. else
  82. log.error("extp", "未知的消息类型:", msg_type)
  83. return false
  84. end
  85. end
  86. -- 获取消息发布状态
  87. -- @param msg_type 消息类型 ("RAW_DATA", "TOUCH_DOWN", "MOVE_X", "MOVE_Y", "SWIPE_LEFT", "SWIPE_RIGHT", "SWIPE_UP", "SWIPE_DOWN", "SINGLE_TAP", "LONG_PRESS")
  88. -- @return boolean|table 发布状态 (true/false) 或所有状态表(当msg_type为nil时)
  89. function extp.getPublishEnabled(msg_type)
  90. if msg_type == nil then
  91. return publish_control
  92. elseif publish_control[msg_type] ~= nil then
  93. return publish_control[msg_type]
  94. else
  95. log.error("extp", "未知的消息类型:", msg_type)
  96. return false
  97. end
  98. end
  99. -- 设置滑动判定阈值
  100. -- @param threshold number 滑动判定阈值(像素)
  101. -- @return boolean 操作是否成功
  102. function extp.setSlideThreshold(threshold)
  103. if type(threshold) == "number" and threshold > 0 then
  104. slide_threshold = threshold
  105. log.info("extp", "滑动判定阈值设置为:", threshold)
  106. return true
  107. else
  108. log.error("extp", "无效的滑动阈值:", threshold)
  109. return false
  110. end
  111. end
  112. -- 设置长按判定阈值
  113. -- @param threshold number 长按判定阈值(毫秒)
  114. -- @return boolean 操作是否成功
  115. function extp.setLongPressThreshold(threshold)
  116. if type(threshold) == "number" and threshold > 0 then
  117. long_press_threshold = threshold
  118. log.info("extp", "长按判定阈值设置为:", threshold)
  119. return true
  120. else
  121. log.error("extp", "无效的长按阈值:", threshold)
  122. return false
  123. end
  124. end
  125. -- 触摸回调函数
  126. -- 参数: tp_device-触摸设备对象, tp_data-触摸数据
  127. local function tp_callback(tp_device, tp_data)
  128. -- 发布原始数据(如果启用)
  129. if publish_control.RAW_DATA then
  130. sys.publish("TP", tp_device, tp_data) --当前消息
  131. -- sys.publish("baseTouchEvent", "RAW_DATA", moveX, 0) 需要适配此条消息
  132. end
  133. -- 兼容多种数据结构:数组[1]或直接单点表;字段名兼容 x/x_coordinate, y/y_coordinate
  134. local p = nil
  135. if type(tp_data) == "table" then
  136. p = tp_data[1] or tp_data
  137. end
  138. if type(p) ~= "table" then return end
  139. local event_type = p.event or p.type or p.evt
  140. local x = p.x or p.x_coordinate or 0
  141. local y = p.y or p.y_coordinate or 0
  142. local timestamp = p.timestamp or p.ts or 0
  143. if not event_type then return end
  144. if event_type == 2 then -- 抬手事件
  145. if state == "DOWN" or state == "MOVE" then
  146. local moveX = x - touch_down_x
  147. local moveY = y - touch_down_y
  148. if moveX < -slide_threshold then
  149. if publish_control.SWIPE_LEFT then
  150. sys.publish("baseTouchEvent", "SWIPE_LEFT", moveX, 0)
  151. end
  152. elseif moveX > slide_threshold then
  153. if publish_control.SWIPE_RIGHT then
  154. sys.publish("baseTouchEvent", "SWIPE_RIGHT", moveX, 0)
  155. end
  156. elseif moveY < -slide_threshold then
  157. if publish_control.SWIPE_UP then
  158. sys.publish("baseTouchEvent", "SWIPE_UP", 0, moveY)
  159. end
  160. elseif moveY > slide_threshold then
  161. if publish_control.SWIPE_DOWN then
  162. sys.publish("baseTouchEvent", "SWIPE_DOWN", 0, moveY)
  163. end
  164. else
  165. -- 计算按下时间
  166. local press_time = timestamp - touch_down_time
  167. -- 判断是单击还是长按
  168. if press_time < long_press_threshold then
  169. if publish_control.SINGLE_TAP then
  170. sys.publish("baseTouchEvent", "SINGLE_TAP", touch_down_x, touch_down_y)
  171. end
  172. else
  173. if publish_control.LONG_PRESS then
  174. sys.publish("baseTouchEvent", "LONG_PRESS", touch_down_x, touch_down_y)
  175. end
  176. end
  177. end
  178. state = "IDLE"
  179. end
  180. elseif event_type == 1 or event_type == 3 then -- 按下或移动事件
  181. if state == "IDLE" and event_type == 1 then
  182. -- 从空闲状态接收到按下事件
  183. state = "DOWN"
  184. touch_down_x = x
  185. touch_down_y = y
  186. touch_down_time = timestamp
  187. slide_direction = nil
  188. -- 发布按下事件
  189. if publish_control.TOUCH_DOWN then
  190. sys.publish("baseTouchEvent", "TOUCH_DOWN", x, y)
  191. end
  192. elseif state == "DOWN" and event_type == 3 then
  193. -- 在按下状态下接收到移动事件
  194. if math.abs(x - touch_down_x) >= slide_threshold or math.abs(y - touch_down_y) >= slide_threshold then
  195. state = "MOVE"
  196. -- 确定滑动方向
  197. if math.abs(x - touch_down_x) > math.abs(y - touch_down_y) then
  198. -- 水平滑动
  199. if x - touch_down_x < 0 then
  200. slide_direction = "LEFT"
  201. else
  202. slide_direction = "RIGHT"
  203. end
  204. else
  205. -- 垂直滑动
  206. if y - touch_down_y < 0 then
  207. slide_direction = "UP"
  208. else
  209. slide_direction = "DOWN"
  210. end
  211. end
  212. end
  213. elseif state == "MOVE" and event_type == 3 then
  214. -- 在移动状态下接收到移动事件
  215. -- 根据滑动方向发布相应的移动事件
  216. if slide_direction == "LEFT" or slide_direction == "RIGHT" then
  217. -- 水平滑动,发布MOVE_X事件
  218. if publish_control.MOVE_X then
  219. sys.publish("baseTouchEvent", "MOVE_X", x - touch_down_x, 0)
  220. end
  221. else
  222. -- 垂直滑动,发布MOVE_Y事件
  223. if publish_control.MOVE_Y then
  224. sys.publish("baseTouchEvent", "MOVE_Y", 0, y - touch_down_y)
  225. end
  226. end
  227. end
  228. end
  229. end
  230. -- 初始化触摸功能
  231. -- @param args table 初始化参数表,包含以下字段:
  232. -- TP_MODEL: string 触摸芯片型号 ("cst820", "cst9220", "gt9157", "jd9261t", "AirLCD_1001", "Air780EHM_LCD_3", "Air780EHM_LCD_4")
  233. -- i2c_id: number I2C总线ID
  234. -- pin_rst: number 复位引脚
  235. -- pin_int: number 中断引脚
  236. -- @return boolean 初始化是否成功
  237. function extp.init(args)
  238. if type(args) ~= "table" then
  239. log.error("extp", "参数必须为表")
  240. return false
  241. end
  242. -- 检查必要参数
  243. if not args.TP_MODEL then
  244. log.error("extp", "缺少必要参数: TP_MODEL")
  245. return false
  246. end
  247. local TP_MODEL, tp_i2c_id, tp_pin_rst, tp_pin_int = args.TP_MODEL, args.i2c_id, args.pin_rst, args.pin_int
  248. -- 检查是否支持该型号
  249. local config = tp_configs[TP_MODEL]
  250. if not config then
  251. log.error("extp", "不支持的触摸型号:", TP_MODEL)
  252. return false
  253. end
  254. -- 统一初始化流程
  255. if config.i2c_speed ~= nil then
  256. i2c.setup(tp_i2c_id, config.i2c_speed)
  257. end
  258. local tp_device = tp.init(config.tp_model, {port=tp_i2c_id, pin_rst=tp_pin_rst, pin_int=tp_pin_int}, tp_callback)
  259. if tp_device ~= nil then return true end
  260. -- 若硬件触摸初始化失败,尝试PC触摸回退
  261. log.warn("extp", "触摸初始化失败,尝试PC触摸回退")
  262. local ok_pc, dev_pc = pcall(tp.init, "pc", { port = 0 }, tp_callback)
  263. if ok_pc and dev_pc then
  264. log.info("extp", "PC触摸回退成功")
  265. return true
  266. end
  267. log.error("extp", "PC触摸回退失败")
  268. return false
  269. end
  270. return extp