ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
waveshare_io_ch32v003.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
4#include <algorithm>
5
7
8static const uint8_t IO_EXTENSION_DIRECTION = 0x02;
9static const uint8_t IO_EXTENSION_IO_OUTPUT_ADDR = 0x03;
10static const uint8_t IO_EXTENSION_IO_INPUT_ADDR = 0x04;
11static const uint8_t IO_EXTENSION_PWM_ADDR = 0x05;
12static const uint8_t IO_EXTENSION_ADC_ADDR = 0x06;
13static const uint8_t IO_EXTENSION_RTC_INT_ADDR = 0x07;
14
15ESPHOME_LOG_TAG(TAG, "waveshare_io_ch32v003");
16
18 this->mode_mask_ = 0xFF; // Set all pins to output mode
19 this->output_mask_ = 0xFF; // Set all pins to high (output mode)
20
21 bool step1 = this->write_gpio_modes_();
22 bool step2 = this->write_gpio_outputs_();
23
24 if (!step1 || !step2) {
25 this->mark_failed(LOG_STR("Failed to initialize Waveshare IO expander"));
26 return;
27 }
28
29 this->disable_loop();
30}
31
33 // bits: 0 = input, 1 = output
34 if (flags == gpio::FLAG_INPUT) {
35 // Clear mode mask bit
36 this->mode_mask_ &= ~(1 << pin);
37 this->enable_loop();
38 } else if (flags == gpio::FLAG_OUTPUT) {
39 // Set mode mask bit
40 this->mode_mask_ |= 1 << pin;
41 }
42 this->write_gpio_modes_();
43}
44
46
48 ESP_LOGCONFIG(TAG, "WaveshareIO:");
49 LOG_I2C_DEVICE(this)
50}
51
53 if (this->is_failed())
54 return 0;
55
56 uint16_t adc_value;
57 if (!this->status_comm_result(this->read_byte_16_le(IO_EXTENSION_ADC_ADDR, &adc_value),
58 LOG_STR("Failed to read ADC register")))
59 return 0;
60 return adc_value;
61}
62
64 if (this->is_failed())
65 return 0;
66
67 uint8_t data = 0;
68 if (!this->status_comm_result(this->read_bytes(IO_EXTENSION_RTC_INT_ADDR, &data, 1),
69 LOG_STR("Failed to read RTC interrupt register")))
70 return 0;
71 return data;
72}
73
75 if (this->is_failed())
76 return;
77
78 // The safe PWM range is enforced by write_pwm_level(), as in the original Waveshare IO
79 // library function "void IO_EXTENSION_Pwm_Output(uint8_t Value)".
80 this->status_comm_result(this->write_byte(IO_EXTENSION_PWM_ADDR, value), LOG_STR("Failed to set PWM duty cycle"));
81}
82
83void WaveshareIOCH32V003Component::write_pwm_level(float state, uint8_t min_value, uint8_t max_value) {
84 auto pwm_value = static_cast<uint8_t>(state * 255.0f);
85 uint8_t final_pwm_value = std::clamp(pwm_value, min_value, max_value);
86 if (final_pwm_value != pwm_value) {
87 ESP_LOGVV(TAG, "Clamping PWM value %u to safe range [%u, %u]", pwm_value, min_value, max_value);
88 }
89 this->set_pwm_value(final_pwm_value);
90}
91
93 if (this->is_failed())
94 return false;
95 return this->status_comm_result(this->write_byte(IO_EXTENSION_DIRECTION, this->mode_mask_),
96 LOG_STR("Failed to write mode register"));
97}
98
100 if (this->is_failed())
101 return false;
102 return this->status_comm_result(this->write_byte(IO_EXTENSION_IO_OUTPUT_ADDR, this->output_mask_),
103 LOG_STR("Failed to write output register"));
104}
105
107 if (this->is_failed())
108 return false;
109
110 uint8_t data = 0;
111 if (!this->status_comm_result(this->read_bytes(IO_EXTENSION_IO_INPUT_ADDR, &data, 1),
112 LOG_STR("Failed to read input register")))
113 return false;
114 this->input_mask_ = data;
115 return true;
116}
117
119 if (this->is_failed())
120 return;
121
122 assign_bits(this->output_mask_, 1 << pin, value);
123
124 uint8_t data = this->output_mask_;
125 this->status_comm_result(this->write_byte(IO_EXTENSION_IO_OUTPUT_ADDR, data),
126 LOG_STR("Failed to write output register"));
127}
128
129bool WaveshareIOCH32V003Component::digital_read_cache(uint8_t pin) { return this->input_mask_ & (1 << pin); }
131
132} // namespace esphome::waveshare_io_ch32v003
133
134ESPHOME_EXPANDER_PIN_SUMMARY(esphome::waveshare_io_ch32v003::WaveshareIOCH32V003Component, "EXIO%u via WaveshareIO")
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void enable_loop()
Enable this component's loop.
Definition component.h:261
void disable_loop()
Disable this component's loop.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
bool read_byte_16_le(uint8_t a_register, uint16_t *data)
Read a 16-bit value stored least significant byte first.
Definition i2c.h:296
void write_pwm_level(float state, uint8_t min_value, uint8_t max_value)
Write an output level, 0.0 to 1.0, clamped to the board's safe PWM range.
uint16_t flags
bool state
Definition fan.h:2
constexpr float IO
For components that represent GPIO pins like PCF8573.
Definition component.h:41
ESPHOME_LOG_TAG(TAG, "waveshare_io_ch32v003.sensor")
uint32_t value
Definition hal.cpp:41
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150