SH1106
esp_lcd panel driver for the SH1106 OLED controller, the chip that sits on
most 128x64 modules, including the “1.3 inch” boards that come with a rotary
encoder.
Why a separate driver
The SH1106 looks like an SSD1306: same I2C address, same control byte, same page-major memory, same commands for contrast, inversion and the display switch. It is not a clone, though, and the SSD1306 driver cannot drive it:
the controller has 132 columns of display memory and a module of 128 pixels uses the columns 2..129, so every line has to be shifted by two columns;
it has no horizontal addressing mode — there are no window commands
0x21and0x22, the only way to write is to select a page and a column and then send the bytes of the page.
A picture written through the SSD1306 driver lands in the wrong place, the rest of the memory keeps whatever was there, and the glass shows noise with a readable stripe where the data happened to hit.
Features
implements
esp_lcd_panel_interface_t, so it plugs into the usualesp_lcd_panel_ops.hcalls and works with anyesp_lcdpanel IO — I2C and SPIwrites the memory page by page with the column offset of the controller
charge pump, contrast, inversion, segment remap and COM scan direction
checks the arguments and reports the codes of the calls it makes
Usage
#include <driver/i2c_master.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lcd_panel_sh1106.h>
i2c_master_bus_handle_t bus;
i2c_master_bus_config_t bus_config = {
.i2c_port = -1,
.sda_io_num = GPIO_NUM_22,
.scl_io_num = GPIO_NUM_23,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &bus));
esp_lcd_panel_io_handle_t io;
esp_lcd_panel_io_i2c_config_t io_config = {
.dev_addr = 0x3C,
.scl_speed_hz = 400000,
.control_phase_bytes = 1,
.dc_bit_offset = 6,
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(bus, &io_config, &io));
esp_lcd_panel_sh1106_config_t sh1106_config = {
.height = 64,
};
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = -1,
.bits_per_pixel = 1,
.vendor_config = &sh1106_config,
};
esp_lcd_panel_handle_t panel;
ESP_ERROR_CHECK(esp_lcd_new_panel_sh1106(io, &panel_config, &panel));
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel, true));
// the memory is page-major: a byte is a column of eight rows, bit 0 on top
uint8_t framebuffer[128 * 64 / 8];
for (int y = 0; y < 64; y += 8)
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel, 0, y, 128, y + 8,
framebuffer + (y / 8) * 128));
Notes
Rows must be page aligned when drawing: both the first row and the row after the last one must be multiples of 8. The controller cannot read its memory back, so an area that starts or ends inside a page cannot be merged with what is already there; the driver returns
ESP_ERR_INVALID_ARGinstead of corrupting the neighbouring rows.The upright orientation is the remapped one: the modules are wired so that the segment remap and the reverse COM scan direction give the picture as it is drawn.
init()sends them, andesp_lcd_panel_mirror()flips the image relative to that.esp_lcd_panel_swap_xy()returnsESP_ERR_NOT_SUPPORTED: the memory is sent page by page and an exchange of the axes would need a transposed buffer, which the interface does not allow to prepare in the call.The column offset is a part of
esp_lcd_panel_sh1106_config_t; the default of 2 suits the common 128x64 modules. A module that starts the picture at another column can be adjusted without rebuilding the driver.
Example
examples/default draws a frame, a grid, a moving bar and a counter of
frames, and switches the inversion of the panel every few seconds.
Installation
idf.py add-dependency "esp-idf-lib/sh1106"
API reference
Functions
- esp_err_t esp_lcd_new_panel_sh1106(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config, esp_lcd_panel_handle_t *ret_panel)
Create an LCD panel for the SH1106 controller.
Example usage:
esp_lcd_panel_sh1106_config_t sh1106_config = { .height = 64, }; esp_lcd_panel_dev_config_t panel_config = { .reset_gpio_num = -1, .bits_per_pixel = 1, .vendor_config = &sh1106_config, }; esp_lcd_panel_handle_t panel = NULL; esp_lcd_new_panel_sh1106(io, &panel_config, &panel);Note
The default panel size is 128x64. Use esp_lcd_panel_sh1106_config_t to set another size, the contrast or the column offset.
Note
The rows of an area passed to esp_lcd_panel_draw_bitmap() must be page aligned: both the first and the last row plus one have to be multiples of 8. The memory of the controller cannot be read back, so an area that starts or ends inside a page cannot be merged with what is already there.
Note
esp_lcd_panel_swap_xy() returns ESP_ERR_NOT_SUPPORTED: the data is sent page by page and an exchange of the axes would need a transposed buffer.
- Parameters:
io – [in] LCD panel IO handle
panel_dev_config – [in] general panel device configuration
ret_panel – [out] returned LCD panel handle
- Returns:
ESP_OK on success
ESP_ERR_INVALID_ARG if the arguments are invalid
ESP_ERR_NO_MEM if out of memory
- struct esp_lcd_panel_sh1106_config_t
#include <esp_lcd_panel_sh1106.h>SH1106 configuration structure.
To be used as esp_lcd_panel_dev_config_t.vendor_config. See esp_lcd_new_panel_sh1106().
Public Members
- uint8_t height
Display height in pixels (64 or 32)
The default is 64.
- uint8_t contrast
Display contrast, 0..255.
The default is 128.
- uint8_t column_offset
Column address offset.
The controller has 132 columns of display memory and a module of 128 pixels uses the columns 2..129, so the first pixel of a line lands in the column 2. The default is 2, which suits the common 128x64 modules.