Skip to content

Instantly share code, notes, and snippets.

@goliatone
Last active February 1, 2017 10:26
Show Gist options
  • Save goliatone/d726667b3a20b4575f96888b64773018 to your computer and use it in GitHub Desktop.
Save goliatone/d726667b3a20b4575f96888b64773018 to your computer and use it in GitHub Desktop.
NodeMCU OLED using u8g
function init_spi_display()
-- Hardware SPI CLK = GPIO14
-- Hardware SPI MOSI = GPIO13
-- Hardware SPI MISO = GPIO12 (not used)
-- CS, D/C, and RES can be assigned freely to available GPIOs
local cs = 8 -- GPIO15, pull-down 10k to GND
local dc = 4 -- GPIO2
local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
disp = u8g.ssd1306_128x64_spi(cs, dc, res)
end
function prepare()
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
end
function stringtest(a)
disp:drawStr(30+a, 31, " 0")
disp:drawStr90(30, 31+a, " 90")
disp:drawStr180(30-a, 31, " 180")
disp:drawStr270(30, 31-a, " 270")
end
function line(a)
disp:drawStr(0, 0, "drawLine")
disp:drawLine(7+a, 10, 40, 55)
disp:drawLine(7+a*2, 10, 60, 55)
disp:drawLine(7+a*3, 10, 80, 55)
disp:drawLine(7+a*4, 10, 100, 55)
end
function triangle(a)
local offset = a
disp:drawStr(0, 0, "drawTriangle")
disp:drawTriangle(14,7, 45,30, 10,40)
disp:drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset)
disp:drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53)
disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset)
end
function ascii_1()
local x, y, s
disp:drawStr(0, 0, "ASCII page 1")
for y = 0, 5, 1 do
for x = 0, 15, 1 do
s = y*16 + x + 32
disp:drawStr(x*7, y*10+10, string.char(s))
end
end
end
function extra_page(a)
disp:drawStr(0, 12, "setScale2x2")
disp:setScale2x2()
disp:drawStr(0, 6+a, "setScale2x2")
disp:undoScale()
end
function draw(draw_state)
local component = bit.rshift(draw_state, 3)
prepare()
if (component == 0) then
stringtest(bit.band(draw_state, 7))
elseif (component == 1) then
line(bit.band(draw_state, 7))
elseif (component == 2) then
triangle(bit.band(draw_state, 7))
elseif (component == 3) then
ascii_1()
elseif (component == 4) then
extra_page(bit.band(draw_state, 7))
end
end
function graphics_test(delay)
print("--- Starting Graphics Test ---")
local draw_state
for draw_state = 0, 4 + 8*8, 1 do
disp:firstPage()
repeat
draw(draw_state)
until disp:nextPage() == false
--print(node.heap())
tmr.delay(delay)
-- re-trigger Watchdog!
tmr.wdclr()
end
print("--- Graphics Test done ---")
end
--init_i2c_display()
init_spi_display()
graphics_test(50000)
myStr0 = "Hello"
myStr1 = "Hello"
myStr2 = "Hello" -- place holders for the other lines
myStr3 = "Hello" -- place holders for the other lines
myStr4 = "Hello"
myStr5 = "Hello"
myStr6 = "Hello"
myStr7 = "Hello"
-- setup SPI and connect display
function init_spi_display()
-- Hardware SPI CLK = GPIO14 5
-- Hardware SPI MOSI = GPIO13 7
-- Hardware SPI MISO = GPIO12 (not used)
-- CS, D/C, and RES can be assigned freely to available GPIOs
local cs = 8 -- GPIO15, pull-down 10k to GND
local dc = 4 -- GPIO2
local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
disp = u8g.ssd1306_128x64_spi(cs, dc, res)
end
-- graphic test components
function prepare()
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
end
function draw(void)
prepare()
disp:drawStr(0,0,myStr0)
disp:drawStr(0,8,myStr1)
disp:drawStr(0,16,myStr2)
disp:drawStr(0,24,myStr3)
disp:drawStr(0,32,myStr4)
disp:drawStr(0,40,myStr5)
disp:drawStr(0,48,myStr6)
disp:drawStr(0,56,myStr7)
end
function loop(void)
-- this is the loop that u8g apparently needs for display
disp:firstPage()
repeat
draw()
until disp:nextPage() == false
end
-- below is to set up the timer to keep hitting the display
init_spi_display()
tmr.alarm(3,1000,1,function()
disp:firstPage()
repeat
draw()
until disp:nextPage() == false
end)
function init_spi_display()
local cs = 8 -- GPIO15, pull-down 10k to GND
local dc = 4 -- GPIO2
local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)
disp = u8g.ssd1306_128x64_spi(cs, dc, res)
end
function draw()
disp:setFont(u8g.font_6x10)
disp:drawStr( 30, 10, "Hello IoT!")
disp:drawStr( 20, 50, "esp8266...")
disp:drawLine(0, 25, 128, 25);
disp:setFont(u8g.font_chikita)
disp:drawStr( 20, 40, "Hello welcome users")
end
function display()
disp:firstPage()
repeat
draw()
until disp:nextPage() == false
display();
end
init_spi_display()
display()
--http://www.seeedstudio.com/recipe/index.php?controller=recipe&action=show&recipe_id=219&ref=product
wifi.setmode(wifi.STATION) --Set mode to STATION so he chip can receive the SSID broadcast
function init_OLED(sda,scl) --Set up the u8glib lib
sla = 0x3c
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.ssd1306_128x64_i2c(sla)
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
end
init_OLED(5,6) --Run setting up
tmr.alarm(0,3000,1,function() --A timer, which used to run the following program
wifi.sta.getap(function(t)
disp:firstPage()
repeat
lines = 0
for k,v in pairs(t) do
disp:drawStr(0,lines * 11,k.." "..v:sub(3,5).."dbi") --Print the data out
lines = lines + 1
end
until disp:nextPage() == false
end)
end)
@goliatone
Copy link
Author

Need float version of firmware, latest.

GND
VCC - 3.3V
SCLK - GPIO14 - D5 Hardware SPI Clock (D0 alt pin nomenclature)
RST - GPIO16 - D0 Reset
CS - GPIO15 - D8 Chip Select
A0/DC - GPIO2 - D4 Data/Command
SDI/MOSI - GPIO13 -D7 Hardware SPI MOSI (D1 alt pin nomenclature)

@goliatone
Copy link
Author

NodeMCU pinout

@goliatone
Copy link
Author

@goliatone
Copy link
Author

@goliatone
Copy link
Author

Need a float build with:

  • bit
  • file
  • gpio
  • net
  • node
  • spi
  • tmr
  • u8g
  • uart
  • wifi.
    Then you need to select the following driver:
    st7565_64128n_hw_spi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment