-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIRController.cpp
More file actions
362 lines (303 loc) · 10.7 KB
/
Copy pathIRController.cpp
File metadata and controls
362 lines (303 loc) · 10.7 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "IRController.h"
IRController::IRController(uint16_t sendPin, uint16_t recvPin, uint16_t captureBufferSize, uint8_t timeout, bool debug)
: irsend(sendPin), irrecv(recvPin, captureBufferSize, timeout, debug), acController(sendPin) {
}
void IRController::beginSend() {
irsend.begin();
loadLastState();
loadDryingSettings();
loadIdentifiedProtocols();
}
void IRController::beginReceive() {
irrecv.enableIRIn();
}
void IRController::setThermostatCharacteristics(SpanCharacteristic *targetState, SpanCharacteristic *targetTemp) {
this->targetState = targetState;
this->targetTemp = targetTemp;
}
void IRController::setFanCharacteristics(SpanCharacteristic *fanSpeed, SpanCharacteristic *swingMode) {
this->fanSpeed = fanSpeed;
this->swingMode = swingMode;
}
void IRController::handleIR() {
decode_results results;
if (irrecv.decode(&results)) {
stdAc::state_t currentState;
String savedProtocol = getProtocol();
// Detect the current protocol
String detectedProtocol = typeToString(results.decode_type);
Serial.println("Received signal from: " + detectedProtocol);
// Ignore "UNKNOWN" protocols
if (detectedProtocol != "UNKNOWN" && !detectedProtocol.isEmpty()) {
if (std::find(identifiedProtocols.begin(), identifiedProtocols.end(), detectedProtocol) == identifiedProtocols.end()) {
// Add detected protocol to identified protocols list if not already present
identifiedProtocols.push_back(detectedProtocol);
saveIdentifiedProtocols(); // Save protocols after adding a new one
}
if (savedProtocol.isEmpty()) {
// If no protocol is saved, check if the detected protocol is valid and supported by IRac
if (IRac::isProtocolSupported(results.decode_type)) {
// Save the first valid and supported detected protocol
saveProtocol(detectedProtocol.c_str());
Serial.println("First valid and supported protocol detected and saved: " + detectedProtocol);
} else {
Serial.println("Detected protocol is not supported by IRac. Ignoring.");
}
} else {
if (detectedProtocol == savedProtocol) {
Serial.println("Using saved protocol: " + savedProtocol);
if (IRAcUtils::decodeToState(&results, ¤tState, &lastState)) {
lastState = currentState;
saveLastState(); // Save the updated lastState
updateHomeKitFromIR();
}
} else {
Serial.println("Detected protocol does not match the saved protocol. Ignoring.");
}
}
} else {
Serial.println("Ignored invalid or unknown protocol: " + detectedProtocol);
}
irrecv.resume();
}
}
void IRController::sendThermostatCommand(bool power, int mode, int temp) {
stdAc::state_t newState = lastState;
newState.power = power;
newState.degrees = temp;
switch (mode) {
// case 0: // Auto
// newState.mode = static_cast<stdAc::opmode_t>(0);
// break;
case 1: // Heating
newState.mode = stdAc::opmode_t::kHeat;
break;
case 2: // Cooling
newState.mode = stdAc::opmode_t::kCool;
break;
case 3: // Fan
newState.mode = stdAc::opmode_t::kFan;
break;
default:
Serial.println("Invalid mode. Defaulting to Auto.");
// newState.mode = static_cast<stdAc::opmode_t>(0);
break;
}
sendCommand(newState);
}
void IRController::sendFanCommand(int fanSpeed, bool swing) {
stdAc::state_t newState = lastState;
int mappedFanSpeed = (fanSpeed == 0) ? 0 : (fanSpeed <= 33) ? 1
: (fanSpeed <= 66) ? 3
: 5;
newState.fanspeed = static_cast<stdAc::fanspeed_t>(mappedFanSpeed);
stdAc::swingv_t swingv = stdAc::swingv_t::kAuto;
stdAc::swingh_t swingh = stdAc::swingh_t::kAuto;
if (swing) {
swingv = stdAc::swingv_t::kOff;
swingh = stdAc::swingh_t::kOff;
}
newState.swingv = swingv;
newState.swingh = swingh;
sendCommand(newState);
}
void IRController::sendCommand(stdAc::state_t newState) {
String savedProtocol = getProtocol();
// Check if a protocol is saved before sending a command
if (savedProtocol.isEmpty()) {
Serial.println("No protocol saved. Cannot send command.");
return; // Exit the function if no protocol is saved
}
irrecv.pause();
delay(10);
// Check if lastState is valid, if not, load the saved lastState
if (!lastStateValid) {
loadLastState();
}
if (acController.sendAc(newState, &lastState)) {
lastState = newState;
saveLastState(); // Save the updated lastState
} else {
Serial.println("Failed to send AC command.");
}
delay(10);
irrecv.resume();
}
void IRController::updateHomeKitFromIR() {
if (targetTemp->getVal() != lastState.degrees) {
targetTemp->setVal(lastState.degrees);
}
if (lastState.power || (targetState->getVal() != static_cast<int>(lastState.mode))) {
switch (lastState.mode) {
case stdAc::opmode_t::kAuto: // Auto Mode
targetState->setVal(3);
break;
case stdAc::opmode_t::kHeat: // Heating Mode
targetState->setVal(1);
break;
case stdAc::opmode_t::kCool: // Cooling Mode
targetState->setVal(2);
break;
// case stdAc::opmode_t::kFan: // Fan Mode
// targetState->setVal(3);
// break;
default:
Serial.println("HomeKit Unrecognized mode received.");
break;
}
int fanNewSpeed;
switch (static_cast<int>(lastState.fanspeed)) {
case 0: // Auto
fanNewSpeed = 0;
break;
case 1: // Low
fanNewSpeed = 25;
break;
case 3: // Medium
fanNewSpeed = 50;
break;
case 5: // High
fanNewSpeed = 100;
break;
default:
fanNewSpeed = 0;
break;
}
if (fanSpeed->getVal() != fanNewSpeed) {
fanSpeed->setVal(fanNewSpeed);
}
int newSwingMode = 1;
if (lastState.swingv != stdAc::swingv_t::kOff || lastState.swingh != stdAc::swingh_t::kOff) {
newSwingMode = 0;
}
if (swingMode->getVal() != newSwingMode) {
swingMode->setVal(newSwingMode);
}
} else {
targetState->setVal(0);
}
}
void IRController::saveProtocol(const char *protocol) {
preferences.begin("IRController", false);
preferences.putString("protocol", protocol);
preferences.end();
}
String IRController::getProtocol() {
preferences.begin("IRController", true);
String protocol = preferences.getString("protocol", "");
preferences.end();
return protocol;
}
std::vector<String> IRController::getIdentifiedProtocols() {
return identifiedProtocols;
}
void IRController::setProtocol(const String &protocol) {
saveProtocol(protocol.c_str());
}
void IRController::deleteIdentifiedProtocols() {
identifiedProtocols.clear();
preferences.begin("IRController", false);
preferences.remove("identifiedProtocols");
preferences.remove("protocol");
preferences.remove("lastState");
preferences.end();
}
void IRController::saveIdentifiedProtocols() {
preferences.begin("IRController", false);
// Join the identifiedProtocols vector into a single string separated by commas
String protocolsString;
for (size_t i = 0; i < identifiedProtocols.size(); i++) {
protocolsString += identifiedProtocols[i];
if (i < identifiedProtocols.size() - 1) {
protocolsString += ","; // Add comma delimiter between protocols
}
}
// Save the serialized string to preferences
preferences.putString("identifiedProtocols", protocolsString);
preferences.end();
}
void IRController::loadIdentifiedProtocols() {
preferences.begin("IRController", true); // Open preferences in read-only mode
// Get the serialized protocols string
String protocolsString = preferences.getString("identifiedProtocols", "");
preferences.end();
identifiedProtocols.clear(); // Clear any existing protocols
// Split the string into individual protocols based on commas
if (protocolsString.length() > 0) {
int start = 0;
int end = protocolsString.indexOf(',');
while (end != -1) {
identifiedProtocols.push_back(protocolsString.substring(start, end));
start = end + 1;
end = protocolsString.indexOf(',', start);
}
// Add the last protocol after the last comma
identifiedProtocols.push_back(protocolsString.substring(start));
}
}
void IRController::saveLastState() {
preferences.begin("IRController", false);
preferences.putBytes("lastState", &lastState, sizeof(lastState));
preferences.end();
}
void IRController::loadLastState() {
preferences.begin("IRController", true);
size_t size = preferences.getBytes("lastState", &lastState, sizeof(lastState));
if (size == sizeof(lastState)) {
lastStateValid = true;
} else {
lastStateValid = false;
}
preferences.end();
}
void IRController::saveDryingSettings() {
preferences.begin("IRController", false);
preferences.putBool("dryingEnabled", dryingBeforeShutdownEnabled);
preferences.putInt("dryingDelay", dryingDelayMinutes);
preferences.end();
delay(1000);
ESP.restart();
}
void IRController::loadDryingSettings() {
preferences.begin("IRController", true);
if (!preferences.isKey("dryingEnabled")) {
dryingBeforeShutdownEnabled = true; // Default to enabled
} else {
dryingBeforeShutdownEnabled = preferences.getBool("dryingEnabled", true);
}
if (!preferences.isKey("dryingDelay")) {
dryingDelayMinutes = 40; // Default delay of 40 minutes
} else {
dryingDelayMinutes = preferences.getInt("dryingDelay", 40);
}
preferences.end();
// saveDryingSettings();
}
void IRController::enableDryingBeforeShutdown(bool enable, int delayMinutes) {
dryingBeforeShutdownEnabled = enable;
dryingDelayMinutes = delayMinutes;
saveDryingSettings();
}
bool IRController::isDryingBeforeShutdownEnabled() const {
return dryingBeforeShutdownEnabled;
}
int IRController::getDryingDelayMinutes() const {
return dryingDelayMinutes;
}
int IRController::getDryingDelayInSeconds() const {
return dryingDelayMinutes * 60;
}
void IRController::startDryingBeforeShutdown() {
dryingInProgress = true;
stdAc::state_t newState = lastState;
targetState->setVal(0);
fanSpeed->setVal(100);
newState.mode = stdAc::opmode_t::kFan;
newState.fanspeed = static_cast<stdAc::fanspeed_t>(5);
sendCommand(newState);
}
void IRController::completeShutdown() {
stdAc::state_t newState = lastState;
newState.power = 0;
sendCommand(newState);
}