ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
qwiic_pir.cpp
Go to the documentation of this file.
1#include "qwiic_pir.h"
2#include "esphome/core/log.h"
3
5
6ESPHOME_LOG_TAG(TAG, "qwiic_pir");
7
9 if (!this->check_chip_id(this, QWIIC_PIR_CHIP_ID, QWIIC_PIR_DEVICE_ID))
10 return;
11
13 this->mark_failed(LOG_STR("Failed to configure debounce time"));
14 return;
15 }
16
18 // Publish the starting raw state of the PIR sensor
19 // If NATIVE mode, the binary_sensor state would be unknown until a motion event
20 if (!this->read_byte(QWIIC_PIR_EVENT_STATUS, &this->event_register_.reg)) {
21 this->mark_failed(LOG_STR("Failed to read initial state"));
22 return;
23 }
24
25 this->publish_state(this->event_register_.raw_reading);
26 }
27}
28
30 // Read Event Register
31 if (!this->read_byte(QWIIC_PIR_EVENT_STATUS, &this->event_register_.reg)) {
32 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
33 return;
34 }
35
37 // Use a combination of the raw sensor reading and the device's event detection to determine state
38 // - The device is hardcoded to use a debounce time of 1 ms in this mode
39 // - Any event, even if it is object_removed, implies motion was active since the last loop, so publish true
40 // - Use ESPHome's built-in filters for debouncing
41 this->publish_state(this->event_register_.raw_reading || this->event_register_.event_available);
42
43 if (this->event_register_.event_available) {
44 this->clear_events_();
45 }
46 } else if (this->debounce_mode_ == NATIVE_DEBOUNCE_MODE) {
47 // Uses the device's firmware to debounce the signal
48 // - Follows the logic of SparkFun's example implementation:
49 // https://github.com/sparkfun/SparkFun_Qwiic_PIR_Arduino_Library/blob/master/examples/Example2_PrintPIRStatus/Example2_PrintPIRStatus.ino
50 // (accessed July 2023)
51 // - Is unreliable at detecting an object being removed, especially at debounce rates even slightly large
52 if (this->event_register_.event_available) {
53 // If an object is detected, publish true
54 if (this->event_register_.object_detected)
55 this->publish_state(true);
56
57 // If an object has been removed, publish false
58 if (this->event_register_.object_removed)
59 this->publish_state(false);
60
61 this->clear_events_();
62 }
63 } else if (this->debounce_mode_ == RAW_DEBOUNCE_MODE) {
64 // Publishes the raw PIR sensor reading with no further logic
65 // - May miss a very short motion detection if the ESP's loop time is slow
66 this->publish_state(this->event_register_.raw_reading);
67 }
68}
69
71 static const char *const RAW = "RAW";
72 static const char *const NATIVE = "NATIVE";
73 static const char *const HYBRID = "HYBRID";
74
75 const char *debounce_mode_str = RAW;
77 debounce_mode_str = NATIVE;
78 } else if (this->debounce_mode_ == HYBRID_DEBOUNCE_MODE) {
79 debounce_mode_str = HYBRID;
80 }
81
82 ESP_LOGCONFIG(TAG,
83 "Qwiic PIR:\n"
84 " Debounce Mode: %s",
85 debounce_mode_str);
87 ESP_LOGCONFIG(TAG, " Debounce Time: %ums", this->debounce_time_);
88 }
89
90 LOG_I2C_DEVICE(this);
91 LOG_BINARY_SENSOR(" ", "Binary Sensor", this);
92}
93
95 // Clear event status register
96 if (!this->write_byte(QWIIC_PIR_EVENT_STATUS, 0x00)) {
97 ESP_LOGW(TAG, "Failed to clear events");
98 }
99}
100
101} // namespace esphome::qwiic_pir
void mark_failed()
Mark this component as failed.
void publish_state(bool new_state)
Publish a new state to the front-end.
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:101
union esphome::qwiic_pir::QwiicPIRComponent::@155 event_register_
ESPHOME_LOG_TAG(TAG, "qwiic_pir")