extp.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. -- extp.lua - 触摸系统模块
  2. --[[
  3. @module extp
  4. @summary 触摸系统拓展库
  5. @version 1.1.1
  6. @date 2025.11.20
  7. @author 江访
  8. @usage
  9. 本文件为触摸系统拓展库,核心业务逻辑为:
  10. 1、初始化触摸设备,支持多种触摸芯片
  11. 2、处理原始触摸数据并解析为各种手势事件
  12. 3、通过统一消息接口发布触摸事件
  13. 4、提供消息发布控制功能
  14. 5、提供滑动和长按阈值配置功能
  15. 支持的触摸事件类型包括:
  16. RAW_DATA、TOUCH_DOWN、MOVE_X、MOVE_Y、SWIPE_LEFT、SWIPE_RIGHT、
  17. SWIPE_UP、SWIPE_DOWN、SINGLE_TAP、LONG_PRESS
  18. 本文件的对外接口有5个:
  19. 1、extp.init(param):触摸设备初始化函数
  20. 2、extp.set_publish_enabled(msg_type, enabled):设置消息发布状态
  21. 3、extp.get_publish_enable(msg_type):获取消息发布状态
  22. 4、extp.set_swipe_threshold(threshold):设置滑动判定阈值
  23. 5、extp.set_long_press_threshold(threshold):设置长按判定阈值
  24. 所有触摸事件均通过sys.publish("BASE_TOUCH_EVENT", event_type, ...)发布
  25. ]]
  26. local extp = {}
  27. -- 触摸状态变量
  28. local state = "IDLE" -- 当前状态:IDLE(空闲), DOWN(按下), MOVE(移动)
  29. local touch_down_x = 0 -- 按下时的X坐标
  30. local touch_down_y = 0 -- 按下时的Y坐标
  31. local touch_down_time = 0 -- 按下时的时间戳
  32. local swipe_threshold = 45 -- 滑动判定阈值(像素)
  33. local long_press_threshold = 500 -- 长按判定阈值(毫秒)
  34. local swipe_direction = nil -- 滑动方向(用于MOVE状态)
  35. -- 消息发布控制表,默认全部打开
  36. local publish_control = {
  37. RAW_DATA = false, -- 原始触摸数据
  38. TOUCH_DOWN = false, -- 按下事件
  39. MOVE_X = false, -- 水平移动
  40. MOVE_Y = false, -- 垂直移动
  41. SWIPE_LEFT = false, -- 向左滑动
  42. SWIPE_RIGHT = false, -- 向右滑动
  43. SWIPE_UP = false, -- 向上滑动
  44. SWIPE_DOWN = false, -- 向下滑动
  45. SINGLE_TAP = true, -- 单击
  46. LONG_PRESS = true -- 长按
  47. }
  48. -- 定义支持的触摸芯片配置
  49. local tp_configs = {
  50. cst820 = { i2c_speed = i2c.FAST, tp_model = "cst820" },
  51. gt9157 = { i2c_speed = i2c.FAST, tp_model = "gt9157" },
  52. cst9220 = { i2c_speed = i2c.SLOW, tp_model = "cst9220" },
  53. jd9261t = { i2c_speed = i2c.FAST, tp_model = "jd9261t" },
  54. gt911 = { i2c_speed = i2c.SLOW, tp_model = "gt911" },
  55. AirLCD_1010 = { i2c_speed = i2c.SLOW, tp_model = "gt911" },
  56. Air780EHM_LCD_4 = { i2c_speed = i2c.SLOW, tp_model = "gt911" },
  57. AirLCD_1020 = { i2c_speed = i2c.SLOW, tp_model = "gt911" }
  58. }
  59. -- 特殊型号的默认配置
  60. local special_tp_configs = {
  61. Air780EHM_LCD_4 = {
  62. i2c_id = 1,
  63. pin_rst = 1,
  64. pin_int = 22
  65. },
  66. AirLCD_1010 = {
  67. i2c_id = 0,
  68. pin_rst = 20,
  69. pin_int = gpio.WAKEUP0
  70. },
  71. AirLCD_1020 = {
  72. i2c_id = i2c.createSoft(0, 1),
  73. pin_rst = 28,
  74. pin_int = 7
  75. }
  76. }
  77. --[[
  78. 设置消息发布状态
  79. @api extp.set_publish_enabled(msg_type, enabled)
  80. @string msg_type 消息类型,支持"ALL"或具体事件类型
  81. @bool enabled 是否启用发布
  82. @return bool 操作成功返回true,失败返回false
  83. @usage
  84. -- 启用单击事件
  85. extp.set_publish_enabled("SINGLE_TAP", true)
  86. -- 禁用所有消息发布
  87. extp.set_publish_enabled("ALL", false)
  88. ]]
  89. function extp.set_publish_enabled(msg_type, enabled)
  90. if msg_type == "ALL" then
  91. for k, _ in pairs(publish_control) do
  92. publish_control[k] = enabled
  93. end
  94. log.info("extp", "所有消息发布", enabled and "启用" or "禁用")
  95. return true
  96. elseif publish_control[msg_type] ~= nil then
  97. publish_control[msg_type] = enabled
  98. log.info("extp", msg_type, "消息发布", enabled and "启用" or "禁用")
  99. return true
  100. else
  101. log.error("extp", "未知的消息类型:", msg_type)
  102. return false
  103. end
  104. end
  105. --[[
  106. 获取消息发布状态
  107. @api extp.get_publish_enable(msg_type)
  108. @string msg_type 消息类型,"ALL"或具体事件类型
  109. @return bool|table 发布状态或所有状态表
  110. @usage
  111. -- 获取单击事件状态
  112. local enabled = extp.get_publish_enable("SINGLE_TAP")
  113. -- 获取所有状态
  114. local all_status = extp.get_publish_enable("ALL")
  115. ]]
  116. function extp.get_publish_enable(msg_type)
  117. if msg_type == "ALL" then
  118. -- 返回完整的发布控制表
  119. return publish_control
  120. elseif publish_control[msg_type] ~= nil then
  121. return publish_control[msg_type]
  122. else
  123. log.error("extp", "未知的消息类型:", msg_type)
  124. return nil
  125. end
  126. end
  127. --[[
  128. 设置滑动判定阈值
  129. @api extp.set_swipe_threshold(threshold)
  130. @number threshold 滑动判定阈值(像素)
  131. @return bool 设置成功返回true,失败返回false
  132. @usage
  133. -- 设置滑动阈值为50像素
  134. extp.set_swipe_threshold(50)
  135. ]]
  136. function extp.set_swipe_threshold(threshold)
  137. if type(threshold) == "number" and threshold > 0 then
  138. swipe_threshold = threshold
  139. log.info("extp", "滑动判定阈值设置为:", threshold)
  140. return true
  141. else
  142. log.error("extp", "无效的滑动阈值:", threshold)
  143. return false
  144. end
  145. end
  146. --[[
  147. 设置长按判定阈值
  148. @api extp.set_long_press_threshold(threshold)
  149. @number threshold 长按判定阈值(毫秒)
  150. @return bool 设置成功返回true,失败返回false
  151. @usage
  152. -- 设置长按阈值为800毫秒
  153. extp.set_long_press_threshold(800)
  154. ]]
  155. function extp.set_long_press_threshold(threshold)
  156. if type(threshold) == "number" and threshold > 0 then
  157. long_press_threshold = threshold
  158. log.info("extp", "长按判定阈值设置为:", threshold)
  159. return true
  160. else
  161. log.error("extp", "无效的长按阈值:", threshold)
  162. return false
  163. end
  164. end
  165. -- 触摸回调函数
  166. -- 参数: tp_device-触摸设备对象, tp_data-触摸数据
  167. local function tp_callback(tp_device, tp_data)
  168. -- 发布原始数据
  169. if publish_control.RAW_DATA then
  170. sys.publish("BASE_TOUCH_EVENT", "RAW_DATA", tp_device, tp_data)
  171. end
  172. if type(tp_data[1]) ~= "table" then return end
  173. local event_type = tp_data[1].event
  174. local x = tp_data[1].x
  175. local y = tp_data[1].y
  176. -- 获取高精度时间戳
  177. local _, ms_l = mcu.ticks2(1)
  178. -- 使用系统时间戳或触摸数据中的时间戳
  179. local timestamp = ms_l or tp_data[1].timestamp
  180. if not event_type then return end
  181. if event_type == 2 then -- 抬手事件
  182. if state == "DOWN" or state == "MOVE" then
  183. local moveX = x - touch_down_x
  184. local moveY = y - touch_down_y
  185. if moveX < -swipe_threshold then
  186. if publish_control.SWIPE_LEFT then
  187. sys.publish("BASE_TOUCH_EVENT", "SWIPE_LEFT", moveX, 0)
  188. end
  189. elseif moveX > swipe_threshold then
  190. if publish_control.SWIPE_RIGHT then
  191. sys.publish("BASE_TOUCH_EVENT", "SWIPE_RIGHT", moveX, 0)
  192. end
  193. elseif moveY < -swipe_threshold then
  194. if publish_control.SWIPE_UP then
  195. sys.publish("BASE_TOUCH_EVENT", "SWIPE_UP", 0, moveY)
  196. end
  197. elseif moveY > swipe_threshold then
  198. if publish_control.SWIPE_DOWN then
  199. sys.publish("BASE_TOUCH_EVENT", "SWIPE_DOWN", 0, moveY)
  200. end
  201. else
  202. -- 计算按下时间
  203. local press_time = timestamp - touch_down_time
  204. -- 判断是单击还是长按
  205. if press_time < long_press_threshold then
  206. if publish_control.SINGLE_TAP then
  207. sys.publish("BASE_TOUCH_EVENT", "SINGLE_TAP", touch_down_x, touch_down_y)
  208. end
  209. else
  210. if publish_control.LONG_PRESS then
  211. sys.publish("BASE_TOUCH_EVENT", "LONG_PRESS", touch_down_x, touch_down_y)
  212. end
  213. end
  214. end
  215. state = "IDLE"
  216. end
  217. elseif event_type == 1 or event_type == 3 then -- 按下或移动事件
  218. if state == "IDLE" and event_type == 1 then
  219. -- 从空闲状态接收到按下事件
  220. state = "DOWN"
  221. touch_down_x = x
  222. touch_down_y = y
  223. touch_down_time = timestamp
  224. swipe_direction = nil
  225. -- 发布按下事件
  226. if publish_control.TOUCH_DOWN then
  227. sys.publish("BASE_TOUCH_EVENT", "TOUCH_DOWN", x, y)
  228. end
  229. elseif state == "DOWN" and event_type == 3 then
  230. -- 在按下状态下接收到移动事件
  231. if math.abs(x - touch_down_x) >= swipe_threshold or math.abs(y - touch_down_y) >= swipe_threshold then
  232. state = "MOVE"
  233. -- 确定滑动方向
  234. if math.abs(x - touch_down_x) > math.abs(y - touch_down_y) then
  235. -- 水平滑动
  236. if x - touch_down_x < 0 then
  237. swipe_direction = "LEFT"
  238. else
  239. swipe_direction = "RIGHT"
  240. end
  241. else
  242. -- 垂直滑动
  243. if y - touch_down_y < 0 then
  244. swipe_direction = "UP"
  245. else
  246. swipe_direction = "DOWN"
  247. end
  248. end
  249. end
  250. elseif state == "MOVE" and event_type == 3 then
  251. -- 在移动状态下接收到移动事件
  252. -- 根据滑动方向发布相应的移动事件
  253. if swipe_direction == "LEFT" or swipe_direction == "RIGHT" then
  254. -- 水平滑动,发布MOVE_X事件
  255. if publish_control.MOVE_X then
  256. sys.publish("BASE_TOUCH_EVENT", "MOVE_X", x - touch_down_x, 0)
  257. end
  258. else
  259. -- 垂直滑动,发布MOVE_Y事件
  260. if publish_control.MOVE_Y then
  261. sys.publish("BASE_TOUCH_EVENT", "MOVE_Y", 0, y - touch_down_y)
  262. end
  263. end
  264. end
  265. end
  266. end
  267. --[[
  268. 初始化触摸设备
  269. @api extp.init(param)
  270. @table param 触摸芯片配置参数,参考库的说明及demo用法
  271. @return bool 初始化成功返回true,失败返回false
  272. @usage
  273. -- 基础触摸初始化
  274. extp.init({
  275. tp_model = "gt911",
  276. i2c_id = 0,
  277. pin_rst = 20,
  278. pin_int = 21
  279. })
  280. -- 使用预定义配置
  281. extp.init({tp_model = "AirLCD_1010"})
  282. -- 带屏幕尺寸的初始化
  283. extp.init({
  284. tp_model = "gt911",
  285. i2c_id = 0,
  286. pin_rst = 20,
  287. pin_int = 21,
  288. w = 480,
  289. h = 320
  290. })
  291. ]]
  292. function extp.init(param)
  293. if type(param) ~= "table" then
  294. log.error("extp", "参数必须为表")
  295. return false
  296. end
  297. -- 检查必要参数
  298. if not param.tp_model then
  299. log.error("extp", "缺少必要参数: tp_model")
  300. return false
  301. end
  302. local tp_model = param.tp_model
  303. -- 检查是否支持该型号
  304. local config = tp_configs[tp_model]
  305. if not config then
  306. log.error("extp", "不支持的触摸型号:", tp_model)
  307. return false
  308. end
  309. -- 特殊型号参数处理
  310. local final_param = {}
  311. final_param.tp_model = tp_model
  312. -- 处理特殊型号的默认配置
  313. if special_tp_configs[tp_model] then
  314. local default_config = special_tp_configs[tp_model]
  315. if tp_model == "Air780EHM_LCD_4" then
  316. -- Air780EHM_LCD_4: 强制使用默认配置,忽略传入的其他参数
  317. final_param.i2c_id = default_config.i2c_id
  318. final_param.pin_rst = default_config.pin_rst
  319. final_param.pin_int = default_config.pin_int
  320. log.info("extp", "Air780EHM_LCD_4使用固定配置")
  321. else
  322. -- AirLCD_1010 和 AirLCD_1020: 使用传入参数,如果未传入则使用默认配置
  323. final_param.i2c_id = param.i2c_id or default_config.i2c_id
  324. final_param.pin_rst = param.pin_rst or default_config.pin_rst
  325. final_param.pin_int = param.pin_int or default_config.pin_int
  326. -- 记录使用的配置来源
  327. if param.i2c_id then
  328. log.info("extp", tp_model, "使用传入的i2c_id")
  329. else
  330. log.info("extp", tp_model, "使用默认i2c_id")
  331. end
  332. if param.pin_rst then
  333. log.info("extp", tp_model, "使用传入的pin_rst")
  334. else
  335. log.info("extp", tp_model, "使用默认pin_rst")
  336. end
  337. if param.pin_int then
  338. log.info("extp", tp_model, "使用传入的pin_int")
  339. else
  340. log.info("extp", tp_model, "使用默认pin_int")
  341. end
  342. end
  343. else
  344. -- 其他型号:直接使用传入参数
  345. final_param.i2c_id = param.i2c_id
  346. final_param.pin_rst = param.pin_rst
  347. final_param.pin_int = param.pin_int
  348. end
  349. local tp_i2c_id, tp_pin_rst, tp_pin_int = final_param.i2c_id, final_param.pin_rst or 255, final_param.pin_int
  350. -- 构建tp.init的参数表,增加w和h可选参数
  351. local tp_init_params = {
  352. port = tp_i2c_id,
  353. pin_rst = tp_pin_rst,
  354. pin_int = tp_pin_int
  355. }
  356. -- 如果传入了w和h参数,则添加到参数表中
  357. if param.w then
  358. tp_init_params.w = param.w
  359. log.info("extp", "设置屏幕宽度:", param.w)
  360. end
  361. if param.h then
  362. tp_init_params.h = param.h
  363. log.info("extp", "设置屏幕高度:", param.h)
  364. end
  365. -- 统一初始化流程
  366. if type(tp_i2c_id) ~= "userdata" and config.i2c_speed ~= nil then
  367. i2c.setup(tp_i2c_id, config.i2c_speed)
  368. end
  369. -- 使用包含w和h参数的table调用tp.init
  370. local tp_device = tp.init(config.tp_model, tp_init_params, tp_callback)
  371. if tp_device ~= nil then return true end
  372. if tp_device == nil then
  373. -- 如果第一次初始化失败,尝试不带pin_rst的初始化
  374. tp_init_params.pin_rst = 255
  375. local tp_device = tp.init(config.tp_model, tp_init_params, tp_callback)
  376. if tp_device ~= nil then return true end
  377. end
  378. -- 若硬件触摸初始化失败,尝试PC触摸回退
  379. log.warn("extp", "触摸初始化失败,尝试PC触摸回退")
  380. local ok_pc, dev_pc = pcall(tp.init, "pc", { port = 0 }, tp_callback)
  381. if ok_pc and dev_pc then
  382. log.info("extp", "PC触摸回退成功")
  383. return true
  384. end
  385. log.error("extp", "PC触摸回退失败")
  386. return false
  387. end
  388. return extp