Skip to content

SSD1306 Module

This Module exposes all functionalities of Solomon SSD1306 OLED Display driver (datasheet).

class SSD1306

class SSD1306(drv, cs, rst, dc, clock=8000000)

Creates an intance of a new SSD1306 using SPI.

SSD1306SPI must be set to ‘true’ in the configuration yml.

  • param spidrv: SPI Bus used ‘( SPI0, … )’
  • param cs: Chip Select
  • param rst: Reset pin
  • param dc: Data/Command control pin
  • param clk: Clock speed, default 8MHz

Example:

from solomon.ssd1306 import ssd1306

...

oled = ssd1306.SSD1306SPI(SPI0,D17,D16,D6)
oled.init()
oled.on()
init

SSD1306.init(screen_width=96, screen_height=40)

Initialize the SSD1306 setting all internal registers and the display dimensions in pixels.

Arguments:

  • screen_width – width in pixels of the display (max 128); default 96
  • screen_height – height in pixels of the display (max 64); default 40
SSD1306.on

on()

Turns on the display.

SSD1306.off

off()

Turns off the display.

SSD1306.invert

invert()

Sets the display in complementary mode.

SSD1306.normal

normal()

Sets the display in normal mode.

SSD1306.set_contrast

set_contrast(contrast=0x7F)

Sets the contrast of the display.

Arguments: contrast – value of the contrast to be set (from 0 to 255), default 0x7F.

SSD1306.clear

clear()

Clears the display.

SSD1306.fill_screen

fill_screen()

Fills the entire display (white screen in normal mode).

SSD1306.fill_rect

fill_rect(x, y, w, h, fill=True)

Draws a filled rectangular area in the screen.

Arguments:

  • x – x-coordinate for left high corner of the rectangular area
  • y – y-coordinate for left high corner of the rectangular area
  • w – width of the rectangular area
  • h – height of the rectangular area
  • fill (bool) – flag for filling the rectagnular area. If True draws white area, otherwise black area (in normal mode); default True

Note

If the display is set in complementary mode (see invert() function), fill flag set to True will draw black area and set to False will draw a white area.

SSD1306.draw_img

draw_img(image, x, y, w, h, fill=True)

Draws the image passed in bytearray format as argument.

Arguments:

  • bytes – bytearray composing the image to draw in the oled display
  • x – x-coordinate for left high corner of the image
  • y – y-coordinate for left high corner of the image
  • w – width of the image
  • h – height of the image
  • fill (bool) – flag for filling the image. If True draws image in standard color, otherwise draws the image in inverted color (display in normal mode); default True.

Note

If the display is set in complementary mode (see invert() function), fill flag set to True will draw the image in inverted color and set to False will draw the image in normal color.

Note

To obtain a converted image in hex array format, you can go and use this online tool.

After uploading your image, you can resize it setting the width and height fields; you can also choose the code format (HEX:0x recommended) and the color format (“Black/White for all draw image function” recommended).

Clicking on the “Get C string” button, the tool converts your image with your settings to a hex string that you can copy and paste inside a bytearray in your project and privide to this function.

SSD1306.draw_pixel

draw_pixel(x, y, fill=True)

Draws a single pixel in the screen.

Arguments:

  • x – pixel x-coordinate
  • y – pixel y-coordinate
  • fill (bool) – flag for filling the pixel. If True draws white pixel, otherwise black pixel (in normal mode); default True.

Note

If the display is set in complementary mode (see invert() function), fill flag set to True will draw black pixel and set to False will draw a white pixel.

SSD1306.draw_text

draw_text(text, x=None, y=None, w=None, h=None, align=None, fill=True)

Prints a string inside a text box in the screen.

Arguments:

  • text – string to be written in the display
  • x – x-coordinate for left high corner of the text box; default None
  • y – y-coordinate for left high corner of the text box; default None
  • w – width of the text box; default None
  • h – height of the text box; default None
  • align – alignment of the text inside the text box (1 for left alignment, 2 for right alignment, 3 for center alignment); default None
  • fill(bool) – flag for filling the text. If True draws white text in black background, othewise black text in white background (in normal mode); default True

Note

If the display is set in complementary mode (see invert() function), fill flag set to True will draw black text in white background and set to False will draw a white text in black background.

Note

If only text argument is provided, an automatic text box is created with the following values:

  • x = 0
  • y = 0
  • w = min text width according to the font
  • h = max char height according to the font
  • align = 3 (centered horizontally)
  • fill = True