import time from machine import Pin, SPI import framebuf import micropython class ST7796: def __init__(self, spi, cs, dc, rst, bl=None, width=480, height=320, invert_color=True): self.spi = spi self.cs = cs self.dc = dc self.rst = rst self.bl = bl self.width = width self.height = height self.invert_color = invert_color # 1. 1-bit Canvas Buffer (Standard MONO_HLSB for drawing) self.hw_len = (self.width * self.height) // 8 self.canvas_buffer = bytearray(self.hw_len) self.canvas = framebuf.FrameBuffer(self.canvas_buffer, self.width, self.height, framebuf.MONO_HLSB) # Pre-allocate chunk buffer for conversion (16 rows: 480 * 16 * 2 = 15360 bytes) self.chunk_rows = 16 self.row_buffer = bytearray(self.width * self.chunk_rows * 2) # Initialize pins self.cs.init(self.cs.OUT, value=1) self.dc.init(self.dc.OUT, value=0) self.rst.init(self.rst.OUT, value=1) if self.bl is not None: # PWM or Pin backlight control if isinstance(self.bl, Pin): self.bl.init(self.bl.OUT, value=1) self.reset() self.init_display() self.clear(0) self.show() # --- DRAWING WRAPPERS --- def pixel(self, x, y, c): self.canvas.pixel(x, y, c) def line(self, x1, y1, x2, y2, c): self.canvas.line(x1, y1, x2, y2, c) def rect(self, x, y, w, h, c): self.canvas.rect(x, y, w, h, c) def fill_rect(self, x, y, w, h, c): self.canvas.fill_rect(x, y, w, h, c) def text(self, msg, x, y, c=1): self.canvas.text(msg, x, y, c) def clear(self, c=0): self.canvas.fill(c) # --- SCALABLE TEXT --- def text_large(self, msg, x, y, scale=2, c=1): char_w = 8; char_h = 8 tmp_buf = bytearray(char_w * char_h // 8) tmp_fb = framebuf.FrameBuffer(tmp_buf, char_w, char_h, framebuf.MONO_HLSB) for char in msg: tmp_fb.fill(0); tmp_fb.text(char, 0, 0, 1) for py in range(8): for px in range(8): if tmp_fb.pixel(px, py): self.canvas.fill_rect(x + (px * scale), y + (py * scale), scale, scale, c) x += (8 * scale) # --- RAW BITMAPS (1:1 scale) --- def bitmap(self, x, y, w, h, pixel_data): img = framebuf.FrameBuffer(pixel_data, w, h, framebuf.MONO_HLSB) self.canvas.blit(img, x, y) # --- PBM FILE LOADER WITH SCALING --- def draw_pbm(self, filename, x, y, scale=1): try: with open(filename, 'rb') as f: line1 = f.readline() if not line1.startswith(b'P4'): print("Err: Not P4 PBM"); return while True: line = f.readline() if not line.startswith(b'#'): break dims = line.split(); w = int(dims[0]); h = int(dims[1]) data = bytearray(f.read()) src_fb = framebuf.FrameBuffer(data, w, h, framebuf.MONO_HLSB) if scale == 1: self.canvas.blit(src_fb, x, y) else: for sy in range(h): for sx in range(w): if src_fb.pixel(sx, sy): self.canvas.fill_rect(x + (sx * scale), y + (sy * scale), scale, scale, 1) print(f"Loaded {filename} (scale {scale})") except OSError: print(f"Error: Could not open {filename}") # --- SCREENSHOT --- def save_screenshot(self, filename): print(f"Saving screenshot to {filename}...") try: with open(filename, 'wb') as f: f.write(b'P4\n') f.write(f"{self.width} {self.height}\n".encode()) f.write(self.canvas_buffer) print("Saved!") except Exception as e: print(f"Error saving screenshot: {e}") # --- HARDWARE LOGIC --- def reset(self): self.rst(1); time.sleep_ms(5); self.rst(0); time.sleep_ms(15); self.rst(1); time.sleep_ms(15) def write_cmd(self, cmd): self.dc(0); self.cs(0); self.spi.write(bytearray([cmd])); self.cs(1) def write_data(self, data): self.dc(1); self.cs(0) if isinstance(data, int): self.spi.write(bytearray([data])) elif isinstance(data, list): self.spi.write(bytearray(data)) else: self.spi.write(data) self.cs(1) def init_display(self): # ST7796 Minimal Initialization Sequence (ported from working C++ codebase) self.write_cmd(0x01) # SWRESET time.sleep_ms(150) self.write_cmd(0x11) # SLPOUT time.sleep_ms(120) # Pixel Format (COLMOD) = 0x55 (16-bit color / RGB565) self.write_cmd(0x3A); self.write_data(0x55) time.sleep_ms(10) # Memory Access Control (MADCTL) = 0xE0 (Landscape: MY=1, MX=1, MV=1, RGB order) self.write_cmd(0x36); self.write_data(0xE0) time.sleep_ms(10) # Display Inversion Control if self.invert_color: self.write_cmd(0x21) # INVON else: self.write_cmd(0x20) # INVOFF time.sleep_ms(10) self.write_cmd(0x13) # NORON time.sleep_ms(10) self.write_cmd(0x29) # DISPON time.sleep_ms(120) def invert(self, enable): if enable: self.write_cmd(0x21) # INVON else: self.write_cmd(0x20) # INVOFF def set_window(self, x0, y0, x1, y1): # Column Address Set (CASET) self.write_cmd(0x2A) self.write_data(bytearray([x0 >> 8, x0 & 0xFF, x1 >> 8, x1 & 0xFF])) # Row Address Set (RASET) self.write_cmd(0x2B) self.write_data(bytearray([y0 >> 8, y0 & 0xFF, y1 >> 8, y1 & 0xFF])) # Memory Write (RAMWR) self.write_cmd(0x2C) @micropython.native def _convert_rows(self, start_row, num_rows, row_buf): """Converts 1-bit monochrome row segment to 16-bit RGB565 format. Compiles block-wise bitwise operations at native speed. """ width = self.width canvas_buf = self.canvas_buffer idx = 0 for y in range(start_row, start_row + num_rows): byte_offset = y * (width // 8) for x_byte_idx in range(width // 8): val = canvas_buf[byte_offset + x_byte_idx] # Unroll 8 bits for speed # Bit 7 if val & 0x80: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 # Bit 6 if val & 0x40: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 # Bit 5 if val & 0x20: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 # Bit 4 if val & 0x10: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 # Bit 3 if val & 0x08: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 # Bit 2 if val & 0x04: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 # Bit 1 if val & 0x02: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 # Bit 0 if val & 0x01: row_buf[idx] = 0xFF; row_buf[idx+1] = 0xFF else: row_buf[idx] = 0x00; row_buf[idx+1] = 0x00 idx += 2 def show(self): """Refreshes the screen by writing the frame buffer segment-by-segment.""" self.set_window(0, 0, self.width - 1, self.height - 1) self.dc(1) self.cs(0) num_chunks = self.height // self.chunk_rows for chunk in range(num_chunks): start_row = chunk * self.chunk_rows self._convert_rows(start_row, self.chunk_rows, self.row_buffer) self.spi.write(self.row_buffer) self.cs(1)