6 Commits

7 changed files with 28 additions and 25 deletions

View File

@@ -1,9 +1,12 @@
# ESP32 ESP-IDF and ESP8266 RTOS SDK component for liquid crystal display module 1602(4)A via I2C connection (PCF8574)
# ESP32 ESP-IDF component for liquid crystal display module 1602(4)A via I2C connection (PCF8574(A))
## Tested on
1. [ESP8266 RTOS_SDK v3.4](https://docs.espressif.com/projects/esp8266-rtos-sdk/en/latest/index.html#)
2. [ESP32 ESP-IDF v5.4](https://docs.espressif.com/projects/esp-idf/en/release-v5.4/esp32/index.html)
1. [ESP32 ESP-IDF v5.5.1](https://docs.espressif.com/projects/esp-idf/en/v5.5.1/esp32/index.html)
## SAST Tools
[PVS-Studio](https://pvs-studio.com/pvs-studio/?utm_source=website&utm_medium=github&utm_campaign=open_source) - static analyzer for C, C++, C#, and Java code.
## Features
@@ -59,9 +62,9 @@ zh_pcf8574_handle_t lcd_160x_handle = {0};
void app_main(void)
{
esp_log_level_set("zh_160x_i2c", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
esp_log_level_set("zh_pcf8574", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
esp_log_level_set("zh_vector", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
esp_log_level_set("zh_160x_i2c", ESP_LOG_ERROR); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
esp_log_level_set("zh_pcf8574", ESP_LOG_ERROR); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
esp_log_level_set("zh_vector", ESP_LOG_ERROR); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig.
#ifdef CONFIG_IDF_TARGET_ESP8266
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
@@ -96,7 +99,7 @@ void app_main(void)
for (;;)
{
zh_160x_set_cursor(&lcd_160x_handle, 0, 0);
zh_160x_print_char(&lcd_160x_handle, "LCD 1602A");
zh_160x_print_char(&lcd_160x_handle, "LCD 160X");
zh_160x_set_cursor(&lcd_160x_handle, 1, 0);
zh_160x_print_char(&lcd_160x_handle, "Hello World!");
vTaskDelay(5000 / portTICK_PERIOD_MS);

View File

View File

@@ -1,9 +1,13 @@
/**
* @file zh_160x_i2c.h
*/
#pragma once
#include "zh_pcf8574.h"
#define ZH_LCD_16X2 1
#define ZH_LCD_16X4 0
#define ZH_LCD_16X2 1 /*!< LCD size 16x2. */
#define ZH_LCD_16X4 0 /*!< LCD size 16x3. */
#ifdef __cplusplus
extern "C"
@@ -33,7 +37,7 @@ extern "C"
* @brief Sets the cursor to a specific position on the LCD.
*
* @param[in] handle Pointer to unique PCF8574 handle.
* @param[in] row The row number (0 or 1).
* @param[in] row The row number.
* @param[in] col The column number (015).
*
* @return ESP_OK if success or an error code otherwise.
@@ -75,7 +79,7 @@ extern "C"
* @brief Displays a progress bar on a specific row of the LCD.
*
* @param[in] handle Pointer to unique PCF8574 handle.
* @param[in] row The row number (0 or 1).
* @param[in] row The row number.
* @param[in] progress The progress percentage (0100).
*
* @return ESP_OK if success or an error code otherwise.
@@ -86,7 +90,7 @@ extern "C"
* @brief Clears a specific row on the LCD.
*
* @param[in] handle Pointer to unique PCF8574 handle.
* @param[in] row The row number (0 or 1).
* @param[in] row The row number.
*
* @return ESP_OK if success or an error code otherwise.
*/

View File

@@ -1 +1 @@
2.0.0
4.0.0

View File

@@ -14,17 +14,11 @@ static const char *TAG = "zh_160x_i2c";
return err; \
}
#ifdef CONFIG_IDF_TARGET_ESP8266
#define esp_delay_us(x) os_delay_us(x)
#else
#define esp_delay_us(x) esp_rom_delay_us(x)
#endif
#define LCD_160X_PULSE \
zh_pcf8574_write_gpio(handle, 2, true); \
esp_delay_us(300); \
esp_rom_delay_us(500); \
zh_pcf8574_write_gpio(handle, 2, false); \
esp_delay_us(400);
esp_rom_delay_us(500);
static void _zh_160x_lcd_init(zh_pcf8574_handle_t *handle);
static void _zh_160x_send_command(zh_pcf8574_handle_t *handle, uint8_t command);
@@ -35,7 +29,9 @@ esp_err_t zh_160x_init(zh_pcf8574_handle_t *handle, bool size)
ZH_160X_I2C_LOGI("160X initialization started.");
ZH_160X_I2C_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "160X initialization failed. Invalid argument.");
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X initialization failed. PCF8574 not initialized.");
handle->system = size;
handle->system = heap_caps_calloc(1, sizeof(uint8_t), MALLOC_CAP_8BIT);
ZH_160X_I2C_CHECK(handle->system != NULL, ESP_ERR_INVALID_ARG, "160X initialization failed. Memory allocation fail.");
*(uint8_t *)handle->system = size;
_zh_160x_lcd_init(handle);
ZH_160X_I2C_LOGI("160X initialization completed successfully.");
return ESP_OK;
@@ -54,7 +50,7 @@ esp_err_t zh_160x_lcd_clear(zh_pcf8574_handle_t *handle)
esp_err_t zh_160x_set_cursor(zh_pcf8574_handle_t *handle, uint8_t row, uint8_t col)
{
ZH_160X_I2C_LOGI("160X set cursor started.");
ZH_160X_I2C_CHECK(row < ((handle->system == ZH_LCD_16X2) ? 2 : 4) && col < 16 && handle != NULL, ESP_ERR_INVALID_ARG, "160X set cursor failed. Invalid argument.");
ZH_160X_I2C_CHECK(row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && col < 16 && handle != NULL, ESP_ERR_INVALID_ARG, "160X set cursor failed. Invalid argument.");
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X set cursor failed. PCF8574 not initialized.");
_zh_160x_send_command(handle, 0x80 | ((row == 0) ? col : (row == 1) ? (0x40 + col)
: (row == 2) ? (0x10 + col)
@@ -103,7 +99,7 @@ esp_err_t zh_160x_print_float(zh_pcf8574_handle_t *handle, float num, uint8_t pr
esp_err_t zh_160x_print_progress_bar(zh_pcf8574_handle_t *handle, uint8_t row, uint8_t progress)
{
ZH_160X_I2C_LOGI("160X print progress bar started.");
ZH_160X_I2C_CHECK(row < ((handle->system == ZH_LCD_16X2) ? 2 : 4) && progress <= 100 && handle != NULL, ESP_ERR_INVALID_ARG, "160X print progress bar failed. Invalid argument.");
ZH_160X_I2C_CHECK(row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && progress <= 100 && handle != NULL, ESP_ERR_INVALID_ARG, "160X print progress bar failed. Invalid argument.");
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X print progress bar failed. PCF8574 not initialized.");
uint8_t blocks = (progress * 16) / 100;
zh_160x_set_cursor(handle, row, 0);
@@ -125,7 +121,7 @@ esp_err_t zh_160x_print_progress_bar(zh_pcf8574_handle_t *handle, uint8_t row, u
esp_err_t zh_160x_clear_row(zh_pcf8574_handle_t *handle, uint8_t row)
{
ZH_160X_I2C_LOGI("160X clear row started.");
ZH_160X_I2C_CHECK(row < ((handle->system == ZH_LCD_16X2) ? 2 : 4) && handle != NULL, ESP_ERR_INVALID_ARG, "160X clear row failed. Invalid argument.");
ZH_160X_I2C_CHECK(row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && handle != NULL, ESP_ERR_INVALID_ARG, "160X clear row failed. Invalid argument.");
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X clear row failed. PCF8574 not initialized.");
zh_160x_set_cursor(handle, row, 0);
for (uint8_t i = 0; i < 16; ++i)