|
| 1 | +# SPDX-FileCopyrightText: 2019 Scott Shawcroft for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`adafruit_displayio_ssd1306` |
| 7 | +================================================================================ |
| 8 | +
|
| 9 | +DisplayIO driver for SSD1306 monochrome displays |
| 10 | +
|
| 11 | +
|
| 12 | +* Author(s): Scott Shawcroft |
| 13 | +
|
| 14 | +Implementation Notes |
| 15 | +-------------------- |
| 16 | +
|
| 17 | +**Hardware:** |
| 18 | +
|
| 19 | +* `Monochrome 1.3" 128x64 OLED graphic display <https://www.adafruit.com/product/938>`_ |
| 20 | +* `Monochrome 128x32 I2C OLED graphic display <https://www.adafruit.com/product/931>`_ |
| 21 | +* `Monochrome 0.96" 128x64 OLED graphic display <https://www.adafruit.com/product/326>`_ |
| 22 | +* `Monochrome 128x32 SPI OLED graphic display <https://www.adafruit.com/product/661>`_ |
| 23 | +* `Adafruit FeatherWing OLED - 128x32 OLED <https://www.adafruit.com/product/2900>`_ |
| 24 | +* Monochrome 0.49" 64x32 I2C OLED graphic display |
| 25 | +* Monochrome 0.66" 64x48 I2C OLED graphic display (eg https://www.amazon.com/gp/product/B07QF7QK6P) |
| 26 | +* Might work on other sub-128 width display: Dots 72x40, 64x48, 96x16 |
| 27 | +
|
| 28 | +**Software and Dependencies:** |
| 29 | +
|
| 30 | +* Adafruit CircuitPython (version 5+) firmware for the supported boards: |
| 31 | + https://github.com/adafruit/circuitpython/releases |
| 32 | +
|
| 33 | +""" |
| 34 | + |
| 35 | +import busdisplay |
| 36 | +try: |
| 37 | + from typing import Union |
| 38 | + import fourwire |
| 39 | + import i2cdisplaybus |
| 40 | +except ImportError: |
| 41 | + pass |
| 42 | + |
| 43 | +__version__ = "0.0.0+auto.0" |
| 44 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_SSD1306.git" |
| 45 | + |
| 46 | +# Sequence from page 19 here: https://cdn-shop.adafruit.com/datasheets/UG-2864HSWEG01+user+guide.pdf |
| 47 | +_INIT_SEQUENCE = ( |
| 48 | + b"\xAE\x00" # DISPLAY_OFF |
| 49 | + b"\x20\x01\x00" # Set memory addressing to horizontal mode. |
| 50 | + b"\x81\x01\xcf" # set contrast control |
| 51 | + b"\xA1\x00" # Column 127 is segment 0 |
| 52 | + b"\xA6\x00" # Normal display |
| 53 | + b"\xc8\x00" # Normal display |
| 54 | + b"\xA8\x01\x3f" # Mux ratio is 1/64 |
| 55 | + b"\xd5\x01\x80" # Set divide ratio |
| 56 | + b"\xd9\x01\xf1" # Set pre-charge period |
| 57 | + b"\xda\x01\x12" # Set com configuration |
| 58 | + b"\xdb\x01\x40" # Set vcom configuration |
| 59 | + b"\x8d\x01\x14" # Enable charge pump |
| 60 | + b"\xAF\x00" # DISPLAY_ON |
| 61 | +) |
| 62 | + |
| 63 | + |
| 64 | +class SSD1306(busdisplay.BusDisplay): |
| 65 | + """ |
| 66 | + SSD1306 driver |
| 67 | +
|
| 68 | + :param int width: The width of the display |
| 69 | + :param int height: The height of the display |
| 70 | + :param int rotation: The rotation of the display in degrees. Default is 0. Must be one of |
| 71 | + (0, 90, 180, 270) |
| 72 | + """ |
| 73 | + |
| 74 | + def __init__( |
| 75 | + self, bus: Union[fourwire.FourWire, i2cdisplaybus.I2CDisplayBus], **kwargs |
| 76 | + ) -> None: |
| 77 | + # Patch the init sequence for 32 pixel high displays. |
| 78 | + init_sequence = bytearray(_INIT_SEQUENCE) |
| 79 | + height = kwargs["height"] |
| 80 | + width = kwargs["width"] |
| 81 | + if "rotation" in kwargs and kwargs["rotation"] % 180 != 0: |
| 82 | + height = kwargs["width"] |
| 83 | + width = kwargs["height"] |
| 84 | + init_sequence[16] = height - 1 # patch mux ratio |
| 85 | + if height == 32 and width == 64: # Make sure this only apply to that resolution |
| 86 | + init_sequence[16] = 64 - 1 # FORCED for 64x32 because it fail with formula |
| 87 | + if height in (32, 16) and width != 64: |
| 88 | + init_sequence[25] = 0x02 # patch com configuration |
| 89 | + col_offset = ( |
| 90 | + 0 if width == 128 else (128 - width) // 2 |
| 91 | + ) # https://github.com/micropython/micropython/pull/7411 |
| 92 | + row_offset = ( |
| 93 | + col_offset if (kwargs["height"] != 48 or kwargs["width"] != 64) else 0 |
| 94 | + ) # fix for 0.66" 64x48 OLED |
| 95 | + super().__init__( |
| 96 | + bus, |
| 97 | + init_sequence, |
| 98 | + **kwargs, |
| 99 | + colstart=col_offset, |
| 100 | + rowstart=row_offset, |
| 101 | + color_depth=1, |
| 102 | + grayscale=True, |
| 103 | + pixels_in_byte_share_row=False, |
| 104 | + set_column_command=0x21, |
| 105 | + set_row_command=0x22, |
| 106 | + data_as_commands=True, |
| 107 | + brightness_command=0x81, |
| 108 | + single_byte_bounds=True, |
| 109 | + ) |
| 110 | + self._is_awake = True # Display starts in active state (_INIT_SEQUENCE) |
| 111 | + |
| 112 | + @property |
| 113 | + def is_awake(self) -> bool: |
| 114 | + """ |
| 115 | + The power state of the display. (read-only) |
| 116 | +
|
| 117 | + `True` if the display is active, `False` if in sleep mode. |
| 118 | +
|
| 119 | + :type: bool |
| 120 | + """ |
| 121 | + return self._is_awake |
| 122 | + |
| 123 | + def sleep(self) -> None: |
| 124 | + """ |
| 125 | + Put display into sleep mode. |
| 126 | +
|
| 127 | + Display uses < 10uA in sleep mode. Display remembers display data and operation mode |
| 128 | + active prior to sleeping. MP can access (update) the built-in display RAM. |
| 129 | + """ |
| 130 | + if self._is_awake: |
| 131 | + self.bus.send(0xAE, b"") # 0xAE = display off, sleep mode |
| 132 | + self._is_awake = False |
| 133 | + |
| 134 | + def wake(self) -> None: |
| 135 | + """ |
| 136 | + Wake display from sleep mode |
| 137 | + """ |
| 138 | + if not self._is_awake: |
| 139 | + self.bus.send(0xAF, b"") # 0xAF = display on |
| 140 | + self._is_awake = True |
0 commit comments