tsb_bmq_page.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. --[[
  2. @module tsb_bmq_page
  3. @summary 编码器演示页面
  4. @version 1.0
  5. @date 2026.03.04
  6. @author 李一玮
  7. @usage
  8. 本文件是编码器演示页面,展示编码器的各种用法。
  9. ]]
  10. local tsb_bmq_page = {}
  11. -- 页面UI元素
  12. local main_container = nil
  13. local common_ui = require("tsb_common_page")
  14. local label_cur_vol = nil
  15. local label_pulse_count = nil
  16. -- GPIO检测配置
  17. local DETECT_PIN = 31 -- 检测引脚:GPIO31
  18. local pulse_count = 0 -- 脉冲计数
  19. local level_check_timer = nil -- 电平检查定时器
  20. -- 电平中断回调函数(检测到高→低跳变时执行)
  21. local function pin_interrupt_callback()
  22. -- 低电平时计数(下降沿)
  23. if label_pulse_count then
  24. pulse_count = pulse_count + 1
  25. label_pulse_count:set_text(tostring(pulse_count))
  26. --log.info("bmq", "检测到下降沿,脉冲数:", pulse_count)
  27. end
  28. end
  29. -- 周期性检查电平状态(用于更新显示)
  30. local function check_level_status()
  31. if label_cur_vol then
  32. local level = gpio.get(DETECT_PIN)
  33. label_cur_vol:set_text(level == 1 and "高" or "低")
  34. end
  35. end
  36. -- 创建UI
  37. function tsb_bmq_page.create_ui()
  38. main_container = airui.container({
  39. x = 0,
  40. y = 0,
  41. w = 480,
  42. h = 320,
  43. color = 0xF5F5F5,
  44. })
  45. --------------------- 标题栏 ------------------------
  46. local title_bar = airui.container({
  47. parent = main_container,
  48. x = 0,
  49. y = 0,
  50. w = 480,
  51. h = 30,
  52. color = 0xFF9800,
  53. })
  54. airui.label({
  55. parent = title_bar,
  56. text = "编码器",
  57. x = 210,
  58. y = 8,
  59. w = 80,
  60. h = 20,
  61. font_size = 16,
  62. color = 0xFFFFFF,
  63. })
  64. -- 标题栏公共信息展示
  65. common_ui.add_battery_display(title_bar)
  66. common_ui.create_back_button(title_bar, tsb_bmq_page.cleanup)
  67. ---------------------------------------------------
  68. -- 滚动容器
  69. local scroll_container = airui.container({
  70. parent = main_container,
  71. x = 0,
  72. y = 30,
  73. w = 480,
  74. h = 270,
  75. color = 0xF5F5F5,
  76. })
  77. local keyboard1 = airui.keyboard({
  78. x = 0,
  79. y = 0,
  80. w = 480,
  81. h = 160, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算
  82. mode = "numeric", -- 键盘模式,可选 "text"/"upper"/"lower"/"numeric"
  83. preview = true,
  84. preview_height = 35,
  85. auto_hide = true, -- 自动隐藏键盘
  86. bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明
  87. on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
  88. log.info("keyboard", "commit")
  89. end
  90. })
  91. ------------------- 模拟编码器输入 ------------------
  92. local bmq_input_container = airui.container({
  93. parent = scroll_container,
  94. x = 10,
  95. y = 5,
  96. w = 200,
  97. h = 260,
  98. color = 0xFFFFFF,
  99. radius = 5,
  100. })
  101. airui.label({
  102. parent = bmq_input_container,
  103. text = "编码器信号输入",
  104. x = 35,
  105. y = 10,
  106. w = 140,
  107. h = 30,
  108. font_size = 18,
  109. })
  110. airui.label({
  111. parent = bmq_input_container,
  112. text = "实时电平:",
  113. x = 20,
  114. y = 80,
  115. w = 80,
  116. h = 30,
  117. font_size = 16,
  118. })
  119. label_cur_vol = airui.label({
  120. parent = bmq_input_container,
  121. text = "高",
  122. x = 100,
  123. y = 80,
  124. w = 80,
  125. h = 30,
  126. font_size = 16,
  127. })
  128. airui.label({
  129. parent = bmq_input_container,
  130. text = "检测脉冲数:",
  131. x = 20,
  132. y = 140,
  133. w = 100,
  134. h = 30,
  135. font_size = 16,
  136. })
  137. label_pulse_count = airui.label({
  138. parent = bmq_input_container,
  139. text = "12345",
  140. x = 120,
  141. y = 140,
  142. w = 70,
  143. h = 30,
  144. font_size = 16,
  145. })
  146. local tax_serial_btn = airui.button({
  147. parent = bmq_input_container,
  148. x = 65,
  149. y = 215,
  150. w = 80,
  151. h = 35,
  152. text = "清零",
  153. on_click = function(self)
  154. log.info("button", "清零按钮被点击")
  155. pulse_count = 0
  156. label_pulse_count:set_text("0")
  157. end
  158. })
  159. ----------------------------------------------------
  160. ------------------- 模拟编码器输出 ------------------
  161. local bmq_output_container = airui.container({
  162. parent = scroll_container,
  163. x = 220,
  164. y = 5,
  165. w = 250,
  166. h = 260,
  167. color = 0xFFFFFF,
  168. radius = 5,
  169. })
  170. airui.label({
  171. parent = bmq_output_container,
  172. text = "编码器信号输出",
  173. x = 60,
  174. y = 10,
  175. w = 140,
  176. h = 30,
  177. font_size = 18,
  178. })
  179. -- 脉冲周期
  180. airui.label({
  181. parent = bmq_output_container,
  182. text = "脉冲周期:",
  183. x = 20,
  184. y = 50,
  185. w = 80,
  186. h = 30,
  187. font_size = 16,
  188. })
  189. local cycle_input = airui.textarea({
  190. parent = bmq_output_container,
  191. x = 100,
  192. y = 40,
  193. w = 60,
  194. h = 35,
  195. text = "5",
  196. max_len = 5,
  197. keyboard = keyboard1
  198. })
  199. airui.label({
  200. parent = bmq_output_container,
  201. text = "ms",
  202. x = 165,
  203. y = 50,
  204. w = 30,
  205. h = 30,
  206. font_size = 16,
  207. })
  208. -- 电压幅度
  209. airui.label({
  210. parent = bmq_output_container,
  211. text = "电压幅度:",
  212. x = 20,
  213. y = 90,
  214. w = 80,
  215. h = 30,
  216. font_size = 16,
  217. })
  218. local voltage_input = airui.textarea({
  219. parent = bmq_output_container,
  220. x = 100,
  221. y = 80,
  222. w = 60,
  223. h = 35,
  224. text = "5",
  225. max_len = 2,
  226. keyboard = keyboard1
  227. })
  228. airui.label({
  229. parent = bmq_output_container,
  230. text = "V",
  231. x = 170,
  232. y = 90,
  233. w = 30,
  234. h = 30,
  235. font_size = 16,
  236. })
  237. -- 当前电平
  238. airui.label({
  239. parent = bmq_output_container,
  240. text = "当前电平:",
  241. x = 20,
  242. y = 130,
  243. w = 80,
  244. h = 30,
  245. font_size = 16,
  246. })
  247. local label_output_level = airui.label({
  248. parent = bmq_output_container,
  249. text = "高",
  250. x = 100,
  251. y = 130,
  252. w = 80,
  253. h = 30,
  254. font_size = 16,
  255. })
  256. -- 输出脉冲数
  257. airui.label({
  258. parent = bmq_output_container,
  259. text = "输出脉冲数:",
  260. x = 20,
  261. y = 170,
  262. w = 100,
  263. h = 30,
  264. font_size = 16,
  265. })
  266. local label_output_pulse = airui.label({
  267. parent = bmq_output_container,
  268. text = "0",
  269. x = 120,
  270. y = 170,
  271. w = 80,
  272. h = 30,
  273. font_size = 16,
  274. })
  275. -- 按钮
  276. local start_btn = airui.button({
  277. parent = bmq_output_container,
  278. x = 40,
  279. y = 215,
  280. w = 80,
  281. h = 35,
  282. text = "开始",
  283. style = { bg_color = 0x2B6FF1,border_color = 0x2B6FF1, text_color = 0xFFFFFF, radius = 8 },
  284. on_click = function(self)
  285. log.info("button", "开始按钮被点击")
  286. end
  287. })
  288. local clear_btn = airui.button({
  289. parent = bmq_output_container,
  290. x = 130,
  291. y = 215,
  292. w = 80,
  293. h = 35,
  294. text = "清零",
  295. on_click = function(self)
  296. log.info("button", "清零按钮被点击")
  297. end
  298. })
  299. ----------------------------------------------------
  300. -- 底部信息
  301. common_ui.create_status_bar(main_container)
  302. end
  303. -- 初始化GPIO检测
  304. local function init_gpio_detect()
  305. -- 配置为下降沿中断检测
  306. gpio.setup(DETECT_PIN, pin_interrupt_callback, gpio.PULLDOWN, gpio.FALLING)
  307. log.info("bmq", "GPIO初始化完成,引脚:", DETECT_PIN)
  308. -- 初始化显示当前电平
  309. local level = gpio.get(DETECT_PIN)
  310. if label_cur_vol then
  311. label_cur_vol:set_text(level == 1 and "高" or "低")
  312. end
  313. if label_pulse_count then
  314. label_pulse_count:set_text("0")
  315. end
  316. -- 启动周期性电平检查定时器(每50ms检查一次)
  317. level_check_timer = sys.timerLoopStart(check_level_status, 2)
  318. log.info("bmq", "电平检查定时器已启动")
  319. end
  320. -- 初始化页面
  321. function tsb_bmq_page.init(params)
  322. tsb_bmq_page.create_ui()
  323. -- 初始化GPIO检测
  324. init_gpio_detect()
  325. end
  326. -- 清理页面
  327. function tsb_bmq_page.cleanup()
  328. -- 停止电平检查定时器
  329. if level_check_timer then
  330. sys.timerStop(level_check_timer)
  331. level_check_timer = nil
  332. log.info("bmq", "电平检查定时器已停止")
  333. end
  334. -- 释放GPIO资源
  335. gpio.setup(DETECT_PIN, nil)
  336. log.info("bmq", "GPIO资源已释放")
  337. if main_container then
  338. main_container:destroy()
  339. main_container = nil
  340. end
  341. end
  342. return tsb_bmq_page