Compare commits
4 Commits
f1ce1febc8
...
c7f8a0f449
| Author | SHA1 | Date | |
|---|---|---|---|
| c7f8a0f449 | |||
| 41c706d4bb | |||
| dc1f2ed526 | |||
| f7ba65e55c |
57
README.md
57
README.md
@@ -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
|
||||
|
||||
@@ -51,48 +54,29 @@ One LCD on bus:
|
||||
|
||||
#define I2C_PORT (I2C_NUM_MAX - 1)
|
||||
|
||||
#ifndef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_master_bus_handle_t i2c_bus_handle = NULL;
|
||||
#endif
|
||||
|
||||
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.
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
i2c_config_t i2c_config = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = GPIO_NUM_4, // In accordance with used chip.
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_io_num = GPIO_NUM_5, // In accordance with used chip.
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
};
|
||||
i2c_driver_install(I2C_PORT, i2c_config.mode);
|
||||
i2c_param_config(I2C_PORT, &i2c_config);
|
||||
#else
|
||||
esp_log_level_set("zh_160x_i2c", ESP_LOG_ERROR);
|
||||
esp_log_level_set("zh_pcf8574", ESP_LOG_ERROR);
|
||||
esp_log_level_set("zh_vector", ESP_LOG_ERROR);
|
||||
i2c_master_bus_config_t i2c_bus_config = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = I2C_PORT,
|
||||
.scl_io_num = GPIO_NUM_22, // In accordance with used chip.
|
||||
.sda_io_num = GPIO_NUM_21, // In accordance with used chip.
|
||||
.scl_io_num = GPIO_NUM_22,
|
||||
.sda_io_num = GPIO_NUM_21,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle);
|
||||
#endif
|
||||
zh_pcf8574_init_config_t pcf8574_init_config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
pcf8574_init_config.i2c_port = I2C_PORT;
|
||||
#else
|
||||
pcf8574_init_config.i2c_handle = i2c_bus_handle;
|
||||
#endif
|
||||
pcf8574_init_config.i2c_address = 0x27;
|
||||
zh_pcf8574_init(&pcf8574_init_config, &lcd_160x_handle);
|
||||
zh_160x_init(&lcd_160x_handle, ZH_LCD_16X2); // For LCD 16X2.
|
||||
// zh_160x_init(&lcd_160x_handle, ZH_LCD_16X4); // For LCD 16X4.
|
||||
zh_pcf8574_init_config_t config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
|
||||
config.i2c_handle = i2c_bus_handle;
|
||||
config.i2c_address = 0x27;
|
||||
zh_pcf8574_init(&config, &lcd_160x_handle);
|
||||
zh_160x_init(&lcd_160x_handle, ZH_LCD_16X2);
|
||||
for (;;)
|
||||
{
|
||||
zh_160x_set_cursor(&lcd_160x_handle, 0, 0);
|
||||
@@ -100,17 +84,14 @@ void app_main(void)
|
||||
zh_160x_set_cursor(&lcd_160x_handle, 1, 0);
|
||||
zh_160x_print_char(&lcd_160x_handle, "Hello World!");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
zh_160x_set_cursor(&lcd_160x_handle, 0, 0); // For LCD 16X2.
|
||||
// zh_160x_set_cursor(&lcd_160x_handle, 2, 0); // For LCD 16X4.
|
||||
zh_160x_set_cursor(&lcd_160x_handle, 0, 0);
|
||||
zh_160x_print_char(&lcd_160x_handle, "Progress: ");
|
||||
for (uint8_t i = 0; i <= 100; ++i)
|
||||
{
|
||||
zh_160x_set_cursor(&lcd_160x_handle, 0, 10); // For LCD 16X2.
|
||||
// zh_160x_set_cursor(&lcd_160x_handle, 2, 10); // For LCD 16X4.
|
||||
zh_160x_set_cursor(&lcd_160x_handle, 0, 10);
|
||||
zh_160x_print_int(&lcd_160x_handle, i);
|
||||
zh_160x_print_char(&lcd_160x_handle, "%");
|
||||
zh_160x_print_progress_bar(&lcd_160x_handle, 1, i); // For LCD 16X2.
|
||||
// zh_160x_print_progress_bar(&lcd_160x_handle, 3, i); // For LCD 16X4.
|
||||
zh_160x_print_progress_bar(&lcd_160x_handle, 1, i);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
|
||||
@@ -1 +1 @@
|
||||
3.0.1
|
||||
4.0.0
|
||||
228
zh_160x_i2c.c
228
zh_160x_i2c.c
@@ -2,174 +2,210 @@
|
||||
|
||||
static const char *TAG = "zh_160x_i2c";
|
||||
|
||||
#define ZH_160X_I2C_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
|
||||
#define ZH_160X_I2C_LOGW(msg, ...) ESP_LOGW(TAG, msg, ##__VA_ARGS__)
|
||||
#define ZH_160X_I2C_LOGE(msg, ...) ESP_LOGE(TAG, msg, ##__VA_ARGS__)
|
||||
#define ZH_160X_I2C_LOGE_ERR(msg, err, ...) ESP_LOGE(TAG, "[%s:%d:%s] " msg, __FILE__, __LINE__, esp_err_to_name(err), ##__VA_ARGS__)
|
||||
#define ZH_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
|
||||
#define ZH_LOGE(msg, err, ...) ESP_LOGE(TAG, "[%s:%d:%s] " msg, __FILE__, __LINE__, esp_err_to_name(err), ##__VA_ARGS__)
|
||||
|
||||
#define ZH_160X_I2C_CHECK(cond, err, msg, ...) \
|
||||
if (!(cond)) \
|
||||
{ \
|
||||
ZH_160X_I2C_LOGE_ERR(msg, err); \
|
||||
return err; \
|
||||
#define ZH_ERROR_CHECK(cond, err, cleanup, msg, ...) \
|
||||
if (!(cond)) \
|
||||
{ \
|
||||
ZH_LOGE(msg, err, ##__VA_ARGS__); \
|
||||
cleanup; \
|
||||
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(500); \
|
||||
zh_pcf8574_write_gpio(handle, 2, false); \
|
||||
esp_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);
|
||||
static void _zh_160x_send_data(zh_pcf8574_handle_t *handle, uint8_t data);
|
||||
static esp_err_t _zh_160x_lcd_init(zh_pcf8574_handle_t *handle);
|
||||
static esp_err_t _zh_160x_send_command(zh_pcf8574_handle_t *handle, uint8_t command);
|
||||
static esp_err_t _zh_160x_send_data(zh_pcf8574_handle_t *handle, uint8_t data);
|
||||
static esp_err_t _zh_160x_pulse(zh_pcf8574_handle_t *handle);
|
||||
|
||||
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.");
|
||||
ZH_LOGI("160X initialization started.");
|
||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "160X initialization failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X initialization failed. PCF8574 not initialized.");
|
||||
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.");
|
||||
ZH_ERROR_CHECK(handle->system != NULL, ESP_ERR_INVALID_ARG, NULL, "160X initialization failed. Memory allocation fail.");
|
||||
*(uint8_t *)handle->system = size;
|
||||
_zh_160x_lcd_init(handle);
|
||||
ZH_160X_I2C_LOGI("160X initialization completed successfully.");
|
||||
esp_err_t err = _zh_160x_lcd_init(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X initialization failed. PCF8574 error.");
|
||||
ZH_LOGI("160X initialization completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_160x_lcd_clear(zh_pcf8574_handle_t *handle)
|
||||
{
|
||||
ZH_160X_I2C_LOGI("160X display cleaning started.");
|
||||
ZH_160X_I2C_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "160X display cleaning failed. Invalid argument.");
|
||||
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X display cleaning failed. PCF8574 not initialized.");
|
||||
_zh_160x_send_command(handle, 0x01);
|
||||
ZH_160X_I2C_LOGI("160X display cleaning completed successfully.");
|
||||
ZH_LOGI("160X display cleaning started.");
|
||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "160X display cleaning failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X display cleaning failed. PCF8574 not initialized.");
|
||||
esp_err_t err = _zh_160x_send_command(handle, 0x01);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X display cleaning failed. PCF8574 error.");
|
||||
ZH_LOGI("160X display cleaning completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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 < ((*(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)
|
||||
: (0x50 + col)));
|
||||
ZH_160X_I2C_LOGI("160X set cursor completed successfully.");
|
||||
ZH_LOGI("160X set cursor started.");
|
||||
ZH_ERROR_CHECK(handle != NULL && row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && col < 16, ESP_ERR_INVALID_ARG, NULL, "160X set cursor failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X set cursor failed. PCF8574 not initialized.");
|
||||
esp_err_t err = _zh_160x_send_command(handle, 0x80 | ((row == 0) ? col : (row == 1) ? (0x40 + col)
|
||||
: (row == 2) ? (0x10 + col)
|
||||
: (0x50 + col)));
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X set cursor failed. PCF8574 error.");
|
||||
ZH_LOGI("160X set cursor completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_160x_print_char(zh_pcf8574_handle_t *handle, const char *str)
|
||||
{
|
||||
ZH_160X_I2C_LOGI("160X print char started.");
|
||||
ZH_160X_I2C_CHECK(str != NULL && handle != NULL, ESP_ERR_INVALID_ARG, "160X print char failed. Invalid argument.");
|
||||
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X print char failed. PCF8574 not initialized.");
|
||||
ZH_LOGI("160X print char started.");
|
||||
ZH_ERROR_CHECK(str != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "160X print char failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X print char failed. PCF8574 not initialized.");
|
||||
while (*str != 0)
|
||||
{
|
||||
_zh_160x_send_data(handle, (uint8_t)*str++);
|
||||
esp_err_t err = _zh_160x_send_data(handle, (uint8_t)*str++);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X print char failed. PCF8574 error.");
|
||||
}
|
||||
ZH_160X_I2C_LOGI("160X print char completed successfully.");
|
||||
ZH_LOGI("160X print char completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_160x_print_int(zh_pcf8574_handle_t *handle, int num)
|
||||
{
|
||||
ZH_160X_I2C_LOGI("160X print int started.");
|
||||
ZH_160X_I2C_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "160X print int failed. Invalid argument.");
|
||||
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X print int failed. PCF8574 not initialized.");
|
||||
ZH_LOGI("160X print int started.");
|
||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "160X print int failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X print int failed. PCF8574 not initialized.");
|
||||
char buffer[12];
|
||||
sprintf(buffer, "%d", num);
|
||||
zh_160x_print_char(handle, buffer);
|
||||
ZH_160X_I2C_LOGI("160X print int completed successfully.");
|
||||
esp_err_t err = zh_160x_print_char(handle, buffer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X print int failed. PCF8574 error.");
|
||||
ZH_LOGI("160X print int completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t zh_160x_print_float(zh_pcf8574_handle_t *handle, float num, uint8_t precision)
|
||||
{
|
||||
ZH_160X_I2C_LOGI("160X print float started.");
|
||||
ZH_160X_I2C_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, "160X print float failed. Invalid argument.");
|
||||
ZH_160X_I2C_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, "160X print float failed. PCF8574 not initialized.");
|
||||
ZH_LOGI("160X print float started.");
|
||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "160X print float failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X print float failed. PCF8574 not initialized.");
|
||||
char buffer[16];
|
||||
sprintf(buffer, "%.*f", precision, num);
|
||||
zh_160x_print_char(handle, buffer);
|
||||
ZH_160X_I2C_LOGI("160X print float completed successfully.");
|
||||
esp_err_t err = zh_160x_print_char(handle, buffer);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X print float failed. PCF8574 error.");
|
||||
ZH_LOGI("160X print float completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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 < ((*(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.");
|
||||
ZH_LOGI("160X print progress bar started.");
|
||||
ZH_ERROR_CHECK(handle != NULL && row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && progress <= 100, ESP_ERR_INVALID_ARG, NULL, "160X print progress bar failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X print progress bar failed. PCF8574 not initialized.");
|
||||
uint8_t blocks = (progress * 16) / 100;
|
||||
zh_160x_set_cursor(handle, row, 0);
|
||||
esp_err_t err = zh_160x_set_cursor(handle, row, 0);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X print progress bar failed. PCF8574 error.");
|
||||
for (uint8_t i = 0; i < 16; ++i)
|
||||
{
|
||||
if (i < blocks)
|
||||
{
|
||||
zh_160x_print_char(handle, "\xFF");
|
||||
err = zh_160x_print_char(handle, "\xFF");
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X print progress bar failed. PCF8574 error.");
|
||||
}
|
||||
else
|
||||
{
|
||||
zh_160x_print_char(handle, " ");
|
||||
err = zh_160x_print_char(handle, " ");
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X print progress bar failed. PCF8574 error.");
|
||||
}
|
||||
}
|
||||
ZH_160X_I2C_LOGI("160X print progress bar completed successfully.");
|
||||
ZH_LOGI("160X print progress bar completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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 < ((*(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);
|
||||
ZH_LOGI("160X clear row started.");
|
||||
ZH_ERROR_CHECK(handle != NULL && row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4), ESP_ERR_INVALID_ARG, NULL, "160X clear row failed. Invalid argument.");
|
||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "160X clear row failed. PCF8574 not initialized.");
|
||||
esp_err_t err = zh_160x_set_cursor(handle, row, 0);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X clear row failed. PCF8574 error.");
|
||||
for (uint8_t i = 0; i < 16; ++i)
|
||||
{
|
||||
zh_160x_print_char(handle, " ");
|
||||
err = zh_160x_print_char(handle, " ");
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X clear row failed. PCF8574 error.");
|
||||
}
|
||||
zh_160x_set_cursor(handle, row, 0);
|
||||
ZH_160X_I2C_LOGI("160X clear row completed successfully.");
|
||||
err = zh_160x_set_cursor(handle, row, 0);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "160X clear row failed. PCF8574 error.");
|
||||
ZH_LOGI("160X clear row completed successfully.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void _zh_160x_lcd_init(zh_pcf8574_handle_t *handle)
|
||||
static esp_err_t _zh_160x_lcd_init(zh_pcf8574_handle_t *handle)
|
||||
{
|
||||
vTaskDelay(20 / portTICK_PERIOD_MS);
|
||||
zh_pcf8574_write(handle, 0x30);
|
||||
LCD_160X_PULSE;
|
||||
zh_pcf8574_write(handle, 0x30);
|
||||
LCD_160X_PULSE;
|
||||
zh_pcf8574_write(handle, 0x30);
|
||||
LCD_160X_PULSE;
|
||||
zh_pcf8574_write(handle, 0x20);
|
||||
LCD_160X_PULSE;
|
||||
_zh_160x_send_command(handle, 0x28);
|
||||
_zh_160x_send_command(handle, 0x28);
|
||||
_zh_160x_send_command(handle, 0x28);
|
||||
_zh_160x_send_command(handle, 0x0C);
|
||||
_zh_160x_send_command(handle, 0x01);
|
||||
_zh_160x_send_command(handle, 0x06);
|
||||
esp_err_t err = zh_pcf8574_write(handle, 0x30);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = zh_pcf8574_write(handle, 0x30);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = zh_pcf8574_write(handle, 0x30);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = zh_pcf8574_write(handle, 0x20);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_send_command(handle, 0x28);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_send_command(handle, 0x28);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_send_command(handle, 0x28);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_send_command(handle, 0x0C);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_send_command(handle, 0x01);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_send_command(handle, 0x06);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void _zh_160x_send_command(zh_pcf8574_handle_t *handle, uint8_t command)
|
||||
static esp_err_t _zh_160x_send_command(zh_pcf8574_handle_t *handle, uint8_t command)
|
||||
{
|
||||
zh_pcf8574_write(handle, (command & 0xF0) | 0x08);
|
||||
LCD_160X_PULSE;
|
||||
zh_pcf8574_write(handle, (command << 4) | 0x08);
|
||||
LCD_160X_PULSE;
|
||||
esp_err_t err = zh_pcf8574_write(handle, (command & 0xF0) | 0x08);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = zh_pcf8574_write(handle, (command << 4) | 0x08);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void _zh_160x_send_data(zh_pcf8574_handle_t *handle, uint8_t data)
|
||||
static esp_err_t _zh_160x_send_data(zh_pcf8574_handle_t *handle, uint8_t data)
|
||||
{
|
||||
zh_pcf8574_write(handle, (data & 0xF0) | 0x09);
|
||||
LCD_160X_PULSE;
|
||||
zh_pcf8574_write(handle, (data << 4) | 0x09);
|
||||
LCD_160X_PULSE;
|
||||
esp_err_t err = zh_pcf8574_write(handle, (data & 0xF0) | 0x09);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = zh_pcf8574_write(handle, (data << 4) | 0x09);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
err = _zh_160x_pulse(handle);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t _zh_160x_pulse(zh_pcf8574_handle_t *handle)
|
||||
{
|
||||
esp_err_t err = zh_pcf8574_write_gpio(handle, 2, true);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
vTaskDelay(1 / portTICK_PERIOD_MS);
|
||||
err = zh_pcf8574_write_gpio(handle, 2, false);
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "PCF8574 error.");
|
||||
vTaskDelay(1 / portTICK_PERIOD_MS);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user