IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
priv_i2c_u8g2.h File Reference

Internal I2C + U8G2 display integration APIs. More...

#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "driver/gpio.h"
#include "driver/i2c_master.h"
#include "esp_err.h"
#include "u8g2.h"
+ Include dependency graph for priv_i2c_u8g2.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  i2c_u8g2_config_t
 Configuration for I2C-backed U8G2 display initialization. More...
 
struct  i2c_u8g2_handle_t
 Display handle containing runtime driver state. More...
 

Macros

#define I2C_U8G2_TX_BUFFER_SIZE   132
 

Typedefs

typedef 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.
 

Functions

i2c_u8g2_config_t i2c_u8g2_config_default (void)
 Fill a config with menuconfig-backed defaults.
 
esp_err_t i2c_u8g2_init (i2c_u8g2_handle_t *handle, const i2c_u8g2_config_t *config)
 Initialize I2C bus and U8G2 display.
 
esp_err_t i2c_u8g2_deinit (i2c_u8g2_handle_t *handle)
 Deinitialize display device and I2C bus.
 
u8g2_t * i2c_u8g2_get_u8g2 (i2c_u8g2_handle_t *handle)
 Get the U8G2 object for draw operations.
 
esp_err_t i2c_u8g2_set_power_save (i2c_u8g2_handle_t *handle, bool enable)
 Set display power save mode.
 
void demo_text_display (u8g2_t *u8g2)
 Demo helper for text rendering tests.
 

Detailed Description

Internal I2C + U8G2 display integration APIs.

Definition in file priv_i2c_u8g2.h.

Macro Definition Documentation

◆ I2C_U8G2_TX_BUFFER_SIZE

#define I2C_U8G2_TX_BUFFER_SIZE   132

Definition at line 20 of file priv_i2c_u8g2.h.

Typedef Documentation

◆ i2c_u8g2_setup_fn_t

typedef 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.

Definition at line 25 of file priv_i2c_u8g2.h.

Function Documentation

◆ demo_text_display()

void demo_text_display ( u8g2_t *  u8g2)

Demo helper for text rendering tests.

Parameters
u8g2Initialized U8G2 object.

◆ i2c_u8g2_config_default()

i2c_u8g2_config_t i2c_u8g2_config_default ( void  )

Fill a config with menuconfig-backed defaults.

Definition at line 241 of file priv_i2c_u8g2.c.

242{
243 i2c_u8g2_config_t config = {
244 .i2c_port = (i2c_port_num_t)I2C_MASTER_NUM,
245 .sda_io_num = (gpio_num_t)I2C_MASTER_SDA_IO,
246 .scl_io_num = (gpio_num_t)I2C_MASTER_SCL_IO,
247 .scl_speed_hz = I2C_FREQ_HZ,
248 .timeout_ms = CONFIG_I2C_U8G2_TIMEOUT_MS,
249 .device_address = I2C_DISPLAY_ADDRESS,
250 .enable_internal_pullup = I2C_INTERNAL_PULLUP,
251 .rotation = U8G2_R0,
252 .setup_fn = NULL,
253 };
254 return config;
255}
#define CONFIG_I2C_U8G2_TIMEOUT_MS
Definition sdkconfig.h:774
#define I2C_MASTER_SCL_IO
#define I2C_FREQ_HZ
#define I2C_INTERNAL_PULLUP
#define I2C_MASTER_NUM
#define I2C_DISPLAY_ADDRESS
#define I2C_MASTER_SDA_IO
Configuration for I2C-backed U8G2 display initialization.
i2c_port_num_t i2c_port
+ Here is the caller graph for this function:

◆ i2c_u8g2_deinit()

esp_err_t i2c_u8g2_deinit ( i2c_u8g2_handle_t *  handle)

Deinitialize display device and I2C bus.

Definition at line 367 of file priv_i2c_u8g2.c.

368{
369 ESP_RETURN_ON_FALSE(handle != NULL, ESP_ERR_INVALID_ARG, TAG, "handle is NULL");
370
371 if (handle->display_dev_handle != NULL) {
372 ESP_RETURN_ON_ERROR(i2c_master_bus_rm_device(handle->display_dev_handle),
373 TAG, "Failed to remove display device");
374 handle->display_dev_handle = NULL;
376 }
377
378 if (handle->i2c_bus_handle != NULL) {
379 ESP_RETURN_ON_ERROR(i2c_del_master_bus(handle->i2c_bus_handle),
380 TAG, "Failed to delete I2C master bus");
381 handle->i2c_bus_handle = NULL;
382 s_i2c_bus_handle = NULL;
383 }
384
385 if (s_active_handle == handle) {
386 s_active_handle = NULL;
387 }
388
389 s_display_available = false;
390 handle->initialized = false;
391 return ESP_OK;
392}
static bool s_display_available
static i2c_master_dev_handle_t s_display_dev_handle
static const char * TAG
static i2c_u8g2_handle_t * s_active_handle
static i2c_master_bus_handle_t s_i2c_bus_handle
i2c_master_bus_handle_t i2c_bus_handle
i2c_master_dev_handle_t display_dev_handle

◆ i2c_u8g2_get_u8g2()

u8g2_t * i2c_u8g2_get_u8g2 ( i2c_u8g2_handle_t *  handle)

Get the U8G2 object for draw operations.

Definition at line 394 of file priv_i2c_u8g2.c.

395{
396 if (handle == NULL || !handle->initialized) {
397 return NULL;
398 }
399 return &handle->u8g2;
400}
+ Here is the caller graph for this function:

◆ i2c_u8g2_init()

esp_err_t i2c_u8g2_init ( i2c_u8g2_handle_t *  handle,
const i2c_u8g2_config_t *  config 
)

Initialize I2C bus and U8G2 display.

If config->setup_fn is NULL, defaults to SSD1306 128x64 noname full buffer setup.

Definition at line 257 of file priv_i2c_u8g2.c.

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");
267 ESP_RETURN_ON_FALSE(config->device_address <= 0x7F, ESP_ERR_INVALID_ARG, TAG,
268 "device_address must be 7-bit");
269
270 memset(handle, 0, sizeof(*handle));
271 handle->timeout_ms = config->timeout_ms;
272 handle->scl_speed_hz = config->scl_speed_hz;
273 handle->device_address = config->device_address;
274
275 s_timeout_ms = config->timeout_ms;
278 s_display_available = false;
279
280 probe_timeout_ms = (config->timeout_ms < I2C_PROBE_TIMEOUT_MS_MAX)
281 ? config->timeout_ms
283
284 i2c_master_bus_config_t bus_config = {
285 .i2c_port = config->i2c_port,
286 .sda_io_num = config->sda_io_num,
287 .scl_io_num = config->scl_io_num,
288 .clk_source = I2C_CLK_SRC_DEFAULT,
289 .glitch_ignore_cnt = 7,
290 .intr_priority = 0,
291 .trans_queue_depth = 0,
292 .flags.enable_internal_pullup = config->enable_internal_pullup,
293 };
294
295 ESP_RETURN_ON_ERROR(i2c_new_master_bus(&bus_config, &s_i2c_bus_handle),
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];
304 size_t total_found = i2c_u8g2_scan_bus(found_addresses,
305 sizeof(found_addresses),
306 probe_timeout_ms);
307
308 i2c_u8g2_log_scan_results(found_addresses,
309 (total_found < sizeof(found_addresses)) ? total_found : sizeof(found_addresses),
310 total_found);
311 } else if (probe_ret == ESP_ERR_TIMEOUT) {
312 ESP_LOGW(TAG,
313 "I2C probe timed out; skipping full bus scan to avoid timeout spam");
314 } else {
315 ESP_LOGW(TAG,
316 "I2C probe failed before scan (%s)",
317 esp_err_to_name(probe_ret));
318 }
319
320 ESP_LOGW(TAG,
321 "No I2C display detected at 0x%02X (SDA=%d, SCL=%d, %uHz)",
322 config->device_address,
323 (int)config->sda_io_num,
324 (int)config->scl_io_num,
325 (unsigned int)config->scl_speed_hz);
326 i2c_del_master_bus(s_i2c_bus_handle);
327 s_i2c_bus_handle = NULL;
328 return (probe_ret == ESP_ERR_NOT_FOUND) ? ESP_ERR_NOT_FOUND : probe_ret;
329 }
330
331 handle->device_address = resolved_address;
332 s_device_address = resolved_address;
333
334 s_display_available = true;
335
336 /* Brief delay to allow display to stabilize after bus init */
337 vTaskDelay(pdMS_TO_TICKS(10));
338
340
341 i2c_u8g2_setup_fn_t setup_fn = config->setup_fn;
342 if (setup_fn == NULL) {
343 setup_fn = u8g2_Setup_ssd1315_i2c_128x64_noname_f;
344 }
345
346 setup_fn(&handle->u8g2,
347 config->rotation != NULL ? config->rotation : U8G2_R0,
350
351 s_active_handle = handle;
352
353 u8g2_InitDisplay(&handle->u8g2);
354 u8g2_SetPowerSave(&handle->u8g2, 0);
355
356 /* Test draw to verify I2C communication works */
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
363 handle->initialized = true;
364 return ESP_OK;
365}
static uint8_t s_device_address
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 uint16_t s_timeout_ms
static uint32_t s_scl_speed_hz
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.
gpio_num_t sda_io_num
gpio_num_t scl_io_num
const u8g2_cb_t * rotation
i2c_u8g2_setup_fn_t setup_fn
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ i2c_u8g2_set_power_save()

esp_err_t i2c_u8g2_set_power_save ( i2c_u8g2_handle_t *  handle,
bool  enable 
)

Set display power save mode.

Definition at line 402 of file priv_i2c_u8g2.c.

403{
404 ESP_RETURN_ON_FALSE(handle != NULL, ESP_ERR_INVALID_ARG, TAG, "handle is NULL");
405 ESP_RETURN_ON_FALSE(handle->initialized, ESP_ERR_INVALID_STATE, TAG, "display not initialized");
406
407 u8g2_SetPowerSave(&handle->u8g2, enable ? 1 : 0);
408 return ESP_OK;
409}