ESPHome  2024.11.1
commands.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <string>
5 
6 #include "esphome/core/helpers.h"
7 
8 namespace esphome {
9 namespace dfrobot_sen0395 {
10 
11 class DfrobotSen0395Component;
12 
13 // Use command queue and time stamps to avoid blocking.
14 // When component has run time, check if minimum time (1s) between
15 // commands has passed. After that run a command from the queue.
16 class Command {
17  public:
18  virtual ~Command() = default;
19  virtual uint8_t execute(DfrobotSen0395Component *parent);
20  virtual uint8_t on_message(std::string &message) = 0;
21 
22  protected:
24  std::string cmd_;
25  bool cmd_sent_{false};
26  int8_t retries_left_{2};
27  uint32_t cmd_duration_ms_{1000};
28  uint32_t timeout_ms_{1500};
29 };
30 
31 class ReadStateCommand : public Command {
32  public:
33  uint8_t execute(DfrobotSen0395Component *parent) override;
34  uint8_t on_message(std::string &message) override;
35 
36  protected:
37  uint32_t timeout_ms_{500};
38 };
39 
40 class PowerCommand : public Command {
41  public:
42  PowerCommand(bool power_on) : power_on_(power_on) {
43  if (power_on) {
44  cmd_ = "sensorStart";
45  } else {
46  cmd_ = "sensorStop";
47  }
48  };
49  uint8_t on_message(std::string &message) override;
50 
51  protected:
52  bool power_on_;
53 };
54 
55 class DetRangeCfgCommand : public Command {
56  public:
57  DetRangeCfgCommand(float min1, float max1, float min2, float max2, float min3, float max3, float min4, float max4);
58  uint8_t on_message(std::string &message) override;
59 
60  protected:
61  float min1_, max1_, min2_, max2_, min3_, max3_, min4_, max4_;
62  // TODO: Set min max values in component, so they can be published as sensor.
63 };
64 
65 class SetLatencyCommand : public Command {
66  public:
67  SetLatencyCommand(float delay_after_detection, float delay_after_disappear);
68  uint8_t on_message(std::string &message) override;
69 
70  protected:
73 };
74 
76  public:
77  SensorCfgStartCommand(bool startup_mode) : startup_mode_(startup_mode) {
78  char tmp_cmd[20] = {0};
79  sprintf(tmp_cmd, "sensorCfgStart %d", startup_mode);
80  cmd_ = std::string(tmp_cmd);
81  }
82  uint8_t on_message(std::string &message) override;
83 
84  protected:
86 };
87 
88 class FactoryResetCommand : public Command {
89  public:
90  FactoryResetCommand() { cmd_ = "factoryReset 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89"; };
91  uint8_t on_message(std::string &message) override;
92 };
93 
94 class ResetSystemCommand : public Command {
95  public:
96  ResetSystemCommand() { cmd_ = "resetSystem"; }
97  uint8_t on_message(std::string &message) override;
98 };
99 
100 class SaveCfgCommand : public Command {
101  public:
102  SaveCfgCommand() { cmd_ = "saveCfg 0x45670123 0xCDEF89AB 0x956128C6 0xDF54AC89"; }
103  uint8_t on_message(std::string &message) override;
104 
105  protected:
106  uint32_t cmd_duration_ms_{3000};
107  uint32_t timeout_ms_{3500};
108 };
109 
110 class LedModeCommand : public Command {
111  public:
112  LedModeCommand(bool active) : active_(active) {
113  if (active) {
114  cmd_ = "setLedMode 1 0";
115  } else {
116  cmd_ = "setLedMode 1 1";
117  }
118  };
119  uint8_t on_message(std::string &message) override;
120 
121  protected:
122  bool active_;
123 };
124 
125 class UartOutputCommand : public Command {
126  public:
127  UartOutputCommand(bool active) : active_(active) {
128  if (active) {
129  cmd_ = "setUartOutput 1 1";
130  } else {
131  cmd_ = "setUartOutput 1 0";
132  }
133  };
134  uint8_t on_message(std::string &message) override;
135 
136  protected:
137  bool active_;
138 };
139 
140 class SensitivityCommand : public Command {
141  public:
142  SensitivityCommand(uint8_t sensitivity) : sensitivity_(sensitivity) {
143  if (sensitivity > 9)
144  sensitivity_ = sensitivity = 9;
145  char tmp_cmd[20] = {0};
146  sprintf(tmp_cmd, "setSensitivity %d", sensitivity);
147  cmd_ = std::string(tmp_cmd);
148  };
149  uint8_t on_message(std::string &message) override;
150 
151  protected:
152  uint8_t sensitivity_;
153 };
154 
155 } // namespace dfrobot_sen0395
156 } // namespace esphome
DfrobotSen0395Component * parent_
Definition: commands.h:23
virtual uint8_t execute(DfrobotSen0395Component *parent)
Definition: commands.cpp:14
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual uint8_t on_message(std::string &message)=0