forked from tretyakovsa/Sonoff_WiFi_switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IR.ino
413 lines (404 loc) · 11.1 KB
/
IR.ino
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#ifdef irTransmitterM
// ----------------------Передатчик ИK
void irTransmitter() {
String moduleName = "irTransmitter";
// Serial.println(moduleName);
byte pin = readArgsInt();
pin = pinTest(pin);
irSender = new IRsend(pin); // Create a new IRrecv object. Change to what ever pin you need etc.
irSender->begin();
sCmd.addCommand("irsend", handleIrTransmit);
commandsReg(irsendS);
modulesReg(moduleName);
}
// команда irsend
void handleIrTransmit() {
uint8_t ir_type = readArgsInt(); // Тип сигнала
String code_str = readArgsString(); // Код
uint16_t repeat = readArgsInt(); // Повтор
uint8_t bits = readArgsInt(); // Длинна кода
uint32_t code = strtol(("0x" + code_str).c_str(), NULL, 0);
//irSender->sendNEC(tmp, 32);
switch (ir_type) {
#if SEND_RC5
case RC5: // 1
if (bits == 0)
bits = RC5_BITS;
irSender->sendRC5(code, bits, repeat);
break;
#endif
#if SEND_RC6
case RC6: // 2
if (bits == 0)
bits = RC6_MODE0_BITS;
irSender->sendRC6(code, bits, repeat);
break;
#endif
#if SEND_NEC
case NEC: // 3
if (bits == 0)
bits = NEC_BITS;
irSender->sendNEC(code, bits, repeat);
break;
#endif
#if SEND_SONY
case SONY: // 4
if (bits == 0)
bits = SONY_12_BITS;
repeat = std::max(repeat, (uint16_t) SONY_MIN_REPEAT);
irSender->sendSony(code, bits, repeat);
break;
#endif
#if SEND_PANASONIC
case PANASONIC: // 5
if (bits == 0)
bits = PANASONIC_BITS;
irSender->sendPanasonic64(code, bits, repeat);
break;
#endif
#if SEND_JVC
case JVC: // 6
if (bits == 0)
bits = JVC_BITS;
irSender->sendJVC(code, bits, repeat);
break;
#endif
#if SEND_SAMSUNG
case SAMSUNG: // 7
if (bits == 0)
bits = SAMSUNG_BITS;
irSender->sendSAMSUNG(code, bits, repeat);
break;
#endif
#if SEND_WHYNTER
case WHYNTER: // 8
if (bits == 0)
bits = WHYNTER_BITS;
irSender->sendWhynter(code, bits, repeat);
break;
#endif
#if SEND_AIWA_RC_T501
case AIWA_RC_T501: // 9
if (bits == 0)
bits = AIWA_RC_T501_BITS;
repeat = std::max(repeat, (uint16_t) AIWA_RC_T501_MIN_REPEAT);
irSender->sendAiwaRCT501(code, bits, repeat);
break;
#endif
#if SEND_LG
case LG: // 10
if (bits == 0)
bits = LG_BITS;
irSender->sendLG(code, bits, repeat);
break;
#endif
#if SEND_MITSUBISHI
case MITSUBISHI: // 12
if (bits == 0)
bits = MITSUBISHI_BITS;
repeat = std::max(repeat, (uint16_t) MITSUBISHI_MIN_REPEAT);
irSender->sendMitsubishi(code, bits, repeat);
break;
#endif
#if SEND_DISH
case DISH: // 13
if (bits == 0)
bits = DISH_BITS;
repeat = std::max(repeat, (uint16_t) DISH_MIN_REPEAT);
irSender->sendDISH(code, bits, repeat);
break;
#endif
#if SEND_SHARP
case SHARP: // 14
if (bits == 0)
bits = SHARP_BITS;
irSender->sendSharpRaw(code, bits, repeat);
break;
#endif
#if SEND_COOLIX
case COOLIX: // 15
if (bits == 0)
bits = COOLIX_BITS;
irSender->sendCOOLIX(code, bits, repeat);
break;
#endif
case DAIKIN: // 16
case KELVINATOR: // 18
case MITSUBISHI_AC: // 20
case GREE: // 24
case ARGO: // 27
case TROTEC: // 28
case TOSHIBA_AC: // 32
case FUJITSU_AC: // 33
case HAIER_AC: // 38
case HITACHI_AC: // 40
case HITACHI_AC1: // 41
case HITACHI_AC2: // 42
parseStringAndSendAirCon(ir_type, code_str);
break;
#if SEND_DENON
case DENON: // 17
if (bits == 0)
bits = DENON_BITS;
irSender->sendDenon(code, bits, repeat);
break;
#endif
#if SEND_SHERWOOD
case SHERWOOD: // 19
if (bits == 0)
bits = SHERWOOD_BITS;
repeat = std::max(repeat, (uint16_t) SHERWOOD_MIN_REPEAT);
irSender->sendSherwood(code, bits, repeat);
break;
#endif
#if SEND_RCMM
case RCMM: // 21
if (bits == 0)
bits = RCMM_BITS;
irSender->sendRCMM(code, bits, repeat);
break;
#endif
#if SEND_SANYO
case SANYO_LC7461: // 22
if (bits == 0)
bits = SANYO_LC7461_BITS;
irSender->sendSanyoLC7461(code, bits, repeat);
break;
#endif
#if SEND_RC5
case RC5X: // 23
if (bits == 0)
bits = RC5X_BITS;
irSender->sendRC5(code, bits, repeat);
break;
#endif
#if SEND_PRONTO
case PRONTO: // 25
// parseStringAndSendPronto(code_str, repeat);
break;
#endif
#if SEND_NIKAI
case NIKAI: // 29
if (bits == 0)
bits = NIKAI_BITS;
irSender->sendNikai(code, bits, repeat);
break;
#endif
#if SEND_RAW
case RAW: // 30
// parseStringAndSendRaw(code_str);
break;
#endif
#if SEND_GLOBALCACHE
case GLOBALCACHE: // 31
// parseStringAndSendGC(code_str);
break;
#endif
#if SEND_MIDEA
case MIDEA: // 34
if (bits == 0)
bits = MIDEA_BITS;
irSender->sendMidea(code, bits, repeat);
break;
#endif
#if SEND_MAGIQUEST
case MAGIQUEST: // 35
if (bits == 0)
bits = MAGIQUEST_BITS;
irSender->sendMagiQuest(code, bits, repeat);
break;
#endif
#if SEND_LASERTAG
case LASERTAG: // 36
if (bits == 0)
bits = LASERTAG_BITS;
irSender->sendLasertag(code, bits, repeat);
break;
#endif
#if SEND_CARRIER_AC
case CARRIER_AC: // 37
if (bits == 0)
bits = CARRIER_AC_BITS;
irSender->sendCarrierAC(code, bits, repeat);
break;
#endif
#if SEND_MITSUBISHI2
case MITSUBISHI2: // 39
if (bits == 0)
bits = MITSUBISHI_BITS;
repeat = std::max(repeat, (uint16_t) MITSUBISHI_MIN_REPEAT);
irSender->sendMitsubishi2(code, bits, repeat);
break;
#endif
#if SEND_GICABLE
case GICABLE: // 43
if (bits == 0)
bits = GICABLE_BITS;
repeat = std::max(repeat, (uint16_t) GICABLE_BITS);
irSender->sendGICable(code, bits, repeat);
break;
#endif
}
}
// Parse an Air Conditioner A/C Hex String/code and send it.
// Args:
// irType: Nr. of the protocol we need to send.
// str: A hexadecimal string containing the state to be sent.
void parseStringAndSendAirCon(const uint16_t irType, const String str) {
uint8_t strOffset = 0;
uint8_t state[STATE_SIZE_MAX] = {0}; // All array elements are set to 0.
uint16_t stateSize = 0;
if (str.startsWith("0x") || str.startsWith("0X"))
strOffset = 2;
// Calculate how many hexadecimal characters there are.
uint16_t inputLength = str.length() - strOffset;
if (inputLength == 0) {
// debug("Zero length AirCon code encountered. Ignored.");
return; // No input. Abort.
}
switch (irType) { // Get the correct state size for the protocol.
case KELVINATOR:
stateSize = KELVINATOR_STATE_LENGTH;
break;
case TOSHIBA_AC:
stateSize = TOSHIBA_AC_STATE_LENGTH;
break;
case DAIKIN:
stateSize = DAIKIN_COMMAND_LENGTH;
break;
case MITSUBISHI_AC:
stateSize = MITSUBISHI_AC_STATE_LENGTH;
break;
case TROTEC:
stateSize = TROTEC_COMMAND_LENGTH;
break;
case ARGO:
stateSize = ARGO_COMMAND_LENGTH;
break;
case GREE:
stateSize = GREE_STATE_LENGTH;
break;
case FUJITSU_AC:
// Fujitsu has four distinct & different size states, so make a best guess
// which one we are being presented with based on the number of
// hexadecimal digits provided. i.e. Zero-pad if you need to to get
// the correct length/byte size.
stateSize = inputLength / 2; // Every two hex chars is a byte.
// Use at least the minimum size.
stateSize = std::max(stateSize,
(uint16_t) (FUJITSU_AC_STATE_LENGTH_SHORT - 1));
// If we think it isn't a "short" message.
if (stateSize > FUJITSU_AC_STATE_LENGTH_SHORT)
// Then it has to be at least the smaller version of the "normal" size.
stateSize = std::max(stateSize,
(uint16_t) (FUJITSU_AC_STATE_LENGTH - 1));
// Lastly, it should never exceed the maximum "normal" size.
stateSize = std::min(stateSize, (uint16_t) FUJITSU_AC_STATE_LENGTH);
break;
case HAIER_AC:
stateSize = HAIER_AC_STATE_LENGTH;
break;
case HITACHI_AC:
stateSize = HITACHI_AC_STATE_LENGTH;
break;
case HITACHI_AC1:
stateSize = HITACHI_AC1_STATE_LENGTH;
break;
case HITACHI_AC2:
stateSize = HITACHI_AC2_STATE_LENGTH;
break;
default: // Not a protocol we expected. Abort.
// debug("Unexpected AirCon protocol detected. Ignoring.");
return;
}
if (inputLength > stateSize * 2) {
// debug("AirCon code to large for the given protocol.");
return;
}
// Ptr to the least significant byte of the resulting state for this protocol.
uint8_t *statePtr = &state[stateSize - 1];
// Convert the string into a state array of the correct length.
for (uint16_t i = 0; i < inputLength; i++) {
// Grab the next least sigificant hexadecimal digit from the string.
uint8_t c = tolower(str[inputLength + strOffset - i - 1]);
if (isxdigit(c)) {
if (isdigit(c))
c -= '0';
else
c = c - 'a' + 10;
} else {
// debug("Aborting! Non-hexadecimal char found in AirCon state: " + str);
return;
}
if (i % 2 == 1) { // Odd: Upper half of the byte.
*statePtr += (c << 4);
statePtr--; // Advance up to the next least significant byte of state.
} else { // Even: Lower half of the byte.
*statePtr = c;
}
}
// Make the appropriate call for the protocol type.
switch (irType) {
#if SEND_KELVINATOR
case KELVINATOR:
irSender->sendKelvinator(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_TOSHIBA_AC
case TOSHIBA_AC:
irSender->sendToshibaAC(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_DAIKIN
case DAIKIN:
irSender->sendDaikin(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if MITSUBISHI_AC
case MITSUBISHI_AC:
irSender->sendMitsubishiAC(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_TROTEC
case TROTEC:
irSender->sendTrotec(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_ARGO
case ARGO:
irSender->sendArgo(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_GREE
case GREE:
irSender->sendGree(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_FUJITSU_AC
case FUJITSU_AC:
irSender->sendFujitsuAC(reinterpret_cast<uint8_t *>(state), stateSize);
break;
#endif
#if SEND_HAIER_AC
case HAIER_AC:
irSender->sendHaierAC(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_HITACHI_AC
case HITACHI_AC:
irSender->sendHitachiAC(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_HITACHI_AC1
case HITACHI_AC1:
irSender->sendHitachiAC1(reinterpret_cast<uint8_t *>(state));
break;
#endif
#if SEND_HITACHI_AC2
case HITACHI_AC2:
irSender->sendHitachiAC2(reinterpret_cast<uint8_t *>(state));
break;
#endif
}
}
#endif