• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    Python
  • License
    Other
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MicroPython driver for the SH1106 OLED controller

Driver for the SH1106 display

This driver consists mostly of the work of Radomir Dopieralski (@deshipu). I added a few functions and changed the existing ones so it matches better my needs for a project.

A modified version of this driver compatible with nano-gui widget library is hosted in that project.

Features

Use OLED display with the SH1106 driver with SPI or I2C. It is based on the MicroPython framebuffer class and consists wrappers for this class as well as special methods for controlling the display.

Content Rotation

The rotate parameter in the constructor allows you to rotate the display by a 90, 180 or 270 degrees clockwise. 180 degrees are easy, because this can be done using only hardware flags of the SH1106 display. 90 and 270 degrees however are not. These come at a price: Since we will have to it in software, a second, internal framebuffer will be created, using an additional width * height / 8 bytes of RAM. Also, each call to show() will take about 33% longer.

Set width and height in the constructor to the physical dimensions of your display, regardless of how you would like to rotate it.

You can use the flip() method to toggle between 0 and 180 degrees of rotation, or between 90 and 270 degrees, at runtime, which is equivalent to rotating the contents for 180 degrees compared to whatever the rotation was before. It is however not possible to switch from "portrait" to "landscape" or vice versa at runtime, because of the additional buffer required.

Connection

The SH1106 supports next to thers the I2C or SPI interface. The connection depends on the interface used and the number of devices in the system. Especially the ESP8266 with their small number of GPIO ports may require optimization.

I2C

SCL and SDA have to be connected as minimum. The driver also resets the device by the reset PIN. If your are low on GPIO ports, reset can be applied by a dedicated circuit, like the MCP100-300.

SPI

SCLK, MOSI, D/C are always required. If the display is the only SPI device in the set-up, CS may be tied to GND. Reset has also to be connected, unless it is driven by an external circuit.

Class

The driver contains the SH1106 class and the derived SH1106_I2C and SH1106_SPI classes. Besides the constructors, the methods are the same.

I2C

display = sh1106.SH1106_I2C(width, height, i2c, reset, address, rotate=0, delay=0)
  • width and height define the size of the display
  • i2c is an I2C object, which has to be created beforehand and tells the ports for SDA and SCL.
  • res is the GPIO Pin object for the reset connection. It will be initialized by the driver. If it is not needed, None has to be supplied.
  • adr is the I2C address of the display. Default 0x3c or 60
  • rotate defines display content rotation. See above for details and caveats.
  • delay specifies an optional delay during poweron. The quantity is ms.

SPI

display = sh1106.SH1106_SPI(width, height, spi, dc, res, cs, rotate=0, delay=0)
  • width and height define the size of the display
  • spi is an SPI object, which has to be created beforehand and tells the ports for SCLJ and MOSI. MISO is not used.
  • dc is the GPIO Pin object for the Data/Command selection. It will be initialized by the driver.
  • res is the GPIO Pin object for the reset connection. It will be initialized by the driver. If it is not needed, it can be set to None or omitted. In this case the default value of None applies.
  • cs is the GPIO Pin object for the CS connection. It will be initialized by the driver. If it is not needed, it can be set to None or omitted. In this case the default value of None applies.
  • rotate defines display content rotation. See above for details and caveats.
  • delay specifies an optional delay during poweron. The quantity is ms.

Methods

display.init_display()

display.init_display()

Initializes the display, fills it with the color 0 and displays the empty screen. It also tries to apply the reset signal, if it is connected ( = not None).

display.power_on() and display.power_off()

display.poweron()
display.poweroff()
display.sleep(state)

Enable and disable the display. display.sleep(True) is identical to display.poweroff(), display.sleep(False) is equivalent to display.poweron(). Other than the literal meaning could tell, it does not switch the power line(Vcc) of the display.

display.contrast()

display.contrast(level)

Set the display's contrast to the given level. The range is 0..255. For a single color display like the SH1106, this is actually the brightness.

display.invert()

display.invert(flag)

Invert the content of the display, depending on the value of Flag. This is immediately effective for the whole display.

  • flag = True Invert
  • flag = False Normal mode

display.flip()

display.flip([flag=None[, update=True]])

Rotate the content of the display an additional 180 degrees, depending on the value of flag.

  • 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.
  • 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.
  • None: Toggle flip on or off: 0 degrees will become 180, 90 will become 270, 180 will become 0 and 270 will become 90.

To become fully effective, you have to run display.show(). If the parameter update is True, show() is called by the function itself.

display.show()

Display the content of the frame buffer on the display.

display.show()

The usual program flow would set-up/update the frame buffer content with a sequence of calls an the call display.show() for the content to be shown (see examples below).

Framebuffer Methods

The below listed display methods of the framebuffer class are mirrored in this class. For a documentation, please look into the MicroPython documentation at http://docs.micropython.org/en/latest/pyboard/library/framebuf.html?highlight=framebuf#module-framebuf:

  • fill
  • fill_rect
  • line
  • vline
  • hline
  • rect
  • pixel
  • scroll
  • text
  • blit

The text is displayed with the built-in 8x8 pixel font, which support the ASCII character set values 32..127. The text overlays the previous content; 'on' pixels in a character will not overwrite existing 'off' pixels. If you want to rewrite an area of the screen, you have to clear it beforehand, e.g. with the fill_rect() method.

Remark: If you want to use other font styles and sizes, have a look at the work of Peter Hinch (@pythoncoder) at https://github.com/peterhinch/micropython-font-to-py

display.reset()

display.reset()

Attempt to reset the display by toggling the reset line. This is obviously only effective is reset is connected. Otherwise it's a No-Op.

Sample Code

SPI

# MicroPython SH1106 OLED driver
#
# Pin Map SPI for ESP8266
#   - 3v - xxxxxx   - Vcc
#   - G  - xxxxxx   - Gnd
#   - D7 - GPIO 13  - Din / MOSI fixed
#   - D5 - GPIO 14  - Clk / SCLK fixed
#   - D8 - GPIO 4   - CS (optional, if the only connected device)
#   - D2 - GPIO 5   - D/C
#   - D1 - GPIO 2   - Res (required, unless a Hardware reset circuit is connected)
#
# for CS, D/C and Res other ports may be chosen.
#
from machine import Pin, SPI
import sh1106

spi = SPI(1, baudrate=1000000)
display = sh1106.SH1106_SPI(128, 64, spi, Pin(5), Pin(2), Pin(4))
display.sleep(False)
display.fill(0)
display.text('Testing 1', 0, 0, 1)
display.show()

I2C

# MicroPython SH1106 OLED driver
#
# Pin Map I2C for ESP8266
#   - 3v - xxxxxx   - Vcc
#   - G  - xxxxxx   - Gnd
#   - D2 - GPIO 5   - SCK / SCL
#   - D1 - GPIO 4   - DIN / SDA
#   - D0 - GPIO 16  - Res (required, unless a Hardware reset circuit is connected)
#   - G  - xxxxxx     CS
#   - G  - xxxxxx     D/C
#
# Pin's for I2C can be set almost arbitrary
#
from machine import Pin, I2C
import sh1106

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
display = sh1106.SH1106_I2C(128, 64, i2c, Pin(16), 0x3c)
display.sleep(False)
display.fill(0)
display.text('Testing 1', 0, 0, 1)
display.show()

More Repositories

1

Micropython-Editor

Small on-board editor for PyBoard, WiPy, ESP8266, ESP32, RP2040, W60x, XBEE 3, Sipeed K210, as well as PyCom and Adafruit devices written in Python
Python
217
star
2

FTP-Server-for-ESP8266-ESP32-and-PYBD

Small FTP server for ESP8266/ESP32/PYBD on the MicroPython platform
Python
148
star
3

BME280

Micropython driver for the BME280 sensor, target platform Pycom devices
Python
100
star
4

ads1x15

Micropython driver for ADS1115 and ADS1015
Python
90
star
5

Shared-Stuff

Various files for MicroPython devices
Python
47
star
6

BME680-Micropython

Micropython driver for the BME680 sensor
Python
32
star
7

SSD1963-TFT-Library-for-PyBoard-and-RP2040

SSD1963 TFT Library for PyBoard and Raspberry Pi PICO
Python
29
star
8

RP2040-Examples

Example code for the RP2040 using MicroPython
Python
28
star
9

BMP085_BMP180

Micropython module for the BMP085 and BMP180 sensors
Python
18
star
10

XPT2046-touch-pad-driver

A driver for the XPT2046 touch pad controller used in many TFT modules
Python
16
star
11

QMC5883

Python class for the QMC5883 Three-Axis Digital Compass IC
Python
12
star
12

Onewire_DS18X20

Classes for driving the DS18x20 sensor with the onewire protocol for Pycom MicroPython
Python
12
star
13

K210-Stuff

Various file for the Sipeed MaixPy K210
C
12
star
14

ATTiny-Watchdog-for-MicroPython

ATTinyx5 based watchdog timer e.g. for MicroPython devices
C++
10
star
15

SI7021

SI7021: Simple class for the SI7021 temperature and relative humidity sensor
Python
9
star
16

INA219

INA219 Micropython driver
Python
9
star
17

owonread

Get screen shots and track data data from Owon TDS series Oscilloscopes with Linux
Python
7
star
18

WS2812

WS2812 NeoPixel driver for Pycom devices
Python
5
star
19

WM_SDK_W60X

WinnerMicro SDK for the W60x chips. Based on Version G3.04.00 with a few modifications and extensions
C
5
star
20

PYBD-Adapter

Breadboard adapter for the 1/20" pitch side connections
3
star
21

SPI_Flash

A small block device driver for using SPI flash with a VFS file system.
Python
3
star
22

MCP3201

Python class interfacing the MCP3201 AD-converter
Python
3
star
23

SHT30

MicroPython driver for the SHT3x sensor.
Python
2
star
24

wm8960

Python driver for the WM8960 codec.
Python
1
star
25

Candle

Just another candle simulation
C
1
star
26

DLV-pressure-sensor

Python
1
star