ESPHome  2022.12.8
filter.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
5 
6 #include <vector>
7 
8 namespace esphome {
9 
10 namespace binary_sensor {
11 
12 class BinarySensor;
13 
14 class Filter {
15  public:
16  virtual optional<bool> new_value(bool value, bool is_initial) = 0;
17 
18  void input(bool value, bool is_initial);
19 
20  void output(bool value, bool is_initial);
21 
22  protected:
23  friend BinarySensor;
24 
25  Filter *next_{nullptr};
26  BinarySensor *parent_{nullptr};
28 };
29 
30 class DelayedOnOffFilter : public Filter, public Component {
31  public:
32  explicit DelayedOnOffFilter(uint32_t delay);
33 
34  optional<bool> new_value(bool value, bool is_initial) override;
35 
36  float get_setup_priority() const override;
37 
38  protected:
39  uint32_t delay_;
40 };
41 
42 class DelayedOnFilter : public Filter, public Component {
43  public:
44  explicit DelayedOnFilter(uint32_t delay);
45 
46  optional<bool> new_value(bool value, bool is_initial) override;
47 
48  float get_setup_priority() const override;
49 
50  protected:
51  uint32_t delay_;
52 };
53 
54 class DelayedOffFilter : public Filter, public Component {
55  public:
56  explicit DelayedOffFilter(uint32_t delay);
57 
58  optional<bool> new_value(bool value, bool is_initial) override;
59 
60  float get_setup_priority() const override;
61 
62  protected:
63  uint32_t delay_;
64 };
65 
66 class InvertFilter : public Filter {
67  public:
68  optional<bool> new_value(bool value, bool is_initial) override;
69 };
70 
72  AutorepeatFilterTiming(uint32_t delay, uint32_t off, uint32_t on) {
73  this->delay = delay;
74  this->time_off = off;
75  this->time_on = on;
76  }
77  uint32_t delay;
78  uint32_t time_off;
79  uint32_t time_on;
80 };
81 
82 class AutorepeatFilter : public Filter, public Component {
83  public:
84  explicit AutorepeatFilter(std::vector<AutorepeatFilterTiming> timings);
85 
86  optional<bool> new_value(bool value, bool is_initial) override;
87 
88  float get_setup_priority() const override;
89 
90  protected:
91  void next_timing_();
92  void next_value_(bool val);
93 
94  std::vector<AutorepeatFilterTiming> timings_;
95  uint8_t active_timing_{0};
96 };
97 
98 class LambdaFilter : public Filter {
99  public:
100  explicit LambdaFilter(std::function<optional<bool>(bool)> f);
101 
102  optional<bool> new_value(bool value, bool is_initial) override;
103 
104  protected:
105  std::function<optional<bool>(bool)> f_;
106 };
107 
108 class UniqueFilter : public Filter {
109  public:
110  optional<bool> new_value(bool value, bool is_initial) override;
111 
112  protected:
113  optional<bool> last_value_{};
114 };
115 
116 } // namespace binary_sensor
117 
118 } // namespace esphome
std::vector< AutorepeatFilterTiming > timings_
Definition: filter.h:94
virtual optional< bool > new_value(bool value, bool is_initial)=0
void input(bool value, bool is_initial)
Definition: filter.cpp:22
void output(bool value, bool is_initial)
Definition: filter.cpp:12
BinarySensor * parent_
Definition: filter.h:26
Deduplicator< bool > dedup_
Definition: filter.h:27
AutorepeatFilterTiming(uint32_t delay, uint32_t off, uint32_t on)
Definition: filter.h:72
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:28
uint32_t val
Definition: datatypes.h:85
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27