OLed

Thư viện: ssd1306.py

boot.py
import gc
import esp
from oled import OLed
from machine import Pin
 
esp.osdebug(None)
gc.collect()
 
oled21 = OLed(scl=22, sda=21)
main.py
import image_asset
from utime import sleep
 
while True:
  oled21.set_message("Test OLed...")
 
  sleep(1)
 
  oled21.display_menu(0, 5, 0)
 
  sleep(1)
 
  oled21.set_images(image_asset.girls)
 
  sleep(1)
oled.py
import ssd1306
from machine import SoftI2C, Pin
 
class OLed:
    def __init__(self, scl, sda):
        self.scl = scl
        self.sda = sda
 
        self.i2c = SoftI2C(scl=Pin(scl), sda=Pin(sda))
        self.oled = ssd1306.SSD1306_I2C(128, 64, self.i2c)        
 
        self.menu_items = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
        self.menu_selected_index = 0
        self.start_index = 0
        self.end_index = min(len(self.menu_items), 6)
 
    def set_message(self, message, x = 0, y = 0, is_fill = True):
        if is_fill:
            self.oled.fill(0)
 
        self.oled.text(message, x, y, 1)
        self.oled.show()
 
    def set_images(self, image_pixels):
        self.oled.fill(0)
 
        line = 0
        for line_pixels in image_pixels:
            column = 0
            for pixel in line_pixels:
                self.oled.pixel(column, line, pixel)
 
                column = column + 1
 
            line = line + 1
 
        self.oled.show()
 
    def display_menu(self, start, end, selected):
        self.oled.fill(0)
        self.set_message("Setting:")
 
        for i in range(start, end):
            y = (i - start) * 10 + 16
 
            if i == selected:
                self.oled.text("-> " + self.menu_items[i], 0, y)
            else:
                self.oled.text(self.menu_items[i], 20, y)
        self.oled.show()