Repository navigation
Expand file tree
/
Copy pathGS32IR.cpp
More file actions
183 lines (163 loc) · 6.48 KB
/
Copy pathGS32IR.cpp
File metadata and controls
183 lines (163 loc) · 6.48 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "GS32IR.h"
GS32IR* GS32IR::_instance = nullptr;
const char* protocolToString(IRProtocol prot) {
switch (prot) {
case IR_PROT_NEC: return "NEC";
case IR_PROT_SAMSUNG: return "SAMSUNG";
default: return "UNKNOWN";
}
}
GS32IR::GS32IR()
: _pin(255), _filterProtocol(IR_PROT_ANY), _anyKeyCallback(nullptr),
_activeCode(0), _activeProtocol(IR_PROT_ANY), _lastActiveTime(0), _isHolding(false),
_lastIsrTime(0), _isrTempState(0), _isrTempBitCount(0), _isrTempResult(0),
_isrDecodedValue(0), _isrTempProtocol(IR_PROT_ANY), _isrDecodedProtocol(IR_PROT_ANY),
_isrActiveProtocol(IR_PROT_ANY), _isrDataReady(false), _isrRepeatReady(false) {
_instance = this;
}
void GS32IR::begin(uint8_t pin, IRProtocol filterProtocol) {
_pin = pin;
_filterProtocol = filterProtocol;
pinMode(_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(_pin), _isr, FALLING);
}
GS32IRKey& GS32IR::addKey(uint32_t code, IRProtocol protocol) {
_keys.emplace_back(code, protocol);
return _keys.back();
}
void GS32IR::onAnyKey(IRGenericCallback cb) {
_anyKeyCallback = cb;
}
void GS32IR::tick() {
uint32_t now = millis();
uint32_t decodedValue = 0;
IRProtocol decodedProtocol = IR_PROT_ANY;
bool hasNewData = false;
bool hasRepeat = false;
// Безопасное чтение данных из ISR прерывания
noInterrupts();
if (_isrDataReady) {
decodedValue = _isrDecodedValue;
decodedProtocol = _isrDecodedProtocol;
hasNewData = true;
_isrDataReady = false;
}
if (_isrRepeatReady) {
hasRepeat = true;
_isrRepeatReady = false;
}
interrupts();
if (hasNewData) {
if (_activeCode != 0 && (_activeCode != decodedValue || _activeProtocol != decodedProtocol)) {
_handleRelease(_activeCode, _activeProtocol);
}
_activeCode = decodedValue;
_activeProtocol = decodedProtocol;
_lastActiveTime = now;
_isHolding = false;
_handlePress(_activeCode, _activeProtocol);
}
else if (hasRepeat) {
if (_activeCode != 0) {
_lastActiveTime = now;
_isHolding = true;
_handleHold(_activeCode, _activeProtocol);
}
}
// Авто-отпускание по тайм-ауту (220 мс)
if (_activeCode != 0 && (now - _lastActiveTime > 220)) {
_handleRelease(_activeCode, _activeProtocol);
_activeCode = 0;
_activeProtocol = IR_PROT_ANY;
_isHolding = false;
noInterrupts();
_isrActiveProtocol = IR_PROT_ANY; // Сбрасываем активный протокол в прерывании
interrupts();
}
}
void GS32IR::_handlePress(uint32_t code, IRProtocol prot) {
if (_anyKeyCallback) _anyKeyCallback(code, 0, prot);
for (auto& key : _keys) {
if (key.getCode() == code && (key.getProtocol() == IR_PROT_ANY || key.getProtocol() == prot)) {
key.triggerPress();
}
}
}
void GS32IR::_handleHold(uint32_t code, IRProtocol prot) {
if (_anyKeyCallback) _anyKeyCallback(code, 1, prot);
for (auto& key : _keys) {
if (key.getCode() == code && (key.getProtocol() == IR_PROT_ANY || key.getProtocol() == prot)) {
key.triggerHold();
}
}
}
void GS32IR::_handleRelease(uint32_t code, IRProtocol prot) {
if (_anyKeyCallback) _anyKeyCallback(code, 2, prot);
for (auto& key : _keys) {
if (key.getCode() == code && (key.getProtocol() == IR_PROT_ANY || key.getProtocol() == prot)) {
key.triggerRelease();
}
}
}
void IRAM_ATTR GS32IR::_isr() {
if (!_instance) return;
uint32_t now = micros();
uint32_t duration = now - _instance->_lastIsrTime;
_instance->_lastIsrTime = now;
// 1. Стартовый импульс NEC (12.0 - 15.0 мс)
if (duration >= 12000 && duration <= 15000) {
if (_instance->_filterProtocol == IR_PROT_ANY || _instance->_filterProtocol == IR_PROT_NEC) {
_instance->_isrTempProtocol = IR_PROT_NEC;
_instance->_isrTempState = 1;
_instance->_isrTempBitCount = 0;
_instance->_isrTempResult = 0;
}
}
// 2. Стартовый импульс SAMSUNG (8.0 - 9.9 мс)
else if (duration >= 8000 && duration <= 9900) {
if (_instance->_filterProtocol == IR_PROT_ANY || _instance->_filterProtocol == IR_PROT_SAMSUNG) {
_instance->_isrTempProtocol = IR_PROT_SAMSUNG;
_instance->_isrTempState = 1;
_instance->_isrTempBitCount = 0;
_instance->_isrTempResult = 0;
}
}
// 3. Импульс повтора NEC (10.0 - 12.5 мс)
else if (duration >= 10000 && duration <= 12500) {
if (_instance->_filterProtocol == IR_PROT_ANY || _instance->_filterProtocol == IR_PROT_NEC) {
if (_instance->_isrActiveProtocol == IR_PROT_NEC) {
_instance->_isrRepeatReady = true;
_instance->_isrTempState = 0;
}
}
}
// 4. Импульс повтора SAMSUNG (6.0 - 7.5 мс)
else if (duration >= 6000 && duration <= 7500) {
if (_instance->_filterProtocol == IR_PROT_ANY || _instance->_filterProtocol == IR_PROT_SAMSUNG) {
if (_instance->_isrActiveProtocol == IR_PROT_SAMSUNG) {
_instance->_isrRepeatReady = true;
_instance->_isrTempState = 0;
}
}
}
// 5. Чтение бит данных
else if (_instance->_isrTempState == 1) {
if (duration >= 700 && duration <= 1500) { // Bit 0
_instance->_isrTempBitCount++;
}
else if (duration >= 1600 && duration <= 2750) { // Bit 1
_instance->_isrTempResult |= (1UL << _instance->_isrTempBitCount);
_instance->_isrTempBitCount++;
}
else {
_instance->_isrTempState = 0;
}
if (_instance->_isrTempBitCount == 32) {
_instance->_isrDecodedValue = _instance->_isrTempResult;
_instance->_isrDecodedProtocol = _instance->_isrTempProtocol;
_instance->_isrActiveProtocol = _instance->_isrTempProtocol; // Запоминаем для повторов
_instance->_isrDataReady = true;
_instance->_isrTempState = 0;
}
}
}