| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- --[[
- @module tsb_waveout_page
- @summary 波形输出页面
- @version 1.0
- @date 2026.03.18
- @author 李一玮
- @usage
- 本文件是波形输出页面,用于配置和控制波形输出。
- ]]
- local tsb_waveout_page = {}
- -- 页面UI元素
- local main_container = nil
- local max_amplitude_input = nil
- local min_amplitude_input = nil
- local frequency_input = nil
- local duty_cycle_input = nil
- local common_ui = require("tsb_common_page")
- local uart1_msg = require("uart1_msg")
- -- 配置参数
- local wave_type = "方波" -- 波形类型
- local max_vol = "5.0" -- 最大电压(V)
- local min_vol = "0.0" -- 最小电压(V)
- local frequency = "1000" -- 频率(Hz)
- local duty_cycle = "50" -- 占空比(%)
- local is_running = false -- 是否正在运行
- -- 发送DAC配置
- local function send_dac_config()
- -- 收集配置参数
- local config = {}
-
- -- 运行控制
- config.run_control = is_running and 1 or 0
-
- -- 波形类型
- local wave_type_map = {
- ["方波"] = 0,
- ["正弦波"] = 1,
- ["三角波"] = 2,
- ["锯齿波"] = 3
- }
- config.wave_type = wave_type_map[wave_type] or 0
-
- -- 最大电压和最小电压(转换为mV)
- local max_vol = max_amplitude_input and tonumber(max_amplitude_input:get_text()) or 5
- local min_vol = min_amplitude_input and tonumber(min_amplitude_input:get_text()) or 0
- config.max_vol = math.floor(max_vol * 1000)
- config.min_vol = math.floor(min_vol * 1000)
-
- -- 频率(Hz)
- config.frequency = frequency_input and tonumber(frequency_input:get_text()) or 1000
-
- -- 占空比(%)
- config.duty_cycle = duty_cycle_input and tonumber(duty_cycle_input:get_text()) or 50
-
- -- 发送配置
- uart1_msg.send_dac_config(config)
- end
- -- 创建UI
- function tsb_waveout_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 = 0x72DDF7,
- })
- airui.label({
- parent = title_bar,
- text = "波形输出",
- x = 200,
- 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_waveout_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"
- auto_hide = true, -- 自动隐藏键盘
- preview = true,
- preview_height = 35,
- bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明
- on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
- log.info("keyboard", "commit")
- send_dac_config()
- end
- })
- -- 配置容器
- local config_container = airui.container({
- parent = scroll_container,
- x = 20,
- y = 20,
- w = 440,
- h = 180,
- color = 0xFFFFFF,
- radius = 8,
- border_width = 1,
- border_color = 0xDDDDDD,
- })
- local config_y = 20
- -- 波形类型
- airui.label({
- parent = config_container,
- text = "波形类型",
- x = 20,
- y = config_y,
- w = 80,
- h = 25,
- font_size = 14,
- color = 0x333333,
- })
- local wave_type_dropdown = airui.dropdown({
- parent = config_container,
- x = 120,
- y = config_y-8,
- w = 100,
- h = 35,
- options = {"方波", "正弦波", "三角波", "锯齿波"},
- default_index = 0,
- on_change = function(self, idx)
- local options = {"方波", "正弦波", "三角波", "锯齿波"}
- wave_type = options[idx + 1]
- log.info("waveout", "波形类型: " .. wave_type)
- send_dac_config()
- end
- })
- config_y = config_y + 40
- -- 幅度(峰峰值)
- airui.label({
- parent = config_container,
- text = "最大电压 (V)",
- x = 20,
- y = config_y,
- w = 100,
- h = 35,
- font_size = 14,
- color = 0x333333,
- })
- max_amplitude_input = airui.textarea({
- parent = config_container,
- x = 120,
- y = config_y-8,
- w = 100,
- h = 35,
- text = "5",
- max_len = 5,
- keyboard = keyboard1
- })
- airui.label({
- parent = config_container,
- text = "最小电压 (V)",
- x = 230,
- y = config_y,
- w = 100,
- h = 35,
- font_size = 14,
- color = 0x333333,
- })
- min_amplitude_input = airui.textarea({
- parent = config_container,
- x = 330,
- y = config_y-8,
- w = 100,
- h = 35,
- text = "0",
- max_len = 5,
- keyboard = keyboard1
- })
- config_y = config_y + 40
- -- 频率
- airui.label({
- parent = config_container,
- text = "频率 (Hz)",
- x = 20,
- y = config_y,
- w = 80,
- h = 25,
- font_size = 14,
- color = 0x333333,
- })
- frequency_input = airui.textarea({
- parent = config_container,
- x = 120,
- y = config_y-8,
- w = 100,
- h = 35,
- text = "1000",
- max_len = 6,
- keyboard = keyboard1
- })
- config_y = config_y + 40
- -- 占空比(仅方波可用)
- airui.label({
- parent = config_container,
- text = "占空比 (%)",
- x = 20,
- y = config_y,
- w = 80,
- h = 25,
- font_size = 14,
- color = 0x333333,
- })
- duty_cycle_input = airui.textarea({
- parent = config_container,
- x = 120,
- y = config_y-8,
- w = 100,
- h = 35,
- text = "50",
- max_len = 3,
- keyboard = keyboard1
- })
- -- 控制按钮区域
- local button_container = airui.container({
- parent = scroll_container,
- x = 20,
- y = 210,
- w = 440,
- h = 40,
- color = 0xF5F5F5,
- })
- -- 开始按钮
- local start_btn = airui.button({
- parent = button_container,
- text = "开始",
- x = 100,
- y = 0,
- w = 100,
- h = 35,
- color = 0x4CAF50,
- text_color = 0xFFFFFF,
- style = { bg_color = 0x2B6FF1,border_color = 0x2B6FF1, text_color = 0xFFFFFF, radius = 8 },
- on_click = function(self)
- if not is_running then
- is_running = true
- self:set_text("运行中")
- -- 获取配置参数
- max_vol = max_amplitude_input:get_text()
- min_vol = min_amplitude_input:get_text()
- frequency = frequency_input:get_text()
- duty_cycle = duty_cycle_input:get_text()
- log.info("waveout", "开始输出波形", wave_type, max_vol, min_vol, frequency, duty_cycle)
- -- 发送配置指令
- send_dac_config()
- end
- end
- })
- -- 停止按钮
- local stop_btn = airui.button({
- parent = button_container,
- text = "停止",
- x = 240,
- y = 0,
- w = 100,
- h = 35,
- color = 0xF44336,
- text_color = 0xFFFFFF,
- on_click = function(self)
- if is_running then
- is_running = false
- start_btn:set_text("开始")
- log.info("waveout", "停止输出波形")
- -- 发送停止指令
- send_dac_config()
- end
- end
- })
- -- 底部信息
- common_ui.create_status_bar(main_container)
- end
- -- 初始化页面
- function tsb_waveout_page.init(params)
- tsb_waveout_page.create_ui()
- end
- -- 直接使用全局变量发送配置(不依赖UI元素)
- local function send_dac_config_direct()
- local config = {}
-
- -- 运行控制
- config.run_control = is_running and 1 or 0
-
- -- 波形类型
- local wave_type_map = {
- ["方波"] = 0,
- ["正弦波"] = 1,
- ["三角波"] = 2,
- ["锯齿波"] = 3
- }
- config.wave_type = wave_type_map[wave_type] or 0
-
- -- 最大电压和最小电压(转换为mV)
- config.max_vol = math.floor((tonumber(max_vol) or 5) * 1000)
- config.min_vol = math.floor((tonumber(min_vol) or 0) * 1000)
-
- -- 频率(Hz)
- config.frequency = tonumber(frequency) or 1000
-
- -- 占空比(%)
- config.duty_cycle = tonumber(duty_cycle) or 50
-
- -- 发送配置
- uart1_msg.send_dac_config(config)
- end
- -- 恢复默认配置并下发
- local function restore_default_config()
- -- 恢复默认配置参数
- wave_type = "方波"
- max_vol = "5.0"
- min_vol = "0.0"
- frequency = "1000"
- duty_cycle = "50"
- is_running = false
-
- -- 使用不依赖UI的方式下发配置
- send_dac_config_direct()
- log.info("waveout", "已恢复默认配置并下发")
- end
- -- 清理页面
- function tsb_waveout_page.cleanup()
- -- 恢复默认配置并下发(在销毁UI之前执行)
- restore_default_config()
-
- if main_container then
- main_container:destroy()
- main_container = nil
- end
- end
- return tsb_waveout_page
|