ESPHome 2025.6.3
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
5namespace esphome {
6namespace lcd_pcf8574 {
7
8static const char *const TAG = "lcd_pcf8574";
9
10static const uint8_t LCD_DISPLAY_BACKLIGHT_ON = 0x08;
11static const uint8_t LCD_DISPLAY_BACKLIGHT_OFF = 0x00;
12
14 ESP_LOGCONFIG(TAG, "Running setup");
15 this->backlight_value_ = LCD_DISPLAY_BACKLIGHT_ON;
16 if (!this->write_bytes(this->backlight_value_, nullptr, 0)) {
17 this->mark_failed();
18 return;
19 }
20
21 LCDDisplay::setup();
22}
24 ESP_LOGCONFIG(TAG,
25 "PCF8574 LCD Display:\n"
26 " Columns: %u, Rows: %u",
27 this->columns_, this->rows_);
28 LOG_I2C_DEVICE(this);
29 LOG_UPDATE_INTERVAL(this);
30 if (this->is_failed()) {
31 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
32 }
33}
34void PCF8574LCDDisplay::write_n_bits(uint8_t value, uint8_t n) {
35 if (n == 4) {
36 // Ugly fix: in the super setup() with n == 4 value needs to be shifted left
37 value <<= 4;
38 }
39 uint8_t data = value | this->backlight_value_; // Set backlight state
40 this->write_bytes(data, nullptr, 0);
41 // Pulse ENABLE
42 this->write_bytes(data | 0x04, nullptr, 0);
43 delayMicroseconds(1); // >450ns
44 this->write_bytes(data, nullptr, 0);
45 delayMicroseconds(100); // >37us
46}
47void PCF8574LCDDisplay::send(uint8_t value, bool rs) {
48 this->write_n_bits((value & 0xF0) | rs, 0);
49 this->write_n_bits(((value << 4) & 0xF0) | rs, 0);
50}
52 this->backlight_value_ = LCD_DISPLAY_BACKLIGHT_ON;
53 this->write_bytes(this->backlight_value_, nullptr, 0);
54}
56 this->backlight_value_ = LCD_DISPLAY_BACKLIGHT_OFF;
57 this->write_bytes(this->backlight_value_, nullptr, 0);
58}
59
60} // namespace lcd_pcf8574
61} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
void send(uint8_t value, bool rs) override
void write_n_bits(uint8_t value, uint8_t n) override
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31