i2c_oled_example_main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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_vendor.h"
  12. #include "esp_lcd_panel_ops.h"
  13. #include "driver/i2c.h"
  14. #include "esp_err.h"
  15. #include "esp_log.h"
  16. #include "lvgl.h"
  17. static const char *TAG = "example";
  18. #define I2C_HOST 0
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. //////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (400 * 1000)
  23. #define EXAMPLE_PIN_NUM_SDA 3
  24. #define EXAMPLE_PIN_NUM_SCL 4
  25. #define EXAMPLE_PIN_NUM_RST -1
  26. #define EXAMPLE_I2C_HW_ADDR 0x3C
  27. // The pixel number in horizontal and vertical
  28. #define EXAMPLE_LCD_H_RES 128
  29. #define EXAMPLE_LCD_V_RES 64
  30. // Bit number used to represent command and parameter
  31. #define EXAMPLE_LCD_CMD_BITS 8
  32. #define EXAMPLE_LCD_PARAM_BITS 8
  33. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  34. extern void example_lvgl_demo_ui(lv_disp_t *disp);
  35. static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
  36. {
  37. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  38. lv_disp_flush_ready(disp_driver);
  39. return false;
  40. }
  41. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  42. {
  43. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  44. int offsetx1 = area->x1;
  45. int offsetx2 = area->x2;
  46. int offsety1 = area->y1;
  47. int offsety2 = area->y2;
  48. // copy a buffer's content to a specific area of the display
  49. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  50. }
  51. static void example_lvgl_set_px_cb(lv_disp_drv_t *disp_drv, uint8_t *buf, lv_coord_t buf_w, lv_coord_t x, lv_coord_t y,
  52. lv_color_t color, lv_opa_t opa)
  53. {
  54. uint16_t byte_index = x + (( y >> 3 ) * buf_w);
  55. uint8_t bit_index = y & 0x7;
  56. if ((color.full == 0) && (LV_OPA_TRANSP != opa)) {
  57. buf[byte_index] |= (1 << bit_index);
  58. } else {
  59. buf[byte_index] &= ~(1 << bit_index);
  60. }
  61. }
  62. static void example_lvgl_rounder(lv_disp_drv_t *disp_drv, lv_area_t *area)
  63. {
  64. area->y1 = area->y1 & (~0x7);
  65. area->y2 = area->y2 | 0x7;
  66. }
  67. static void example_increase_lvgl_tick(void *arg)
  68. {
  69. /* Tell LVGL how many milliseconds has elapsed */
  70. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  71. }
  72. void app_main(void)
  73. {
  74. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  75. static lv_disp_drv_t disp_drv; // contains callback functions
  76. ESP_LOGI(TAG, "Initialize I2C bus");
  77. i2c_config_t i2c_conf = {
  78. .mode = I2C_MODE_MASTER,
  79. .sda_io_num = EXAMPLE_PIN_NUM_SDA,
  80. .scl_io_num = EXAMPLE_PIN_NUM_SCL,
  81. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  82. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  83. .master.clk_speed = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  84. };
  85. ESP_ERROR_CHECK(i2c_param_config(I2C_HOST, &i2c_conf));
  86. ESP_ERROR_CHECK(i2c_driver_install(I2C_HOST, I2C_MODE_MASTER, 0, 0, 0));
  87. ESP_LOGI(TAG, "Install panel IO");
  88. esp_lcd_panel_io_handle_t io_handle = NULL;
  89. esp_lcd_panel_io_i2c_config_t io_config = {
  90. .dev_addr = EXAMPLE_I2C_HW_ADDR,
  91. .control_phase_bytes = 1, // According to SSD1306 datasheet
  92. .dc_bit_offset = 6, // According to SSD1306 datasheet
  93. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS, // According to SSD1306 datasheet
  94. .lcd_param_bits = EXAMPLE_LCD_CMD_BITS, // According to SSD1306 datasheet
  95. .on_color_trans_done = example_notify_lvgl_flush_ready,
  96. .user_ctx = &disp_drv,
  97. };
  98. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)I2C_HOST, &io_config, &io_handle));
  99. ESP_LOGI(TAG, "Install SSD1306 panel driver");
  100. esp_lcd_panel_handle_t panel_handle = NULL;
  101. esp_lcd_panel_dev_config_t panel_config = {
  102. .bits_per_pixel = 1,
  103. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  104. };
  105. ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));
  106. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  107. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  108. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  109. ESP_LOGI(TAG, "Initialize LVGL library");
  110. lv_init();
  111. // alloc draw buffers used by LVGL
  112. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  113. lv_color_t *buf1 = malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t));
  114. assert(buf1);
  115. lv_color_t *buf2 = malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t));
  116. assert(buf2);
  117. // initialize LVGL draw buffers
  118. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 20);
  119. ESP_LOGI(TAG, "Register display driver to LVGL");
  120. lv_disp_drv_init(&disp_drv);
  121. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  122. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  123. disp_drv.flush_cb = example_lvgl_flush_cb;
  124. disp_drv.draw_buf = &disp_buf;
  125. disp_drv.user_data = panel_handle;
  126. disp_drv.rounder_cb = example_lvgl_rounder;
  127. disp_drv.set_px_cb = example_lvgl_set_px_cb;
  128. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  129. ESP_LOGI(TAG, "Install LVGL tick timer");
  130. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  131. const esp_timer_create_args_t lvgl_tick_timer_args = {
  132. .callback = &example_increase_lvgl_tick,
  133. .name = "lvgl_tick"
  134. };
  135. esp_timer_handle_t lvgl_tick_timer = NULL;
  136. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  137. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  138. ESP_LOGI(TAG, "Display LVGL Scroll Text");
  139. example_lvgl_demo_ui(disp);
  140. while (1) {
  141. // raise the task priority of LVGL and/or reduce the handler period can improve the performance
  142. vTaskDelay(pdMS_TO_TICKS(10));
  143. // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
  144. lv_timer_handler();
  145. }
  146. }