from machine import Pin, I2C
import framebuf
import time
class SSD1306_I2C(framebuf.FrameBuffer):
def __init__(self, width, height, i2c, addr=0x3C):
self.width= width
self.height= height
self.i2c= i2c
self.addr= addr
self.pages= height//8
self.buffer= bytearray(self.pages * width)
super().__init__(self.buffer, width, height, framebuf.MONO_VLSB)
self.init_display()
def write_cmd(self, cmd):
self.i2c.writeto(self.addr, b'\x80' + bytes([cmd]))
def init_display(self):
for cmd in(
0xAE,
0x20, 0x00,
0x40,
0xA1,
0xA8, self.height - 1,
0xC8,
0xD3, 0x00,
0xDA, 0x12,
0xD5, 0x80,
0x81, 0xFF,
0xA4,
0xA6,
0x8D, 0x14,
0xAF
):
self.write_cmd(cmd)
self.fill(0)
self.show()
def show(self):
self.write_cmd(0x21)
self.write_cmd(0)
self.write_cmd(self.width - 1)
self.write_cmd(0x22)
self.write_cmd(0)
self.write_cmd(self.pages - 1)
self.i2c.writeto(self.addr, b'\x40' + self.buffer)
i2c=I2C(0, scl=Pin(22), sda=Pin(21))
oled=SSD1306_I2C(128, 64, i2c)
oled.text("ESP32 OLED",0 , 0)
oled.text("MicroPython",0 , 12)
oled.text("Hello World!",0 , 24)
oled.show()
while True:
time.sleep(1)