14static constexpr float DET_RANGE_STEP_M = 0.15f;
15static constexpr float LATENCY_STEP_S = 0.025f;
17static constexpr char PROMPT[] PROGMEM =
"leapMMW:/>";
21 "configure sensor startup behavior",
"factory reset",
"",
"",
"set led mode",
22 "set uart output mode",
"set sensitivity");
24 "BusyActions needs one entry per CommandType");
27 ESP_LOGCONFIG(TAG,
"Dfrobot Mmwave Radar:");
28#ifdef USE_BINARY_SENSOR
29 LOG_BINARY_SENSOR(
" ",
"Registered", this->detected_binary_sensor_);
32 LOG_SWITCH(
" ",
"Sensor Active Switch", this->sensor_active_switch_);
33 LOG_SWITCH(
" ",
"Turn on LED Switch", this->turn_on_led_switch_);
34 LOG_SWITCH(
" ",
"Presence via UART Switch", this->presence_via_uart_switch_);
35 LOG_SWITCH(
" ",
"Start after Boot Switch", this->start_after_boot_switch_);
39void DfrobotSen0395Component::loop() {
54 ESP_LOGE(TAG,
"Command queue is full");
60bool DfrobotSen0395Component::has_queue_room(
size_t count)
const {
61 if (COMMAND_QUEUE_SIZE - this->
cmd_queue_.size() >= count)
63 ESP_LOGE(TAG,
"Command queue is full");
67void DfrobotSen0395Component::enqueue_det_range_cfg(
float min1,
float max1,
float min2,
float max2,
float min3,
68 float max3,
float min4,
float max4) {
70 const float values[8] = {min1, max1, min2, max2, min3, max3, min4, max4};
73 for (
size_t i = 0; i < 8; i += 2) {
74 in_use = in_use && values[i] >= 0 && values[i + 1] >= 0;
75 for (
size_t j = i;
j < i + 2;
j++) {
76 steps[
j] = in_use ?
static_cast<int16_t
>(clamp(roundf(values[j] / DET_RANGE_STEP_M), 0.0f, 32767.0f)) : -1;
80 ESP_LOGW(TAG,
"Invalid detection range, using 0 0");
81 steps[0] = steps[1] = 0;
87void DfrobotSen0395Component::enqueue_set_latency(
float delay_after_detection,
float delay_after_disappear) {
92 static_cast<uint16_t
>(clamp(std::round(delay_after_detection / LATENCY_STEP_S), 0.0f, 65535.0f));
94 static_cast<uint16_t
>(clamp(std::round(delay_after_disappear / LATENCY_STEP_S), 0.0f, 65535.0f));
98 const bool was_active = this->
active_;
99 if (!this->has_queue_room(was_active ? 4 : 2))
115 this->format_command(cmd, buf,
sizeof(buf));
116 ESP_LOGD(TAG,
"Sending command: %s", buf);
125 if (ESPHOME_strstr_P(
message, ESPHOME_PSTR(
"is not recognized as a CLI command")) ==
nullptr) {
131 ESP_LOGD(TAG,
"Command not recognized properly by sensor");
135 ESP_LOGD(TAG,
"Command timeout");
144 ESP_LOGD(TAG,
"Retrying");
150 if (message ==
nullptr)
152 if (ESPHOME_strstr_P(
message, ESPHOME_PSTR(
"$JYBSS,0, , , *")) !=
nullptr) {
154 this->set_active(
true);
155 }
else if (ESPHOME_strstr_P(
message, ESPHOME_PSTR(
"$JYBSS,1, , , *")) !=
nullptr) {
157 this->set_active(
true);
161static size_t append_cfg_key(
char *buf,
size_t size,
size_t pos) {
165void DfrobotSen0395Component::format_command(
const Command &cmd,
char *buf,
size_t size)
const {
178 pos = buf_append_printf(buf,
size,
pos,
" %d %d", this->det_range_steps_[i], this->det_range_steps_[i + 1]);
183 buf_append_printf(buf,
size, 0,
"setLatency %.03f %.03f", this->
latency_steps_[0] * LATENCY_STEP_S,
187 buf_append_printf(buf,
size, 0,
"sensorCfgStart %d", cmd.arg);
199 buf_append_printf(buf,
size, 0,
"setLedMode 1 %d", cmd.arg ? 0 : 1);
202 buf_append_printf(buf,
size, 0,
"setUartOutput 1 %d", cmd.arg ? 1 : 0);
205 buf_append_printf(buf,
size, 0,
"setSensitivity %d", cmd.arg);
211 auto is = [
message](ESPHOME_PGM_P text) {
return ESPHOME_strcmp_P(
message, text) == 0; };
216 if (is(ESPHOME_PSTR(
"sensor stopped already"))) {
217 this->set_active(
false);
218 ESP_LOGI(TAG,
"Stopped sensor (already stopped)");
219 }
else if (is(ESPHOME_PSTR(
"sensor started already"))) {
220 this->set_active(
true);
221 ESP_LOGI(TAG,
"Started sensor (already started)");
222 }
else if (is(ESPHOME_PSTR(
"new parameter isn't save, can't startSensor"))) {
223 this->set_active(
false);
224 ESP_LOGE(TAG,
"Can't start sensor! (Use SaveCfgCommand to save config first)");
225 }
else if (is(ESPHOME_PSTR(
"Done"))) {
226 this->set_active(cmd.
arg);
227 ESP_LOGI(TAG,
"%s", cmd.
arg ? LOG_STR_LITERAL(
"Started sensor") : LOG_STR_LITERAL(
"Stopped sensor"));
235 ESP_LOGI(TAG,
"Restarted sensor.");
238 if (is(ESPHOME_PSTR(
"no parameter has changed"))) {
239 ESP_LOGI(TAG,
"Not saving config (no parameter changed).");
240 }
else if (is(ESPHOME_PSTR(
"Done"))) {
241 ESP_LOGI(TAG,
"Saved config. Saving a lot may damage the sensor.");
251 if (is(ESPHOME_PSTR(
"sensor is not stopped"))) {
252 ESP_LOGE(TAG,
"Cannot %s. Sensor is not stopped!",
253 LOG_STR_ARG(BusyActions::get_log_str(
static_cast<uint8_t
>(cmd.
type), 0)));
256 if (!is(ESPHOME_PSTR(
"Done")))
261 ESP_LOGI(TAG,
"Updated detection area config:");
263 ESP_LOGI(TAG,
" Detection area %u from %.02fm to %.02fm.",
static_cast<unsigned>(i / 2 + 1),
264 this->det_range_steps_[i] * DET_RANGE_STEP_M, this->det_range_steps_[i + 1] * DET_RANGE_STEP_M);
269 "Updated output latency config:\n"
270 " Signal that someone was detected is delayed by %.03f s.\n"
271 " Signal that nobody is detected anymore is delayed by %.03f s.",
275 this->set_start_after_boot(cmd.
arg);
276 ESP_LOGI(TAG,
"Updated sensor startup behavior: %s",
277 cmd.
arg ? LOG_STR_LITERAL(
"sensor will start automatically after power-on.")
278 : LOG_STR_LITERAL(
"sensor needs to be started manually after power-on."));
281 ESP_LOGI(TAG,
"Sensor factory reset done.");
284 this->set_led_active(cmd.
arg);
285 ESP_LOGI(TAG,
"Set led mode done. %s",
286 cmd.
arg ? LOG_STR_LITERAL(
"Sensor LED will blink.") : LOG_STR_LITERAL(
"Turned off LED."));
289 this->set_uart_presence_active(cmd.
arg);
290 ESP_LOGI(TAG,
"Set uart mode done. %s",
291 cmd.
arg ? LOG_STR_LITERAL(
"Presence information is sent via UART and GPIO.")
292 : LOG_STR_LITERAL(
"Presence information is only sent via GPIO."));
295 ESP_LOGI(TAG,
"Set sensitivity done. Set to value %d.", cmd.
arg);
310 if (line ==
nullptr &&
byte ==
'>' && this->
line_reader_.
length() ==
sizeof(PROMPT) - 1) {
314 if (line !=
nullptr) {
315 ESP_LOGV(TAG,
"Message: %s", line);
325#ifdef USE_BINARY_SENSOR
326 PUBLISH_STATE(this->detected_binary_sensor_, detected);
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void dump_config()
const char * partial()
The partial line, NUL terminated, for prompts with no line end; the first N - 1 characters on the byt...
size_t length() const
Length of the partial line; 0 after a line is returned and while an overlong line is dropped.
char * feed(uint8_t c)
Feed one byte; returns the completed line (empty lines included), valid until the next call,...
void reset()
Forget the partial line and start a new one with the next byte.
void log_if_overflowed(const char *tag) const
Log the shared "line dropped" warning when the last feed() overflowed.
StaticRingBuffer< Command, COMMAND_QUEUE_SIZE > cmd_queue_
LineReader< MMWAVE_READ_BUFFER_LENGTH > line_reader_
int16_t det_range_steps_[8]
uint16_t latency_steps_[2]
uint32_t ts_last_cmd_sent_
const char * read_message_()
The next complete line, valid until the next call, or nullptr.
void set_detected_(bool detected)
bool handle_reply_(const Command &cmd, const char *message)
Handle a reply to a sent command; true when the command is done.
bool run_command_(const Command &cmd, uint32_t now)
Run the command at the front of the queue; true when it is done.
void write_str(const char *str)
bool read_byte(uint8_t *data)
const LogString * message
@ COMMAND_TYPE_FACTORY_RESET
@ COMMAND_TYPE_DET_RANGE_CFG
@ COMMAND_TYPE_RESET_SYSTEM
@ COMMAND_TYPE_SENSOR_CFG_START
@ COMMAND_TYPE_SET_LATENCY
@ COMMAND_TYPE_SENSITIVITY
@ COMMAND_TYPE_UART_OUTPUT
PROGMEM_STRING_TABLE(BusyActions, "", "configure range config", "configure output latency", "configure sensor startup behavior", "factory reset", "", "", "set led mode", "set uart output mode", "set sensitivity")
ESPHOME_LOG_TAG(TAG, "dfrobot_sen0395")
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
Application App
Global storage of Application pointer - only one Application can exist.