/ adafruit_displayio_ssd1306.py
adafruit_displayio_ssd1306.py
 1  # The MIT License (MIT)
 2  #
 3  # Copyright (c) 2019 Scott Shawcroft 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_displayio_ssd1306`
24  ================================================================================
25  
26  DisplayIO driver for SSD1306 monochrome displays
27  
28  
29  * Author(s): Scott Shawcroft
30  
31  Implementation Notes
32  --------------------
33  
34  **Hardware:**
35  
36  * `Monochrome 1.3" 128x64 OLED graphic display <https://www.adafruit.com/product/938>`_
37  * `Monochrome 128x32 I2C OLED graphic display  <https://www.adafruit.com/product/931>`_
38  * `Monochrome 0.96" 128x64 OLED graphic display <https://www.adafruit.com/product/326>`_
39  * `Monochrome 128x32 SPI OLED graphic display <https://www.adafruit.com/product/661>`_
40  * `Adafruit FeatherWing OLED - 128x32 OLED <https://www.adafruit.com/product/2900>`_
41  
42  **Software and Dependencies:**
43  
44  * Adafruit CircuitPython (version 5+) firmware for the supported boards:
45    https://github.com/adafruit/circuitpython/releases
46  
47  """
48  
49  import displayio
50  
51  __version__ = "0.0.0-auto.0"
52  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_SSD1306.git"
53  
54  # Sequence from page 19 here: https://cdn-shop.adafruit.com/datasheets/UG-2864HSWEG01+user+guide.pdf
55  _INIT_SEQUENCE = (
56      b"\xAE\x00"  # DISPLAY_OFF
57      b"\x20\x01\x00"  # Set memory addressing to horizontal mode.
58      b"\x81\x01\xcf"  # set contrast control
59      b"\xA1\x00"  # Column 127 is segment 0
60      b"\xA6\x00"  # Normal display
61      b"\xc8\x00"  # Normal display
62      b"\xA8\x01\x3f"  # Mux ratio is 1/64
63      b"\xd5\x01\x80"  # Set divide ratio
64      b"\xd9\x01\xf1"  # Set pre-charge period
65      b"\xda\x01\x12"  # Set com configuration
66      b"\xdb\x01\x40"  # Set vcom configuration
67      b"\x8d\x01\x14"  # Enable charge pump
68      b"\xAF\x00\x00"  # DISPLAY_ON
69  )
70  
71  # pylint: disable=too-few-public-methods
72  class SSD1306(displayio.Display):
73      """SSD1306 driver"""
74  
75      def __init__(self, bus, **kwargs):
76          # Patch the init sequence for 32 pixel high displays.
77          init_sequence = bytearray(_INIT_SEQUENCE)
78          height = kwargs["height"]
79          if "rotation" in kwargs and kwargs["rotation"] % 180 != 0:
80              height = kwargs["width"]
81          init_sequence[16] = height - 1  # patch mux ratio
82          if kwargs["height"] == 32:
83              init_sequence[25] = 0x02  # patch com configuration
84          super().__init__(
85              bus,
86              init_sequence,
87              **kwargs,
88              color_depth=1,
89              grayscale=True,
90              pixels_in_byte_share_row=False,
91              set_column_command=0x21,
92              set_row_command=0x22,
93              data_as_commands=True,
94              set_vertical_scroll=0xD3,
95              brightness_command=0x81,
96              single_byte_bounds=True,
97          )