/ adafruit_rgb_display / ssd1331.py
ssd1331.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.ssd1331` 24 ==================================================== 25 26 A simple driver for the SSD1331-based displays. 27 28 * Author(s): Radomir Dopieralski, Michael McWethy 29 """ 30 31 from micropython import const 32 from adafruit_rgb_display.rgb import DisplaySPI 33 34 __version__ = "0.0.0-auto.0" 35 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display.git" 36 37 38 _DRAWLINE = const(0x21) 39 _DRAWRECT = const(0x22) 40 _FILL = const(0x26) 41 _PHASEPERIOD = const(0x12) 42 _SETCOLUMN = const(0x15) 43 _SETROW = const(0x75) 44 _CONTRASTA = const(0x81) 45 _CONTRASTB = const(0x82) 46 _CONTRASTC = const(0x83) 47 _MASTERCURRENT = const(0x87) 48 _SETREMAP = const(0xA0) 49 _STARTLINE = const(0xA1) 50 _DISPLAYOFFSET = const(0xA2) 51 _NORMALDISPLAY = const(0xA4) 52 _DISPLAYALLON = const(0xA5) 53 _DISPLAYALLOFF = const(0xA6) 54 _INVERTDISPLAY = const(0xA7) 55 _SETMULTIPLEX = const(0xA8) 56 _SETMASTER = const(0xAD) 57 _DISPLAYOFF = const(0xAE) 58 _DISPLAYON = const(0xAF) 59 _POWERMODE = const(0xB0) 60 _PRECHARGE = const(0xB1) 61 _CLOCKDIV = const(0xB3) 62 _PRECHARGEA = const(0x8A) 63 _PRECHARGEB = const(0x8B) 64 _PRECHARGEC = const(0x8C) 65 _PRECHARGELEVEL = const(0xBB) 66 _VCOMH = const(0xBE) 67 _LOCK = const(0xFD) 68 69 70 class SSD1331(DisplaySPI): 71 """ 72 A simple driver for the SSD1331-based displays. 73 74 .. code-block:: python 75 76 import busio 77 import digitalio 78 import board 79 from adafruit_rgb_display import color565 80 import adafruit_rgb_display.ssd1331 as ssd1331 81 spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO) 82 display = ssd1331.SSD1331(spi, cs=digitalio.DigitalInOut(board.GPIO0), 83 dc=digitalio.DigitalInOut(board.GPIO15), 84 rst=digitalio.DigitalInOut(board.GPIO16)) 85 86 display.fill(0x7521) 87 display.pixel(32, 32, 0) 88 89 """ 90 91 _COLUMN_SET = _SETCOLUMN 92 _PAGE_SET = _SETROW 93 _RAM_WRITE = None 94 _RAM_READ = None 95 _INIT = ( 96 (_DISPLAYOFF, b""), 97 (_LOCK, b"\x0b"), 98 (_SETREMAP, b"\x72"), # RGB Color 99 (_STARTLINE, b"\x00"), 100 (_DISPLAYOFFSET, b"\x00"), 101 (_NORMALDISPLAY, b""), 102 # (_FILL, b'\x01'), 103 (_PHASEPERIOD, b"\x31"), 104 (_SETMULTIPLEX, b"\x3f"), 105 (_SETMASTER, b"\x8e"), 106 (_POWERMODE, b"\x0b"), 107 (_PRECHARGE, b"\x31"), # ;//0x1F - 0x31 108 (_CLOCKDIV, b"\xf0"), 109 (_VCOMH, b"\x3e"), # ;//0x3E - 0x3F 110 (_MASTERCURRENT, b"\x0c"), # ;//0x06 - 0x0F 111 (_PRECHARGEA, b"\x64"), 112 (_PRECHARGEB, b"\x78"), 113 (_PRECHARGEC, b"\x64"), 114 (_PRECHARGELEVEL, b"\x3a"), # 0x3A - 0x00 115 (_CONTRASTA, b"\x91"), # //0xEF - 0x91 116 (_CONTRASTB, b"\x50"), # ;//0x11 - 0x50 117 (_CONTRASTC, b"\x7d"), # ;//0x48 - 0x7D 118 (_DISPLAYON, b""), 119 ) 120 _ENCODE_PIXEL = ">H" 121 _ENCODE_POS = ">BB" 122 123 # pylint: disable-msg=useless-super-delegation, too-many-arguments 124 # super required to allow override of default values 125 def __init__( 126 self, 127 spi, 128 dc, 129 cs, 130 rst=None, 131 width=96, 132 height=64, 133 baudrate=16000000, 134 polarity=0, 135 phase=0, 136 *, 137 rotation=0 138 ): 139 super().__init__( 140 spi, 141 dc, 142 cs, 143 rst, 144 width, 145 height, 146 baudrate=baudrate, 147 polarity=polarity, 148 phase=phase, 149 rotation=rotation, 150 ) 151 152 # pylint: disable=no-member 153 def write(self, command=None, data=None): 154 """write procedure specific to SSD1331""" 155 self.dc_pin.value = command is None 156 with self.spi_device as spi: 157 if command is not None: 158 spi.write(bytearray([command])) 159 print(bytearray([command])) 160 if data is not None: 161 spi.write(data) 162 print(data)