{"id":13613350,"url":"https://github.com/robert-hh/SH1106","last_synced_at":"2025-04-13T15:33:04.916Z","repository":{"id":18654469,"uuid":"84852060","full_name":"robert-hh/SH1106","owner":"robert-hh","description":"MicroPython driver for the SH1106 OLED controller","archived":false,"fork":false,"pushed_at":"2025-03-16T12:45:49.000Z","size":36,"stargazers_count":181,"open_issues_count":4,"forks_count":44,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-16T13:35:24.075Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robert-hh.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-13T16:55:21.000Z","updated_at":"2025-03-16T12:45:53.000Z","dependencies_parsed_at":"2025-03-16T13:30:37.763Z","dependency_job_id":"06177746-cea1-4ffe-8016-914ab2d6ee75","html_url":"https://github.com/robert-hh/SH1106","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FSH1106","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FSH1106/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FSH1106/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robert-hh%2FSH1106/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robert-hh","download_url":"https://codeload.github.com/robert-hh/SH1106/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248736128,"owners_count":21153537,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T20:00:44.869Z","updated_at":"2025-04-13T15:33:04.905Z","avatar_url":"https://github.com/robert-hh.png","language":"Python","funding_links":[],"categories":["Libraries","精选驱动库"],"sub_categories":["Display","显示类"],"readme":"# Driver for the SH1106 display\n\nThis driver consists mostly of the work of Radomir Dopieralski (@deshipu).\nI added a few functions and changed the existing ones so it matches better\nmy needs for a project.\n\nA modified version of this driver compatible with [nano-gui widget library](https://github.com/peterhinch/micropython-nano-gui) is hosted in that project.\n\n## Features\n\nUse OLED display with the SH1106 driver with SPI or I2C. It is based on the MicroPython\nframebuffer class and consists wrappers for this class as well as special methods\nfor controlling the display.\n\n## Type Stubs for IDE Support\nThis library now includes `.pyi` type stubs for better IDE integration. To enable autocomplete and type checking in VS Code or other editors:\n- Ensure the `.pyi` files are in your project directory or accessible in your Python path.\n- If needed, add the directory to your IDE's settings (e.g., `python.analysis.extraPaths` in VS Code).\n- If you are using a virtual environment, ensure the stubs are installed in the virtual environment.\n\n### Content Rotation\n\nThe `rotate` parameter in the constructor allows you to rotate the display by a 90, 180 or 270\ndegrees clockwise. 180 degrees are easy, because this can be done using only hardware flags of the\nSH1106 display. 90 and 270 degrees however are not. These come at a price: Since we will have to it\nin software, a second, internal framebuffer will be created, using an additional\n`width * height / 8` bytes of RAM. Also, each call to `show()` will take about 33% longer.\n\nSet `width` and `height` in the constructor to the _physical_ dimensions of your display, regardless\nof how you would like to rotate it.\n\nYou can use the `flip()` method to toggle between 0 and 180 degrees of rotation, or between 90 and\n270 degrees, at runtime, which is equivalent to rotating the contents for 180 degrees compared to\nwhatever the rotation was before. It is however not possible to switch from \"portrait\" to\n\"landscape\" or vice versa at runtime, because of the additional buffer required.\n\n## Connection\n\nThe SH1106 supports next to thers the I2C or SPI interface. The connection depends on the interface used\nand the number of devices in the system. Especially the ESP8266 with their small\nnumber of GPIO ports may require optimization.\n\n### I2C\nSCL and SDA have to be connected as minimum. The driver also resets the device by the reset PIN.\nIf your are low on GPIO ports, reset can be applied by a dedicated circuit, like the MCP100-300.\n\n### SPI\nSCLK, MOSI, D/C are always required. If the display is the only SPI device in the set-up,\nCS may be tied to GND. Reset has also to be connected, unless it is driven\nby an external circuit.\n\n\n## Class\n\nThe driver contains the SH1106 class and the derived SH1106_I2C and SH1106_SPI classes.\nBesides the constructors, the methods are the same.\n\n### I2C\n```\ndisplay = sh1106.SH1106_I2C(width, height, i2c, reset, address, rotate=0, delay=0)\n```\n- width and height define the size of the display\n- i2c is an I2C object, which has to be created beforehand and tells the ports for SDA and SCL.\n- res is the GPIO Pin object for the reset connection. It will be initialized by the driver.\nIf it is not needed, `None` has to be supplied.\n- adr is the I2C address of the display. Default 0x3c or 60\n- rotate defines display content rotation. See above for details and caveats.\n- delay specifies an optional delay during poweron. The quantity is ms.\n\n\n### SPI\n```\ndisplay = sh1106.SH1106_SPI(width, height, spi, dc, res, cs, rotate=0, delay=0)\n```\n- width and height define the size of the display\n- spi is an SPI object, which has to be created beforehand and tells the ports for SCLJ and MOSI.\nMISO is not used.\n- dc is the GPIO Pin object for the Data/Command selection. It will be initialized by the driver.\n- res is the GPIO Pin object for the reset connection. It will be initialized by the driver.\nIf it is not needed, it can be set to `None` or omitted. In this case the default value\nof `None` applies.\n- cs is the GPIO Pin object for the CS connection. It will be initialized by the driver.\nIf it is not needed, it can be set to `None` or omitted. In this case the default value\nof `None` applies.\n- rotate defines display content rotation. See above for details and caveats.\n- delay specifies an optional delay during poweron. The quantity is ms.\n\n\n## Methods\n\n### display.init_display()\n```\ndisplay.init_display()\n```\nInitializes the display, fills it with the color 0 and displays the empty screen. It also tries\nto apply the reset signal, if it is connected ( = not `None`).\n\n### display.power_on() and display.power_off()\n\n```\ndisplay.poweron()\ndisplay.poweroff()\ndisplay.sleep(state)\n```\nEnable and disable the display. `display.sleep(True)` is identical to `display.poweroff()`,\n`display.sleep(False)` is equivalent to `display.poweron()`.\nOther than the literal meaning could tell, it does not switch the power line(Vcc)\nof the display.\n\n###  display.contrast()\n\n```\ndisplay.contrast(level)\n```\nSet the display's contrast to the given level. The range is 0..255. For a single color\ndisplay like the SH1106, this is actually the brightness.\n\n###  display.invert()\n```\ndisplay.invert(flag)\n```\nInvert the content of the display, depending on the value of Flag. This is immediately\neffective for the whole display.\n- flag = True  Invert\n- flag = False Normal mode\n\n###  display.flip()\n```\ndisplay.flip([flag=None[, update=True]])\n```\nRotate the content of the display an additional 180 degrees, depending on the value of `flag`.\n\n- `True`: If you selected 0 or 90 degrees of rotation in the constructor, rotation will be set to 180 or 270, respectively. Else, it has no effect.\n- `False`: If you selected 180 or 270 degrees of rotation in the constructor, rotation will be set to 0 or 90, respectively. Else, it has no effect.\n- `None`: Toggle flip on or off: 0 degrees will become 180, 90 will become 270, 180 will become 0 and 270 will become 90.\n\nTo become fully effective, you have to run `display.show()`. If the parameter `update` is `True`,\n`show()` is called by the function itself.\n\n###  display.show()\n\nDisplay the content of the frame buffer on the display.\n```\ndisplay.show()\n```\nThe usual program flow would set-up/update the frame buffer content with a sequence of calls\nan the call display.show() for the content to be shown (see examples below).\n\n## Framebuffer Methods\n\nThe below listed display methods of the framebuffer class are mirrored in this\nclass. For a documentation, please look into the MicroPython documentation at http://docs.micropython.org/en/latest/pyboard/library/framebuf.html?highlight=framebuf#module-framebuf:\n\n- fill\n- fill_rect\n- line\n- vline\n- hline\n- rect\n- pixel\n- scroll\n- text\n- blit\n- ellipse\n\n\nThe text is displayed with the built-in\n8x8 pixel font, which support the ASCII character set values 32..127. The text overlays\nthe previous content; 'on' pixels in a character will not overwrite existing 'off' pixels.\nIf you want to rewrite an area of the screen, you have to clear it beforehand,\ne.g. with the `fill_rect()` method.\n\nRemark: If you want to use other font styles and sizes, have a look at\nthe work of Peter Hinch (@pythoncoder) at https://github.com/peterhinch/micropython-font-to-py\n\n### display.reset()\n```\ndisplay.reset()\n```\nAttempt to reset the display by toggling the reset line. This is obviously only effective\nis reset is connected. Otherwise it's a No-Op.\n\n\n# Sample Code\n\n## SPI\n```\n# MicroPython SH1106 OLED driver\n#\n# Pin Map SPI for ESP8266\n#   - 3v - xxxxxx   - Vcc\n#   - G  - xxxxxx   - Gnd\n#   - D7 - GPIO 13  - Din / MOSI fixed\n#   - D5 - GPIO 14  - Clk / SCLK fixed\n#   - D8 - GPIO 4   - CS (optional, if the only connected device)\n#   - D2 - GPIO 5   - D/C\n#   - D1 - GPIO 2   - Res (required, unless a Hardware reset circuit is connected)\n#\n# for CS, D/C and Res other ports may be chosen.\n#\nfrom machine import Pin, SPI\nimport sh1106\n\nspi = SPI(1, baudrate=1000000)\ndisplay = sh1106.SH1106_SPI(128, 64, spi, Pin(5), Pin(2), Pin(4))\ndisplay.sleep(False)\ndisplay.fill(0)\ndisplay.text('Testing 1', 0, 0, 1)\ndisplay.show()\n```\n## I2C\n\n```\n# MicroPython SH1106 OLED driver\n#\n# Pin Map I2C for ESP8266\n#   - 3v - xxxxxx   - Vcc\n#   - G  - xxxxxx   - Gnd\n#   - D2 - GPIO 5   - SCK / SCL\n#   - D1 - GPIO 4   - DIN / SDA\n#   - D0 - GPIO 16  - Res (required, unless a Hardware reset circuit is connected)\n#   - G  - xxxxxx     CS\n#   - G  - xxxxxx     D/C\n#\n# Pin's for I2C can be set almost arbitrary\n#\nfrom machine import Pin, I2C\nimport sh1106\n\ni2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)\ndisplay = sh1106.SH1106_I2C(128, 64, i2c, Pin(16), 0x3c)\ndisplay.sleep(False)\ndisplay.fill(0)\ndisplay.text('Testing 1', 0, 0, 1)\ndisplay.show()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobert-hh%2FSH1106","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobert-hh%2FSH1106","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobert-hh%2FSH1106/lists"}