graphics_test.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. -- display object
  12. local disp
  13. local draw_state, loop_tmr = 0, tmr.create()
  14. -- setup I2c and connect display
  15. local function init_i2c_display()
  16. -- SDA and SCL can be assigned freely to available GPIOs
  17. local sda = 5 -- GPIO14
  18. local scl = 6 -- GPIO12
  19. local sla = 0x3c
  20. i2c.setup(0, sda, scl, i2c.SLOW)
  21. disp = u8g2.ssd1306_i2c_128x64_noname(0, sla)
  22. end
  23. -- setup SPI and connect display
  24. local function init_spi_display() -- luacheck: no unused
  25. -- Hardware SPI CLK = GPIO14
  26. -- Hardware SPI MOSI = GPIO13
  27. -- Hardware SPI MISO = GPIO12 (not used)
  28. -- Hardware SPI /CS = GPIO15 (not used)
  29. -- CS, D/C, and RES can be assigned freely to available GPIOs
  30. local cs = 8 -- GPIO15, pull-down 10k to GND
  31. local dc = 4 -- GPIO2
  32. local res = 0 -- GPIO16
  33. spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
  34. -- we won't be using the HSPI /CS line, so disable it again
  35. gpio.mode(8, gpio.INPUT, gpio.PULLUP)
  36. disp = u8g2.ssd1306_128x64_noname(1, cs, dc, res)
  37. end
  38. local function u8g2_prepare()
  39. disp:setFont(u8g2.font_6x10_tf)
  40. disp:setFontRefHeightExtendedText()
  41. disp:setDrawColor(1)
  42. disp:setFontPosTop()
  43. disp:setFontDirection(0)
  44. end
  45. local function u8g2_box_frame(a)
  46. disp:drawStr( 0, 0, "drawBox")
  47. disp:drawBox(5,10,20,10)
  48. disp:drawBox(10+a,15,30,7)
  49. disp:drawStr( 0, 30, "drawFrame")
  50. disp:drawFrame(5,10+30,20,10)
  51. disp:drawFrame(10+a,15+30,30,7)
  52. end
  53. local function u8g2_disc_circle(a)
  54. disp:drawStr( 0, 0, "drawDisc")
  55. disp:drawDisc(10,18,9)
  56. disp:drawDisc(24+a,16,7)
  57. disp:drawStr( 0, 30, "drawCircle")
  58. disp:drawCircle(10,18+30,9)
  59. disp:drawCircle(24+a,16+30,7)
  60. end
  61. local function u8g2_r_frame(a)
  62. disp:drawStr( 0, 0, "drawRFrame/Box")
  63. disp:drawRFrame(5, 10,40,30, a+1)
  64. disp:drawRBox(50, 10,25,40, a+1)
  65. end
  66. local function u8g2_string(a)
  67. disp:setFontDirection(0)
  68. disp:drawStr(30+a,31, " 0")
  69. disp:setFontDirection(1)
  70. disp:drawStr(30,31+a, " 90")
  71. disp:setFontDirection(2)
  72. disp:drawStr(30-a,31, " 180")
  73. disp:setFontDirection(3)
  74. disp:drawStr(30,31-a, " 270")
  75. end
  76. local function u8g2_line(a)
  77. disp:drawStr( 0, 0, "drawLine")
  78. disp:drawLine(7+a, 10, 40, 55)
  79. disp:drawLine(7+a*2, 10, 60, 55)
  80. disp:drawLine(7+a*3, 10, 80, 55)
  81. disp:drawLine(7+a*4, 10, 100, 55)
  82. end
  83. local function u8g2_triangle(a)
  84. local offset = a
  85. disp:drawStr( 0, 0, "drawTriangle")
  86. disp:drawTriangle(14,7, 45,30, 10,40)
  87. disp:drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset)
  88. disp:drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53)
  89. disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset)
  90. end
  91. local function u8g2_ascii_1()
  92. disp:drawStr( 0, 0, "ASCII page 1")
  93. for y = 0, 5 do
  94. for x = 0, 15 do
  95. disp:drawStr(x*7, y*10+10, string.char(y*16 + x + 32))
  96. end
  97. end
  98. end
  99. local function u8g2_ascii_2()
  100. disp:drawStr( 0, 0, "ASCII page 2")
  101. for y = 0, 5 do
  102. for x = 0, 15 do
  103. disp:drawStr(x*7, y*10+10, string.char(y*16 + x + 160))
  104. end
  105. end
  106. end
  107. local function u8g2_extra_page(a)
  108. disp:drawStr( 0, 0, "Unicode")
  109. disp:setFont(u8g2.font_unifont_t_symbols)
  110. disp:setFontPosTop()
  111. disp:drawUTF8(0, 24, "☀ ☁")
  112. if a <= 3 then
  113. disp:drawUTF8(a*3, 36, "☂")
  114. else
  115. disp:drawUTF8(a*3, 36, "☔")
  116. end
  117. end
  118. local cross_width = 24
  119. local cross_height = 24
  120. local cross_bits = string.char(
  121. 0x00, 0x18, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x42, 0x00,
  122. 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00,
  123. 0xC0, 0x00, 0x03, 0x38, 0x3C, 0x1C, 0x06, 0x42, 0x60, 0x01, 0x42, 0x80,
  124. 0x01, 0x42, 0x80, 0x06, 0x42, 0x60, 0x38, 0x3C, 0x1C, 0xC0, 0x00, 0x03,
  125. 0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00,
  126. 0x00, 0x42, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x18, 0x00)
  127. -- luacheck: push no unused
  128. local cross_fill_width = 24
  129. local cross_fill_height = 24
  130. local cross_fill_bits = string.char(
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x64, 0x00, 0x26,
  132. 0x84, 0x00, 0x21, 0x08, 0x81, 0x10, 0x08, 0x42, 0x10, 0x10, 0x3C, 0x08,
  133. 0x20, 0x00, 0x04, 0x40, 0x00, 0x02, 0x80, 0x00, 0x01, 0x80, 0x18, 0x01,
  134. 0x80, 0x18, 0x01, 0x80, 0x00, 0x01, 0x40, 0x00, 0x02, 0x20, 0x00, 0x04,
  135. 0x10, 0x3C, 0x08, 0x08, 0x42, 0x10, 0x08, 0x81, 0x10, 0x84, 0x00, 0x21,
  136. 0x64, 0x00, 0x26, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
  137. -- luacheck: pop
  138. local cross_block_width = 14
  139. local cross_block_height = 14
  140. local cross_block_bits = string.char(
  141. 0xFF, 0x3F, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
  142. 0xC1, 0x20, 0xC1, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20,
  143. 0x01, 0x20, 0xFF, 0x3F)
  144. local function u8g2_bitmap_overlay(a)
  145. local frame_size = 28
  146. disp:drawStr(0, 0, "Bitmap overlay")
  147. disp:drawStr(0, frame_size + 12, "Solid / transparent")
  148. disp:setBitmapMode(0) -- solid
  149. disp:drawFrame(0, 10, frame_size, frame_size)
  150. disp:drawXBM(2, 12, cross_width, cross_height, cross_bits)
  151. if bit.band(a, 4) > 0 then
  152. disp:drawXBM(7, 17, cross_block_width, cross_block_height, cross_block_bits)
  153. end
  154. disp:setBitmapMode(1) -- transparent
  155. disp:drawFrame(frame_size + 5, 10, frame_size, frame_size)
  156. disp:drawXBM(frame_size + 7, 12, cross_width, cross_height, cross_bits)
  157. if bit.band(a, 4) then
  158. disp:drawXBM(frame_size + 12, 17, cross_block_width, cross_block_height, cross_block_bits)
  159. end
  160. end
  161. local function u8g2_bitmap_modes(transparent)
  162. local frame_size = 24
  163. disp:drawBox(0, frame_size * 0.5, frame_size * 5, frame_size)
  164. disp:drawStr(frame_size * 0.5, 50, "Black")
  165. disp:drawStr(frame_size * 2, 50, "White")
  166. disp:drawStr(frame_size * 3.5, 50, "XOR")
  167. if transparent == 0 then
  168. disp:setBitmapMode(0) -- solid
  169. disp:drawStr(0, 0, "Solid bitmap")
  170. else
  171. disp:setBitmapMode(1) -- transparent
  172. disp:drawStr(0, 0, "Transparent bitmap")
  173. end
  174. disp:setDrawColor(0) -- Black
  175. disp:drawXBM(frame_size * 0.5, 24, cross_width, cross_height, cross_bits)
  176. disp:setDrawColor(1) -- White
  177. disp:drawXBM(frame_size * 2, 24, cross_width, cross_height, cross_bits)
  178. disp:setDrawColor(2) -- XOR
  179. disp:drawXBM(frame_size * 3.5, 24, cross_width, cross_height, cross_bits)
  180. end
  181. local function draw()
  182. u8g2_prepare()
  183. local d3 = bit.rshift(draw_state, 3)
  184. local d7 = bit.band(draw_state, 7)
  185. if d3 == 0 then
  186. u8g2_box_frame(d7)
  187. elseif d3 == 1 then
  188. u8g2_disc_circle(d7)
  189. elseif d3 == 2 then
  190. u8g2_r_frame(d7)
  191. elseif d3 == 3 then
  192. u8g2_string(d7)
  193. elseif d3 == 4 then
  194. u8g2_line(d7)
  195. elseif d3 == 5 then
  196. u8g2_triangle(d7)
  197. elseif d3 == 6 then
  198. u8g2_ascii_1()
  199. elseif d3 == 7 then
  200. u8g2_ascii_2()
  201. elseif d3 == 8 then
  202. u8g2_extra_page(d7)
  203. elseif d3 == 9 then
  204. u8g2_bitmap_modes(0)
  205. elseif d3 == 10 then
  206. u8g2_bitmap_modes(1)
  207. elseif d3 == 11 then
  208. u8g2_bitmap_overlay(d7)
  209. end
  210. end
  211. local function loop()
  212. -- picture loop
  213. disp:clearBuffer()
  214. draw()
  215. disp:sendBuffer()
  216. -- increase the state
  217. draw_state = draw_state + 1
  218. if draw_state >= 12*8 then
  219. draw_state = 0
  220. end
  221. -- delay between each frame
  222. loop_tmr:start()
  223. end
  224. do
  225. loop_tmr:register(100, tmr.ALARM_SEMI, loop)
  226. init_i2c_display()
  227. --init_spi_display()
  228. print("--- Starting Graphics Test ---")
  229. loop_tmr:start()
  230. end