|
| 1 | +import ustruct |
| 2 | +import utime |
| 3 | + |
| 4 | + |
| 5 | +class ST7735R: |
| 6 | + width = 128 |
| 7 | + height = 128 |
| 8 | + |
| 9 | + def __init__(self, spi, dc, cs, rotation=0x06): |
| 10 | + self.spi = spi |
| 11 | + self.dc = dc |
| 12 | + self.dc(1) |
| 13 | + self.cs = cs |
| 14 | + self.cs(0) |
| 15 | + self.rotation = rotation |
| 16 | + utime.sleep_ms(100) |
| 17 | + for command, data, delay in ( |
| 18 | + (b'\x01', b'', 120), |
| 19 | + (b'\x11', b'', 120), |
| 20 | + (b'\x36', bytes(((rotation & 0x07) << 5,)), 0), |
| 21 | + (b'\x3a', b'\x05', 0), |
| 22 | + (b'\xb4', b'\x07', 0), |
| 23 | + (b'\xb1', b'\x01\x2c\x2d', 0), |
| 24 | + (b'\xb2', b'\x01\x2c\x2d', 0), |
| 25 | + (b'\xb3', b'\x01\x2c\x2d\x01\x2c\x2d', 0), |
| 26 | + (b'\xc0', b'\x02\x02\x84', 0), |
| 27 | + (b'\xc1', b'\xc5', 0), |
| 28 | + (b'\xc2', b'\x0a\x00', 0), |
| 29 | + (b'\xc3', b'\x8a\x2a', 0), |
| 30 | + (b'\xc4', b'\x8a\xee', 0), |
| 31 | + (b'\xc5', b'\x0e', 0), |
| 32 | + (b'\x20', b'', 0), |
| 33 | + (b'\xe0', b'\x02\x1c\x07\x12\x37\x32\x29\x2d' |
| 34 | + b'\x29\x25\x2B\x39\x00\x01\x03\x10', 0), |
| 35 | + (b'\xe1', b'\x03\x1d\x07\x06\x2E\x2C\x29\x2D' |
| 36 | + b'\x2E\x2E\x37\x3F\x00\x00\x02\x10', 0), |
| 37 | + (b'\x13', b'', 10), |
| 38 | + (b'\x29', b'', 120), |
| 39 | + ): |
| 40 | + self.write(command, data) |
| 41 | + utime.sleep_ms(delay) |
| 42 | + self.dc(0) |
| 43 | + self.cs(1) |
| 44 | + |
| 45 | + def block(self, x0, y0, x1, y1): |
| 46 | + if self.rotation & 0x01: |
| 47 | + x0 += 3 # 32 # alternate st7735 display |
| 48 | + x1 += 3 # 32 |
| 49 | + y0 += 2 # 0 |
| 50 | + y1 += 2 # 0 |
| 51 | + else: |
| 52 | + x0 += 2 # 0 |
| 53 | + x1 += 2 # 0 |
| 54 | + y0 += 3 # 32 |
| 55 | + y1 += 3 # 32 |
| 56 | + xpos = ustruct.pack('>HH', x0, x1) |
| 57 | + ypos = ustruct.pack('>HH', y0, y1) |
| 58 | + self.write(b'\x2a', xpos) |
| 59 | + self.write(b'\x2b', ypos) |
| 60 | + self.write(b'\x2c') |
| 61 | + self.dc(1) |
| 62 | + |
| 63 | + def write(self, command=None, data=None): |
| 64 | + if command is not None: |
| 65 | + self.dc(0) |
| 66 | + self.spi.write(command) |
| 67 | + if data: |
| 68 | + self.dc(1) |
| 69 | + self.spi.write(data) |
| 70 | + |
| 71 | + def clear(self, color=0x00): |
| 72 | + self.block(0, 0, self.width - 1, self.height - 1) |
| 73 | + pixel = color.to_bytes(2, 'big') |
| 74 | + data = pixel * 256 |
| 75 | + for count in range(self.width * self.height // 256): |
| 76 | + self.write(None, data) |
| 77 | + |
| 78 | + def __enter__(self): |
| 79 | + self.cs(0) |
| 80 | + return self |
| 81 | + |
| 82 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 83 | + self.cs(1) |
0 commit comments