u8g_bitmaps.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. -- ***************************************************************************
  2. -- Bitmaps Test
  3. --
  4. -- This script executes the bitmap features of u8glib to test their Lua
  5. -- integration.
  6. --
  7. -- Note: It is prepared for SSD1306-based displays. Select your connectivity
  8. -- type by calling either init_i2c_display() or init_spi_display() at
  9. -- the bottom of this file.
  10. --
  11. -- ***************************************************************************
  12. -- setup I2c and connect display
  13. function init_i2c_display()
  14. -- SDA and SCL can be assigned freely to available GPIOs
  15. local sda = 5 -- GPIO14
  16. local scl = 6 -- GPIO12
  17. local sla = 0x3c
  18. i2c.setup(0, sda, scl, i2c.SLOW)
  19. disp = u8g.ssd1306_128x64_i2c(sla)
  20. end
  21. -- setup SPI and connect display
  22. function init_spi_display()
  23. -- Hardware SPI CLK = GPIO14
  24. -- Hardware SPI MOSI = GPIO13
  25. -- Hardware SPI MISO = GPIO12 (not used)
  26. -- Hardware SPI /CS = GPIO15 (not used)
  27. -- CS, D/C, and RES can be assigned freely to available GPIOs
  28. local cs = 8 -- GPIO15, pull-down 10k to GND
  29. local dc = 4 -- GPIO2
  30. local res = 0 -- GPIO16
  31. spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
  32. -- we won't be using the HSPI /CS line, so disable it again
  33. gpio.mode(8, gpio.INPUT, gpio.PULLUP)
  34. disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
  35. end
  36. function xbm_picture()
  37. disp:setFont(u8g.font_6x10)
  38. disp:drawStr( 0, 10, "XBM picture")
  39. disp:drawXBM( 0, 20, 38, 24, xbm_data )
  40. end
  41. function bitmap_picture(state)
  42. disp:setFont(u8g.font_6x10)
  43. disp:drawStr( 0, 10, "Bitmap picture")
  44. disp:drawBitmap( 0 + (state * 10), 20 + (state * 4), 1, 8, bm_data )
  45. end
  46. -- the draw() routine
  47. function draw(draw_state)
  48. local component = bit.rshift(draw_state, 3)
  49. if (component == 0) then
  50. xbm_picture(bit.band(draw_state, 7))
  51. elseif (component == 1) then
  52. bitmap_picture(bit.band(draw_state, 7))
  53. end
  54. end
  55. function draw_loop()
  56. -- Draws one page and schedules the next page, if there is one
  57. local function draw_pages()
  58. draw(draw_state)
  59. if disp:nextPage() then
  60. node.task.post(draw_pages)
  61. else
  62. node.task.post(bitmap_test)
  63. end
  64. end
  65. -- Restart the draw loop and start drawing pages
  66. disp:firstPage()
  67. node.task.post(draw_pages)
  68. end
  69. function bitmap_test()
  70. if (draw_state <= 7 + 1*8) then
  71. draw_state = draw_state + 1
  72. else
  73. print("--- Restarting Bitmap Test ---")
  74. draw_state = 1
  75. end
  76. print("Heap: " .. node.heap())
  77. -- retrigger draw_loop
  78. node.task.post(draw_loop)
  79. end
  80. draw_state = 1
  81. init_i2c_display()
  82. --init_spi_display()
  83. -- read XBM picture
  84. file.open("u8glib_logo.xbm", "r")
  85. xbm_data = file.read()
  86. file.close()
  87. -- read Bitmap picture
  88. file.open("u8g_rook.bm", "r")
  89. bm_data = file.read()
  90. file.close()
  91. print("--- Starting Bitmap Test ---")
  92. node.task.post(draw_loop)