I got a 0.96" SSD1306 OLED and tried to use it from micro:bit.
I first wrote the code referring to the library of grove but it did not work with this. (Grove 's oled module worked naturally)
When I experimented with Arduino, I was able to confirm the operation only with the Adafruit library eventually, so I scripted it referring to the Adafruit library.
as follows.
ssd1306.py
from microbit import *
oled_address = 0x3c
vram = bytearray(1025)
def command(val):
i2c.write(oled_address,bytes([0x80,val]))
def brightness(val):
a = bytes([0,0x81,val])
i2c.write(oled_address,a)
def init():
command(0xAE)
command(0xD5)
command(0x80)
command(0xA8)
command(63)
command(0xD3)
command(0)
command(0x40)
command(0x8D)
command(0x14)
command(0x20)
command(0)
command(0xA1)
command(0xC8)
command(0xDA)
command(0x12)
command(0x81)
command(0xCF)
command(0xF1)
command(0xDB)
command(0x40)
command(0xA4)
command(0xa6)
command(0x2E)
command(0xAF)
def position(row,col):
a = bytes([0,0xb0+row,0x21,col,0x7f])
i2c.write(oled_address,a)
def write(row,col,data):
vram[row*128+col+1] = data
def update():
position(0,0)
send(vram)
def send(data):
i2c.write(oled_address,data)
def inverse():
command(0xA7)
def normal():
command(0xA6)
def flip(d):
if(d==0):
command(0xA0)
command(0xC0)
else:
command(0xA1)
command(0xC8)
def clear():
position(0,0)
vram[0]=0x40
for i in range(0,1024):
vram[i+1]=0
send(vram)
Use ufs to transfer ssd1306.py to micro:bit and use it.
There are two kinds of methods: direct drawing and buffing it in memory.
Run the following code with REPL etc.
import ssd1306 as oled
oled.init()
oled.clear()
#drawing direct
for i in range(0,128*8):
oled.send(bytes([0x40,i]))
sleep(1000)
oled.clear()
#use memory buffer
for r in range(0,8):
for c in range(0,128):
oled.write(r,c,r*128+c)
oled.update()
sleep(1000)
oled.inverse()
sleep(1000)
oled.normal()