extp.lua 12 KB

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