graphics_test.lua 7.0 KB

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