/ adafruit_rgb_display / st7789.py
st7789.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2019 Melissa LeBlanc-Williams for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 `adafruit_rgb_display.st7789` 24 ==================================================== 25 26 A simple driver for the ST7789-based displays. 27 28 * Author(s): Melissa LeBlanc-Williams 29 """ 30 31 try: 32 import struct 33 except ImportError: 34 import ustruct as struct 35 from micropython import const 36 from adafruit_rgb_display.rgb import DisplaySPI 37 38 __version__ = "0.0.0-auto.0" 39 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display.git" 40 41 _NOP = const(0x00) 42 _SWRESET = const(0x01) 43 _RDDID = const(0x04) 44 _RDDST = const(0x09) 45 46 _SLPIN = const(0x10) 47 _SLPOUT = const(0x11) 48 _PTLON = const(0x12) 49 _NORON = const(0x13) 50 51 _INVOFF = const(0x20) 52 _INVON = const(0x21) 53 _DISPOFF = const(0x28) 54 _DISPON = const(0x29) 55 _CASET = const(0x2A) 56 _RASET = const(0x2B) 57 _RAMWR = const(0x2C) 58 _RAMRD = const(0x2E) 59 60 _PTLAR = const(0x30) 61 _COLMOD = const(0x3A) 62 _MADCTL = const(0x36) 63 64 _FRMCTR1 = const(0xB1) 65 _FRMCTR2 = const(0xB2) 66 _FRMCTR3 = const(0xB3) 67 _INVCTR = const(0xB4) 68 _DISSET5 = const(0xB6) 69 70 _PWCTR1 = const(0xC0) 71 _PWCTR2 = const(0xC1) 72 _PWCTR3 = const(0xC2) 73 _PWCTR4 = const(0xC3) 74 _PWCTR5 = const(0xC4) 75 _VMCTR1 = const(0xC5) 76 77 _RDID1 = const(0xDA) 78 _RDID2 = const(0xDB) 79 _RDID3 = const(0xDC) 80 _RDID4 = const(0xDD) 81 82 _PWCTR6 = const(0xFC) 83 84 _GMCTRP1 = const(0xE0) 85 _GMCTRN1 = const(0xE1) 86 87 88 class ST7789(DisplaySPI): 89 """ 90 A simple driver for the ST7789-based displays. 91 92 >>> import busio 93 >>> import digitalio 94 >>> import board 95 >>> from adafruit_rgb_display import color565 96 >>> import adafruit_rgb_display.st7789 as st7789 97 >>> spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO) 98 >>> display = st7789.ST7789(spi, cs=digitalio.DigitalInOut(board.GPIO0), 99 ... dc=digitalio.DigitalInOut(board.GPIO15), rst=digitalio.DigitalInOut(board.GPIO16)) 100 >>> display.fill(0x7521) 101 >>> display.pixel(64, 64, 0) 102 """ 103 104 _COLUMN_SET = _CASET 105 _PAGE_SET = _RASET 106 _RAM_WRITE = _RAMWR 107 _RAM_READ = _RAMRD 108 _INIT = ( 109 (_SWRESET, None), 110 (_SLPOUT, None), 111 (_COLMOD, b"\x55"), # 16bit color 112 (_MADCTL, b"\x08"), 113 ) 114 115 # pylint: disable-msg=useless-super-delegation, too-many-arguments 116 def __init__( 117 self, 118 spi, 119 dc, 120 cs, 121 rst=None, 122 width=240, 123 height=320, 124 baudrate=16000000, 125 polarity=0, 126 phase=0, 127 *, 128 x_offset=0, 129 y_offset=0, 130 rotation=0 131 ): 132 super().__init__( 133 spi, 134 dc, 135 cs, 136 rst, 137 width, 138 height, 139 baudrate=baudrate, 140 polarity=polarity, 141 phase=phase, 142 x_offset=x_offset, 143 y_offset=y_offset, 144 rotation=rotation, 145 ) 146 147 def init(self): 148 149 super().init() 150 cols = struct.pack(">HH", self._X_START, self.width + self._X_START) 151 rows = struct.pack(">HH", self._Y_START, self.height + self._Y_START) 152 for command, data in ( 153 (_CASET, cols), 154 (_RASET, rows), 155 (_INVON, None), 156 (_NORON, None), 157 (_DISPON, None), 158 (_MADCTL, b"\xc0"), # Set rotation to 0 and use RGB 159 ): 160 self.write(command, data)