ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
x9c.cpp
Go to the documentation of this file.
1#include "x9c.h"
2#include "esphome/core/log.h"
3
4namespace esphome::x9c {
5
6static const char *const TAG = "x9c.output";
7
8void X9cOutput::trim_value(int32_t change_amount) {
9 if (change_amount == 0) {
10 return;
11 }
12
13 if (change_amount > 0) { // Set change direction
14 this->ud_pin_->digital_write(true);
15 } else {
16 this->ud_pin_->digital_write(false);
17 }
18
19 this->inc_pin_->digital_write(true);
20 this->cs_pin_->digital_write(false); // Select chip
21
22 for (int i = 0; i < abs(change_amount); i++) { // Move wiper
23 this->inc_pin_->digital_write(true);
25 this->inc_pin_->digital_write(false);
27 }
28
29 delayMicroseconds(100); // Let value settle
30
31 this->inc_pin_->digital_write(false);
32 this->cs_pin_->digital_write(true); // Deselect chip safely (no save)
33}
34
36 this->inc_pin_->get_pin();
37 this->inc_pin_->setup();
38 this->inc_pin_->digital_write(false);
39
40 this->cs_pin_->get_pin();
41 this->cs_pin_->setup();
42 this->cs_pin_->digital_write(true);
43
44 this->ud_pin_->get_pin();
45 this->ud_pin_->setup();
46
47 if (this->initial_value_ <= 0.50) {
48 this->trim_value(-101); // Set min value (beyond 0)
49 this->trim_value(lroundf(this->initial_value_ * 100));
50 } else {
51 this->trim_value(101); // Set max value (beyond 100)
52 this->trim_value(lroundf(this->initial_value_ * 100) - 100);
53 }
54 this->pot_value_ = this->initial_value_;
55 this->write_state(this->initial_value_);
56}
57
59 this->trim_value(lroundf((state - this->pot_value_) * 100));
60 this->pot_value_ = state;
61}
62
64 ESP_LOGCONFIG(TAG,
65 "X9C Potentiometer Output:\n"
66 " Initial Value: %f\n"
67 " Step Delay: %d",
68 this->initial_value_, this->step_delay_);
69 LOG_PIN(" Chip Select Pin: ", this->cs_pin_);
70 LOG_PIN(" Increment Pin: ", this->inc_pin_);
71 LOG_PIN(" Up/Down Pin: ", this->ud_pin_);
72 LOG_FLOAT_OUTPUT(this);
73}
74
75} // namespace esphome::x9c
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual uint8_t get_pin() const =0
void setup() override
Definition x9c.cpp:35
InternalGPIOPin * inc_pin_
Definition x9c.h:25
void trim_value(int32_t change_amount)
Definition x9c.cpp:8
InternalGPIOPin * ud_pin_
Definition x9c.h:26
void dump_config() override
Definition x9c.cpp:63
InternalGPIOPin * cs_pin_
Definition x9c.h:24
void write_state(float state) override
Definition x9c.cpp:58
bool state
Definition fan.h:2
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48