u8g_graphics_test.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. -- ***************************************************************************
  2. -- Graphics Test
  3. --
  4. -- This script executes several features of u8glib to test their Lua bindings.
  5. --
  6. -- Note: It is prepared for SSD1306-based displays. Select your connectivity
  7. -- type by calling either init_i2c_display() or init_spi_display() at
  8. -- the bottom of this file.
  9. --
  10. -- ***************************************************************************
  11. -- setup I2c and connect display
  12. function init_i2c_display()
  13. -- SDA and SCL can be assigned freely to available GPIOs
  14. local sda = 5 -- GPIO14
  15. local scl = 6 -- GPIO12
  16. local sla = 0x3c
  17. i2c.setup(0, sda, scl, i2c.SLOW)
  18. disp = u8g.ssd1306_128x64_i2c(sla)
  19. end
  20. -- setup SPI and connect display
  21. function init_spi_display()
  22. -- Hardware SPI CLK = GPIO14
  23. -- Hardware SPI MOSI = GPIO13
  24. -- Hardware SPI MISO = GPIO12 (not used)
  25. -- Hardware SPI /CS = GPIO15 (not used)
  26. -- CS, D/C, and RES can be assigned freely to available GPIOs
  27. local cs = 8 -- GPIO15, pull-down 10k to GND
  28. local dc = 4 -- GPIO2
  29. local res = 0 -- GPIO16
  30. spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
  31. -- we won't be using the HSPI /CS line, so disable it again
  32. gpio.mode(8, gpio.INPUT, gpio.PULLUP)
  33. disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
  34. end
  35. -- graphic test components
  36. function prepare()
  37. disp:setFont(u8g.font_6x10)
  38. disp:setFontRefHeightExtendedText()
  39. disp:setDefaultForegroundColor()
  40. disp:setFontPosTop()
  41. end
  42. function box_frame(a)
  43. disp:drawStr(0, 0, "drawBox")
  44. disp:drawBox(5, 10, 20, 10)
  45. disp:drawBox(10+a, 15, 30, 7)
  46. disp:drawStr(0, 30, "drawFrame")
  47. disp:drawFrame(5, 10+30, 20, 10)
  48. disp:drawFrame(10+a, 15+30, 30, 7)
  49. end
  50. function disc_circle(a)
  51. disp:drawStr(0, 0, "drawDisc")
  52. disp:drawDisc(10, 18, 9)
  53. disp:drawDisc(24+a, 16, 7)
  54. disp:drawStr(0, 30, "drawCircle")
  55. disp:drawCircle(10, 18+30, 9)
  56. disp:drawCircle(24+a, 16+30, 7)
  57. end
  58. function r_frame(a)
  59. disp:drawStr(0, 0, "drawRFrame/Box")
  60. disp:drawRFrame(5, 10, 40, 30, a+1)
  61. disp:drawRBox(50, 10, 25, 40, a+1)
  62. end
  63. function stringtest(a)
  64. disp:drawStr(30+a, 31, " 0")
  65. disp:drawStr90(30, 31+a, " 90")
  66. disp:drawStr180(30-a, 31, " 180")
  67. disp:drawStr270(30, 31-a, " 270")
  68. end
  69. function line(a)
  70. disp:drawStr(0, 0, "drawLine")
  71. disp:drawLine(7+a, 10, 40, 55)
  72. disp:drawLine(7+a*2, 10, 60, 55)
  73. disp:drawLine(7+a*3, 10, 80, 55)
  74. disp:drawLine(7+a*4, 10, 100, 55)
  75. end
  76. function triangle(a)
  77. local offset = a
  78. disp:drawStr(0, 0, "drawTriangle")
  79. disp:drawTriangle(14,7, 45,30, 10,40)
  80. disp:drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset)
  81. disp:drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53)
  82. disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset)
  83. end
  84. function ascii_1()
  85. local x, y, s
  86. disp:drawStr(0, 0, "ASCII page 1")
  87. for y = 0, 5, 1 do
  88. for x = 0, 15, 1 do
  89. s = y*16 + x + 32
  90. disp:drawStr(x*7, y*10+10, string.char(s))
  91. end
  92. end
  93. end
  94. function extra_page(a)
  95. disp:drawStr(0, 12, "setScale2x2")
  96. disp:setScale2x2()
  97. disp:drawStr(0, 6+a, "setScale2x2")
  98. disp:undoScale()
  99. end
  100. -- the draw() routine
  101. function draw(draw_state)
  102. local component = bit.rshift(draw_state, 3)
  103. prepare()
  104. if (component == 0) then
  105. box_frame(bit.band(draw_state, 7))
  106. elseif (component == 1) then
  107. disc_circle(bit.band(draw_state, 7))
  108. elseif (component == 2) then
  109. r_frame(bit.band(draw_state, 7))
  110. elseif (component == 3) then
  111. stringtest(bit.band(draw_state, 7))
  112. elseif (component == 4) then
  113. line(bit.band(draw_state, 7))
  114. elseif (component == 5) then
  115. triangle(bit.band(draw_state, 7))
  116. elseif (component == 6) then
  117. ascii_1()
  118. elseif (component == 7) then
  119. extra_page(bit.band(draw_state, 7))
  120. end
  121. end
  122. function draw_loop()
  123. -- Draws one page and schedules the next page, if there is one
  124. local function draw_pages()
  125. draw(draw_state)
  126. if disp:nextPage() then
  127. node.task.post(draw_pages)
  128. else
  129. node.task.post(graphics_test)
  130. end
  131. end
  132. -- Restart the draw loop and start drawing pages
  133. disp:firstPage()
  134. node.task.post(draw_pages)
  135. end
  136. function graphics_test()
  137. if (draw_state <= 7 + 8*8) then
  138. draw_state = draw_state + 1
  139. else
  140. print("--- Restarting Graphics Test ---")
  141. draw_state = 0
  142. end
  143. print("Heap: " .. node.heap())
  144. -- retrigger draw_loop
  145. node.task.post(draw_loop)
  146. end
  147. draw_state = 0
  148. init_i2c_display()
  149. --init_spi_display()
  150. print("--- Starting Graphics Test ---")
  151. node.task.post(draw_loop)