i2c_oled_example_main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_timer.h"
  10. #include "esp_lcd_panel_io.h"
  11. #include "esp_lcd_panel_ops.h"
  12. #include "driver/i2c.h"
  13. #include "esp_err.h"
  14. #include "esp_log.h"
  15. #include "lvgl.h"
  16. #include "esp_lvgl_port.h"
  17. #if CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
  18. #include "esp_lcd_sh1107.h"
  19. #else
  20. #include "esp_lcd_panel_vendor.h"
  21. #endif
  22. static const char *TAG = "example";
  23. #define I2C_HOST 0
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. //////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (400 * 1000)
  28. #define EXAMPLE_PIN_NUM_SDA 3
  29. #define EXAMPLE_PIN_NUM_SCL 4
  30. #define EXAMPLE_PIN_NUM_RST -1
  31. #define EXAMPLE_I2C_HW_ADDR 0x3C
  32. // The pixel number in horizontal and vertical
  33. #if CONFIG_EXAMPLE_LCD_CONTROLLER_SSD1306
  34. #define EXAMPLE_LCD_H_RES 128
  35. #define EXAMPLE_LCD_V_RES CONFIG_EXAMPLE_SSD1306_HEIGHT
  36. #elif CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
  37. #define EXAMPLE_LCD_H_RES 64
  38. #define EXAMPLE_LCD_V_RES 128
  39. #endif
  40. // Bit number used to represent command and parameter
  41. #define EXAMPLE_LCD_CMD_BITS 8
  42. #define EXAMPLE_LCD_PARAM_BITS 8
  43. extern void example_lvgl_demo_ui(lv_disp_t *disp);
  44. void app_main(void)
  45. {
  46. ESP_LOGI(TAG, "Initialize I2C bus");
  47. i2c_config_t i2c_conf = {
  48. .mode = I2C_MODE_MASTER,
  49. .sda_io_num = EXAMPLE_PIN_NUM_SDA,
  50. .scl_io_num = EXAMPLE_PIN_NUM_SCL,
  51. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  52. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  53. .master.clk_speed = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  54. };
  55. ESP_ERROR_CHECK(i2c_param_config(I2C_HOST, &i2c_conf));
  56. ESP_ERROR_CHECK(i2c_driver_install(I2C_HOST, I2C_MODE_MASTER, 0, 0, 0));
  57. ESP_LOGI(TAG, "Install panel IO");
  58. esp_lcd_panel_io_handle_t io_handle = NULL;
  59. esp_lcd_panel_io_i2c_config_t io_config = {
  60. .dev_addr = EXAMPLE_I2C_HW_ADDR,
  61. .control_phase_bytes = 1, // According to SSD1306 datasheet
  62. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS, // According to SSD1306 datasheet
  63. .lcd_param_bits = EXAMPLE_LCD_CMD_BITS, // According to SSD1306 datasheet
  64. #if CONFIG_EXAMPLE_LCD_CONTROLLER_SSD1306
  65. .dc_bit_offset = 6, // According to SSD1306 datasheet
  66. #elif CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
  67. .dc_bit_offset = 0, // According to SH1107 datasheet
  68. .flags =
  69. {
  70. .disable_control_phase = 1,
  71. }
  72. #endif
  73. };
  74. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(I2C_HOST, &io_config, &io_handle));
  75. ESP_LOGI(TAG, "Install SSD1306 panel driver");
  76. esp_lcd_panel_handle_t panel_handle = NULL;
  77. esp_lcd_panel_dev_config_t panel_config = {
  78. .bits_per_pixel = 1,
  79. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  80. };
  81. #if CONFIG_EXAMPLE_LCD_CONTROLLER_SSD1306
  82. esp_lcd_panel_ssd1306_config_t ssd1306_config = {
  83. .height = EXAMPLE_LCD_V_RES,
  84. };
  85. panel_config.vendor_config = &ssd1306_config;
  86. ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));
  87. #elif CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
  88. ESP_ERROR_CHECK(esp_lcd_new_panel_sh1107(io_handle, &panel_config, &panel_handle));
  89. #endif
  90. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  91. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  92. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  93. #if CONFIG_EXAMPLE_LCD_CONTROLLER_SH1107
  94. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  95. #endif
  96. ESP_LOGI(TAG, "Initialize LVGL");
  97. const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
  98. lvgl_port_init(&lvgl_cfg);
  99. const lvgl_port_display_cfg_t disp_cfg = {
  100. .io_handle = io_handle,
  101. .panel_handle = panel_handle,
  102. .buffer_size = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES,
  103. .double_buffer = true,
  104. .hres = EXAMPLE_LCD_H_RES,
  105. .vres = EXAMPLE_LCD_V_RES,
  106. .monochrome = true,
  107. .rotation = {
  108. .swap_xy = false,
  109. .mirror_x = false,
  110. .mirror_y = false,
  111. }
  112. };
  113. lv_disp_t * disp = lvgl_port_add_disp(&disp_cfg);
  114. /* Rotation of the screen */
  115. lv_disp_set_rotation(disp, LV_DISP_ROT_NONE);
  116. ESP_LOGI(TAG, "Display LVGL Scroll Text");
  117. // Lock the mutex due to the LVGL APIs are not thread-safe
  118. if (lvgl_port_lock(0)) {
  119. example_lvgl_demo_ui(disp);
  120. // Release the mutex
  121. lvgl_port_unlock();
  122. }
  123. }