# Draw an image on the OLED display # This assumes that you've connected it as follows: # GND -> GND # VCC -> 3.3V # SCL -> D2 # SDA -> D4 from machine import Pin, I2C import framebuf import ssd1306 from time import sleep # Loads an image from a file, and returns a Buffer than be used later # The file contains the raw pixel data (not an PNG or JPEG image). See the image2image.py # converter example to convert "regular" images into this format. def loadImage(filename, width=128, height=64): # Allocate a buffer to store the image rawbuf = bytearray(width*height//8) raw_mv = memoryview(rawbuf) # Read the file into the memory array file = open(filename) bytes_read = file.readinto(raw_mv) # Create the framebuffer to wrap the raw data, and return it to the user return framebuf.FrameBuffer(rawbuf, width, height, framebuf.MONO_HLSB) # Set up the display i2c = I2C(0, scl=Pin(2), sda=Pin(4)) display = ssd1306.SSD1306_I2C(128, 64, i2c) # Load the image (using the function above) smile = loadImage('smile.img') # If you'd like, you can load more than one image, and quickly switch between them with blit() # Put the smile image on the display display.blit(smile, 0, 0) display.show()