/ adafruit_ssd1322.py
adafruit_ssd1322.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_ssd1322`
 24  ================================================================================
 25  
 26  DisplayIO driver for grayscale OLEDs driven by SSD1322
 27  
 28  
 29  * Author(s): Scott Shawcroft
 30  
 31  Implementation Notes
 32  --------------------
 33  
 34  **Hardware:**
 35  
 36  * 3.12" Newhaven Display 256x64 Grayscale Blue OLED:
 37      https://www.newhavendisplay.com/nhd31225664ucb2-p-3622.html
 38  
 39  **Software and Dependencies:**
 40  
 41  * Adafruit CircuitPython 5+ firmware for the supported boards:
 42    https://github.com/adafruit/circuitpython/releases
 43  
 44  """
 45  
 46  import displayio
 47  
 48  __version__ = "0.0.0-auto.0"
 49  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SSD1322.git"
 50  
 51  _INIT_SEQUENCE = (
 52      b"\xfd\x01\x12"  # Set_Command_Lock(0x12);// Unlock Basic Commands (0x12/0x16)
 53      b"\xae\x00"  # Set_Display_On_Off(0x00);// Display Off (0x00/0x01)
 54      b"\xb3\x01\x91"  # Set_Display_Clock(0x91);// Set Clock as 80 Frames/Sec
 55      b"\xca\x01\x3f"  # Set_Multiplex_Ratio(0x3F);// 1/64 Duty (0x0F~0x3F)
 56      b"\xa2\x01\x00"  # Set_Display_Offset(0x00);// Shift Mapping RAM Counter (0x00~0x3F)
 57      b"\xa1\x01\x00"  # Set_Start_Line(0x00);// Set Mapping RAM Display Start Line (0x00~0x7F)
 58      b"\xa0\x02\x14\x11"  # Set_Remap_Format(0x14);// Set Horizontal Address Increment
 59      # //     Column Address 0 Mapped to SEG0
 60      # //     Disable Nibble Remap
 61      # //     Scan from COM[N-1] to COM0
 62      # //     Disable COM Split Odd Even
 63      # //     Enable Dual COM Line Mode
 64      b"\xb5\x01\x00"  # Set_GPIO(0x00);// Disable GPIO Pins Input
 65      b"\xab\x01\x01"  # Set_Function_Selection(0x01);// Enable Internal VDD Regulator
 66      b"\xb4\x02\xa0\xfd"  # Set_Display_Enhancement_A(0xA0,0xFD);// Enable External VSL
 67      b"\xc1\x01\x9f"  # Set_Contrast_Current(0x9F); // Set Segment Output Current
 68      b"\xc7\x01\x0f"  # Set_Master_Current(0x0F);//Set Scale Factor of Segment Output Current Control
 69      b"\xb8\x0f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x10\x40\x90\xa0\xb0\xb4"  # Set graytable
 70      # b"\xb9\x00" # Set_Linear_Gray_Scale_Table();//set default linear gray scale table
 71      b"\xb1\x01\xe2"  # Set_Phase_Length(0xE2);// Set Phase 1 as 5 Clocks & Phase 2 as 14 Clocks
 72      # Set_Display_Enhancement_B(0x20);// Enhance Driving Scheme Capability (0x00/0x20)
 73      b"\xd1\x02\xa2\x20"
 74      b"\xbb\x01\x1f"  # Set_Precharge_Voltage(0x1F);// Set Pre-Charge Voltage Level as 0.60*VCC
 75      b"\xb6\x01\x08"  # Set_Precharge_Period(0x08);// Set Second Pre-Charge Period as 8 Clocks
 76      b"\xbe\x01\x07"  # Set_VCOMH(0x07);// Set Common Pins Deselect Voltage Level as 0.86*VCC
 77      b"\xa6\x00"  # Set_Display_Mode(0x02);// Normal Display Mode (0x00/0x01/0x02/0x03)
 78      b"\xa9\x00"  # Set_Partial_Display(0x01,0x00,0x00);// Disable Partial Display
 79      b"\xaf\x00"  # Set_Display_On_Off(0x01);
 80  )
 81  
 82  # pylint: disable=too-few-public-methods
 83  class SSD1322(displayio.Display):
 84      """SSD1322 driver"""
 85  
 86      def __init__(self, bus, **kwargs):
 87          # Patch the init sequence for 32 pixel high displays.
 88          init_sequence = bytearray(_INIT_SEQUENCE)
 89          height = kwargs["height"]
 90          if "rotation" in kwargs and kwargs["rotation"] % 180 != 0:
 91              height = kwargs["width"]
 92          init_sequence[10] = height - 1  # patch mux ratio
 93          super().__init__(
 94              bus,
 95              _INIT_SEQUENCE,
 96              **kwargs,
 97              color_depth=4,
 98              grayscale=True,
 99              set_column_command=0x15,
100              set_row_command=0x75,
101              set_vertical_scroll=0xD3,
102              write_ram_command=0x5C,
103              single_byte_bounds=True,
104              reverse_pixels_in_byte=True,
105              bytes_per_cell=2,
106          )