mirror of
https://github.com/esphome/esphome.git
synced 2026-11-18 02:55:59 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c29ca6323e | ||
|
|
95ca52e270 | ||
|
|
be9ddd3243 | ||
|
|
7256cb5bb0 |
No files matched your search
@@ -10,12 +10,10 @@ CODEOWNERS = ["@kahrendt"]
|
||||
|
||||
qwiic_pir_ns = cg.esphome_ns.namespace("qwiic_pir")
|
||||
|
||||
DebounceMode = qwiic_pir_ns.enum("DebounceMode")
|
||||
DEBOUNCE_MODE_OPTIONS = {
|
||||
"RAW": DebounceMode.RAW_DEBOUNCE_MODE,
|
||||
"NATIVE": DebounceMode.NATIVE_DEBOUNCE_MODE,
|
||||
"HYBRID": DebounceMode.HYBRID_DEBOUNCE_MODE,
|
||||
}
|
||||
DebounceMode = qwiic_pir_ns.enum("DebounceMode", is_class=True)
|
||||
DEBOUNCE_MODE_OPTIONS = DebounceMode.options(
|
||||
"RAW", "NATIVE", "HYBRID", prefix="DEBOUNCE_MODE_"
|
||||
)
|
||||
|
||||
CONF_DEBOUNCE_MODE = "debounce_mode"
|
||||
|
||||
@@ -47,12 +45,12 @@ CONFIG_SCHEMA = cv.All(
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
var = await binary_sensor.new_binary_sensor(config)
|
||||
# Without a debounce time the device is set to 1 ms; NATIVE is the only mode that takes one
|
||||
debounce = config.get(CONF_DEBOUNCE)
|
||||
var = await binary_sensor.new_binary_sensor(
|
||||
config,
|
||||
config[CONF_DEBOUNCE_MODE],
|
||||
1 if debounce is None else debounce.total_milliseconds,
|
||||
)
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
if debounce_time_setting := config.get(CONF_DEBOUNCE):
|
||||
cg.add(var.set_debounce_time(debounce_time_setting.total_milliseconds))
|
||||
else:
|
||||
cg.add(var.set_debounce_time(1)) # default to 1 ms if not configured
|
||||
cg.add(var.set_debounce_mode(config[CONF_DEBOUNCE_MODE]))
|
||||
@@ -1,99 +1,85 @@
|
||||
#include "qwiic_pir.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/progmem.h"
|
||||
|
||||
namespace esphome::qwiic_pir {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "qwiic_pir");
|
||||
|
||||
static constexpr uint8_t REG_CHIP_ID = 0x00;
|
||||
static constexpr uint8_t REG_EVENT_STATUS = 0x03;
|
||||
static constexpr uint8_t REG_DEBOUNCE_TIME = 0x05; // milliseconds, 16 bits little endian
|
||||
|
||||
static constexpr uint8_t DEVICE_ID = 0x72;
|
||||
|
||||
// REG_EVENT_STATUS bits
|
||||
static constexpr uint8_t EVENT_RAW_READING = 0x01; // raw state of the PIR sensor
|
||||
static constexpr uint8_t EVENT_AVAILABLE = 0x02; // a debounced object was detected or removed
|
||||
static constexpr uint8_t EVENT_OBJECT_REMOVED = 0x04; // a debounced object is no longer detected
|
||||
static constexpr uint8_t EVENT_OBJECT_DETECTED = 0x08; // a debounced object has been detected
|
||||
|
||||
// Indexed by DebounceMode
|
||||
PROGMEM_STRING_TABLE(DebounceModeStrings, "RAW", "NATIVE", "HYBRID");
|
||||
|
||||
void QwiicPIRComponent::setup() {
|
||||
if (!this->check_chip_id(this, QWIIC_PIR_CHIP_ID, QWIIC_PIR_DEVICE_ID))
|
||||
if (!this->check_chip_id(this, REG_CHIP_ID, DEVICE_ID))
|
||||
return;
|
||||
|
||||
if (!this->setup_check(this->write_byte_16(QWIIC_PIR_DEBOUNCE_TIME, this->debounce_time_),
|
||||
if (!this->setup_check(this->write_byte_16_le(REG_DEBOUNCE_TIME, this->debounce_time_),
|
||||
LOG_STR("Failed to configure debounce time")))
|
||||
return;
|
||||
|
||||
if (this->debounce_mode_ == NATIVE_DEBOUNCE_MODE) {
|
||||
// Publish the starting raw state of the PIR sensor
|
||||
// If NATIVE mode, the binary_sensor state would be unknown until a motion event
|
||||
if (!this->setup_check(this->read_byte(QWIIC_PIR_EVENT_STATUS, &this->event_register_.reg),
|
||||
LOG_STR("Failed to read initial state")))
|
||||
if (this->debounce_mode_ == DebounceMode::DEBOUNCE_MODE_NATIVE) {
|
||||
// NATIVE only publishes on motion events, so start from the raw state
|
||||
uint8_t event;
|
||||
if (!this->setup_check(this->read_byte(REG_EVENT_STATUS, &event), LOG_STR("Failed to read initial state")))
|
||||
return;
|
||||
|
||||
this->publish_state(this->event_register_.raw_reading);
|
||||
this->publish_state((event & EVENT_RAW_READING) != 0);
|
||||
}
|
||||
}
|
||||
|
||||
void QwiicPIRComponent::loop() {
|
||||
// Read Event Register
|
||||
if (!this->read_byte(QWIIC_PIR_EVENT_STATUS, &this->event_register_.reg)) {
|
||||
ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
|
||||
uint8_t event;
|
||||
if (!this->status_comm_check(this->read_byte(REG_EVENT_STATUS, &event)))
|
||||
return;
|
||||
|
||||
switch (this->debounce_mode_) {
|
||||
case DebounceMode::DEBOUNCE_MODE_HYBRID:
|
||||
// The device debounces for 1 ms and ESPHome filters do the rest; any event means motion since the last loop
|
||||
this->publish_state((event & (EVENT_RAW_READING | EVENT_AVAILABLE)) != 0);
|
||||
break;
|
||||
case DebounceMode::DEBOUNCE_MODE_NATIVE:
|
||||
// The device's own debounce, as in SparkFun's Example2_PrintPIRStatus; it often misses objects being removed
|
||||
if (event & EVENT_AVAILABLE) {
|
||||
if (event & EVENT_OBJECT_DETECTED)
|
||||
this->publish_state(true);
|
||||
if (event & EVENT_OBJECT_REMOVED)
|
||||
this->publish_state(false);
|
||||
}
|
||||
break;
|
||||
case DebounceMode::DEBOUNCE_MODE_RAW:
|
||||
// Can miss a very short detection when the loop is slow
|
||||
this->publish_state((event & EVENT_RAW_READING) != 0);
|
||||
break;
|
||||
}
|
||||
|
||||
if (this->debounce_mode_ == HYBRID_DEBOUNCE_MODE) {
|
||||
// Use a combination of the raw sensor reading and the device's event detection to determine state
|
||||
// - The device is hardcoded to use a debounce time of 1 ms in this mode
|
||||
// - Any event, even if it is object_removed, implies motion was active since the last loop, so publish true
|
||||
// - Use ESPHome's built-in filters for debouncing
|
||||
this->publish_state(this->event_register_.raw_reading || this->event_register_.event_available);
|
||||
|
||||
if (this->event_register_.event_available) {
|
||||
this->clear_events_();
|
||||
}
|
||||
} else if (this->debounce_mode_ == NATIVE_DEBOUNCE_MODE) {
|
||||
// Uses the device's firmware to debounce the signal
|
||||
// - Follows the logic of SparkFun's example implementation:
|
||||
// https://github.com/sparkfun/SparkFun_Qwiic_PIR_Arduino_Library/blob/master/examples/Example2_PrintPIRStatus/Example2_PrintPIRStatus.ino
|
||||
// (accessed July 2023)
|
||||
// - Is unreliable at detecting an object being removed, especially at debounce rates even slightly large
|
||||
if (this->event_register_.event_available) {
|
||||
// If an object is detected, publish true
|
||||
if (this->event_register_.object_detected)
|
||||
this->publish_state(true);
|
||||
|
||||
// If an object has been removed, publish false
|
||||
if (this->event_register_.object_removed)
|
||||
this->publish_state(false);
|
||||
|
||||
this->clear_events_();
|
||||
}
|
||||
} else if (this->debounce_mode_ == RAW_DEBOUNCE_MODE) {
|
||||
// Publishes the raw PIR sensor reading with no further logic
|
||||
// - May miss a very short motion detection if the ESP's loop time is slow
|
||||
this->publish_state(this->event_register_.raw_reading);
|
||||
}
|
||||
// HYBRID and NATIVE clear a handled event; the last bus step of the poll decides the warning
|
||||
bool ok = true;
|
||||
if (this->debounce_mode_ != DebounceMode::DEBOUNCE_MODE_RAW && (event & EVENT_AVAILABLE))
|
||||
ok = this->write_byte(REG_EVENT_STATUS, 0x00);
|
||||
this->status_comm_result(ok);
|
||||
}
|
||||
|
||||
void QwiicPIRComponent::dump_config() {
|
||||
static const char *const RAW = "RAW";
|
||||
static const char *const NATIVE = "NATIVE";
|
||||
static const char *const HYBRID = "HYBRID";
|
||||
|
||||
const char *debounce_mode_str = RAW;
|
||||
if (this->debounce_mode_ == NATIVE_DEBOUNCE_MODE) {
|
||||
debounce_mode_str = NATIVE;
|
||||
} else if (this->debounce_mode_ == HYBRID_DEBOUNCE_MODE) {
|
||||
debounce_mode_str = HYBRID;
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"Qwiic PIR:\n"
|
||||
" Debounce Mode: %s",
|
||||
debounce_mode_str);
|
||||
if (this->debounce_mode_ == NATIVE_DEBOUNCE_MODE) {
|
||||
LOG_STR_ARG(DebounceModeStrings::get_log_str(static_cast<uint8_t>(this->debounce_mode_), 0)));
|
||||
if (this->debounce_mode_ == DebounceMode::DEBOUNCE_MODE_NATIVE) {
|
||||
ESP_LOGCONFIG(TAG, " Debounce Time: %ums", this->debounce_time_);
|
||||
}
|
||||
|
||||
LOG_I2C_DEVICE(this);
|
||||
LOG_BINARY_SENSOR(" ", "Binary Sensor", this);
|
||||
}
|
||||
|
||||
void QwiicPIRComponent::clear_events_() {
|
||||
// Clear event status register
|
||||
if (!this->write_byte(QWIIC_PIR_EVENT_STATUS, 0x00)) {
|
||||
ESP_LOGW(TAG, "Failed to clear events");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::qwiic_pir
|
||||
@@ -2,8 +2,8 @@
|
||||
* Adds support for Qwiic PIR motion sensors that communicate over an I2C bus.
|
||||
* These sensors use Sharp PIR motion sensors to detect motion. A firmware running on an ATTiny84 translates the digital
|
||||
* output to I2C communications.
|
||||
* ATTiny84 firmware: https://github.com/sparkfun/Qwiic_PIR (accessed July 2023)
|
||||
* SparkFun's Arduino library: https://github.com/sparkfun/SparkFun_Qwiic_PIR_Arduino_Library (accessed July 2023)
|
||||
* ATTiny84 firmware: https://github.com/sparkfun/Qwiic_PIR
|
||||
* SparkFun's Arduino library: https://github.com/sparkfun/SparkFun_Qwiic_PIR_Arduino_Library
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@@ -14,48 +14,24 @@
|
||||
|
||||
namespace esphome::qwiic_pir {
|
||||
|
||||
// Qwiic PIR I2C Register Addresses
|
||||
enum {
|
||||
QWIIC_PIR_CHIP_ID = 0x00,
|
||||
QWIIC_PIR_EVENT_STATUS = 0x03,
|
||||
QWIIC_PIR_DEBOUNCE_TIME = 0x05, // uint16_t debounce time in milliseconds
|
||||
enum class DebounceMode : uint8_t {
|
||||
DEBOUNCE_MODE_RAW,
|
||||
DEBOUNCE_MODE_NATIVE,
|
||||
DEBOUNCE_MODE_HYBRID,
|
||||
};
|
||||
|
||||
enum DebounceMode {
|
||||
RAW_DEBOUNCE_MODE,
|
||||
NATIVE_DEBOUNCE_MODE,
|
||||
HYBRID_DEBOUNCE_MODE,
|
||||
};
|
||||
|
||||
static const uint8_t QWIIC_PIR_DEVICE_ID = 0x72;
|
||||
|
||||
class QwiicPIRComponent final : public Component, public i2c::I2CDevice, public binary_sensor::BinarySensor {
|
||||
public:
|
||||
QwiicPIRComponent(DebounceMode debounce_mode, uint16_t debounce_time)
|
||||
: debounce_time_(debounce_time), debounce_mode_(debounce_mode) {}
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
void set_debounce_time(uint16_t debounce_time) { this->debounce_time_ = debounce_time; }
|
||||
void set_debounce_mode(DebounceMode mode) { this->debounce_mode_ = mode; }
|
||||
|
||||
protected:
|
||||
uint16_t debounce_time_{};
|
||||
|
||||
DebounceMode debounce_mode_{};
|
||||
|
||||
union {
|
||||
struct {
|
||||
bool raw_reading : 1; // raw state of PIR sensor
|
||||
bool event_available : 1; // a debounced object has been detected or removed
|
||||
bool object_removed : 1; // a debounced object is no longer detected
|
||||
bool object_detected : 1; // a debounced object has been detected
|
||||
bool : 4;
|
||||
};
|
||||
uint8_t reg;
|
||||
} event_register_ = {.reg = 0};
|
||||
|
||||
void clear_events_();
|
||||
uint16_t debounce_time_;
|
||||
DebounceMode debounce_mode_;
|
||||
};
|
||||
|
||||
} // namespace esphome::qwiic_pir
|
||||
@@ -2,3 +2,14 @@ binary_sensor:
|
||||
- platform: qwiic_pir
|
||||
i2c_id: i2c_bus
|
||||
name: Qwiic PIR Motion Sensor
|
||||
- platform: qwiic_pir
|
||||
i2c_id: i2c_bus
|
||||
address: 0x13
|
||||
name: Qwiic PIR Native
|
||||
debounce_mode: NATIVE
|
||||
debounce: 10ms
|
||||
- platform: qwiic_pir
|
||||
i2c_id: i2c_bus
|
||||
address: 0x14
|
||||
name: Qwiic PIR Raw
|
||||
debounce_mode: RAW
|
||||
@@ -1,4 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
qwiic_pir: !include common.yaml
|
||||
@@ -1,4 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/esp8266-ard.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
qwiic_pir: !include common.yaml
|
||||
@@ -1,4 +1,3 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/rp2040-ard.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
qwiic_pir: !include common.yaml
|
||||
Reference in new issue
Block a user