/ adafruit_rgb_display / ili9341.py
ili9341.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2017 Radomir Dopieralski and 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.ili9341`
 24  ====================================================
 25  
 26  A simple driver for the ILI9341/ILI9340-based displays.
 27  
 28  * Author(s): Radomir Dopieralski, Michael McWethy
 29  """
 30  
 31  try:
 32      import struct
 33  except ImportError:
 34      import ustruct as struct
 35  
 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  
 42  class ILI9341(DisplaySPI):
 43      """
 44      A simple driver for the ILI9341/ILI9340-based displays.
 45  
 46      >>> import busio
 47      >>> import digitalio
 48      >>> import board
 49      >>> from adafruit_rgb_display import color565
 50      >>> import adafruit_rgb_display.ili9341 as ili9341
 51      >>> spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
 52      >>> display = ili9341.ILI9341(spi, cs=digitalio.DigitalInOut(board.GPIO0),
 53      ...    dc=digitalio.DigitalInOut(board.GPIO15))
 54      >>> display.fill(color565(0xff, 0x11, 0x22))
 55      >>> display.pixel(120, 160, 0)
 56      """
 57  
 58      _COLUMN_SET = 0x2A
 59      _PAGE_SET = 0x2B
 60      _RAM_WRITE = 0x2C
 61      _RAM_READ = 0x2E
 62      _INIT = (
 63          (0xEF, b"\x03\x80\x02"),
 64          (0xCF, b"\x00\xc1\x30"),
 65          (0xED, b"\x64\x03\x12\x81"),
 66          (0xE8, b"\x85\x00\x78"),
 67          (0xCB, b"\x39\x2c\x00\x34\x02"),
 68          (0xF7, b"\x20"),
 69          (0xEA, b"\x00\x00"),
 70          (0xC0, b"\x23"),  # Power Control 1, VRH[5:0]
 71          (0xC1, b"\x10"),  # Power Control 2, SAP[2:0], BT[3:0]
 72          (0xC5, b"\x3e\x28"),  # VCM Control 1
 73          (0xC7, b"\x86"),  # VCM Control 2
 74          (0x36, b"\x48"),  # Memory Access Control
 75          (0x3A, b"\x55"),  # Pixel Format
 76          (0xB1, b"\x00\x18"),  # FRMCTR1
 77          (0xB6, b"\x08\x82\x27"),  # Display Function Control
 78          (0xF2, b"\x00"),  # 3Gamma Function Disable
 79          (0x26, b"\x01"),  # Gamma Curve Selected
 80          (
 81              0xE0,  # Set Gamma
 82              b"\x0f\x31\x2b\x0c\x0e\x08\x4e\xf1\x37\x07\x10\x03\x0e\x09\x00",
 83          ),
 84          (
 85              0xE1,  # Set Gamma
 86              b"\x00\x0e\x14\x03\x11\x07\x31\xc1\x48\x08\x0f\x0c\x31\x36\x0f",
 87          ),
 88          (0x11, None),
 89          (0x29, None),
 90      )
 91      _ENCODE_PIXEL = ">H"
 92      _ENCODE_POS = ">HH"
 93      _DECODE_PIXEL = ">BBB"
 94  
 95      # pylint: disable-msg=too-many-arguments
 96      def __init__(
 97          self,
 98          spi,
 99          dc,
100          cs,
101          rst=None,
102          width=240,
103          height=320,
104          baudrate=16000000,
105          polarity=0,
106          phase=0,
107          rotation=0,
108      ):
109          super().__init__(
110              spi,
111              dc,
112              cs,
113              rst=rst,
114              width=width,
115              height=height,
116              baudrate=baudrate,
117              polarity=polarity,
118              phase=phase,
119              rotation=rotation,
120          )
121          self._scroll = 0
122  
123      # pylint: enable-msg=too-many-arguments
124  
125      def scroll(self, dy=None):  # pylint: disable-msg=invalid-name
126          """Scroll the display by delta y"""
127          if dy is None:
128              return self._scroll
129          self._scroll = (self._scroll + dy) % self.height
130          self.write(0x37, struct.pack(">H", self._scroll))
131          return None