-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir_capture.cpp
More file actions
181 lines (156 loc) · 5.13 KB
/
ir_capture.cpp
File metadata and controls
181 lines (156 loc) · 5.13 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
#include "ir_capture.h"
IRCaptureManager::IRCaptureManager()
: _irrecv(IR_RECV_PIN, CAPTURE_BUFFER_SIZE, CAPTURE_TIMEOUT, true),
_newCapture(false),
_captureCounter(0),
_lastCaptureTime(0),
_blockUntil(0) {
}
void IRCaptureManager::begin() {
_irrecv.setUnknownThreshold(MIN_UNKNOWN_SIZE);
_irrecv.setTolerance(kTolerance);
_irrecv.enableIRIn();
Serial.println("[IR] Receptor iniciado no GPIO " + String(IR_RECV_PIN));
Serial.println("[IR] Unknown threshold: " + String(MIN_UNKNOWN_SIZE) + " pulsos");
}
bool IRCaptureManager::check() {
// Bloqueio pós-transmissão: ignora tudo até o tempo passar
if (millis() < _blockUntil) {
if (_irrecv.decode(&_results)) {
_irrecv.resume(); // Descarta silenciosamente
}
return false;
}
if (_irrecv.decode(&_results)) {
// Debounce: ignora capturas muito próximas
if (millis() - _lastCaptureTime < CAPTURE_DEBOUNCE_MS) {
_irrecv.resume();
return false;
}
_lastCaptureTime = millis();
_processResults();
_irrecv.resume();
return true;
}
return false;
}
void IRCaptureManager::blockFor(unsigned long ms) {
_blockUntil = millis() + ms;
Serial.println("[IR] Receptor bloqueado por " + String(ms) + "ms");
}
void IRCaptureManager::_processResults() {
_lastCapture = IRCapture();
_lastCapture.valid = true;
_lastCapture.id = _generateId();
_lastCapture.timestamp = _getTimestamp();
_lastCapture.tags = "[]";
_lastCapture.notes = "";
// Protocolo
if (_results.decode_type != decode_type_t::UNKNOWN) {
_lastCapture.protocolName = typeToString(_results.decode_type);
_lastCapture.bits = _results.bits;
_lastCapture.decodedHex = resultToHexidecimal(&_results);
_lastCapture.description = _buildDescription(&_results);
_lastCapture.hashCode = "";
} else {
_lastCapture.protocolName = "UNKNOWN";
_lastCapture.bits = 0;
_lastCapture.decodedHex = "";
_lastCapture.description = "Protocolo nao identificado";
// Hash para identificação
char hashBuf[12];
snprintf(hashBuf, sizeof(hashBuf), "0x%08X", (uint32_t)_results.value);
_lastCapture.hashCode = String(hashBuf);
}
// Raw timings - copiar do buffer
_lastCapture.rawLen = _results.rawlen - 1; // primeiro elemento é gap
for (uint16_t i = 1; i < _results.rawlen; i++) {
_lastCapture.rawTimings[i - 1] = _results.rawbuf[i] * kRawTick;
}
_newCapture = true;
// Log serial
Serial.println("[IR] Captura: " + _lastCapture.protocolName +
" (" + String(_lastCapture.bits) + " bits)");
if (_lastCapture.decodedHex.length() > 0) {
Serial.println("[IR] Hex: " + _lastCapture.decodedHex);
}
Serial.println("[IR] Raw: " + String(_lastCapture.rawLen) + " timings");
}
String IRCaptureManager::_buildDescription(decode_results* results) {
String desc = "";
// Tentar usar IRAc para decodificação rica de A/C
if (hasACState(results->decode_type)) {
desc = IRAcUtils::resultAcToString(results);
if (desc.length() > 0) {
return desc;
}
}
// Descrição básica
desc = typeToString(results->decode_type);
desc += " " + String(results->bits) + " bits";
return desc;
}
String IRCaptureManager::_generateId() {
_captureCounter++;
unsigned long ms = millis();
char buf[32];
// Formato: cap_MMMMMMMMM_NNN (millis + contador sequencial)
snprintf(buf, sizeof(buf), "cap_%09lu_%03u", ms, (unsigned int)(_captureCounter % 1000));
return String(buf);
}
String IRCaptureManager::_getTimestamp() {
// Sem RTC, usamos uptime em formato legível
unsigned long sec = millis() / 1000;
unsigned long min = sec / 60;
unsigned long hr = min / 60;
char buf[16];
snprintf(buf, sizeof(buf), "%02lu:%02lu:%02lu", hr, min % 60, sec % 60);
return String(buf);
}
bool IRCaptureManager::hasNewCapture() {
return _newCapture;
}
void IRCaptureManager::clearNewCapture() {
_newCapture = false;
}
IRCapture IRCaptureManager::getLastCapture() {
return _lastCapture;
}
String IRCaptureManager::captureToJSON(const IRCapture& cap) {
String json = "{";
json += "\"id\":\"" + cap.id + "\",";
json += "\"timestamp\":\"" + cap.timestamp + "\",";
json += "\"tags\":" + cap.tags + ",";
json += "\"notes\":\"" + cap.notes + "\",";
// Protocol
json += "\"protocol\":{";
json += "\"name\":\"" + cap.protocolName + "\",";
json += "\"bits\":" + String(cap.bits) + ",";
if (cap.decodedHex.length() > 0) {
json += "\"decoded\":\"" + cap.decodedHex + "\",";
} else {
json += "\"decoded\":null,";
}
if (cap.hashCode.length() > 0) {
json += "\"hash\":\"" + cap.hashCode + "\",";
}
json += "\"description\":\"" + cap.description + "\"";
json += "},";
// Raw
json += "\"raw\":{";
json += "\"count\":" + String(cap.rawLen) + ",";
json += "\"frequency_khz\":38,";
json += "\"timings\":[";
for (uint16_t i = 0; i < cap.rawLen; i++) {
if (i > 0) json += ",";
json += String(cap.rawTimings[i]);
}
json += "]},";
// Device info
json += "\"device_info\":{";
json += "\"firmware_version\":\"" FIRMWARE_VERSION "\",";
json += "\"ir_lib_version\":\"" _IRREMOTEESP8266_VERSION_ "\"";
json += "}";
json += "}";
return json;
}