ESPHome 2026.6.2
Loading...
Searching...
No Matches
pcm5122_gpio.cpp
Go to the documentation of this file.
1#include "pcm5122_gpio.h"
2
4#include "esphome/core/log.h"
5
6namespace esphome::pcm5122 {
7
8static const char *const TAG = "pcm5122.gpio";
9
10void PCM5122GPIOPin::setup() { this->pin_mode(this->flags_); }
11
13 this->flags_ = flags;
14 if (!this->parent_->select_page_(0)) {
15 ESP_LOGE(TAG, "Failed to select page 0");
16 return;
17 }
18 optional<uint8_t> curr = this->parent_->read_byte(PCM5122_REG_GPIO_ENABLE);
19 if (!curr.has_value()) {
20 ESP_LOGE(TAG, "Failed to read GPIO_ENABLE");
21 return;
22 }
23 if (flags & gpio::FLAG_INPUT) {
24 this->parent_->reg(PCM5122_REG_GPIO_ENABLE) = curr.value() & ~(1 << (this->pin_ - 1));
25 } else if (flags & gpio::FLAG_OUTPUT) {
26 this->parent_->reg(PCM5122_REG_GPIO_ENABLE) = curr.value() | (1 << (this->pin_ - 1));
27 this->parent_->reg(PCM5122_REG_GPIO_OUTPUT_SELECT + (this->pin_ - 1)) = PCM5122_GPIO_OUTPUT_SELECT_REGISTER;
28 optional<uint8_t> invert = this->parent_->read_byte(PCM5122_REG_GPIO_INVERT);
29 if (!invert.has_value()) {
30 ESP_LOGE(TAG, "Failed to read GPIO_INVERT");
31 return;
32 }
33 if (this->inverted_) {
34 this->parent_->reg(PCM5122_REG_GPIO_INVERT) = invert.value() | (1 << (this->pin_ - 1));
35 } else {
36 this->parent_->reg(PCM5122_REG_GPIO_INVERT) = invert.value() & ~(1 << (this->pin_ - 1));
37 }
38 }
39}
40
42 if (!this->parent_->select_page_(0))
43 return;
44 optional<uint8_t> curr = this->parent_->read_byte(PCM5122_REG_GPIO_OUTPUT);
45 if (!curr.has_value())
46 return;
47 if (value) {
48 this->parent_->reg(PCM5122_REG_GPIO_OUTPUT) = curr.value() | (1 << (this->pin_ - 1));
49 } else {
50 this->parent_->reg(PCM5122_REG_GPIO_OUTPUT) = curr.value() & ~(1 << (this->pin_ - 1));
51 }
52}
53
55 if (!this->parent_->select_page_(0))
56 return this->value_;
57 optional<uint8_t> read = this->parent_->read_byte(PCM5122_REG_GPIO_INPUT);
58 if (read.has_value()) {
59 // GPIO input register has RSV at bit 0; GPIN_N is at bit N (unlike other GPIO registers)
60 this->value_ = !!(read.value() & (1 << this->pin_)) != this->inverted_;
61 }
62 return this->value_;
63}
64
65size_t PCM5122GPIOPin::dump_summary(char *buffer, size_t len) const {
66 return buf_append_printf(buffer, len, 0, "PCM5122 GPIO%u", this->pin_);
67}
68
69} // namespace esphome::pcm5122
void pin_mode(gpio::Flags flags) override
void digital_write(bool value) override
size_t dump_summary(char *buffer, size_t len) const override
uint16_t flags
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_INPUT
Definition gpio.h:27
const void size_t len
Definition hal.h:64