SSD1306 OLED Controller (display-oled-ssd1306)
SSD1306 OLED Display

General Description

Support functions that allow the u8g2 graphics library to work with the SSD1306 OLED display.

Basic set of APIs for interacting with the SSD1306 OLED display.

This provides basic initialization and access to to the display.

Code snippets

Snippet 1: Simple initialization with U8G2

The following snippet initializes the OLED display with the U8G2 graphics library.

/* Initialize the U8 Display */
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_hw_i2c,
u8x8_gpio_and_delay_cb);
/* Send init sequence to the display, display is in sleep mode after this */
u8g2_InitDisplay(&u8g2);
/* wake up display */
u8g2_SetPowerSave(&u8g2, 0);
u8g2_SetFontPosCenter(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_crox3h_tf);
u8g2_ClearDisplay(&u8g2);
u8g2_ClearBuffer(&u8g2);

Snippet 2: Simple text alignment with U8G2

The following snippet demonstrates how to use the U8G2 graphics library to align and print strings to the display.

const char line1[] = "SSD1306";
const char line2[] = "OLED Controller";
const char line3[] = "u8g2 Example";
uint8_t width1 = u8g2_GetUTF8Width(&u8g2, line1);
uint8_t width2 = u8g2_GetUTF8Width(&u8g2, line2);
uint8_t width3 = u8g2_GetUTF8Width(&u8g2, line3);
uint8_t width = u8g2_GetDisplayWidth(&u8g2);
uint8_t height = u8g2_GetDisplayHeight(&u8g2);
u8g2_DrawFrame(&u8g2, 0, 0, width, height);
u8g2_DrawStr(&u8g2, (width - width1) / 2, (height / 2) - (FONT_SIZE + 4), line1);
u8g2_DrawStr(&u8g2, (width - width2) / 2, height / 2, line2);
u8g2_DrawStr(&u8g2, (width - width3) / 2, (height / 2) + (FONT_SIZE + 4), line3);
u8g2_SendBuffer(&u8g2);

Snippet 3: Simple initialization with emWin

The following snippet initializes the OLED display with the emWin graphics library.

/* Initialize the OLED display */
rslt = mtb_ssd1306_init_i2c(&i2c_obj);
/* OLED init failed. Stop program execution */
CY_ASSERT(rslt == CY_RSLT_SUCCESS);
/* Initialize emWin GUI */
GUI_Init();
/* Set font size, background color and text mode */
GUI_SetFont(GUI_FONT_16_1);
#define FONT_SIZE 16
GUI_SetBkColor(GUI_BLACK);
GUI_SetColor(GUI_WHITE);
GUI_SetTextMode(GUI_TM_NORMAL);
/* Clear the display */
GUI_Clear();
cy_rslt_t mtb_ssd1306_init_i2c(cyhal_i2c_t *i2c_inst)
Initialize the I2C communication with the OLED display.
Definition: mtb_ssd1306.c:41

Snippet 4: Simple text alignment with emWin

The following snippet demonstrates how to use the emWin graphics library to align and print strings to the display.

/* Update display */
GUI_SetTextAlign(GUI_TA_HCENTER);
GUI_DispStringAt("SSD1306", LCD_GetXSize() / 2,
(LCD_GetYSize() / 2) - (FONT_SIZE + (FONT_SIZE / 2)));
GUI_SetTextAlign(GUI_TA_HCENTER);
GUI_DispStringAt("OLED Controller", LCD_GetXSize() / 2, (LCD_GetYSize() / 2) - (FONT_SIZE / 2));
GUI_SetTextAlign(GUI_TA_HCENTER);
GUI_DispStringAt("emWin Example", LCD_GetXSize() / 2, (LCD_GetYSize() / 2) + (FONT_SIZE / 2));

Macros

#define CY_RSLT_SSD1306_INIT_FAIL
 Initialization failure error. More...
 

Functions

cy_rslt_t mtb_ssd1306_init_i2c (cyhal_i2c_t *i2c_inst)
 Initialize the I2C communication with the OLED display. More...
 
void mtb_ssd1306_free (void)
 Frees up any resources allocated by the display as part of mtb_ssd1306_init_i2c().
 

Macro Definition Documentation

◆ CY_RSLT_SSD1306_INIT_FAIL

#define CY_RSLT_SSD1306_INIT_FAIL
Value:
(CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR,\
CY_RSLT_MODULE_BOARD_HARDWARE_SSD1306, 0))

Initialization failure error.

Function Documentation

◆ mtb_ssd1306_init_i2c()

cy_rslt_t mtb_ssd1306_init_i2c ( cyhal_i2c_t *  i2c_inst)

Initialize the I2C communication with the OLED display.

Parameters
[in]i2c_instI2C instance to use for communicating with the SSD1306 controller.
Returns
CY_RSLT_SUCCESS if properly initialized, else an error indicating what went wrong.