ui.lua 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. --- 模块功能:菜单UI
  2. -- @module ui
  3. -- @author 稀饭放姜
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2017.10.12 10:00
  7. module(..., package.seeall)
  8. require "pins"
  9. -- 菜单按键IO列表
  10. local escKey, leftKey, rightKey, enterKey
  11. --LCD分辨率的宽度和高度(单位是像素)
  12. local WIDTH, HEIGHT, BPP = disp.getlcdinfo() or 128
  13. --1个ASCII字符宽度为8像素,高度为16像素;汉字宽度和高度都为16像素
  14. local CHAR_WIDTH = 8
  15. --[[
  16. 函数名:getxpos
  17. 功能 :计算字符串居中显示的X坐标
  18. 参数 :
  19. str:string类型,要显示的字符串
  20. 返回值:X坐标
  21. ]]
  22. local function getxpos(str)
  23. return (WIDTH - string.len(str) * CHAR_WIDTH) / 2
  24. end
  25. --- UI初始化方法
  26. -- @param esc, 返回按键PIO
  27. -- @param left, 移动按键PIO
  28. -- @param right,移动按键PIO
  29. -- @param ent, 确定按键PIO
  30. -- @return nothing
  31. -- @usage ui.init(pio.P0_8,pio.P0_10,pio.P0_11,pio.P0_12)
  32. function init(esc, left, right, ent)
  33. escKey = esc or pio.P0_8
  34. leftKey = left or pio.P0_10
  35. rightKey = right or pio.P0_11
  36. enterKey = ent or pio.P0_12
  37. end
  38. --- 创建UI菜单列表,支持两种风格--图标列表和标题列表
  39. -- @param t, 用户自定义的菜单标题名称或图标的文件名的table
  40. -- @param style,显示风格--false为图标风格,true为标题风格
  41. -- @param ... ,可变参数,用于复盖类的按键动作方法
  42. -- @return table,返回包含标题、子菜单、父菜单、按键动作的table
  43. -- @usage ui.newList(menuBar)
  44. -- @usage ui.newList(menuItem,true)
  45. function newList(t, style, ...)
  46. -- 根菜单条表
  47. local self = {titles = t, parent = {}, list = {}}
  48. -- 附加菜单列表到根菜单条
  49. self.append = function(title, list)
  50. self.list[title] = list
  51. list.parent = self
  52. end
  53. -- 显示菜单
  54. self.display = function()
  55. disp.clear()
  56. if style then
  57. disp.puttext(self.titles[#self.titles], getxpos(self.titles[#self.titles]), 2)
  58. disp.setcolor(0x0000)
  59. disp.drawrect(0, 21, 128, 43, 0xffff)
  60. disp.puttext(self.titles[1], getxpos(self.titles[1]), 24)
  61. disp.setcolor(0xffff)
  62. disp.puttext(self.titles[2], getxpos(self.titles[2]), 46)
  63. else
  64. disp.putimage("/ldata/" .. self.titles[#self.titles] .. "_small.bmp", 0, 12)
  65. disp.putimage("/ldata/" .. self.titles[1] .. ".bmp", 32, 0, -1)
  66. disp.putimage("/ldata/" .. self.titles[2] .. "_small.bmp", 96, 12)
  67. disp.puttext("..", 10, 40)
  68. disp.puttext("..", 107, 40)
  69. end
  70. disp.update()
  71. pins.setup(escKey, self.escFun)
  72. pins.setup(leftKey, self.leftFun)
  73. pins.setup(rightKey, self.rightFun)
  74. pins.setup(enterKey, self.enterFun)
  75. end
  76. self.escFun = arg[1] or function(intid)
  77. if intid == cpu.INT_GPIO_NEGEDGE then return end
  78. if self.parent.escFun ~= nil then self.parent.display() end
  79. end
  80. self.leftFun = arg[2] or function(intid)
  81. if intid == cpu.INT_GPIO_NEGEDGE then return end
  82. table.insert(self.titles, 1, table.remove(self.titles))
  83. self.display() end
  84. self.rightFun = arg[3] or function(intid)
  85. if intid == cpu.INT_GPIO_NEGEDGE then return end
  86. table.insert(self.titles, table.remove(self.titles, 1))
  87. self.display() end
  88. self.enterFun = arg[4] or function(intid)
  89. if intid == cpu.INT_GPIO_NEGEDGE then return end
  90. if self.list[self.titles[1]] then self.list[self.titles[1]].display() end
  91. end
  92. return self
  93. end
  94. -- 默认按键需要打开电压域
  95. pmd.ldoset(6, pmd.LDO_VIB)