test_i2c_lcd_panel.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "sdkconfig.h"
  4. #include "unity.h"
  5. #include "test_utils.h"
  6. #include "driver/i2c.h"
  7. #include "driver/gpio.h"
  8. #include "esp_lcd_panel_io.h"
  9. #include "esp_lcd_panel_vendor.h"
  10. #include "esp_lcd_panel_ops.h"
  11. #include "esp_system.h"
  12. #define TEST_LCD_H_RES (128)
  13. #define TEST_LCD_V_RES (64)
  14. #define TEST_I2C_SDA_GPIO (3)
  15. #define TEST_I2C_SCL_GPIO (4)
  16. #define TEST_I2C_HOST_ID (0)
  17. #define TEST_I2C_DEV_ADDR (0x3C)
  18. #define TEST_LCD_PIXEL_CLOCK_HZ (400 * 1000)
  19. TEST_CASE("lcd panel with i2c interface (ssd1306)", "[lcd]")
  20. {
  21. const uint8_t pattern[][16] = {{
  22. 0x00, 0x7E, 0x42, 0x42, 0x42, 0x42, 0x7E, 0x00,
  23. 0x00, 0x7E, 0x42, 0x42, 0x42, 0x42, 0x7E, 0x00
  24. },
  25. {
  26. 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81,
  27. 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81
  28. }
  29. };
  30. i2c_config_t conf = {
  31. .mode = I2C_MODE_MASTER,
  32. .sda_io_num = TEST_I2C_SDA_GPIO,
  33. .scl_io_num = TEST_I2C_SCL_GPIO,
  34. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  35. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  36. .master.clk_speed = TEST_LCD_PIXEL_CLOCK_HZ,
  37. };
  38. TEST_ESP_OK(i2c_param_config(TEST_I2C_HOST_ID, &conf));
  39. TEST_ESP_OK(i2c_driver_install(TEST_I2C_HOST_ID, I2C_MODE_MASTER, 0, 0, 0));
  40. esp_lcd_panel_io_handle_t io_handle = NULL;
  41. esp_lcd_panel_io_i2c_config_t io_config = {
  42. .dev_addr = TEST_I2C_DEV_ADDR,
  43. .control_phase_bytes = 1, // According to SSD1306 datasheet
  44. .dc_bit_offset = 6, // According to SSD1306 datasheet
  45. .lcd_cmd_bits = 8, // According to SSD1306 datasheet
  46. .lcd_param_bits = 8, // According to SSD1306 datasheet
  47. };
  48. TEST_ESP_OK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TEST_I2C_HOST_ID, &io_config, &io_handle));
  49. esp_lcd_panel_handle_t panel_handle = NULL;
  50. esp_lcd_panel_dev_config_t panel_config = {
  51. .bits_per_pixel = 1,
  52. .color_space = ESP_LCD_COLOR_SPACE_MONOCHROME,
  53. .reset_gpio_num = -1,
  54. };
  55. TEST_ESP_OK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));
  56. TEST_ESP_OK(esp_lcd_panel_reset(panel_handle));
  57. TEST_ESP_OK(esp_lcd_panel_init(panel_handle));
  58. for (int i = 0; i < TEST_LCD_H_RES / 16; i++) {
  59. for (int j = 0; j < TEST_LCD_V_RES / 8; j++) {
  60. TEST_ESP_OK(esp_lcd_panel_draw_bitmap(panel_handle, i * 16, j * 8, i * 16 + 16, j * 8 + 8, pattern[i & 0x01]));
  61. }
  62. }
  63. TEST_ESP_OK(esp_lcd_panel_del(panel_handle));
  64. TEST_ESP_OK(esp_lcd_panel_io_del(io_handle));
  65. TEST_ESP_OK(i2c_driver_delete(TEST_I2C_HOST_ID));
  66. }
  67. // The following test shows a porting example of LVGL GUI library
  68. // To run the LVGL tests, you need to clone the LVGL library into components directory firstly
  69. #if CONFIG_LV_USE_USER_DATA
  70. #include "test_lvgl_port.h"
  71. #if CONFIG_LV_COLOR_DEPTH_1
  72. static bool notify_lvgl_ready_to_flush(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
  73. {
  74. lv_disp_t *disp = *(lv_disp_t **)user_ctx;
  75. lv_disp_flush_ready(&disp->driver);
  76. return false;
  77. }
  78. TEST_CASE("lvgl gui with i2c interface (ssd1306)", "[lcd][lvgl][ignore]")
  79. {
  80. // initialize LVGL graphics library
  81. lv_disp_t *disp = NULL;
  82. lv_init();
  83. i2c_config_t conf = {
  84. .mode = I2C_MODE_MASTER,
  85. .sda_io_num = TEST_I2C_SDA_GPIO,
  86. .scl_io_num = TEST_I2C_SCL_GPIO,
  87. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  88. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  89. .master.clk_speed = TEST_LCD_PIXEL_CLOCK_HZ,
  90. };
  91. TEST_ESP_OK(i2c_param_config(TEST_I2C_HOST_ID, &conf));
  92. TEST_ESP_OK(i2c_driver_install(TEST_I2C_HOST_ID, I2C_MODE_MASTER, 0, 0, 0));
  93. esp_lcd_panel_io_handle_t io_handle = NULL;
  94. esp_lcd_panel_io_i2c_config_t io_config = {
  95. .dev_addr = TEST_I2C_DEV_ADDR,
  96. .control_phase_bytes = 1, // According to SSD1306 datasheet
  97. .dc_bit_offset = 6, // According to SSD1306 datasheet
  98. .lcd_cmd_bits = 8, // According to SSD1306 datasheet
  99. .lcd_param_bits = 8, // According to SSD1306 datasheet
  100. .on_color_trans_done = notify_lvgl_ready_to_flush,
  101. .user_ctx = &disp,
  102. };
  103. TEST_ESP_OK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)TEST_I2C_HOST_ID, &io_config, &io_handle));
  104. esp_lcd_panel_handle_t panel_handle = NULL;
  105. esp_lcd_panel_dev_config_t panel_config = {
  106. .bits_per_pixel = 1,
  107. .color_space = ESP_LCD_COLOR_SPACE_MONOCHROME,
  108. .reset_gpio_num = -1,
  109. };
  110. TEST_ESP_OK(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle));
  111. TEST_ESP_OK(esp_lcd_panel_reset(panel_handle));
  112. TEST_ESP_OK(esp_lcd_panel_init(panel_handle));
  113. test_lvgl_task_loop(panel_handle, TEST_LCD_H_RES, TEST_LCD_V_RES, &disp);
  114. }
  115. #endif // CONFIG_LV_COLOR_DEPTH_1
  116. #endif // CONFIG_LV_USE_USER_DATA