ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
polygon.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6
7namespace esphome::ld2450 {
8
9static constexpr uint8_t MIN_POLYGON_POINTS = 3;
10// The most points whose text always fits Home Assistant's 255-character limit: 23 x "-4860,7560" + 22 x ';' = 252
11static constexpr uint8_t MAX_POLYGON_POINTS = 23;
12// Coordinate limits of the radar detection area, in mm
13static constexpr int16_t POLYGON_MIN_X = -4860;
14static constexpr int16_t POLYGON_MAX_X = 4860;
15static constexpr int16_t POLYGON_MIN_Y = 0;
16static constexpr int16_t POLYGON_MAX_Y = 7560;
17// Longest point is "-4860,7560;" (11 characters), plus the null terminator
18static constexpr size_t POLYGON_STR_SIZE = MAX_POLYGON_POINTS * 11 + 1;
19
20struct Point {
21 int16_t x;
22 int16_t y;
23};
24
26struct Polygon {
27 std::array<Point, MAX_POLYGON_POINTS> points{};
28 uint8_t count{0};
29
31 bool empty() const { return this->count == 0; }
32
34 bool is_valid() const;
35
37 bool contains(int16_t x, int16_t y) const;
38
44 bool parse(const char *str, size_t len);
45
47 void format(char *buf) const;
48};
49
50// text.py counts on this size
51static_assert(sizeof(Polygon) == 94, "Polygon layout changed; update POLYGON_SAVED_BYTES in text.py");
52
53} // namespace esphome::ld2450
const char * format
const void size_t len
Definition hal.h:68
A polygon in radar coordinates. Trivially copyable so it can be saved to preferences as-is.
Definition polygon.h:26
std::array< Point, MAX_POLYGON_POINTS > points
Definition polygon.h:27
bool parse(const char *str, size_t len)
Parse "x,y;x,y;..." (mm) into this polygon.
Definition polygon.cpp:91
bool is_valid() const
True when the polygon is empty, or has a valid number of points that are all inside the radar area.
Definition polygon.cpp:60
bool contains(int16_t x, int16_t y) const
Return whether the point is inside the polygon (even-odd rule). Always false for an empty polygon.
Definition polygon.cpp:73
bool empty() const
True when the polygon has no points, which disables the zone.
Definition polygon.h:31
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6