ESPHome  2024.9.2
sen21231.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 // ref:
8 // https://github.com/usefulsensors/person_sensor_pico_c/blob/main/person_sensor.h
9 
10 namespace esphome {
11 namespace sen21231_sensor {
12 // The I2C address of the person sensor board.
13 static const uint8_t PERSON_SENSOR_I2C_ADDRESS = 0x62;
14 static const uint8_t PERSON_SENSOR_REG_MODE = 0x01;
15 static const uint8_t PERSON_SENSOR_REG_ENABLE_ID = 0x02;
16 static const uint8_t PERSON_SENSOR_REG_SINGLE_SHOT = 0x03;
17 static const uint8_t PERSON_SENSOR_REG_CALIBRATE_ID = 0x04;
18 static const uint8_t PERSON_SENSOR_REG_PERSIST_IDS = 0x05;
19 static const uint8_t PERSON_SENSOR_REG_ERASE_IDS = 0x06;
20 static const uint8_t PERSON_SENSOR_REG_DEBUG_MODE = 0x07;
21 
22 static const uint8_t PERSON_SENSOR_MAX_FACES_COUNT = 4;
23 static const uint8_t PERSON_SENSOR_MAX_IDS_COUNT = 7;
24 
25 // The results returned from the sensor have a short header providing
26 // information about the length of the data packet:
27 // reserved: Currently unused bytes.
28 // data_size: Length of the entire packet, excluding the header and
29 // checksum.
30 // For version 1.0 of the sensor, this should be 40.
31 using person_sensor_results_header_t = struct {
32  uint8_t reserved[2]; // Bytes 0-1.
33  uint16_t data_size; // Bytes 2-3.
34 };
35 
36 // Each face found has a set of information associated with it:
37 // box_confidence: How certain we are we have found a face, from 0 to 255.
38 // box_left: X coordinate of the left side of the box, from 0 to 255.
39 // box_top: Y coordinate of the top edge of the box, from 0 to 255.
40 // box_width: Width of the box, where 255 is the full view port size.
41 // box_height: Height of the box, where 255 is the full view port size.
42 // id_confidence: How sure the sensor is about the recognition result.
43 // id: Numerical ID assigned to this face.
44 // is_looking_at: Whether the person is facing the camera, 0 or 1.
45 using person_sensor_face_t = struct __attribute__((__packed__)) {
46  uint8_t box_confidence; // Byte 1.
47  uint8_t box_left; // Byte 2.
48  uint8_t box_top; // Byte 3.
49  uint8_t box_right; // Byte 4.
50  uint8_t box_bottom; // Byte 5.
51  int8_t id_confidence; // Byte 6.
52  int8_t id; // Byte 7
53  uint8_t is_facing; // Byte 8.
54 };
55 
56 // This is the full structure of the packet returned over the wire from the
57 // sensor when we do an I2C read from the peripheral address.
58 // The checksum should be the CRC16 of bytes 0 to 38. You shouldn't need to
59 // verify this in practice, but we found it useful during our own debugging.
60 using person_sensor_results_t = struct __attribute__((__packed__)) {
61  person_sensor_results_header_t header; // Bytes 0-4.
62  int8_t num_faces; // Byte 5.
63  person_sensor_face_t faces[PERSON_SENSOR_MAX_FACES_COUNT]; // Bytes 6-37.
64  uint16_t checksum; // Bytes 38-39.
65 };
66 
68  public:
69  void update() override;
70  void dump_config() override;
71 
72  protected:
73  void read_data_();
74 };
75 
76 } // namespace sen21231_sensor
77 } // namespace esphome
struct { uint8_t reserved[2] person_sensor_results_header_t
Definition: sen21231.h:32
This class simplifies creating components that periodically check a state.
Definition: component.h:283
struct __attribute__((__packed__)) { person_sensor_results_header_t header person_sensor_results_t
Definition: sen21231.h:61
struct __attribute__((__packed__)) { uint8_t box_confidence person_sensor_face_t
Definition: sen21231.h:46
enum esphome::EntityCategory __attribute__
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
person_sensor_face_t faces[PERSON_SENSOR_MAX_FACES_COUNT]
Definition: sen21231.h:63
Base-class for all sensors.
Definition: sensor.h:57
uint16_t reserved
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133