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