Convert a PIL image (mode '1', 128x64) to SSD1306 page-format bytes. Output: 1024 bytes — 8 pages × 128 columns. Each byte represents 8 vertical pixels (LSB = topmost pixel in page). Pixel value 255 = white/on, 0 = black/off.
(self, image)
| 91 | logger.info("SSD1306 cleared") |
| 92 | |
| 93 | def getbuffer(self, image): |
| 94 | """Convert a PIL image (mode '1', 128x64) to SSD1306 page-format bytes. |
| 95 | |
| 96 | Output: 1024 bytes — 8 pages × 128 columns. |
| 97 | Each byte represents 8 vertical pixels (LSB = topmost pixel in page). |
| 98 | Pixel value 255 = white/on, 0 = black/off. |
| 99 | """ |
| 100 | from PIL import Image as PILImage |
| 101 | |
| 102 | # Ensure correct size and mode |
| 103 | if image.width != self.width or image.height != self.height: |
| 104 | logger.warning( |
| 105 | "SSD1306 getbuffer: image size %dx%d → resizing to %dx%d", |
| 106 | image.width, image.height, self.width, self.height, |
| 107 | ) |
| 108 | image = image.resize((self.width, self.height)) |
| 109 | |
| 110 | if image.mode != "1": |
| 111 | image = image.convert("1") |
| 112 | |
| 113 | buf = bytearray(self.width * (self.height // 8)) # 1024 bytes |
| 114 | for page in range(self.height // 8): # pages 0..7 |
| 115 | for x in range(self.width): # columns 0..127 |
| 116 | byte = 0 |
| 117 | for bit in range(8): |
| 118 | y = page * 8 + bit |
| 119 | pixel = image.getpixel((x, y)) |
| 120 | # In mode "1": non-zero = white/on |
| 121 | if pixel: |
| 122 | byte |= (1 << bit) |
| 123 | buf[page * self.width + x] = byte |
| 124 | return bytes(buf) |
| 125 | |
| 126 | def display(self, buf): |
| 127 | """Write a full-screen framebuffer (1024 bytes) to the display.""" |