u8g_rotation.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- ***************************************************************************
  2. -- Rotation Test
  3. --
  4. -- This script executes the rotation 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. -- the draw() routine
  37. function draw()
  38. disp:setFont(u8g.font_6x10)
  39. disp:drawStr( 0+0, 20+0, "Hello!")
  40. disp:drawStr( 0+2, 20+16, "Hello!")
  41. disp:drawBox(0, 0, 3, 3)
  42. disp:drawBox(disp:getWidth()-6, 0, 6, 6)
  43. disp:drawBox(disp:getWidth()-9, disp:getHeight()-9, 9, 9)
  44. disp:drawBox(0, disp:getHeight()-12, 12, 12)
  45. end
  46. function rotate()
  47. if (next_rotation < tmr.now() / 1000) then
  48. if (dir == 0) then
  49. disp:undoRotation()
  50. elseif (dir == 1) then
  51. disp:setRot90()
  52. elseif (dir == 2) then
  53. disp:setRot180()
  54. elseif (dir == 3) then
  55. disp:setRot270()
  56. end
  57. dir = dir + 1
  58. dir = bit.band(dir, 3)
  59. -- schedule next rotation step in 1000ms
  60. next_rotation = tmr.now() / 1000 + 1000
  61. end
  62. end
  63. function rotation_test()
  64. print("--- Starting Rotation Test ---")
  65. dir = 0
  66. next_rotation = 0
  67. local loopcnt
  68. for loopcnt = 1, 100, 1 do
  69. rotate()
  70. disp:firstPage()
  71. repeat
  72. draw(draw_state)
  73. until disp:nextPage() == false
  74. tmr.delay(100000)
  75. tmr.wdclr()
  76. end
  77. print("--- Rotation Test done ---")
  78. end
  79. --init_i2c_display()
  80. init_spi_display()
  81. rotation_test()