|
| 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 | + _COLUMN_SET = _CASET |
| 104 | + _PAGE_SET = _RASET |
| 105 | + _RAM_WRITE = _RAMWR |
| 106 | + _RAM_READ = _RAMRD |
| 107 | + _Y_START = 80 |
| 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__(self, spi, dc, cs, rst=None, width=240, height=240, |
| 117 | + baudrate=16000000, polarity=0, phase=0): |
| 118 | + super().__init__(spi, dc, cs, rst, width, height, |
| 119 | + baudrate=baudrate, polarity=polarity, phase=phase) |
| 120 | + |
| 121 | + def init(self): |
| 122 | + super().init() |
| 123 | + cols = struct.pack('>HH', 0, self.width) |
| 124 | + rows = struct.pack('>HH', self._Y_START, self.height + self._Y_START) |
| 125 | + for command, data in ( |
| 126 | + (_CASET, cols), |
| 127 | + (_RASET, rows), |
| 128 | + (_INVON, None), |
| 129 | + (_NORON, None), |
| 130 | + (_DISPON, None), |
| 131 | + (_MADCTL, b'\xc0'), #Set rotation to 0 and use RGB |
| 132 | + ): |
| 133 | + self.write(command, data) |
0 commit comments