ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
sen0192.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sen0192.cpp
6 * @brief SEN0192 microwave motion sensor - debounced presence tracker + GPIO binding. See sen0192.h.
7 */
8
10
11#if PC_ENABLE_SEN0192
12
13// ---------------------------------------------------------------------------
14// Pure presence state machine (host-tested).
15// ---------------------------------------------------------------------------
16
17#if defined(ARDUINO)
18#include "services/system/clock.h" // pc_millis()
19#include <Arduino.h>
20#endif
21void pc_sen0192_motion_init(Sen0192Motion *m, uint32_t hold_ms, bool active_high)
22{
23 m->present = false;
24 m->seeded = false;
25 m->active_high = active_high;
26 m->hold_ms = hold_ms;
27 m->last_active_ms = 0;
28 m->motion_events = 0;
29}
30
31bool pc_sen0192_motion_update(Sen0192Motion *m, bool level_high, uint32_t now_ms)
32{
33 bool active = (level_high == m->active_high);
34 if (active)
35 {
36 m->last_active_ms = now_ms;
37 m->seeded = true;
38 if (!m->present)
39 {
40 m->present = true;
41 m->motion_events++;
42 return true; // clear -> present edge
43 }
44 return false;
45 }
46 pc_sen0192_motion_tick(m, now_ms); // inactive sample: presence may age out
47 return false;
48}
49
50bool pc_sen0192_motion_tick(Sen0192Motion *m, uint32_t now_ms)
51{
52 if (m->present && m->seeded && (uint32_t)(now_ms - m->last_active_ms) > m->hold_ms)
53 {
54 m->present = false;
55 }
56 return m->present;
57}
58
59bool pc_sen0192_motion_present(const Sen0192Motion *m)
60{
61 return m->present;
62}
63
64uint32_t pc_sen0192_motion_events(const Sen0192Motion *m)
65{
66 return m->motion_events;
67}
68
69uint32_t pc_sen0192_motion_active_age_ms(const Sen0192Motion *m, uint32_t now_ms)
70{
71 return m->seeded ? (uint32_t)(now_ms - m->last_active_ms) : 0;
72}
73
74// ---------------------------------------------------------------------------
75// ESP32 GPIO binding
76// ---------------------------------------------------------------------------
77
78#if defined(ARDUINO)
79
80namespace
81{
82// The SEN0192 binding state, owned by one instance (internal linkage): the presence tracker and the pin.
83struct Sen0192Ctx
84{
85 Sen0192Motion motion;
86 int pin = -1;
87};
88Sen0192Ctx s_sen;
89} // namespace
90
91bool pc_sen0192_begin(void)
92{
93 s_sen.pin = PC_SEN0192_PIN;
94 pinMode(s_sen.pin, INPUT);
95 pc_sen0192_motion_init(&s_sen.motion, PC_SEN0192_HOLD_MS, PC_SEN0192_ACTIVE_HIGH != 0);
96 return true;
97}
98
99bool pc_sen0192_poll(void)
100{
101 if (s_sen.pin < 0)
102 {
103 return false;
104 }
105 bool level = digitalRead(s_sen.pin) != 0;
106 return pc_sen0192_motion_update(&s_sen.motion, level, pc_millis());
107}
108
109bool pc_sen0192_present(void)
110{
111 pc_sen0192_motion_tick(&s_sen.motion, pc_millis()); // age presence out even between poll()s
112 return pc_sen0192_motion_present(&s_sen.motion);
113}
114
115uint32_t pc_sen0192_motion_count(void)
116{
117 return pc_sen0192_motion_events(&s_sen.motion);
118}
119
120#else // host build: no GPIO. The presence state machine above is host-tested.
121
122bool pc_sen0192_begin(void)
123{
124 return false;
125}
126bool pc_sen0192_poll(void)
127{
128 return false;
129}
130bool pc_sen0192_present(void)
131{
132 return false;
133}
134uint32_t pc_sen0192_motion_count(void)
135{
136 return 0;
137}
138
139#endif // ARDUINO
140
141#endif // PC_ENABLE_SEN0192
Pluggable monotonic clock for all library timing.
uint32_t pc_millis(void)
The library's monotonic time at 1000 Hz (milliseconds).
Definition clock.h:71
#define PC_SEN0192_PIN
GPIO the SEN0192 OUT line is wired to.
#define PC_SEN0192_HOLD_MS
Presence is held this many ms after the last active (motion) sample before it clears.
#define PC_SEN0192_ACTIVE_HIGH
SEN0192 OUT polarity: 1 = the OUT line reads HIGH on motion, 0 = active-LOW.
DFRobot SEN0192 10.525 GHz microwave Doppler motion sensor (PC_ENABLE_SEN0192).