Files
zh_avr_160x_i2c/zh_avr_160x_i2c.c
2025-08-12 12:25:03 +03:00

140 lines
4.9 KiB
C

#include "zh_avr_160x_i2c.h"
#define LCD_160X_PULSE \
zh_avr_pcf8574_write_gpio(handle, 2, true); \
_delay_us(300); \
zh_avr_pcf8574_write_gpio(handle, 2, false); \
_delay_us(400);
static void _zh_avr_160x_lcd_init(zh_avr_pcf8574_handle_t *handle);
static void _zh_avr_160x_send_command(zh_avr_pcf8574_handle_t *handle, uint8_t command);
static void _zh_avr_160x_send_data(zh_avr_pcf8574_handle_t *handle, uint8_t data);
avr_err_t zh_avr_160x_init(zh_avr_pcf8574_handle_t *handle, bool size)
{
ZH_ERROR_CHECK(handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
handle->system = pvPortCalloc(1, sizeof(uint8_t));
ZH_ERROR_CHECK(handle->system != NULL, AVR_ERR_INVALID_ARG);
*(uint8_t *)handle->system = size;
_zh_avr_160x_lcd_init(handle);
return AVR_OK;
}
avr_err_t zh_avr_160x_lcd_clear(zh_avr_pcf8574_handle_t *handle)
{
ZH_ERROR_CHECK(handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
_zh_avr_160x_send_command(handle, 0x01);
return AVR_OK;
}
avr_err_t zh_avr_160x_set_cursor(zh_avr_pcf8574_handle_t *handle, uint8_t row, uint8_t col)
{
ZH_ERROR_CHECK(row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && col < 16 && handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
_zh_avr_160x_send_command(handle, 0x80 | ((row == 0) ? col : (row == 1) ? (0x40 + col)
: (row == 2) ? (0x10 + col)
: (0x50 + col)));
return AVR_OK;
}
avr_err_t zh_avr_160x_print_char(zh_avr_pcf8574_handle_t *handle, const char *str)
{
ZH_ERROR_CHECK(str != NULL && handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
while (*str != 0)
{
_zh_avr_160x_send_data(handle, (uint8_t)*str++);
}
return AVR_OK;
}
avr_err_t zh_avr_160x_print_int(zh_avr_pcf8574_handle_t *handle, int num)
{
ZH_ERROR_CHECK(handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
char buffer[12];
sprintf(buffer, "%d", num);
zh_avr_160x_print_char(handle, buffer);
return AVR_OK;
}
avr_err_t zh_avr_160x_print_float(zh_avr_pcf8574_handle_t *handle, float num, uint8_t precision)
{
ZH_ERROR_CHECK(handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
char buffer[16];
sprintf(buffer, "%.*f", precision, num);
zh_avr_160x_print_char(handle, buffer);
return AVR_OK;
}
avr_err_t zh_avr_160x_print_progress_bar(zh_avr_pcf8574_handle_t *handle, uint8_t row, uint8_t progress)
{
ZH_ERROR_CHECK(row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && progress <= 100 && handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
uint8_t blocks = (progress * 16) / 100;
zh_avr_160x_set_cursor(handle, row, 0);
for (uint8_t i = 0; i < 16; ++i)
{
if (i < blocks)
{
zh_avr_160x_print_char(handle, "\xFF");
}
else
{
zh_avr_160x_print_char(handle, " ");
}
}
return AVR_OK;
}
avr_err_t zh_avr_160x_clear_row(zh_avr_pcf8574_handle_t *handle, uint8_t row)
{
ZH_ERROR_CHECK(row < ((*(uint8_t *)handle->system == ZH_LCD_16X2) ? 2 : 4) && handle != NULL, AVR_ERR_INVALID_ARG);
ZH_ERROR_CHECK(handle->is_initialized == true, AVR_ERR_INVALID_STATE);
zh_avr_160x_set_cursor(handle, row, 0);
for (uint8_t i = 0; i < 16; ++i)
{
zh_avr_160x_print_char(handle, " ");
}
zh_avr_160x_set_cursor(handle, row, 0);
return AVR_OK;
}
static void _zh_avr_160x_lcd_init(zh_avr_pcf8574_handle_t *handle)
{
vTaskDelay(20 / portTICK_PERIOD_MS);
zh_avr_pcf8574_write(handle, 0x30);
LCD_160X_PULSE;
zh_avr_pcf8574_write(handle, 0x30);
LCD_160X_PULSE;
zh_avr_pcf8574_write(handle, 0x30);
LCD_160X_PULSE;
zh_avr_pcf8574_write(handle, 0x20);
LCD_160X_PULSE;
_zh_avr_160x_send_command(handle, 0x28);
_zh_avr_160x_send_command(handle, 0x28);
_zh_avr_160x_send_command(handle, 0x28);
_zh_avr_160x_send_command(handle, 0x0C);
_zh_avr_160x_send_command(handle, 0x01);
_zh_avr_160x_send_command(handle, 0x06);
}
static void _zh_avr_160x_send_command(zh_avr_pcf8574_handle_t *handle, uint8_t command)
{
zh_avr_pcf8574_write(handle, (command & 0xF0) | 0x08);
LCD_160X_PULSE;
zh_avr_pcf8574_write(handle, (command << 4) | 0x08);
LCD_160X_PULSE;
}
static void _zh_avr_160x_send_data(zh_avr_pcf8574_handle_t *handle, uint8_t data)
{
zh_avr_pcf8574_write(handle, (data & 0xF0) | 0x09);
LCD_160X_PULSE;
zh_avr_pcf8574_write(handle, (data << 4) | 0x09);
LCD_160X_PULSE;
}