GraphicsTest.lua 3.5 KB

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