|
| 1 | +# Copyright (c) 2024 - 2025 Kevin G. Schlosser |
| 2 | + |
| 3 | +import time |
| 4 | +from micropython import const # NOQA |
| 5 | + |
| 6 | +import lvgl as lv # NOQA |
| 7 | + |
| 8 | + |
| 9 | +_NOP = const(0x00) |
| 10 | +_SWRESET = const(0x01) |
| 11 | +_RDDID = const(0x04) |
| 12 | +_RDDST = const(0x09) |
| 13 | + |
| 14 | +_SLPIN = const(0x10) |
| 15 | +_SLPOUT = const(0x11) |
| 16 | +_PTLON = const(0x12) |
| 17 | +_NORON = const(0x13) |
| 18 | + |
| 19 | +_DISPOFF = const(0x28) |
| 20 | +_DISPON = const(0x29) |
| 21 | + |
| 22 | +_PTLAR = const(0x30) |
| 23 | +_COLMOD = const(0x3A) |
| 24 | +_MADCTL = const(0x36) |
| 25 | + |
| 26 | +_FRMCTR1 = const(0xB1) |
| 27 | +_FRMCTR2 = const(0xB2) |
| 28 | +_FRMCTR3 = const(0xB3) |
| 29 | +_INVCTR = const(0xB4) |
| 30 | +_DISSET5 = const(0xB6) |
| 31 | + |
| 32 | +_PWCTR1 = const(0xC0) |
| 33 | +_PWCTR2 = const(0xC1) |
| 34 | +_PWCTR3 = const(0xC2) |
| 35 | +_PWCTR4 = const(0xC3) |
| 36 | +_PWCTR5 = const(0xC4) |
| 37 | +_VMCTR1 = const(0xC5) |
| 38 | + |
| 39 | +_PWCTR6 = const(0xFC) |
| 40 | + |
| 41 | +_GMCTRP1 = const(0xE0) |
| 42 | +_GMCTRN1 = const(0xE1) |
| 43 | + |
| 44 | + |
| 45 | +def init(self): |
| 46 | + buf = bytearray(16) |
| 47 | + mv = memoryview(buf) |
| 48 | + |
| 49 | + self._reset() |
| 50 | + |
| 51 | + self._writecommand(_SWRESET) |
| 52 | + time.sleep_ms(50) # NOQA |
| 53 | + |
| 54 | + self._writecommand(_SLPOUT) |
| 55 | + time.sleep_ms(255) # NOQA |
| 56 | + |
| 57 | + buf[:3] = bytearray([0x01, 0x2C, 0x2D]) |
| 58 | + self._writecommand(_FRMCTR1, mv[:3]) |
| 59 | + time.sleep_us(10) # NOQA |
| 60 | + |
| 61 | + self._writecommand(_FRMCTR2, mv[:3]) |
| 62 | + time.sleep_us(10) # NOQA |
| 63 | + |
| 64 | + self._writecommand(_FRMCTR3, mv[:3]) |
| 65 | + time.sleep_us(10) # NOQA |
| 66 | + |
| 67 | + buf[0] = 0x07 |
| 68 | + self._writecommand(_INVCTR, mv[:1]) |
| 69 | + |
| 70 | + buf[:3] = bytearray([0xA2, 0x02, 0x84]) |
| 71 | + self._writecommand(_PWCTR1, mv[:3]) |
| 72 | + time.sleep_us(10) # NOQA |
| 73 | + |
| 74 | + buf[0] = 0xC5 |
| 75 | + self._writecommand(_PWCTR2, mv[:1]) |
| 76 | + |
| 77 | + buf[:2] = bytearray([0x0A, 0x00]) |
| 78 | + self._writecommand(_PWCTR3, mv[:2]) |
| 79 | + |
| 80 | + buf[:2] = bytearray([0x8A, 0x2A]) |
| 81 | + self._writecommand(_PWCTR4, mv[:2]) |
| 82 | + |
| 83 | + buf[:2] = bytearray([0x8A, 0xEE]) |
| 84 | + self._writecommand(_PWCTR5, mv[:2]) |
| 85 | + |
| 86 | + buf[0] = 0x0E |
| 87 | + self._writecommand(_VMCTR1, mv[:1]) |
| 88 | + time.sleep_us(10) # NOQA |
| 89 | + |
| 90 | + self._writecommand(_MADCTL, mv[:1]) |
| 91 | + buf[0] = 0xC8 |
| 92 | + |
| 93 | + buf[:16] = bytearray([0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, |
| 94 | + 0x29, 0x25, 0x2b, 0x39, 0x00, 0x01, 0x03, 0x10]) |
| 95 | + self._writecommand(_GMCTRP1, mv[:16]) |
| 96 | + |
| 97 | + buf[:16] = bytearray([0x03, 0x1d, 0x07, 0x06, 0x2e, 0x2c, 0x29, 0x2d, |
| 98 | + 0x2e, 0x2e, 0x37, 0x3f, 0x00, 0x00, 0x02, 0x10]) |
| 99 | + self._writecommand(_GMCTRN1, mv[:16]) |
| 100 | + time.sleep_us(10) # NOQA |
| 101 | + |
| 102 | + color_size = lv.color_format_get_size(self._color_space) |
| 103 | + if color_size == 2: # NOQA |
| 104 | + pixel_format = 0x55 |
| 105 | + elif color_size == 3: |
| 106 | + pixel_format = 0x77 |
| 107 | + else: |
| 108 | + raise RuntimeError( |
| 109 | + 'ST7796 IC only supports ' |
| 110 | + 'lv.COLOR_FORMAT.RGB565 or lv.COLOR_FORMAT.RGB888' |
| 111 | + ) |
| 112 | + |
| 113 | + buf[0] = pixel_format |
| 114 | + self.set_params(_COLMOD, mv[:1]) |
| 115 | + |
| 116 | + time.sleep_us(10) # NOQA |
| 117 | + |
| 118 | + self._writecommand(_NORON) |
| 119 | + time.sleep_us(10) # NOQA |
| 120 | + |
| 121 | + self._writecommand(_DISPON) |
| 122 | + time.sleep_us(500) # NOQA |
0 commit comments