ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pcf8574_display.cpp
Go to the documentation of this file.
1#include "pcf8574_display.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
6
7ESPHOME_LOG_TAG(TAG, "lcd_pcf8574");
8
9static const uint8_t LCD_DISPLAY_BACKLIGHT_ON = 0x08;
10static const uint8_t LCD_DISPLAY_BACKLIGHT_OFF = 0x00;
11
13 this->backlight_value_ = LCD_DISPLAY_BACKLIGHT_ON;
14 if (!this->write_bytes(this->backlight_value_, nullptr, 0)) {
15 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
16 return;
17 }
18
19 LCDDisplay::setup();
20}
22 ESP_LOGCONFIG(TAG,
23 "PCF8574 LCD Display:\n"
24 " Columns: %u, Rows: %u",
25 this->columns_, this->rows_);
26 LOG_I2C_DEVICE(this);
27 LOG_UPDATE_INTERVAL(this);
28}
29void PCF8574LCDDisplay::write_n_bits(uint8_t value, uint8_t n) {
30 if (n == 4) {
31 // Ugly fix: in the super setup() with n == 4 value needs to be shifted left
32 value <<= 4;
33 }
34 uint8_t data = value | this->backlight_value_; // Set backlight state
35 this->write_bytes(data, nullptr, 0);
36 // Pulse ENABLE
37 this->write_bytes(data | 0x04, nullptr, 0);
38 delayMicroseconds(1); // >450ns
39 this->write_bytes(data, nullptr, 0);
40 delayMicroseconds(100); // >37us
41}
42void PCF8574LCDDisplay::send(uint8_t value, bool rs) {
43 this->write_n_bits((value & 0xF0) | rs, 0);
44 this->write_n_bits(((value << 4) & 0xF0) | rs, 0);
45}
47 this->backlight_value_ = LCD_DISPLAY_BACKLIGHT_ON;
48 this->write_bytes(this->backlight_value_, nullptr, 0);
49}
51 this->backlight_value_ = LCD_DISPLAY_BACKLIGHT_OFF;
52 this->write_bytes(this->backlight_value_, nullptr, 0);
53}
54
55} // namespace esphome::lcd_pcf8574
void mark_failed()
Mark this component as failed.
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
void send(uint8_t value, bool rs) override
void write_n_bits(uint8_t value, uint8_t n) override
ESPHOME_LOG_TAG(TAG, "lcd_pcf8574")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48