-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotionSensorManager.cpp
More file actions
130 lines (111 loc) · 4.84 KB
/
MotionSensorManager.cpp
File metadata and controls
130 lines (111 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* MotionHub project
* Copyright (c) 2026 Fedir Vilhota <fredy31415@gmail.com>
* This software is released under the MIT License.
* See the LICENSE file in the project root for full license information.
*/
#include "MotionSensorManager.h"
#include "driver/gpio.h"
MotionSensorManager::MotionSensorManager(uint8_t pinIn1, uint8_t pinIn2, uint8_t pinOut1, uint8_t pinOut2) {
_sensors[0] = {
.pinIn = pinIn1,
.pinOut = pinOut1,
.mode = MODE_TRANSPARENT,
.currentInputState = false,
.lastInputState = false,
.lastDebounceTime = 0,
.lastRealMotionTime = 0,
.simulationEndTime = 0
};
_sensors[1] = {
.pinIn = pinIn2,
.pinOut = pinOut2,
.mode = MODE_TRANSPARENT,
.currentInputState = false,
.lastInputState = false,
.lastDebounceTime = 0,
.lastRealMotionTime = 0,
.simulationEndTime = 0
};
}
void MotionSensorManager::begin() {
for (int i = 0; i < 2; i++) {
pinMode(_sensors[i].pinIn, INPUT_PULLDOWN);
pinMode(_sensors[i].pinOut, OUTPUT);
// Згідно з логікою інвертора: HIGH = транзистор відкритий (на виході 0, рух ВІДСУТНІЙ)
digitalWrite(_sensors[i].pinOut, HIGH);
}
}
void MotionSensorManager::update() {
unsigned long currentMillis = millis();
for (int i = 0; i < 2; i++) {
// Зчитування стану з піна
bool reading = digitalRead(_sensors[i].pinIn);
// Перевірка на зміну стану (дебаунсінг)
if (reading != _sensors[i].lastInputState) {
_sensors[i].lastDebounceTime = currentMillis;
_sensors[i].lastInputState = reading;
}
if ((currentMillis - _sensors[i].lastDebounceTime) > DEBOUNCE_DELAY_MS) {
// Якщо стан стабілізувався
if (reading != _sensors[i].currentInputState) {
_sensors[i].currentInputState = reading;
// Якщо зафіксовано фронт (поява руху) або продовжується утримання (застаріле оновлення)
// Для датчиків зазвичай 1 = рух
if (_sensors[i].currentInputState == HIGH) {
_sensors[i].lastRealMotionTime = currentMillis;
}
}
}
// Якщо стан досі активний (HIGH), оновлюємо час останнього руху
if (_sensors[i].currentInputState == HIGH) {
_sensors[i].lastRealMotionTime = currentMillis;
}
// Логіка керування виходом
bool shouldOutputBeHigh = false;
// 1. Якщо активна імітація
if (currentMillis < _sensors[i].simulationEndTime) {
shouldOutputBeHigh = true;
}
// 2. Якщо режим "прозорий" і є реальний рух
else if (_sensors[i].mode == MODE_TRANSPARENT && _sensors[i].currentInputState == HIGH) {
shouldOutputBeHigh = true;
}
// Встановлення стану виходу. LOW закриває NPN-транзистор, колектор підтягується до 1 (імітація/прозорий рух).
// HIGH відкриває транзистор, колектор просідає на землю (нема руху).
digitalWrite(_sensors[i].pinOut, shouldOutputBeHigh ? LOW : HIGH);
}
}
bool MotionSensorManager::hasRecentMotion(uint8_t sensorIndex) {
if (sensorIndex > 1) return false;
unsigned long currentMillis = millis();
// Перевірка тільки реального руху
return (currentMillis - _sensors[sensorIndex].lastRealMotionTime) < MOTION_DURATION_MS;
}
void MotionSensorManager::simulateMotion(uint8_t sensorIndex) {
if (sensorIndex > 1) return;
_sensors[sensorIndex].simulationEndTime = millis() + MOTION_DURATION_MS;
}
void MotionSensorManager::prepareSleep() {
for (int i = 0; i < 2; i++) {
digitalWrite(_sensors[i].pinOut, LOW);
gpio_hold_en((gpio_num_t)_sensors[i].pinOut);
gpio_hold_en((gpio_num_t)_sensors[i].pinIn);
}
gpio_deep_sleep_hold_en();
}
void MotionSensorManager::wakeUp() {
for (int i = 0; i < 2; i++) {
gpio_hold_dis((gpio_num_t)_sensors[i].pinOut);
gpio_hold_dis((gpio_num_t)_sensors[i].pinIn);
}
gpio_deep_sleep_hold_dis();
}
void MotionSensorManager::setMode(uint8_t sensorIndex, SensorMode mode) {
if (sensorIndex > 1) return;
_sensors[sensorIndex].mode = mode;
}
MotionSensorManager::SensorMode MotionSensorManager::getMode(uint8_t sensorIndex) {
if (sensorIndex > 1) return MODE_CLOSED;
return _sensors[sensorIndex].mode;
}