|
| 1 | +# Pure python implementation of MicroPython framebuf module. |
| 2 | +# This is intended for boards with limited flash memory and the inability to |
| 3 | +# use the native C version of the framebuf module. This python module can be |
| 4 | +# added to the board's file system to provide a functionally identical framebuf |
| 5 | +# interface but at the expense of speed (this python version will be _much_ |
| 6 | +# slower than the C version). |
| 7 | +# This is a direct port of the framebuf module C code to python: |
| 8 | +# https://github.com/micropython/micropython/blob/master/extmod/modframebuf.c |
| 9 | +# Original file created by Damien P. George. |
| 10 | +# Python port below created by Tony DiCola. |
| 11 | + |
| 12 | + |
| 13 | +# Framebuf format constats: |
| 14 | +MVLSB = 0 # Single bit displays (like SSD1306 OLED) |
| 15 | +RGB565 = 1 # 16-bit color displays |
| 16 | +GS4_HMSB = 2 # Unimplemented! |
| 17 | + |
| 18 | + |
| 19 | +class MVLSBFormat: |
| 20 | + |
| 21 | + def setpixel(self, fb, x, y, color): |
| 22 | + index = (y >> 3) * fb.stride + x |
| 23 | + offset = y & 0x07 |
| 24 | + fb.buf[index] = (fb.buf[index] & ~(0x01 << offset)) | ((color != 0) << offset) |
| 25 | + |
| 26 | + def getpixel(self, fb, x, y): |
| 27 | + index = (y >> 3) * fb.stride + x |
| 28 | + offset = y & 0x07 |
| 29 | + return ((fb.buf[index] >> offset) & 0x01) |
| 30 | + |
| 31 | + def fill_rect(self, fb, x, y, width, height, color): |
| 32 | + while height > 0: |
| 33 | + index = (y >> 3) * fb.stride + x |
| 34 | + offset = y & 0x07 |
| 35 | + for ww in range(width): |
| 36 | + fb.buf[index+ww] = (fb.buf[index+ww] & ~(0x01 << offset)) | ((color != 0) << offset) |
| 37 | + y += 1 |
| 38 | + height -= 1 |
| 39 | + |
| 40 | + |
| 41 | +class RGB565Format: |
| 42 | + |
| 43 | + def setpixel(self, fb, x, y, color): |
| 44 | + index = (x + y * fb.stride) * 2 |
| 45 | + fb.buf[index] = (color >> 8) & 0xFF |
| 46 | + fb.buf[index+1] = color & 0xFF |
| 47 | + |
| 48 | + def getpixel(self, fb, x, y): |
| 49 | + index = (x + y * fb.stride) * 2 |
| 50 | + return (fb.buf[index] << 8) | fb.buf[index+1] |
| 51 | + |
| 52 | + def fill_rect(self, fb, x, y, width, height, color): |
| 53 | + while height > 0: |
| 54 | + for ww in range(width): |
| 55 | + index = (ww + x + y * fb.stride) * 2 |
| 56 | + fb.buf[index] = (color >> 8) & 0xFF |
| 57 | + fb.buf[index+1] = color & 0xFF |
| 58 | + y += 1 |
| 59 | + height -= 1 |
| 60 | + |
| 61 | + |
| 62 | +class FrameBuffer: |
| 63 | + |
| 64 | + def __init__(self, buf, width, height, buf_format=MVLSB, stride=None): |
| 65 | + self.buf = buf |
| 66 | + self.width = width |
| 67 | + self.height = height |
| 68 | + self.stride = stride |
| 69 | + if self.stride is None: |
| 70 | + self.stride = width |
| 71 | + if buf_format == MVLSB: |
| 72 | + self.format = MVLSBFormat() |
| 73 | + elif buf_format == RGB565: |
| 74 | + self.format = RGB565Format() |
| 75 | + else: |
| 76 | + raise ValueError('invalid format') |
| 77 | + |
| 78 | + def fill(self, color): |
| 79 | + self.format.fill_rect(self, 0, 0, self.width, self.height, color) |
| 80 | + |
| 81 | + def fill_rect(self, x, y, width, height, color): |
| 82 | + if width < 1 or height < 1 or (x+width) <= 0 or (y+height) <= 0 or y >= self.height or x >= self.width: |
| 83 | + return |
| 84 | + xend = min(self.width, x+width) |
| 85 | + yend = min(self.height, y+height) |
| 86 | + x = max(x, 0) |
| 87 | + y = max(y, 0) |
| 88 | + self.format.fill_rect(self, x, y, xend-x, yend-y, color) |
| 89 | + |
| 90 | + def pixel(self, x, y, color=None): |
| 91 | + if x < 0 or x >= self.width or y < 0 or y >= self.height: |
| 92 | + return |
| 93 | + if color is None: |
| 94 | + return self.format.getpixel(self, x, y) |
| 95 | + else: |
| 96 | + self.format.setpixel(self, x, y, color) |
| 97 | + |
| 98 | + def hline(self, x, y, width, color): |
| 99 | + self.fill_rect(x, y, width, 1, color) |
| 100 | + |
| 101 | + def vline(self, x, y, height, color): |
| 102 | + self.fill_rect(x, y, 1, height, color) |
| 103 | + |
| 104 | + def rect(self, x, y, width, height, color): |
| 105 | + self.fill_rect(x, y, width, 1, color) |
| 106 | + self.fill_rect(x, y+height, width, 1, color) |
| 107 | + self.fill_rect(self, x, y, 1, height, color) |
| 108 | + self.fill_rect(self, x+width, y, 1, height, color) |
| 109 | + |
| 110 | + def line(self): |
| 111 | + raise NotImplementedError() |
| 112 | + |
| 113 | + def blit(self): |
| 114 | + raise NotImplementedError() |
| 115 | + |
| 116 | + def scroll(self): |
| 117 | + raise NotImplementedError() |
| 118 | + |
| 119 | + def text(self): |
| 120 | + raise NotImplementedError() |
| 121 | + |
| 122 | + |
| 123 | +class FrameBuffer1(FrameBuffer): |
| 124 | + pass |
0 commit comments