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