tsb_common_page.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. --[[
  2. @module common_ui
  3. @summary 公共UI组件
  4. @version 1.0
  5. @date 2026.03.05
  6. @author 李一玮
  7. @usage
  8. 本文件提供公共UI组件功能,如电量显示等。
  9. ]]
  10. local common_ui = {}
  11. local time1_id = nil
  12. local show_charge_info = false -- 控制显示哪个文本
  13. local switch_counter = 0 -- 计数器,用于控制文本切换频率
  14. local shutdown_msgbox = nil -- 跟踪关机弹窗状态
  15. local bq25895_iic_id = 1
  16. local bq25895 = nil
  17. local bq25895_addr = 0x6A -- 充电芯片I2C地址
  18. --local bq25895_reg2 = 0x02 -- 控制ADC检测开关
  19. local bq25895_reg9 = 0x09 -- bit5位可以控制VSYS到VBAT的通路开关(默认0导通,1关闭)
  20. -- 在现有标题栏添加电量显示
  21. -- @param title_bar 标题栏容器对象
  22. -- @return battery_label 电量标签对象,用于后续更新
  23. function common_ui.add_battery_display(parent)
  24. -- 电量图标
  25. airui.image({
  26. parent = parent,
  27. x = 410,
  28. y = 5,
  29. w = 40,
  30. h = 20,
  31. src = "/luadb/dian3.png",
  32. zoom = 200,
  33. })
  34. -- 电量值标签
  35. local battery_label = airui.label({
  36. parent = parent,
  37. text = "70%",
  38. x = 445,
  39. y = 8,
  40. w = 35,
  41. h = 20,
  42. font_size = 14,
  43. color = 0xFFFFFF,
  44. })
  45. -- 信号图标
  46. airui.image({
  47. parent = parent,
  48. x = 355,
  49. y = 5,
  50. w = 40,
  51. h = 20,
  52. src = "/luadb/4g3.png",
  53. zoom = 160,
  54. })
  55. -- 信号值标签
  56. local battery_label = airui.label({
  57. parent = parent,
  58. text = "4G",
  59. x = 390,
  60. y = 8,
  61. w = 45,
  62. h = 20,
  63. font_size = 14,
  64. color = 0xFFFFFF,
  65. })
  66. -- WIFI图标
  67. airui.image({
  68. parent = parent,
  69. x = 330,
  70. y = 5,
  71. w = 40,
  72. h = 20,
  73. src = "/luadb/wifi3.png",
  74. zoom = 150,
  75. })
  76. return battery_label
  77. end
  78. function common_ui.create_back_button(parent, cleanup_func)
  79. -- local back_btn = airui.button({
  80. -- parent = parent,
  81. -- x = 10,
  82. -- y = 3,
  83. -- w = 45,
  84. -- h = 25,
  85. -- text = "返回",
  86. -- on_click = function()
  87. -- -- 调用当前页面的清理函数
  88. -- if cleanup_func then
  89. -- cleanup_func()
  90. -- end
  91. -- -- 返回上一页
  92. -- go_back()
  93. -- end
  94. -- })
  95. local back_img = airui.image({
  96. parent = parent,
  97. x = 5,
  98. y = 4,
  99. w = 20,
  100. h = 25,
  101. src = "/luadb/back1.png",
  102. zoom = 256,
  103. opacity = 255,
  104. on_click = function()
  105. -- 调用当前页面的清理函数
  106. if cleanup_func then
  107. cleanup_func()
  108. end
  109. -- 返回上一页
  110. go_back()
  111. end
  112. })
  113. local back_label = airui.label({
  114. parent = parent,
  115. text = "返回",
  116. x = 22,
  117. y = 9,
  118. w = 45,
  119. h = 20,
  120. font_size = 15,
  121. color = 0xFFFFFF,
  122. on_click = function()
  123. -- 调用当前页面的清理函数
  124. if cleanup_func then
  125. cleanup_func()
  126. end
  127. -- 返回上一页
  128. go_back()
  129. end
  130. })
  131. -- local home_btn = airui.button({
  132. -- parent = parent,
  133. -- x = 60,
  134. -- y = 3,
  135. -- w = 45,
  136. -- h = 25,
  137. -- text = "首页",
  138. -- on_click = function()
  139. -- if cleanup_func then
  140. -- cleanup_func()
  141. -- end
  142. -- go_home()
  143. -- end
  144. -- })
  145. local home_img = airui.image({
  146. parent = parent,
  147. x = 65,
  148. y = 4,
  149. w = 20,
  150. h = 25,
  151. src = "/luadb/home2.png",
  152. zoom = 140,
  153. opacity = 255,
  154. on_click = function()
  155. if cleanup_func then
  156. cleanup_func()
  157. end
  158. go_home()
  159. end
  160. })
  161. local home_label = airui.label({
  162. parent = parent,
  163. text = "首页",
  164. x = 87,
  165. y = 9,
  166. w = 45,
  167. h = 20,
  168. font_size = 15,
  169. color = 0xFFFFFF,
  170. on_click = function()
  171. if cleanup_func then
  172. cleanup_func()
  173. end
  174. go_home()
  175. end
  176. })
  177. return back_img
  178. end
  179. function common_ui.add_background_png(parent)
  180. local background_img = airui.image({
  181. parent = parent,
  182. x = 0,
  183. y = 0,
  184. w = 480,
  185. h = 270,
  186. src = "/luadb/1.png",
  187. zoom = 256,
  188. opacity = 255
  189. })
  190. return background_img
  191. end
  192. -- 创建底部信息栏
  193. -- @param main_container 主容器对象
  194. -- @return status_bar 底部信息栏容器对象
  195. function common_ui.create_status_bar(parent)
  196. -- 底部信息
  197. local status_bar = airui.container({
  198. parent = parent,
  199. x = 0,
  200. y = 300,
  201. w = 480,
  202. h = 20,
  203. color = 0xDFEAFF,
  204. })
  205. local version_charge_msg = airui.label({
  206. parent = status_bar,
  207. text = "TSB-3.0 v1.0.0",
  208. x = 0,
  209. y = 0,
  210. w = 180,
  211. h = 20,
  212. font_size = 14,
  213. })
  214. airui.label({
  215. parent = status_bar,
  216. text = "SN:",
  217. x = 350,
  218. y = 0,
  219. w = 40,
  220. h = 18,
  221. font_size = 14,
  222. })
  223. local sn_label = airui.label({
  224. parent = status_bar,
  225. text = "55",
  226. x = 380,
  227. y = 0,
  228. w = 40,
  229. h = 18,
  230. font_size = 14,
  231. })
  232. local shutdown_image = airui.image({
  233. parent = status_bar,
  234. x = 422,
  235. y = 0,
  236. w = 30,
  237. h = 20,
  238. src = "/luadb/shutdown.png",
  239. zoom = 200,
  240. opacity = 255,
  241. on_click = function(self)
  242. common_ui.show_shutdown_confirm(status_bar)
  243. end
  244. })
  245. local shutdwon_label = airui.label({
  246. parent = status_bar,
  247. text = "关机",
  248. x = 445,
  249. y = 2,
  250. w = 30,
  251. h = 18,
  252. font_size = 14,
  253. on_click = function(self)
  254. common_ui.show_shutdown_confirm(status_bar)
  255. end
  256. })
  257. local dynamic_label = airui.label({
  258. parent = status_bar,
  259. text = os.date("%Y-%m-%d %H:%M:%S"),
  260. x = 170,
  261. y = 0,
  262. w = 180,
  263. h = 20,
  264. font_size = 14,
  265. })
  266. -- 先停止已存在的定时器,避免多个定时器同时运行
  267. if time1_id then
  268. sys.timerStop(time1_id)
  269. time1_id = nil
  270. end
  271. -- 启动新的定时器
  272. time1_id = sys.timerLoopStart(function()
  273. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  274. -- 检查dynamic_label是否仍然存在
  275. if dynamic_label then
  276. dynamic_label:set_text(current_time)
  277. else
  278. -- 如果标签不存在,停止定时器
  279. if time1_id then
  280. sys.timerStop(time1_id)
  281. time1_id = nil
  282. end
  283. end
  284. -- 每两次定时切换一次文本(即1800ms)
  285. if version_charge_msg then
  286. switch_counter = switch_counter + 1
  287. if switch_counter >= 2 then
  288. show_charge_info = not show_charge_info -- 切换状态
  289. if show_charge_info then
  290. version_charge_msg:set_text("充电中。。充电功率:7.5W")
  291. else
  292. version_charge_msg:set_text("TSB-3.0 v1.0.0")
  293. end
  294. switch_counter = 0 -- 重置计数器
  295. end
  296. end
  297. end, 900) -- 每900ms更新一次时间
  298. return status_bar
  299. end
  300. -- 显示关机确认弹窗
  301. -- @param parent 父容器对象
  302. function common_ui.show_shutdown_confirm(parent)
  303. -- 检查是否已经存在关机弹窗
  304. if shutdown_msgbox then
  305. log.info("shutdown", "关机弹窗已存在,不重复创建")
  306. return
  307. end
  308. shutdown_msgbox = airui.msgbox({
  309. title = "系统提示",
  310. text = "是否确认关机?",
  311. buttons = { "否", "是" },
  312. on_action = function(self, label)
  313. if label == "是" then
  314. -- 执行关机操作
  315. log.info("shutdown", "用户确认关机")
  316. i2c.setup(bq25895_iic_id,i2c.SLOW)
  317. local reg9_data = i2c.readReg(bq25895_iic_id, bq25895_addr, bq25895_reg9, 1)
  318. local reg9_temp = (reg9_data == "") and 0x00 or string.byte(reg9_data)
  319. local reg9_data_result = reg9_temp | 0x20
  320. local reg9_result = i2c.writeReg(bq25895_iic_id, bq25895_addr, bq25895_reg9, string.char(reg9_data_result))
  321. pm.shutdown()
  322. else
  323. -- 取消关机操作
  324. log.info("shutdown", "用户取消关机")
  325. end
  326. self:hide()
  327. -- 重置弹窗状态
  328. shutdown_msgbox = nil
  329. end
  330. })
  331. shutdown_msgbox:show()
  332. end
  333. -- 清理函数,用于停止定时器
  334. function common_ui.cleanup()
  335. if time1_id then
  336. sys.timerStop(time1_id)
  337. time1_id = nil
  338. end
  339. end
  340. return common_ui