from machine import I2C, Pin import ssd1306 import framebuf class OLED: def __init__(self): i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=400_000) self.width = 128 self.height = 64 # Real OLED self.oled = ssd1306.SSD1306_I2C(self.width, self.height, i2c) # Off-screen buffer (normal orientation) self.buf = bytearray(self.width * self.height // 8) self.fb = framebuf.FrameBuffer(self.buf, self.width, self.height, framebuf.MONO_VLSB) # -------------------------------------------------- # Rotate framebuffer 180° # -------------------------------------------------- def rotate_180(self): self.oled.fill(0) for y in range(self.height): for x in range(self.width): if self.fb.pixel(x, y): self.oled.pixel( self.width - 1 - x, self.height - 1 - y, 1 ) # -------------------------------------------------- # Display content # -------------------------------------------------- def show(self, rtc, pressure, percent, cpu_temp, rssi_percent, depth_mm): dt = rtc.datetime() ctime = f"{dt[4]:02}:{dt[5]:02}:{dt[6]:02}" # Draw NORMAL orientation into framebuffer self.fb.fill(0) self.fb.text(ctime, 30, 0) self.fb.text(f"P: {pressure:.2f} kPa", 0, 10) self.fb.text(f"D: {depth_mm:.0f} mm", 0, 20) self.fb.text(f"L: {percent:.2f} %", 0, 30) self.fb.text(f"CPU: {cpu_temp:.1f} C", 0, 40) self.fb.text(f"WiFi: {rssi_percent}%", 0, 50) # Rotate and display self.rotate_180() self.oled.show()