From 73583fc3180d66ee6f14d2cc6b83a907a989ec49 Mon Sep 17 00:00:00 2001 From: UncleRedz Date: Wed, 23 Mar 2016 21:51:09 +0100 Subject: [PATCH 1/3] Example of u8glib draw loop Example of an u8glib draw loop that works with WiFi by keeping the draw loop execution time to a minimum. --- lua_examples/u8glib/u8g_drawloop.lua | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 lua_examples/u8glib/u8g_drawloop.lua diff --git a/lua_examples/u8glib/u8g_drawloop.lua b/lua_examples/u8glib/u8g_drawloop.lua new file mode 100644 index 00000000..2f391d18 --- /dev/null +++ b/lua_examples/u8glib/u8g_drawloop.lua @@ -0,0 +1,75 @@ +------------------------------------------------------------------------------ +-- u8glib example which shows how to implement the draw loop without causing +-- timeout issues with the WiFi stack. This is done by drawing one page at +-- a time, allowing the ESP SDK to do it's house keeping between the page +-- draws. +-- +-- This example assumes you have an SSD1306 display connected to pins 4 and 5 +-- using I2C and that the profont22r is compiled into the firmware. +-- Please edit the init_display function to match your setup. +-- +-- Example: +-- dofile("u8g_displayloop.lua") +------------------------------------------------------------------------------ + +local updateDrawFunc +local disp +local font + +function init_display() + local sda = 4 + local sdl = 5 + local sla = 0x3c + i2c.setup(0,sda,sdl, i2c.SLOW) + disp = u8g.ssd1306_128x64_i2c(sla) + font = u8g.font_profont22r +end + +local function setLargeFont() + disp:setFont(font) + disp:setFontRefHeightExtendedText() + disp:setDefaultForegroundColor() + disp:setFontPosTop() +end + +-- Draws one page and schedules the next page, if there is one +local function drawPages() + updateDrawFunc() + if (disp:nextPage() == true) then + node.task.post(drawPages) + end +end + +-- Start the draw loop with the draw implementation in the provided function callback +function updateDisplay(func) + updateDrawFunc = func + disp:firstPage() + node.task.post(drawPages) +end + +function drawHello() + setLargeFont() + disp:drawStr(30,22, "Hello") +end + +function drawWorld() + setLargeFont() + disp:drawStr(30,22, "World") +end + +local drawDemo = { drawHello, drawWorld } +local drawIndex = 1 + +function demoLoop() + -- Start the draw loop with one of the demo functions + updateDisplay(drawDemo[drawIndex]) + drawIndex = drawIndex + 1 + if drawDemo[drawIndex] == nil then + drawIndex = 1 + end +end + +-- Initialise the display and start the demo loop +init_display() +demoLoop() +tmr.alarm(4, 5000, 1, demoLoop) From 6092219f5e060d7f13f4a391b48c8190164bfcce Mon Sep 17 00:00:00 2001 From: UncleRedz Date: Sun, 27 Mar 2016 19:17:24 +0200 Subject: [PATCH 2/3] Some cleanup based on review comments Cleaned up the draw loop and changed some comments. --- lua_examples/u8glib/u8g_drawloop.lua | 36 +++++++++++++--------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/lua_examples/u8glib/u8g_drawloop.lua b/lua_examples/u8glib/u8g_drawloop.lua index 2f391d18..a8184fd1 100644 --- a/lua_examples/u8glib/u8g_drawloop.lua +++ b/lua_examples/u8glib/u8g_drawloop.lua @@ -1,7 +1,7 @@ ------------------------------------------------------------------------------ -- u8glib example which shows how to implement the draw loop without causing -- timeout issues with the WiFi stack. This is done by drawing one page at --- a time, allowing the ESP SDK to do it's house keeping between the page +-- a time, allowing the ESP SDK to do its house keeping between the page -- draws. -- -- This example assumes you have an SSD1306 display connected to pins 4 and 5 @@ -9,10 +9,9 @@ -- Please edit the init_display function to match your setup. -- -- Example: --- dofile("u8g_displayloop.lua") +-- dofile("u8g_drawloop.lua") ------------------------------------------------------------------------------ -local updateDrawFunc local disp local font @@ -32,17 +31,16 @@ local function setLargeFont() disp:setFontPosTop() end --- Draws one page and schedules the next page, if there is one -local function drawPages() - updateDrawFunc() - if (disp:nextPage() == true) then - node.task.post(drawPages) - end -end - -- Start the draw loop with the draw implementation in the provided function callback function updateDisplay(func) - updateDrawFunc = func + -- Draws one page and schedules the next page, if there is one + local function drawPages() + func() + if (disp:nextPage() == true) then + node.task.post(drawPages) + end + end + -- Restart the draw loop and start drawing pages disp:firstPage() node.task.post(drawPages) end @@ -58,18 +56,18 @@ function drawWorld() end local drawDemo = { drawHello, drawWorld } -local drawIndex = 1 function demoLoop() -- Start the draw loop with one of the demo functions - updateDisplay(drawDemo[drawIndex]) - drawIndex = drawIndex + 1 - if drawDemo[drawIndex] == nil then - drawIndex = 1 - end + local f = table.remove(drawDemo,1) + updateDisplay(f) + table.insert(drawDemo,f) end --- Initialise the display and start the demo loop +-- Initialise the display init_display() + +-- Draw demo page immediately and then schedule an update every 5 seconds. +-- To test your own drawXYZ function, disable the next two lines and call updateDisplay(drawXYZ) instead. demoLoop() tmr.alarm(4, 5000, 1, demoLoop) From c091738e596c4cf6474364f2d156630c2188a4a9 Mon Sep 17 00:00:00 2001 From: UncleRedz Date: Sun, 27 Mar 2016 19:22:50 +0200 Subject: [PATCH 3/3] Corrected typo in u8g module documentation Corrected typo in markup for nextPage in u8glib module documentation. --- docs/en/modules/u8g.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/modules/u8g.md b/docs/en/modules/u8g.md index 494a7fd2..4c08fac6 100644 --- a/docs/en/modules/u8g.md +++ b/docs/en/modules/u8g.md @@ -342,7 +342,7 @@ See [u8glib getWidth()](https://github.com/olikraus/u8glib/wiki/userreference#ge See [u8glib getStrWidth](https://github.com/olikraus/u8glib/wiki/userreference#getstrwidth). ## u8g.disp:nextPage() -See [u8glib nextPage()(https://github.com/olikraus/u8glib/wiki/userreference#nextpage). +See [u8glib nextPage()](https://github.com/olikraus/u8glib/wiki/userreference#nextpage). ## u8g.disp:setColorIndex() See [u8glib setColorIndex()](https://github.com/olikraus/u8glib/wiki/userreference#setcolortndex).