ESP32S3基于espidf lvgl驱动i2c ssd1306/sh1106/7屏幕使用
ESP32S3基于espidf lvgl驱动i2c ssd1306/sh1106/7屏幕使用
- 🔖个人使用的espidf版本:V5.4,lvgl组件版本:8.3.0(可在
idf_component.yml
文件中看到)
对于 i2c ssd1306接口屏幕,可以直接使用自带的demo例程(
i2c_oled
)即可快速点亮屏幕。使用sh1106/7接口屏幕,需要手动安装组件sh1107库才行。
- 使用SH1106接口的屏幕,可以使用SH1107库。个人所使用的就是SH1106 1.3"屏幕驱动的。
- 对于使用demo例程创建的工程,对于这种单色屏显示,需要配置的参数很少,主要是针对屏幕尺寸,引脚定义。
📝例程demo代码
- 在源代码基础上,新增了一个字符串的固定显示函数。
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "esp_err.h"
#include "esp_log.h"
#include "driver/i2c_master.h"
#include "esp_lvgl_port.h"
#include "lvgl.h"
#if CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
#include "esp_lcd_sh1107.h"
#else
#include "esp_lcd_panel_vendor.h"
#endif
static const char *TAG = "i2c oled";
#define I2C_BUS_PORT 0
Please update the following configuration according to your LCD spec //
#define EXAMPLE_LCD_PIXEL_CLOCK_HZ (400 * 1000)
#define EXAMPLE_PIN_NUM_SDA 3
#define EXAMPLE_PIN_NUM_SCL 4
#define EXAMPLE_PIN_NUM_RST -1
#define EXAMPLE_I2C_HW_ADDR 0x3C
// The pixel number in horizontal and vertical
#if CONFIG_EXAMPLE_LCD_CONTROLLER_SSD1306
#define EXAMPLE_LCD_H_RES 128
#define EXAMPLE_LCD_V_RES CONFIG_EXAMPLE_SSD1306_HEIGHT
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
#define EXAMPLE_LCD_H_RES 132
#define EXAMPLE_LCD_V_RES 64
#endif
// Bit number used to represent command and parameter
#define EXAMPLE_LCD_CMD_BITS 8
#define EXAMPLE_LCD_PARAM_BITS 8
extern void example_lvgl_demo_ui(lv_disp_t *disp);
extern void example_lvgl_str_ui(lv_disp_t *disp);
void app_main(void)
{
ESP_LOGI(TAG, "Initialize I2C bus");
i2c_master_bus_handle_t i2c_bus = NULL;
i2c_master_bus_config_t bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.i2c_port = I2C_BUS_PORT,
.sda_io_num = EXAMPLE_PIN_NUM_SDA,
.scl_io_num = EXAMPLE_PIN_NUM_SCL,
.flags.enable_internal_pullup = true,
};
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &i2c_bus));
ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_i2c_config_t io_config = {
.dev_addr = EXAMPLE_I2C_HW_ADDR,
.scl_speed_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
.control_phase_bytes = 1, // According to SSD1306 datasheet
.lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS, // According to SSD1306 datasheet
.lcd_param_bits = EXAMPLE_LCD_CMD_BITS, // According to SSD1306 datasheet
#if CONFIG_EXAMPLE_LCD_CONTROLLER_SSD1306
.dc_bit_offset = 6, // According to SSD1306:6 bit is DC bit
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
.dc_bit_offset = 0, // According to SH1107 datasheet
// According to SH1106 datasheet
.flags=
{
.disable_control_phase = 1,
}
#endif
};
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus, &io_config, &io_handle));
ESP_LOGI(TAG, "Install SSD1306 panel driver");
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_panel_dev_config_t panel_config = {
.bits_per_pixel = 1,
.reset_gpio_num = EXAMPLE_PIN_NUM_RST,
};
#if CONFIG_EXAMPLE_LCD_CONTROLLER_SSD1306
esp_lcd_panel_ssd1306_config_t ssd1306_config = {
.height = EXAMPLE_LCD_V_RES,
};
panel_config.vendor_config = &ssd1306_config;
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
ESP_ERROR_CHECK(esp_lcd_new_panel_sh1107(io_handle, &panel_config, &panel_handle));
#endif
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
#if CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
#endif
ESP_LOGI(TAG, "Initialize LVGL");
const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
lvgl_port_init(&lvgl_cfg);
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = io_handle,
.panel_handle = panel_handle,
.buffer_size = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES,
.double_buffer = true,
.hres = EXAMPLE_LCD_H_RES,
.vres = EXAMPLE_LCD_V_RES,
.monochrome = true,
.rotation = {
.swap_xy = false,
.mirror_x = false,
.mirror_y = false,
}
};
lv_disp_t *disp = lvgl_port_add_disp(&disp_cfg);
/* Rotation of the screen 屏幕旋转*/
lv_disp_set_rotation(disp, LV_DISP_ROT_180);//LV_DISP_ROT_180 LV_DISP_ROT_270 LV_DISP_ROT_90 LV_DISP_ROT_NONE
// lv_disp_set_rotation(disp, LV_DISP_ROT_270);
ESP_LOGI(TAG, "Display LVGL Hello World");
ESP_LOGI(TAG, "Display LVGL Scroll Text");
// Lock the mutex due to the LVGL APIs are not thread-safe
if (lvgl_port_lock(0)) {
example_lvgl_demo_ui(disp);
example_lvgl_str_ui(disp);
// Release the mutex
lvgl_port_unlock();//释放互斥锁
}
}
- 内容显实现示
lvgl_demo_ui.c
文件
void example_lvgl_demo_ui(lv_disp_t *disp) //循环滚动字符串内容显示
{
lv_obj_t *scr = lv_disp_get_scr_act(disp);
lv_obj_t *label = lv_label_create(scr);
lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL); /* Circular scroll 设置标签的长文本模式为循环滚动*/
lv_label_set_text(label, "Hello Espressif,LVGL.");
/* Size of the screen (if you use rotation 90 or 270, please set disp->driver->ver_res) */
lv_obj_set_width(label, disp->driver->hor_res);//设置标签的宽度为屏幕的水平分辨率
//lv_obj_set_width(label, disp->driver->ver_res);
/* Align to the top 将标签对象对齐到屏幕的顶部中央位置*/
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0);
}
void example_lvgl_str_ui(lv_disp_t *disp) //固定字符串内容显示
{
lv_obj_t *scr = lv_disp_get_scr_act(disp);
lv_obj_t *label = lv_label_create(scr);
lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP); /* 设置标签的长文本模式为自动换行 */
lv_label_set_text_fmt(label, "Hello Esp32 LVGL."); /* 设置标签的文本内容 */
/* Size of the screen (if you use rotation 90 or 270, please set disp->driver->ver_res) */
lv_obj_set_width(label, disp->driver->hor_res); /* 设置标签的宽度为屏幕的水平分辨率 */
/* Align to the top 将标签对象对齐到屏幕的顶部中央位置 */
lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, 0);
// 设置字体大小
static lv_style_t style;
lv_style_init(&style);
lv_style_set_text_font(&style, &lv_font_montserrat_12); // 使用Montserrat字体,大小为16px
lv_obj_add_style(label, &style, 0);
}
⛳SH1106显示偏移问题
- ✨由于espidf中没有提供专门驱动SH1106的组件库,只能使用SH1107组件库,作为驱动SH1106显示使用,虽然绝多数指令和代码都通用,但是存在一点问题,就是横坐标显示偏移问题。由于手上没有SH1107的屏幕,不知道SH1107的显示效果如何。
- 解决的方法:
- 找到SH1107组件驱动库文件夹中的
esp_lvgl_port.c
文件,在回调显示函数:lvgl_port_flush_callback
中,手动添加补偿偏移量。
static void lvgl_port_flush_callback(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
assert(drv != NULL);
lvgl_port_display_ctx_t *disp_ctx = (lvgl_port_display_ctx_t *)drv->user_data;
assert(disp_ctx != NULL);
const int offsetx1 = area->x1;
const int offsetx2 = area->x2;
const int offsety1 = area->y1;
const int offsety2 = area->y2;
// copy a buffer's content to a specific area of the display
esp_lcd_panel_draw_bitmap(disp_ctx->panel_handle, offsetx1+1, offsety1+1, offsetx2 + 1, offsety2 + 1, color_map);//这里修改补偿偏移量
}
📗SH1107库安装
- 找到组件管理
-
在组件里,搜索关键字
SH1107
-
点开库,点击install,进行安装
-
安装成功后,在项目栏下可以看到对应安装的库。
-
🌿屏幕接口配置:
-
🎉只有安装了SH1107组件,这里才会显示出SSD1306和SH1107 两个配置选项,默认的demo例程加载打开后,这里只有SSD1306一个型号选项显示。
SH1106 1.3"屏幕像素设置
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
#define EXAMPLE_LCD_H_RES 132
#define EXAMPLE_LCD_V_RES 64
#endif
🛠SH1106 初始化函数指令修改
- 在
esp_lcd_sh1107.c
文件中,显示起始位置。如果不修改的话,SH1106屏幕显示,上和下部分的内容是反的。在使用顶部(lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0);
)或底部(lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, 0);
)对齐时,两者是反的。上半部分的内容显示在下半部分,下半部分的内容显示在上半部分。
static const uint8_t vendor_specific_init[] = {
0xAE, /* turn off OLED display */
0xdc, /* set display start line */
0x00,
0x81, /* contrast control */
0x2f, /* 128 */
0x20, /* Set Memory addressing mode (0x20/0x21) */
0xA0, /* Non-flipped horizontal */
0xC7, /* Non-flipped vertical */
0xa8, /* multiplex ratio */
0x7f, /* duty = 1/64 */
0xd3, /* set display offset */
0x00, //SH1107:0x60,SH1106:0X00
0xd5, /* set osc division */
0x51,
0xd9, /* set pre-charge period */
0x22,
0xdb, /* set vcomh */
0x35,
0xB0, /* Set page address */
0xDA, /* Set com pins */
0x12,
0xa4, /* output ram to display */
0xa6, /* normal / inverted colors */
0xFF, //END
};