|
| 1 | +import time |
| 2 | +import ustruct |
| 3 | + |
| 4 | +import adafruit_busdevice.spi_device as spi_device |
| 5 | + |
| 6 | + |
| 7 | +def color565(r, g, b): |
| 8 | + return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3 |
| 9 | + |
| 10 | + |
| 11 | +class DummyPin: |
| 12 | + """A fake gpio pin for when you want to skip pins.""" |
| 13 | + def init(self, *args, **kwargs): |
| 14 | + pass |
| 15 | + |
| 16 | + def low(self): |
| 17 | + pass |
| 18 | + |
| 19 | + def high(self): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +class Display: |
| 24 | + _PAGE_SET = None |
| 25 | + _COLUMN_SET = None |
| 26 | + _RAM_WRITE = None |
| 27 | + _RAM_READ = None |
| 28 | + _INIT = () |
| 29 | + _ENCODE_PIXEL = ">H" |
| 30 | + _ENCODE_POS = ">HH" |
| 31 | + _DECODE_PIXEL = ">BBB" |
| 32 | + |
| 33 | + def __init__(self, width, height): |
| 34 | + self.width = width |
| 35 | + self.height = height |
| 36 | + self.init() |
| 37 | + |
| 38 | + def init(self): |
| 39 | + """Run the initialization commands.""" |
| 40 | + for command, data in self._INIT: |
| 41 | + self._write(command, data) |
| 42 | + |
| 43 | + def _block(self, x0, y0, x1, y1, data=None): |
| 44 | + """Read or write a block of data.""" |
| 45 | + self._write(self._COLUMN_SET, self._encode_pos(x0, x1)) |
| 46 | + self._write(self._PAGE_SET, self._encode_pos(y0, y1)) |
| 47 | + if data is None: |
| 48 | + size = ustruct.calcsize(self._DECODE_PIXEL) |
| 49 | + return self._read(self._RAM_READ, |
| 50 | + (x1 - x0 + 1) * (y1 - y0 + 1) * size) |
| 51 | + self._write(self._RAM_WRITE, data) |
| 52 | + |
| 53 | + def _encode_pos(self, a, b): |
| 54 | + """Encode a postion into bytes.""" |
| 55 | + return ustruct.pack(self._ENCODE_POS, a, b) |
| 56 | + |
| 57 | + def _encode_pixel(self, color): |
| 58 | + """Encode a pixel color into bytes.""" |
| 59 | + return ustruct.pack(self._ENCODE_PIXEL, color) |
| 60 | + |
| 61 | + def _decode_pixel(self, data): |
| 62 | + """Decode bytes into a pixel color.""" |
| 63 | + return color565(*ustruct.unpack(self._DECODE_PIXEL, data)) |
| 64 | + |
| 65 | + def pixel(self, x, y, color=None): |
| 66 | + """Read or write a pixel.""" |
| 67 | + if color is None: |
| 68 | + return self._decode_pixel(self._block(x, y, x, y)) |
| 69 | + if not 0 <= x < self.width or not 0 <= y < self.height: |
| 70 | + return |
| 71 | + self._block(x, y, x, y, self._encode_pixel(color)) |
| 72 | + |
| 73 | + def fill_rectangle(self, x, y, width, height, color): |
| 74 | + """Draw a filled rectangle.""" |
| 75 | + x = min(self.width - 1, max(0, x)) |
| 76 | + y = min(self.height - 1, max(0, y)) |
| 77 | + w = min(self.width - x, max(1, width)) |
| 78 | + h = min(self.height - y, max(1, height)) |
| 79 | + self._block(x, y, x + w - 1, y + h - 1, b'') |
| 80 | + chunks, rest = divmod(w * h, 512) |
| 81 | + pixel = self._encode_pixel(color) |
| 82 | + if chunks: |
| 83 | + data = pixel * 512 |
| 84 | + for count in range(chunks): |
| 85 | + self._write(None, data) |
| 86 | + self._write(None, pixel * rest) |
| 87 | + |
| 88 | + def fill(self, color=0): |
| 89 | + """Fill whole screen.""" |
| 90 | + self.fill_rectangle(0, 0, self.width, self.height, color) |
| 91 | + |
| 92 | + def hline(self, x, y, width, color): |
| 93 | + """Draw a horizontal line.""" |
| 94 | + self.fill_rectangle(x, y, width, 1, color) |
| 95 | + |
| 96 | + def vline(self, x, y, height, color): |
| 97 | + """Draw a vertical line.""" |
| 98 | + self.fill_rectangle(x, y, 1, height, color) |
| 99 | + |
| 100 | + |
| 101 | +class DisplaySPI(Display): |
| 102 | + def __init__(self, spi, dc, cs, rst=None, width=1, height=1, baudrate=1000000, |
| 103 | + polarity=0, phase=0): |
| 104 | + self.spi_device = spi_device.SPIDevice(spi, cs, baudrate=baudrate, |
| 105 | + polarity=polarity, phase=phase) |
| 106 | + self.dc = dc |
| 107 | + self.rst = rst |
| 108 | + self.dc.switch_to_output(value=0) |
| 109 | + if self.rst: |
| 110 | + self.rst.switch_to_output(value=0) |
| 111 | + self.reset() |
| 112 | + super().__init__(width, height) |
| 113 | + |
| 114 | + def reset(self): |
| 115 | + self.rst.value = 0 |
| 116 | + time.sleep(0.050) # 50 milliseconds |
| 117 | + self.rst.value = 1 |
| 118 | + time.sleep(0.050) # 50 milliseconds |
| 119 | + |
| 120 | + def _write(self, command=None, data=None): |
| 121 | + if command is not None: |
| 122 | + self.dc.value = 0 |
| 123 | + with self.spi_device as spi: |
| 124 | + spi.write(bytearray([command])) |
| 125 | + if data is not None: |
| 126 | + self.dc.value = 1 |
| 127 | + with self.spi_device as spi: |
| 128 | + spi.write(data) |
| 129 | + |
| 130 | + def _read(self, command=None, count=0): |
| 131 | + self.dc.value = 0 |
| 132 | + with self.spi_device as spi: |
| 133 | + if command is not None: |
| 134 | + spi.write(bytearray([command])) |
| 135 | + if count: |
| 136 | + data = spi.read(count) |
| 137 | + return data |
0 commit comments