component_page.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. --[[
  2. @module component_page
  3. @summary U8G2组件演示页面模块 - 128x64屏幕
  4. @version 1.0
  5. @date 2025.12.11
  6. @author 江访
  7. @usage
  8. 本文件为组件演示页面模块,核心业务逻辑为:
  9. 1、展示U8G2图形组件的绘制能力;
  10. 2、显示进度条和基本图形(圆形、矩形、三角形等);
  11. 3、提供进度调整功能和返回首页功能;
  12. 4、支持BOOT键和PWR键的导航操作;
  13. 本文件的对外接口有4个:
  14. 1、component_page.draw():绘制页面内容;
  15. 2、component_page.handle_key(key_type):处理按键事件;
  16. 3、component_page.on_enter():页面进入回调;
  17. 4、component_page.on_leave():页面离开回调;
  18. ]]
  19. local component_page = {}
  20. -- 进度条当前值(0-100)
  21. local progress_value = 30
  22. -- 当前选中按钮的索引(1:返回, 2:+10%)
  23. local selected_index = 1
  24. --[[
  25. @api draw()
  26. @summary 绘制组件演示页面内容
  27. @return 无返回值
  28. @usage
  29. -- 在UI主循环中调用
  30. component_page.draw()
  31. ]]
  32. function component_page.draw()
  33. -- 标题
  34. u8g2.SetFont(u8g2.font_6x10)
  35. u8g2.DrawUTF8("组件页", 35, 10)
  36. -- 进度条区域
  37. u8g2.DrawUTF8("进度条:", 5, 25)
  38. -- 进度条背景(更大)
  39. u8g2.DrawFrame(42, 15, 50, 12)
  40. -- 进度条前景
  41. local fill_width = math.floor(50 * progress_value / 100)
  42. u8g2.DrawBox(41, 15, fill_width, 12)
  43. -- 进度文本
  44. u8g2.DrawUTF8(progress_value .. "%", 95,25)
  45. -- 图形演示区域
  46. u8g2.DrawUTF8("图形:", 5, 40)
  47. -- 绘制基本图形(增加间距)
  48. u8g2.DrawCircle(40, 36, 5, u8g2.DRAW_ALL)
  49. u8g2.DrawDisc(60, 36, 5, u8g2.DRAW_ALL)
  50. u8g2.DrawFrame(75, 31, 10, 10)
  51. u8g2.DrawBox(90, 31, 10, 10)
  52. u8g2.DrawTriangle(105, 40, 110, 30, 115, 40)
  53. -- 按钮区域(布局更宽松)
  54. if selected_index == 1 then
  55. -- 返回按钮:选中状态
  56. u8g2.DrawButtonUTF8("返回", 10, 58, u8g2.BTN_INV + u8g2.BTN_BW1, 0, 2, 0)
  57. else
  58. -- 返回按钮:未选中状态
  59. u8g2.DrawButtonUTF8("返回", 10, 58, u8g2.BTN_BW1, 0, 2, 0)
  60. end
  61. if selected_index == 2 then
  62. -- +10%按钮:选中状态
  63. u8g2.DrawButtonUTF8("+10%", 70, 58, u8g2.BTN_INV + u8g2.BTN_BW1, 0, 2, 0)
  64. else
  65. -- +10%按钮:未选中状态
  66. u8g2.DrawButtonUTF8("+10%", 70, 58, u8g2.BTN_BW1, 0, 2, 0)
  67. end
  68. end
  69. --[[
  70. @api handle_key(key_type)
  71. @summary 处理按键事件,实现进度调整和页面导航
  72. @param string key_type 按键类型,可选值:
  73. - "confirm":确认键,执行当前选中按钮的功能
  74. - "next":切换到下一个按钮
  75. - "prev":切换到上一个按钮
  76. @return bool 是否已处理该按键,true表示已处理
  77. @usage
  78. -- 在UI主循环中调用
  79. local handled = component_page.handle_key("confirm")
  80. ]]
  81. function component_page.handle_key(key_type)
  82. log.info("component_page.handle_key", "key_type:", key_type)
  83. if key_type == "confirm" then
  84. -- 确认键:执行当前选中按钮的功能
  85. if selected_index == 1 then
  86. -- 返回按钮:返回首页
  87. switch_page("home")
  88. elseif selected_index == 2 then
  89. -- +10%按钮:增加进度值,最大不超过100%
  90. progress_value = math.min(100, progress_value + 10)
  91. end
  92. return true
  93. elseif key_type == "next" then
  94. -- 切换到下一个按钮
  95. selected_index = selected_index % 2 + 1
  96. return true
  97. elseif key_type == "prev" then
  98. -- 切换到上一个按钮
  99. selected_index = (selected_index - 2) % 2 + 1
  100. return true
  101. end
  102. return false
  103. end
  104. --[[
  105. @api on_enter()
  106. @summary 页面进入时的初始化操作,重置选中项和进度值
  107. @return 无返回值
  108. @usage
  109. -- 在页面切换时自动调用
  110. component_page.on_enter()
  111. ]]
  112. function component_page.on_enter()
  113. -- 页面进入时初始化
  114. selected_index = 1 -- 默认选中返回按钮
  115. end
  116. --[[
  117. @api on_leave()
  118. @summary 页面离开时的清理操作
  119. @return 无返回值
  120. @usage
  121. -- 在页面切换时自动调用
  122. component_page.on_leave()
  123. ]]
  124. function component_page.on_leave()
  125. -- 页面离开时的清理操作
  126. -- 当前无需特殊清理
  127. end
  128. return component_page