ESPHome  2024.11.0
dfrobot_sen0395.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #ifdef USE_BINARY_SENSOR
9 #endif
10 #ifdef USE_SWITCH
12 #endif
13 
14 #include "commands.h"
15 
16 namespace esphome {
17 namespace dfrobot_sen0395 {
18 
19 const uint8_t MMWAVE_READ_BUFFER_LENGTH = 255;
20 
21 // forward declaration due to circular dependency
23 
24 static const uint8_t COMMAND_QUEUE_SIZE = 20;
25 
27  public:
28  int8_t enqueue(std::unique_ptr<Command> cmd);
29  std::unique_ptr<Command> dequeue();
30  bool is_empty();
31  bool is_full();
32  uint8_t process(DfrobotSen0395Component *parent);
33 
34  protected:
35  int front_{-1};
36  int rear_{-1};
37  std::unique_ptr<Command> commands_[COMMAND_QUEUE_SIZE];
38 };
39 
41 #ifdef USE_SWITCH
42  SUB_SWITCH(sensor_active)
43  SUB_SWITCH(turn_on_led)
44  SUB_SWITCH(presence_via_uart)
45  SUB_SWITCH(start_after_boot)
46 #endif
47 
48  public:
49  void dump_config() override;
50  void loop() override;
51  void set_active(bool active) {
52  if (active != active_) {
53 #ifdef USE_SWITCH
54  if (this->sensor_active_switch_ != nullptr)
55  this->sensor_active_switch_->publish_state(active);
56 #endif
57  active_ = active;
58  }
59  }
60  bool is_active() { return active_; }
61 
62  void set_led_active(bool active) {
63  if (led_active_ != active) {
64 #ifdef USE_SWITCH
65  if (this->turn_on_led_switch_ != nullptr)
66  this->turn_on_led_switch_->publish_state(active);
67 #endif
68  led_active_ = active;
69  }
70  }
71  bool is_led_active() { return led_active_; }
72 
73  void set_uart_presence_active(bool active) {
74  uart_presence_active_ = active;
75 #ifdef USE_SWITCH
76  if (this->presence_via_uart_switch_ != nullptr)
77  this->presence_via_uart_switch_->publish_state(active);
78 #endif
79  }
80  bool is_uart_presence_active() { return uart_presence_active_; }
81 
82  void set_start_after_boot(bool start) {
83  start_after_boot_ = start;
84 #ifdef USE_SWITCH
85  if (this->start_after_boot_switch_ != nullptr)
86  this->start_after_boot_switch_->publish_state(start);
87 #endif
88  }
89  bool does_start_after_boot() { return start_after_boot_; }
90 
91 #ifdef USE_BINARY_SENSOR
92  void set_detected_binary_sensor(binary_sensor::BinarySensor *detected_binary_sensor) {
93  detected_binary_sensor_ = detected_binary_sensor;
94  }
95 #endif
96 
97  int8_t enqueue(std::unique_ptr<Command> cmd);
98 
99  protected:
100 #ifdef USE_BINARY_SENSOR
101  binary_sensor::BinarySensor *detected_binary_sensor_{nullptr};
102 #endif
103 
104  bool detected_{false};
105  bool active_{false};
106  bool led_active_{false};
107  bool uart_presence_active_{false};
108  bool start_after_boot_{false};
109  char read_buffer_[MMWAVE_READ_BUFFER_LENGTH];
110  size_t read_pos_{0};
112  uint32_t ts_last_cmd_sent_{0};
113 
114  uint8_t read_message_();
115  uint8_t find_prompt_();
116  uint8_t send_cmd_(const char *cmd, uint32_t duration);
117 
118  void set_detected_(bool detected);
119 
120  friend class Command;
121  friend class ReadStateCommand;
122 };
123 
124 } // namespace dfrobot_sen0395
125 } // namespace esphome
void loop()
uint8_t process(DfrobotSen0395Component *parent)
int8_t enqueue(std::unique_ptr< Command > cmd)
const uint8_t MMWAVE_READ_BUFFER_LENGTH
std::unique_ptr< Command > commands_[COMMAND_QUEUE_SIZE]
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
stm32_cmd_t * cmd
Definition: stm32flash.h:96