u8g_drawloop.lua 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ------------------------------------------------------------------------------
  2. -- u8glib example which shows how to implement the draw loop without causing
  3. -- timeout issues with the WiFi stack. This is done by drawing one page at
  4. -- a time, allowing the ESP SDK to do its house keeping between the page
  5. -- draws.
  6. --
  7. -- This example assumes you have an SSD1306 display connected to pins 4 and 5
  8. -- using I2C and that the profont22r is compiled into the firmware.
  9. -- Please edit the init_display function to match your setup.
  10. --
  11. -- Example:
  12. -- dofile("u8g_drawloop.lua")
  13. ------------------------------------------------------------------------------
  14. local disp
  15. local font
  16. function init_display()
  17. local sda = 4
  18. local sdl = 5
  19. local sla = 0x3c
  20. i2c.setup(0,sda,sdl, i2c.SLOW)
  21. disp = u8g.ssd1306_128x64_i2c(sla)
  22. font = u8g.font_profont22r
  23. end
  24. local function setLargeFont()
  25. disp:setFont(font)
  26. disp:setFontRefHeightExtendedText()
  27. disp:setDefaultForegroundColor()
  28. disp:setFontPosTop()
  29. end
  30. -- Start the draw loop with the draw implementation in the provided function callback
  31. function updateDisplay(func)
  32. -- Draws one page and schedules the next page, if there is one
  33. local function drawPages()
  34. func()
  35. if (disp:nextPage() == true) then
  36. node.task.post(drawPages)
  37. end
  38. end
  39. -- Restart the draw loop and start drawing pages
  40. disp:firstPage()
  41. node.task.post(drawPages)
  42. end
  43. function drawHello()
  44. setLargeFont()
  45. disp:drawStr(30,22, "Hello")
  46. end
  47. function drawWorld()
  48. setLargeFont()
  49. disp:drawStr(30,22, "World")
  50. end
  51. local drawDemo = { drawHello, drawWorld }
  52. function demoLoop()
  53. -- Start the draw loop with one of the demo functions
  54. local f = table.remove(drawDemo,1)
  55. updateDisplay(f)
  56. table.insert(drawDemo,f)
  57. end
  58. -- Initialise the display
  59. init_display()
  60. -- Draw demo page immediately and then schedule an update every 5 seconds.
  61. -- To test your own drawXYZ function, disable the next two lines and call updateDisplay(drawXYZ) instead.
  62. demoLoop()
  63. tmr.alarm(4, 5000, 1, demoLoop)