file.remove("oled.lua")
file.open("oled.lua","w")
file.writeline([[function init_i2c_display()]])
file.writeline([[sda = 5 ]])
file.writeline([[scl = 6 ]])
file.writeline([[sla = 0x3c]])
file.writeline([[i2c.setup(0, sda, scl, i2c.SLOW)]])
file.writeline([[disp = u8g.ssd1306_128x64_i2c(sla)]])
file.writeline([[end]])
file.writeline([[function init_spi_display()]])
file.writeline([[cs = 6 ]])
file.writeline([[dc = 4 ]])
file.writeline([[res = 0 ]])
file.writeline([[spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)]])
file.writeline([[disp = u8g.ssd1306_128x64_spi(cs, dc, res)]])
file.writeline([[end]])
file.writeline([[function prepare()]])
file.writeline([[disp:setFont(u8g.font_6x10)]])
file.writeline([[disp:setFontRefHeightExtendedText()]])
file.writeline([[disp:setDefaultForegroundColor()]])
file.writeline([[disp:setFontPosTop()]])
file.writeline([[end]])
file.writeline([[function box_frame(a)]])
file.writeline([[disp:drawStr(0, 0, "drawBox")]])
file.writeline([[disp:drawBox(5, 10, 20, 10)]])
file.writeline([[disp:drawBox(10+a, 15, 30, 7)]])
file.writeline([[disp:drawStr(0, 30, "drawFrame")]])
file.writeline([[disp:drawFrame(5, 10+30, 20, 10)]])
file.writeline([[disp:drawFrame(10+a, 15+30, 30, 7)]])
file.writeline([[end]])
file.writeline([[function disc_circle(a)]])
file.writeline([[disp:drawStr(0, 0, "drawDisc")]])
file.writeline([[disp:drawDisc(10, 18, 9)]])
file.writeline([[disp:drawDisc(24+a, 16, 7)]])
file.writeline([[disp:drawStr(0, 30, "drawCircle")]])
file.writeline([[disp:drawCircle(10, 18+30, 9)]])
file.writeline([[disp:drawCircle(24+a, 16+30, 7)]])
file.writeline([[end]])
file.writeline([[function r_frame(a)]])
file.writeline([[disp:drawStr(0, 0, "drawRFrame/Box")]])
file.writeline([[disp:drawRFrame(5, 10, 40, 30, a+1)]])
file.writeline([[disp:drawRBox(50, 10, 25, 40, a+1)]])
file.writeline([[end]])
file.writeline([[function stringtest(a)]])
file.writeline([[disp:drawStr(30+a, 31, " 0")]])
file.writeline([[disp:drawStr90(30, 31+a, " 90")]])
file.writeline([[disp:drawStr180(30-a, 31, " 180")]])
file.writeline([[disp:drawStr270(30, 31-a, " 270")]])
file.writeline([[end]])
file.writeline([[function line(a)]])
file.writeline([[disp:drawStr(0, 0, "drawLine")]])
file.writeline([[disp:drawLine(7+a, 10, 40, 55)]])
file.writeline([[disp:drawLine(7+a*2, 10, 60, 55)]])
file.writeline([[disp:drawLine(7+a*3, 10, 80, 55)]])
file.writeline([[disp:drawLine(7+a*4, 10, 100, 55)]])
file.writeline([[end]])
file.writeline([[function triangle(a)]])
file.writeline([[local offset = a]])
file.writeline([[disp:drawStr(0, 0, "drawTriangle")]])
file.writeline([[disp:drawTriangle(14,7, 45,30, 10,40)]])
file.writeline([[disp:drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset)]])
file.writeline([[disp:drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53)]])
file.writeline([[disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset)]])
file.writeline([[end]])
file.writeline([[function ascii_1()]])
file.writeline([[local x, y, s]])
file.writeline([[disp:drawStr(0, 0, "ASCII page 1")]])
file.writeline([[for y = 0, 5, 1 do]])
file.writeline([[for x = 0, 15, 1 do]])
file.writeline([[s = y*16 + x + 32]])
file.writeline([[disp:drawStr(x*7, y*10+10, string.char(s))]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[function ascii_2()]])
file.writeline([[local x, y, s]])
file.writeline([[disp:drawStr(0, 0, "ASCII page 2")]])
file.writeline([[for y = 0, 5, 1 do]])
file.writeline([[for x = 0, 15, 1 do]])
file.writeline([[s = y*16 + x + 160]])
file.writeline([[disp:drawStr(x*7, y*10+10, string.char(s))]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[function extra_page(a)]])
file.writeline([[disp:drawStr(0, 12, "setScale2x2")]])
file.writeline([[disp:setScale2x2()]])
file.writeline([[disp:drawStr(0, 6+a, "setScale2x2")]])
file.writeline([[disp:undoScale()]])
file.writeline([[end]])
file.writeline([[function draw(draw_state)]])
file.writeline([[local component = bit.rshift(draw_state, 3)]])
file.writeline([[prepare()]])
file.writeline([[if (component == 0) then]])
file.writeline([[box_frame(bit.band(draw_state, 7))]])
file.writeline([[elseif (component == 1) then]])
file.writeline([[disc_circle(bit.band(draw_state, 7))]])
file.writeline([[elseif (component == 2) then]])
file.writeline([[r_frame(bit.band(draw_state, 7))]])
file.writeline([[elseif (component == 3) then]])
file.writeline([[stringtest(bit.band(draw_state, 7))]])
file.writeline([[elseif (component == 4) then]])
file.writeline([[line(bit.band(draw_state, 7))]])
file.writeline([[elseif (component == 5) then]])
file.writeline([[triangle(bit.band(draw_state, 7))]])
file.writeline([[elseif (component == 6) then]])
file.writeline([[ascii_1()]])
file.writeline([[elseif (component == 7) then]])
file.writeline([[ascii_2()]])
file.writeline([[elseif (component == 8) then]])
file.writeline([[extra_page(bit.band(draw_state, 7))]])
file.writeline([[end]])
file.writeline([[end]])
file.writeline([[function graphics_test(delay)]])
file.writeline([[print("Starting Graphics Test")]])
file.writeline([[local draw_state]])
file.writeline([[for draw_state = 0, 7 + 8*8, 1 do]])
file.writeline([[disp:firstPage()]])
file.writeline([[repeat]])
file.writeline([[draw(draw_state)]])
file.writeline([[until disp:nextPage() == false]])
file.writeline([[tmr.delay(delay)]])
file.writeline([[tmr.wdclr()]])
file.writeline([[end]])
file.writeline([[print("Graphics Test done")]])
file.writeline([[end]])
file.writeline([[init_spi_display()]])
file.writeline([[graphics_test(50000)]])
file.close()
dofile("oled.lua")