tsb_waveout_page.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. --[[
  2. @module tsb_waveout_page
  3. @summary 波形输出页面
  4. @version 1.0
  5. @date 2026.03.18
  6. @author 李一玮
  7. @usage
  8. 本文件是波形输出页面,用于配置和控制波形输出。
  9. ]]
  10. local tsb_waveout_page = {}
  11. -- 页面UI元素
  12. local main_container = nil
  13. local max_amplitude_input = nil
  14. local min_amplitude_input = nil
  15. local frequency_input = nil
  16. local duty_cycle_input = nil
  17. local common_ui = require("tsb_common_page")
  18. local uart1_msg = require("uart1_msg")
  19. -- 配置参数
  20. local wave_type = "方波" -- 波形类型
  21. local max_vol = "5.0" -- 最大电压(V)
  22. local min_vol = "0.0" -- 最小电压(V)
  23. local frequency = "1000" -- 频率(Hz)
  24. local duty_cycle = "50" -- 占空比(%)
  25. local is_running = false -- 是否正在运行
  26. -- 发送DAC配置
  27. local function send_dac_config()
  28. -- 收集配置参数
  29. local config = {}
  30. -- 运行控制
  31. config.run_control = is_running and 1 or 0
  32. -- 波形类型
  33. local wave_type_map = {
  34. ["方波"] = 0,
  35. ["正弦波"] = 1,
  36. ["三角波"] = 2,
  37. ["锯齿波"] = 3
  38. }
  39. config.wave_type = wave_type_map[wave_type] or 0
  40. -- 最大电压和最小电压(转换为mV)
  41. local max_vol = max_amplitude_input and tonumber(max_amplitude_input:get_text()) or 5
  42. local min_vol = min_amplitude_input and tonumber(min_amplitude_input:get_text()) or 0
  43. config.max_vol = math.floor(max_vol * 1000)
  44. config.min_vol = math.floor(min_vol * 1000)
  45. -- 频率(Hz)
  46. config.frequency = frequency_input and tonumber(frequency_input:get_text()) or 1000
  47. -- 占空比(%)
  48. config.duty_cycle = duty_cycle_input and tonumber(duty_cycle_input:get_text()) or 50
  49. -- 发送配置
  50. uart1_msg.send_dac_config(config)
  51. end
  52. -- 创建UI
  53. function tsb_waveout_page.create_ui()
  54. main_container = airui.container({
  55. x = 0,
  56. y = 0,
  57. w = 480,
  58. h = 320,
  59. color = 0xF5F5F5,
  60. })
  61. --------------------- 标题栏 ------------------------
  62. local title_bar = airui.container({
  63. parent = main_container,
  64. x = 0,
  65. y = 0,
  66. w = 480,
  67. h = 30,
  68. color = 0x72DDF7,
  69. })
  70. airui.label({
  71. parent = title_bar,
  72. text = "波形输出",
  73. x = 200,
  74. y = 8,
  75. w = 80,
  76. h = 20,
  77. font_size = 16,
  78. color = 0xFFFFFF,
  79. })
  80. -- 标题栏公共信息展示
  81. common_ui.add_battery_display(title_bar)
  82. common_ui.create_back_button(title_bar, tsb_waveout_page.cleanup)
  83. ---------------------------------------------------
  84. -- 主容器
  85. local scroll_container = airui.container({
  86. parent = main_container,
  87. x = 0,
  88. y = 30,
  89. w = 480,
  90. h = 270,
  91. color = 0xF5F5F5,
  92. })
  93. local keyboard1 = airui.keyboard({
  94. x = 0,
  95. y = 0,
  96. w = 480,
  97. h = 160, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算
  98. mode = "numeric", -- 键盘模式,可选 "text"/"upper"/"lower"/"numeric"
  99. auto_hide = true, -- 自动隐藏键盘
  100. preview = true,
  101. preview_height = 35,
  102. bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明
  103. on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
  104. log.info("keyboard", "commit")
  105. send_dac_config()
  106. end
  107. })
  108. -- 配置容器
  109. local config_container = airui.container({
  110. parent = scroll_container,
  111. x = 20,
  112. y = 20,
  113. w = 440,
  114. h = 180,
  115. color = 0xFFFFFF,
  116. radius = 8,
  117. border_width = 1,
  118. border_color = 0xDDDDDD,
  119. })
  120. local config_y = 20
  121. -- 波形类型
  122. airui.label({
  123. parent = config_container,
  124. text = "波形类型",
  125. x = 20,
  126. y = config_y,
  127. w = 80,
  128. h = 25,
  129. font_size = 14,
  130. color = 0x333333,
  131. })
  132. local wave_type_dropdown = airui.dropdown({
  133. parent = config_container,
  134. x = 120,
  135. y = config_y-8,
  136. w = 100,
  137. h = 35,
  138. options = {"方波", "正弦波", "三角波", "锯齿波"},
  139. default_index = 0,
  140. on_change = function(self, idx)
  141. local options = {"方波", "正弦波", "三角波", "锯齿波"}
  142. wave_type = options[idx + 1]
  143. log.info("waveout", "波形类型: " .. wave_type)
  144. send_dac_config()
  145. end
  146. })
  147. config_y = config_y + 40
  148. -- 幅度(峰峰值)
  149. airui.label({
  150. parent = config_container,
  151. text = "最大电压 (V)",
  152. x = 20,
  153. y = config_y,
  154. w = 100,
  155. h = 35,
  156. font_size = 14,
  157. color = 0x333333,
  158. })
  159. max_amplitude_input = airui.textarea({
  160. parent = config_container,
  161. x = 120,
  162. y = config_y-8,
  163. w = 100,
  164. h = 35,
  165. text = "5",
  166. max_len = 5,
  167. keyboard = keyboard1
  168. })
  169. airui.label({
  170. parent = config_container,
  171. text = "最小电压 (V)",
  172. x = 230,
  173. y = config_y,
  174. w = 100,
  175. h = 35,
  176. font_size = 14,
  177. color = 0x333333,
  178. })
  179. min_amplitude_input = airui.textarea({
  180. parent = config_container,
  181. x = 330,
  182. y = config_y-8,
  183. w = 100,
  184. h = 35,
  185. text = "0",
  186. max_len = 5,
  187. keyboard = keyboard1
  188. })
  189. config_y = config_y + 40
  190. -- 频率
  191. airui.label({
  192. parent = config_container,
  193. text = "频率 (Hz)",
  194. x = 20,
  195. y = config_y,
  196. w = 80,
  197. h = 25,
  198. font_size = 14,
  199. color = 0x333333,
  200. })
  201. frequency_input = airui.textarea({
  202. parent = config_container,
  203. x = 120,
  204. y = config_y-8,
  205. w = 100,
  206. h = 35,
  207. text = "1000",
  208. max_len = 6,
  209. keyboard = keyboard1
  210. })
  211. config_y = config_y + 40
  212. -- 占空比(仅方波可用)
  213. airui.label({
  214. parent = config_container,
  215. text = "占空比 (%)",
  216. x = 20,
  217. y = config_y,
  218. w = 80,
  219. h = 25,
  220. font_size = 14,
  221. color = 0x333333,
  222. })
  223. duty_cycle_input = airui.textarea({
  224. parent = config_container,
  225. x = 120,
  226. y = config_y-8,
  227. w = 100,
  228. h = 35,
  229. text = "50",
  230. max_len = 3,
  231. keyboard = keyboard1
  232. })
  233. -- 控制按钮区域
  234. local button_container = airui.container({
  235. parent = scroll_container,
  236. x = 20,
  237. y = 210,
  238. w = 440,
  239. h = 40,
  240. color = 0xF5F5F5,
  241. })
  242. -- 开始按钮
  243. local start_btn = airui.button({
  244. parent = button_container,
  245. text = "开始",
  246. x = 100,
  247. y = 0,
  248. w = 100,
  249. h = 35,
  250. color = 0x4CAF50,
  251. text_color = 0xFFFFFF,
  252. style = { bg_color = 0x2B6FF1,border_color = 0x2B6FF1, text_color = 0xFFFFFF, radius = 8 },
  253. on_click = function(self)
  254. if not is_running then
  255. is_running = true
  256. self:set_text("运行中")
  257. -- 获取配置参数
  258. max_vol = max_amplitude_input:get_text()
  259. min_vol = min_amplitude_input:get_text()
  260. frequency = frequency_input:get_text()
  261. duty_cycle = duty_cycle_input:get_text()
  262. log.info("waveout", "开始输出波形", wave_type, max_vol, min_vol, frequency, duty_cycle)
  263. -- 发送配置指令
  264. send_dac_config()
  265. end
  266. end
  267. })
  268. -- 停止按钮
  269. local stop_btn = airui.button({
  270. parent = button_container,
  271. text = "停止",
  272. x = 240,
  273. y = 0,
  274. w = 100,
  275. h = 35,
  276. color = 0xF44336,
  277. text_color = 0xFFFFFF,
  278. on_click = function(self)
  279. if is_running then
  280. is_running = false
  281. start_btn:set_text("开始")
  282. log.info("waveout", "停止输出波形")
  283. -- 发送停止指令
  284. send_dac_config()
  285. end
  286. end
  287. })
  288. -- 底部信息
  289. common_ui.create_status_bar(main_container)
  290. end
  291. -- 初始化页面
  292. function tsb_waveout_page.init(params)
  293. tsb_waveout_page.create_ui()
  294. end
  295. -- 直接使用全局变量发送配置(不依赖UI元素)
  296. local function send_dac_config_direct()
  297. local config = {}
  298. -- 运行控制
  299. config.run_control = is_running and 1 or 0
  300. -- 波形类型
  301. local wave_type_map = {
  302. ["方波"] = 0,
  303. ["正弦波"] = 1,
  304. ["三角波"] = 2,
  305. ["锯齿波"] = 3
  306. }
  307. config.wave_type = wave_type_map[wave_type] or 0
  308. -- 最大电压和最小电压(转换为mV)
  309. config.max_vol = math.floor((tonumber(max_vol) or 5) * 1000)
  310. config.min_vol = math.floor((tonumber(min_vol) or 0) * 1000)
  311. -- 频率(Hz)
  312. config.frequency = tonumber(frequency) or 1000
  313. -- 占空比(%)
  314. config.duty_cycle = tonumber(duty_cycle) or 50
  315. -- 发送配置
  316. uart1_msg.send_dac_config(config)
  317. end
  318. -- 恢复默认配置并下发
  319. local function restore_default_config()
  320. -- 恢复默认配置参数
  321. wave_type = "方波"
  322. max_vol = "5.0"
  323. min_vol = "0.0"
  324. frequency = "1000"
  325. duty_cycle = "50"
  326. is_running = false
  327. -- 使用不依赖UI的方式下发配置
  328. send_dac_config_direct()
  329. log.info("waveout", "已恢复默认配置并下发")
  330. end
  331. -- 清理页面
  332. function tsb_waveout_page.cleanup()
  333. -- 恢复默认配置并下发(在销毁UI之前执行)
  334. restore_default_config()
  335. if main_container then
  336. main_container:destroy()
  337. main_container = nil
  338. end
  339. end
  340. return tsb_waveout_page