GraphicsTest.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. -- setup SPI and connect display
  2. function init_spi_display()
  3. -- Hardware SPI CLK = GPIO14
  4. -- Hardware SPI MOSI = GPIO13
  5. -- Hardware SPI MISO = GPIO12 (not used)
  6. -- Hardware SPI /CS = GPIO15 (not used)
  7. -- CS, D/C, and RES can be assigned freely to available GPIOs
  8. local cs = 8 -- GPIO15, pull-down 10k to GND
  9. local dc = 4 -- GPIO2
  10. local res = 0 -- GPIO16
  11. spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
  12. -- we won't be using the HSPI /CS line, so disable it again
  13. gpio.mode(8, gpio.INPUT, gpio.PULLUP)
  14. -- initialize the matching driver for your display
  15. -- see app/include/ucg_config.h
  16. --disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
  17. disp = ucg.st7735_18x128x160_hw_spi(cs, dc, res)
  18. end
  19. -- switch statement http://lua-users.org/wiki/SwitchStatement
  20. function switch(c)
  21. local swtbl = {
  22. casevar = c,
  23. caseof = function (self, code)
  24. local f
  25. if (self.casevar) then
  26. f = code[self.casevar] or code.default
  27. else
  28. f = code.missing or code.default
  29. end
  30. if f then
  31. if type(f)=="function" then
  32. return f(self.casevar,self)
  33. else
  34. error("case "..tostring(self.casevar).." not a function")
  35. end
  36. end
  37. end
  38. }
  39. return swtbl
  40. end
  41. z = 127 -- start value
  42. function lcg_rnd()
  43. z = bit.band(65 * z + 17, 255)
  44. return z
  45. end
  46. function millis()
  47. local usec = tmr.now()
  48. return usec/1000
  49. end
  50. function set_clip_range()
  51. local x, y, w, h
  52. w = bit.band(lcg_rnd(), 31)
  53. h = bit.band(lcg_rnd(), 31)
  54. w = w + 25
  55. h = h + 25
  56. x = bit.rshift(lcg_rnd() * (disp:getWidth() - w), 8)
  57. y = bit.rshift(lcg_rnd() * (disp:getHeight() - h), 8)
  58. disp:setClipRange(x, y, w, h)
  59. end
  60. function loop()
  61. if (loop_idx == 0) then
  62. switch(bit.band(r, 3)) : caseof {
  63. [0] = function() disp:undoRotate() end,
  64. [1] = function() disp:setRotate90() end,
  65. [2] = function() disp:setRotate180() end,
  66. default = function() disp:setRotate270() end
  67. }
  68. if ( r > 3 ) then
  69. disp:clearScreen()
  70. set_clip_range()
  71. else
  72. disp:undoClipRange()
  73. end
  74. r = bit.band(r + 1, 7)
  75. end
  76. switch(loop_idx) : caseof {
  77. [0] = function() end,
  78. [1] = function() require("GT_graphics_test").run() end,
  79. [2] = function() require("GT_cross").run() end,
  80. [3] = function() require("GT_pixel_and_lines").run() end,
  81. [4] = function() require("GT_color_test").run() end,
  82. [5] = function() require("GT_triangle").run() end,
  83. [6] = function() require("GT_fonts").run() end,
  84. [7] = function() require("GT_text").run() end,
  85. [8] = function() if r <= 3 then require("GT_clip").run() end end,
  86. [9] = function() require("GT_box").run() end,
  87. [10] = function() require("GT_gradient").run() end,
  88. [11] = function() disp:setMaxClipRange() end,
  89. default = function() loop_idx = -1 end
  90. }
  91. loop_idx = loop_idx + 1
  92. print("Heap: " .. node.heap())
  93. end
  94. T = 1000
  95. r = 0
  96. loop_idx = 0
  97. init_spi_display()
  98. disp:begin(ucg.FONT_MODE_TRANSPARENT)
  99. disp:setFont(ucg.font_ncenR14_hr)
  100. disp:clearScreen()
  101. tmr.register(0, 3000, tmr.ALARM_AUTO, function() loop() end)
  102. tmr.start(0)