--[[ @module tsb_bmq_page @summary 编码器演示页面 @version 1.0 @date 2026.03.04 @author 李一玮 @usage 本文件是编码器演示页面,展示编码器的各种用法。 ]] local tsb_bmq_page = {} -- 页面UI元素 local main_container = nil local common_ui = require("tsb_common_page") local label_cur_vol = nil local label_pulse_count = nil -- GPIO检测配置 local DETECT_PIN = 31 -- 检测引脚:GPIO31 local pulse_count = 0 -- 脉冲计数 local level_check_timer = nil -- 电平检查定时器 -- 电平中断回调函数(检测到高→低跳变时执行) local function pin_interrupt_callback() -- 低电平时计数(下降沿) if label_pulse_count then pulse_count = pulse_count + 1 label_pulse_count:set_text(tostring(pulse_count)) --log.info("bmq", "检测到下降沿,脉冲数:", pulse_count) end end -- 周期性检查电平状态(用于更新显示) local function check_level_status() if label_cur_vol then local level = gpio.get(DETECT_PIN) label_cur_vol:set_text(level == 1 and "高" or "低") end end -- 创建UI function tsb_bmq_page.create_ui() main_container = airui.container({ x = 0, y = 0, w = 480, h = 320, color = 0xF5F5F5, }) --------------------- 标题栏 ------------------------ local title_bar = airui.container({ parent = main_container, x = 0, y = 0, w = 480, h = 30, color = 0xFF9800, }) airui.label({ parent = title_bar, text = "编码器", x = 210, y = 8, w = 80, h = 20, font_size = 16, color = 0xFFFFFF, }) -- 标题栏公共信息展示 common_ui.add_battery_display(title_bar) common_ui.create_back_button(title_bar, tsb_bmq_page.cleanup) --------------------------------------------------- -- 滚动容器 local scroll_container = airui.container({ parent = main_container, x = 0, y = 30, w = 480, h = 270, color = 0xF5F5F5, }) local keyboard1 = airui.keyboard({ x = 0, y = 0, w = 480, h = 160, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算 mode = "numeric", -- 键盘模式,可选 "text"/"upper"/"lower"/"numeric" preview = true, preview_height = 35, auto_hide = true, -- 自动隐藏键盘 bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明 on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发 log.info("keyboard", "commit") end }) ------------------- 模拟编码器输入 ------------------ local bmq_input_container = airui.container({ parent = scroll_container, x = 10, y = 5, w = 200, h = 260, color = 0xFFFFFF, radius = 5, }) airui.label({ parent = bmq_input_container, text = "编码器信号输入", x = 35, y = 10, w = 140, h = 30, font_size = 18, }) airui.label({ parent = bmq_input_container, text = "实时电平:", x = 20, y = 80, w = 80, h = 30, font_size = 16, }) label_cur_vol = airui.label({ parent = bmq_input_container, text = "高", x = 100, y = 80, w = 80, h = 30, font_size = 16, }) airui.label({ parent = bmq_input_container, text = "检测脉冲数:", x = 20, y = 140, w = 100, h = 30, font_size = 16, }) label_pulse_count = airui.label({ parent = bmq_input_container, text = "12345", x = 120, y = 140, w = 70, h = 30, font_size = 16, }) local tax_serial_btn = airui.button({ parent = bmq_input_container, x = 65, y = 215, w = 80, h = 35, text = "清零", on_click = function(self) log.info("button", "清零按钮被点击") pulse_count = 0 label_pulse_count:set_text("0") end }) ---------------------------------------------------- ------------------- 模拟编码器输出 ------------------ local bmq_output_container = airui.container({ parent = scroll_container, x = 220, y = 5, w = 250, h = 260, color = 0xFFFFFF, radius = 5, }) airui.label({ parent = bmq_output_container, text = "编码器信号输出", x = 60, y = 10, w = 140, h = 30, font_size = 18, }) -- 脉冲周期 airui.label({ parent = bmq_output_container, text = "脉冲周期:", x = 20, y = 50, w = 80, h = 30, font_size = 16, }) local cycle_input = airui.textarea({ parent = bmq_output_container, x = 100, y = 40, w = 60, h = 35, text = "5", max_len = 5, keyboard = keyboard1 }) airui.label({ parent = bmq_output_container, text = "ms", x = 165, y = 50, w = 30, h = 30, font_size = 16, }) -- 电压幅度 airui.label({ parent = bmq_output_container, text = "电压幅度:", x = 20, y = 90, w = 80, h = 30, font_size = 16, }) local voltage_input = airui.textarea({ parent = bmq_output_container, x = 100, y = 80, w = 60, h = 35, text = "5", max_len = 2, keyboard = keyboard1 }) airui.label({ parent = bmq_output_container, text = "V", x = 170, y = 90, w = 30, h = 30, font_size = 16, }) -- 当前电平 airui.label({ parent = bmq_output_container, text = "当前电平:", x = 20, y = 130, w = 80, h = 30, font_size = 16, }) local label_output_level = airui.label({ parent = bmq_output_container, text = "高", x = 100, y = 130, w = 80, h = 30, font_size = 16, }) -- 输出脉冲数 airui.label({ parent = bmq_output_container, text = "输出脉冲数:", x = 20, y = 170, w = 100, h = 30, font_size = 16, }) local label_output_pulse = airui.label({ parent = bmq_output_container, text = "0", x = 120, y = 170, w = 80, h = 30, font_size = 16, }) -- 按钮 local start_btn = airui.button({ parent = bmq_output_container, x = 40, y = 215, w = 80, h = 35, text = "开始", style = { bg_color = 0x2B6FF1,border_color = 0x2B6FF1, text_color = 0xFFFFFF, radius = 8 }, on_click = function(self) log.info("button", "开始按钮被点击") end }) local clear_btn = airui.button({ parent = bmq_output_container, x = 130, y = 215, w = 80, h = 35, text = "清零", on_click = function(self) log.info("button", "清零按钮被点击") end }) ---------------------------------------------------- -- 底部信息 common_ui.create_status_bar(main_container) end -- 初始化GPIO检测 local function init_gpio_detect() -- 配置为下降沿中断检测 gpio.setup(DETECT_PIN, pin_interrupt_callback, gpio.PULLDOWN, gpio.FALLING) log.info("bmq", "GPIO初始化完成,引脚:", DETECT_PIN) -- 初始化显示当前电平 local level = gpio.get(DETECT_PIN) if label_cur_vol then label_cur_vol:set_text(level == 1 and "高" or "低") end if label_pulse_count then label_pulse_count:set_text("0") end -- 启动周期性电平检查定时器(每50ms检查一次) level_check_timer = sys.timerLoopStart(check_level_status, 2) log.info("bmq", "电平检查定时器已启动") end -- 初始化页面 function tsb_bmq_page.init(params) tsb_bmq_page.create_ui() -- 初始化GPIO检测 init_gpio_detect() end -- 清理页面 function tsb_bmq_page.cleanup() -- 停止电平检查定时器 if level_check_timer then sys.timerStop(level_check_timer) level_check_timer = nil log.info("bmq", "电平检查定时器已停止") end -- 释放GPIO资源 gpio.setup(DETECT_PIN, nil) log.info("bmq", "GPIO资源已释放") if main_container then main_container:destroy() main_container = nil end end return tsb_bmq_page