ESPHome 2026.5.0b1
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
6static const char *const TAG = "qwiic_pir";
7
9 // Verify I2C communcation by reading and verifying the chip ID
10 uint8_t chip_id;
11 if (!this->read_byte(QWIIC_PIR_CHIP_ID, &chip_id)) {
12 ESP_LOGE(TAG, "Failed to read chip ID");
13 this->error_code_ = ERROR_COMMUNICATION_FAILED;
14 this->mark_failed();
15 return;
16 }
17
18 if (chip_id != QWIIC_PIR_DEVICE_ID) {
19 ESP_LOGE(TAG, "Unknown chip ID");
20 this->error_code_ = ERROR_WRONG_CHIP_ID;
21 this->mark_failed();
22 return;
23 }
24
26 ESP_LOGE(TAG, "Failed to configure debounce time");
27 this->error_code_ = ERROR_COMMUNICATION_FAILED;
28 this->mark_failed();
29 return;
30 }
31
33 // Publish the starting raw state of the PIR sensor
34 // If NATIVE mode, the binary_sensor state would be unknown until a motion event
35 if (!this->read_byte(QWIIC_PIR_EVENT_STATUS, &this->event_register_.reg)) {
36 ESP_LOGE(TAG, "Failed to read initial state");
37 this->error_code_ = ERROR_COMMUNICATION_FAILED;
38 this->mark_failed();
39 return;
40 }
41
42 this->publish_state(this->event_register_.raw_reading);
43 }
44}
45
47 // Read Event Register
48 if (!this->read_byte(QWIIC_PIR_EVENT_STATUS, &this->event_register_.reg)) {
49 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
50 return;
51 }
52
54 // Use a combination of the raw sensor reading and the device's event detection to determine state
55 // - The device is hardcoded to use a debounce time of 1 ms in this mode
56 // - Any event, even if it is object_removed, implies motion was active since the last loop, so publish true
57 // - Use ESPHome's built-in filters for debouncing
58 this->publish_state(this->event_register_.raw_reading || this->event_register_.event_available);
59
60 if (this->event_register_.event_available) {
61 this->clear_events_();
62 }
63 } else if (this->debounce_mode_ == NATIVE_DEBOUNCE_MODE) {
64 // Uses the device's firmware to debounce the signal
65 // - Follows the logic of SparkFun's example implementation:
66 // https://github.com/sparkfun/SparkFun_Qwiic_PIR_Arduino_Library/blob/master/examples/Example2_PrintPIRStatus/Example2_PrintPIRStatus.ino
67 // (accessed July 2023)
68 // - Is unreliable at detecting an object being removed, especially at debounce rates even slightly large
69 if (this->event_register_.event_available) {
70 // If an object is detected, publish true
71 if (this->event_register_.object_detected)
72 this->publish_state(true);
73
74 // If an object has been removed, publish false
75 if (this->event_register_.object_removed)
76 this->publish_state(false);
77
78 this->clear_events_();
79 }
80 } else if (this->debounce_mode_ == RAW_DEBOUNCE_MODE) {
81 // Publishes the raw PIR sensor reading with no further logic
82 // - May miss a very short motion detection if the ESP's loop time is slow
83 this->publish_state(this->event_register_.raw_reading);
84 }
85}
86
88 static const char *const RAW = "RAW";
89 static const char *const NATIVE = "NATIVE";
90 static const char *const HYBRID = "HYBRID";
91
92 const char *debounce_mode_str = RAW;
94 debounce_mode_str = NATIVE;
95 } else if (this->debounce_mode_ == HYBRID_DEBOUNCE_MODE) {
96 debounce_mode_str = HYBRID;
97 }
98
99 ESP_LOGCONFIG(TAG,
100 "Qwiic PIR:\n"
101 " Debounce Mode: %s",
102 debounce_mode_str);
104 ESP_LOGCONFIG(TAG, " Debounce Time: %ums", this->debounce_time_);
105 }
106
107 switch (this->error_code_) {
108 case NONE:
109 break;
111 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
112 break;
114 ESP_LOGE(TAG, "Unknown chip ID");
115 break;
116 default:
117 ESP_LOGE(TAG, "Error %d", (int) this->error_code_);
118 break;
119 }
120
121 LOG_I2C_DEVICE(this);
122 LOG_BINARY_SENSOR(" ", "Binary Sensor", this);
123}
124
126 // Clear event status register
127 if (!this->write_byte(QWIIC_PIR_EVENT_STATUS, 0x00))
128 ESP_LOGW(TAG, "Failed to clear events");
129}
130
131} // 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 write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
enum esphome::qwiic_pir::QwiicPIRComponent::ErrorCode NONE
union esphome::qwiic_pir::QwiicPIRComponent::@148 event_register_