SSD1306 Module

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

init(screen_width=96, screen_height=40)

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

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

Turns on the display.

off()

Turns off the display.

invert()

Sets the display in complementary mode.

normal()

Sets the display in normal mode.

set_contrast(contrast=0x7F)

Sets the contrast of the display.

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

Clears the display.

fill_screen()

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

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

Draws a filled rectangular area in the screen.

Parameters:
  • 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.

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

Draws the image passed in bytearray format as argument.

Parameters:
  • 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.

draw_pixel(x, y, fill=True)

Draws a single pixel in the screen.

Parameters:
  • 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.

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.

Parameters:
  • 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, otherwise 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