mohan-bee/compact-pir
Compact 3.3 V ESP8684 wireless motion/GPIO breakout board with LiPo regulation, 40 MHz crystal, U.FL antenna matching, digital PIR sensor, switched LED, decoupling, and dual 10-pin headers.
- Version
- 1.0.2
- License
- unset
- Stars
- 0
firmware/main/motion_logic.h
#pragma once
#include <stdbool.h>
#include <stdint.h>
typedef struct {
uint64_t boot_ms;
uint64_t last_motion_ms;
bool settled;
bool seen_motion;
} motion_state_t;
static inline bool motion_update(motion_state_t *state, uint64_t now_ms,
bool pir_high, uint64_t settle_ms,
uint64_t hold_ms)
{
// Unsigned elapsed times remain correct across the millisecond counter wrap.
if (!state->settled) {
if ((uint64_t)(now_ms - state->boot_ms) < settle_ms) {
return false;
}
state->settled = true;
}
if (pir_high) {
state->last_motion_ms = now_ms;
state->seen_motion = true;
}
if (state->seen_motion &&
(uint64_t)(now_ms - state->last_motion_ms) < hold_ms) {
return true;
}
state->seen_motion = false;
return false;
}