Initialize I2C bus and U8G2 display.
If config->setup_fn is NULL, defaults to SSD1306 128x64 noname full buffer setup.
258{
259 esp_err_t probe_ret;
260 uint8_t resolved_address;
261 uint16_t probe_timeout_ms;
262
263 ESP_RETURN_ON_FALSE(handle != NULL, ESP_ERR_INVALID_ARG,
TAG,
"handle is NULL");
264 ESP_RETURN_ON_FALSE(config != NULL, ESP_ERR_INVALID_ARG,
TAG,
"config is NULL");
265 ESP_RETURN_ON_FALSE(config->
timeout_ms > 0, ESP_ERR_INVALID_ARG,
TAG,
"timeout_ms must be > 0");
266 ESP_RETURN_ON_FALSE(config->
scl_speed_hz > 0, ESP_ERR_INVALID_ARG,
TAG,
"scl_speed_hz must be > 0");
268 "device_address must be 7-bit");
269
270 memset(handle, 0, sizeof(*handle));
274
279
283
284 i2c_master_bus_config_t bus_config = {
288 .clk_source = I2C_CLK_SRC_DEFAULT,
289 .glitch_ignore_cnt = 7,
290 .intr_priority = 0,
291 .trans_queue_depth = 0,
293 };
294
296 TAG,
"Failed to create I2C master bus");
297
299 probe_timeout_ms,
300 &resolved_address);
301 if (probe_ret != ESP_OK) {
302 if (probe_ret == ESP_ERR_NOT_FOUND) {
303 uint8_t found_addresses[12];
305 sizeof(found_addresses),
306 probe_timeout_ms);
307
309 (total_found < sizeof(found_addresses)) ? total_found : sizeof(found_addresses),
310 total_found);
311 } else if (probe_ret == ESP_ERR_TIMEOUT) {
313 "I2C probe timed out; skipping full bus scan to avoid timeout spam");
314 } else {
316 "I2C probe failed before scan (%s)",
317 esp_err_to_name(probe_ret));
318 }
319
321 "No I2C display detected at 0x%02X (SDA=%d, SCL=%d, %uHz)",
328 return (probe_ret == ESP_ERR_NOT_FOUND) ? ESP_ERR_NOT_FOUND : probe_ret;
329 }
330
333
335
336
337 vTaskDelay(pdMS_TO_TICKS(10));
338
340
342 if (setup_fn == NULL) {
343 setup_fn = u8g2_Setup_ssd1315_i2c_128x64_noname_f;
344 }
345
346 setup_fn(&handle->
u8g2,
350
352
353 u8g2_InitDisplay(&handle->
u8g2);
354 u8g2_SetPowerSave(&handle->
u8g2, 0);
355
356
357 u8g2_ClearBuffer(&handle->
u8g2);
358 u8g2_SetFont(&handle->
u8g2, u8g2_font_6x10_tf);
359 u8g2_DrawStr(&handle->
u8g2, 0, 12,
"Init OK");
360 u8g2_SendBuffer(&handle->
u8g2);
361 ESP_LOGI(
TAG,
"Test draw completed");
362
364 return ESP_OK;
365}
static uint8_t i2c_u8g2_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
static void i2c_u8g2_log_scan_results(const uint8_t *found_addresses, size_t stored_count, size_t total_count)
static esp_err_t i2c_u8g2_resolve_display_address(uint8_t preferred_address, uint16_t probe_timeout_ms, uint8_t *resolved_address)
#define I2C_PROBE_TIMEOUT_MS_MAX
static size_t i2c_u8g2_scan_bus(uint8_t *found_addresses, size_t max_found, uint16_t probe_timeout_ms)
static uint8_t i2c_u8g2_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
void(* i2c_u8g2_setup_fn_t)(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)
U8G2 setup callback signature.
const u8g2_cb_t * rotation
i2c_u8g2_setup_fn_t setup_fn
bool enable_internal_pullup