ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
dfrobot_sen0395.cpp
Go to the documentation of this file.
1#include "dfrobot_sen0395.h"
2
4#include "esphome/core/log.h"
5
7
8static const char *const TAG = "dfrobot_sen0395";
9const char ASCII_CR = 0x0D;
10const char ASCII_LF = 0x0A;
11
13 ESP_LOGCONFIG(TAG, "Dfrobot Mmwave Radar:");
14#ifdef USE_BINARY_SENSOR
15 LOG_BINARY_SENSOR(" ", "Registered", this->detected_binary_sensor_);
16#endif
17#ifdef USE_SWITCH
18 LOG_SWITCH(" ", "Sensor Active Switch", this->sensor_active_switch_);
19 LOG_SWITCH(" ", "Turn on LED Switch", this->turn_on_led_switch_);
20 LOG_SWITCH(" ", "Presence via UART Switch", this->presence_via_uart_switch_);
21 LOG_SWITCH(" ", "Start after Boot Switch", this->start_after_boot_switch_);
22#endif
23}
24
25void DfrobotSen0395Component::loop() {
26 if (cmd_queue_.is_empty()) {
27 // Command queue empty. Read sensor state.
28 cmd_queue_.enqueue(make_unique<ReadStateCommand>());
29 }
30
31 // Commands are non-blocking and need to be called repeatedly.
32 if (cmd_queue_.process(this)) {
33 // Dequeue if command is done
35 }
36}
37
38int8_t DfrobotSen0395Component::enqueue(std::unique_ptr<Command> cmd) {
39 return cmd_queue_.enqueue(std::move(cmd)); // Transfer ownership using std::move
40}
41
43 while (this->available()) {
44 uint8_t byte;
45 this->read_byte(&byte);
46
48 this->read_pos_ = 0;
49
50 ESP_LOGVV(TAG, "Buffer pos: %u %d", this->read_pos_, byte);
51
52 if (byte == ASCII_CR)
53 continue;
54 if (byte >= 0x7F)
55 byte = '?'; // needs to be valid utf8 string for log functions.
56 this->read_buffer_[this->read_pos_] = byte;
57
58 if (this->read_pos_ == 9 && byte == '>')
59 this->read_buffer_[++this->read_pos_] = ASCII_LF;
60
61 if (this->read_buffer_[this->read_pos_] == ASCII_LF) {
62 this->read_buffer_[this->read_pos_] = 0;
63 this->read_pos_ = 0;
64 ESP_LOGV(TAG, "Message: %s", this->read_buffer_);
65 return 1; // Full message in buffer
66 } else {
67 this->read_pos_++;
68 }
69 }
70 return 0; // No full message yet
71}
72
74 if (this->read_message_()) {
75 std::string message(this->read_buffer_);
76 if (message.rfind("leapMMW:/>") != std::string::npos) {
77 return 1; // Prompt found
78 }
79 }
80 return 0; // Not found yet
81}
82
84 // The interval between two commands must be larger than the specified duration (in ms).
86 this->write_str(cmd);
88 return 1; // Command sent
89 }
90 // Could not send command yet as command duration did not fully pass yet.
91 return 0;
92}
93
95 this->detected_ = detected;
96#ifdef USE_BINARY_SENSOR
97 if (this->detected_binary_sensor_ != nullptr)
99#endif
100}
101
102int8_t CircularCommandQueue::enqueue(std::unique_ptr<Command> cmd) {
103 if (this->is_full()) {
104 ESP_LOGE(TAG, "Command queue is full");
105 return -1;
106 } else if (this->is_empty()) {
107 front_++;
108 }
109 rear_ = (rear_ + 1) % COMMAND_QUEUE_SIZE;
110 commands_[rear_] = std::move(cmd); // Transfer ownership using std::move
111 return 1;
112}
113
114std::unique_ptr<Command> CircularCommandQueue::dequeue() {
115 if (this->is_empty())
116 return nullptr;
117 std::unique_ptr<Command> dequeued_cmd = std::move(commands_[front_]);
118 if (front_ == rear_) {
119 front_ = -1;
120 rear_ = -1;
121 } else {
122 front_ = (front_ + 1) % COMMAND_QUEUE_SIZE;
123 }
124
125 return dequeued_cmd;
126}
127
129
130bool CircularCommandQueue::is_full() { return (rear_ + 1) % COMMAND_QUEUE_SIZE == front_; }
131
132// Run execute method of first in line command.
133// Execute is non-blocking and has to be called until it returns 1.
135 if (!is_empty()) {
136 return commands_[front_]->execute(parent);
137 } else {
138 return 1;
139 }
140}
141
142} // namespace esphome::dfrobot_sen0395
virtual void dump_config()
void publish_state(bool new_state)
Publish a new state to the front-end.
std::unique_ptr< Command > commands_[COMMAND_QUEUE_SIZE]
int8_t enqueue(std::unique_ptr< Command > cmd)
uint8_t process(DfrobotSen0395Component *parent)
binary_sensor::BinarySensor * detected_binary_sensor_
uint8_t send_cmd_(const char *cmd, uint32_t duration)
void write_str(const char *str)
Definition uart.h:32
bool read_byte(uint8_t *data)
Definition uart.h:34
const char * message
Definition component.cpp:35
uint8_t duration
Definition msa3xx.h:0
const uint8_t MMWAVE_READ_BUFFER_LENGTH
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t