ESPHome  2024.10.3
binary_output.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 
6 #ifdef USE_POWER_SUPPLY
8 #endif
9 
10 namespace esphome {
11 namespace output {
12 
13 #define LOG_BINARY_OUTPUT(this) \
14  if (this->inverted_) { \
15  ESP_LOGCONFIG(TAG, " Inverted: YES"); \
16  }
17 
18 class BinaryOutput {
19  public:
21  void set_inverted(bool inverted) { this->inverted_ = inverted; }
22 
23 #ifdef USE_POWER_SUPPLY
24 
30  void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); }
31 #endif
32 
34  virtual void set_state(bool state) {
35  if (state) {
36  this->turn_on();
37  } else {
38  this->turn_off();
39  }
40  }
41 
43  virtual void turn_on() {
44 #ifdef USE_POWER_SUPPLY
45  this->power_.request();
46 #endif
47  this->write_state(!this->inverted_);
48  }
49 
51  virtual void turn_off() {
52 #ifdef USE_POWER_SUPPLY
53  this->power_.unrequest();
54 #endif
55  this->write_state(this->inverted_);
56  }
57 
58  // ========== INTERNAL METHODS ==========
59  // (In most use cases you won't need these)
61  bool is_inverted() const { return this->inverted_; }
62 
63  protected:
64  virtual void write_state(bool state) = 0;
65 
66  bool inverted_{false};
67 #ifdef USE_POWER_SUPPLY
69 #endif
70 };
71 
72 } // namespace output
73 } // namespace esphome
virtual void turn_on()
Enable this binary output.
Definition: binary_output.h:43
virtual void turn_off()
Disable this binary output.
Definition: binary_output.h:51
virtual void set_state(bool state)
Enable or disable this binary output.
Definition: binary_output.h:34
bool is_inverted() const
Return whether this binary output is inverted.
Definition: binary_output.h:61
power_supply::PowerSupplyRequester power_
Definition: binary_output.h:68
void set_parent(PowerSupply *parent)
Definition: power_supply.h:47
void set_power_supply(power_supply::PowerSupply *power_supply)
Use this to connect up a power supply to this output.
Definition: binary_output.h:30
virtual void write_state(bool state)=0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_inverted(bool inverted)
Set the inversion state of this binary output.
Definition: binary_output.h:21
bool state
Definition: fan.h:34