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