-
Notifications
You must be signed in to change notification settings - Fork 0
/
irmp.c
3018 lines (2778 loc) · 171 KB
/
irmp.c
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------------------------------------------------------------
* irmp.c - infrared multi-protocol decoder, supports several remote control protocols
*
* Copyright (c) 2009-2019 Frank Meyer - frank(at)fli4l.de
*
* Supported AVR mikrocontrollers:
*
* ATtiny87, ATtiny167
* ATtiny45, ATtiny85
* ATtiny44, ATtiny84
* ATmega8, ATmega16, ATmega32
* ATmega162
* ATmega164, ATmega324, ATmega644, ATmega644P, ATmega1284, ATmega1284P
* ATmega88, ATmega88P, ATmega168, ATmega168P, ATmega328P
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*---------------------------------------------------------------------------------------------------------------------------------------------------
*/
#include "irmp.h"
#include "irmpcalculatedconstants.h"
#include "irmplog.h"
#include "irmpprotocolparams.h"
#ifdef ANALYZE
/* not every PIC compiler knows variadic macros :-( */
# define ANALYZE_PUTCHAR(a) { if (! silent) { putchar (a); } }
# define ANALYZE_ONLY_NORMAL_PUTCHAR(a) { if (! silent && !verbose) { putchar (a); } }
# define ANALYZE_PRINT(text) { if (verbose) { printf (text); } }
# define ANALYZE_PRINTF_1(text, a1) { if (verbose) { printf (text, (a1)); } }
# define ANALYZE_PRINTF_2(text, a1, a2) { if (verbose) { printf (text, (a1), (a2)); } }
# define ANALYZE_PRINTF_3(text, a1, a2, a3) { if (verbose) { printf (text, (a1), (a2), (a3)); } }
# define ANALYZE_PRINTF_4(text, a1, a2, a3, a4) { if (verbose) { printf (text, (a1), (a2), (a3), (a4)); } }
# define ANALYZE_ONLY_NORMAL_PRINT(text) { if (! silent && !verbose) { printf (text); } }
# define ANALYZE_ONLY_NORMAL_PRINTF_1(text, a1) { if (! silent && !verbose) { printf (text, (a1)); } }
# define ANALYZE_ONLY_NORMAL_PRINTF_2(text, a1, a2) { if (! silent && !verbose) { printf (text, (a1), (a2)); } }
# define ANALYZE_ONLY_NORMAL_PRINTF_3(text, a1, a2, a3) { if (! silent && !verbose) { printf (text, (a1), (a2), (a3)); } }
# define ANALYZE_ONLY_NORMAL_PRINTF_4(text, a1, a2, a3, a4) { if (! silent && !verbose) { printf (text, (a1), (a2), (a3), (a4)); } }
# define ANALYZE_NEWLINE() { if (verbose) { putchar ('\n'); } }
static int silent;
static int time_counter;
static int verbose;
#else
# define ANALYZE_PUTCHAR(a)
# define ANALYZE_ONLY_NORMAL_PUTCHAR(a)
# define ANALYZE_PRINT(text)
# define ANALYZE_PRINTF_1(text, a1)
# define ANALYZE_PRINTF_2(text, a1, a2)
# define ANALYZE_PRINTF_3(text, a1, a2, a3)
# define ANALYZE_PRINTF_4(text, a1, a2, a3, a4)
# define ANALYZE_ONLY_NORMAL_PRINT(text)
# define ANALYZE_ONLY_NORMAL_PRINTF_1(text, a1)
# define ANALYZE_ONLY_NORMAL_PRINTF_2(text, a1, a2)
# define ANALYZE_ONLY_NORMAL_PRINTF_3(text, a1, a2, a3)
# define ANALYZE_ONLY_NORMAL_PRINTF_4(text, a1, a2, a3, a4)
# define ANALYZE_NEWLINE()
#endif
#if IRMP_USE_CALLBACK == 1
static void (*irmp_callback_ptr) (uint_fast8_t);
#endif // IRMP_USE_CALLBACK == 1
#define PARITY_CHECK_OK 1
#define PARITY_CHECK_FAILED 0
static uint_fast8_t irmp_bit; // current bit position
static IRMP_PARAMETER irmp_param;
#if IRMP_SUPPORT_RC5_PROTOCOL == 1 && (IRMP_SUPPORT_FDC_PROTOCOL == 1 || IRMP_SUPPORT_RCCAR_PROTOCOL == 1)
static IRMP_PARAMETER irmp_param2;
#endif
static volatile uint_fast8_t irmp_ir_detected = FALSE;
static volatile uint_fast8_t irmp_protocol;
static volatile uint_fast16_t irmp_address;
#if IRMP_32_BIT == 1
static volatile uint_fast32_t irmp_command;
#else
static volatile uint_fast16_t irmp_command;
#endif
static volatile uint_fast16_t irmp_id; // only used for SAMSUNG protocol
static volatile uint_fast8_t irmp_flags;
// static volatile uint_fast8_t irmp_busy_flag;
#if defined(__MBED__)
// DigitalIn inputPin(IRMP_PIN, PullUp); // this requires mbed.h and source to be compiled as cpp
gpio_t gpioIRin; // use low level c function instead
#endif
#ifdef ANALYZE
#define input(x) (x)
static uint_fast8_t IRMP_PIN;
static uint_fast8_t radio;
#endif
/*---------------------------------------------------------------------------------------------------------------------------------------------------
* Initialize IRMP decoder
* @details Configures IRMP input pin
*---------------------------------------------------------------------------------------------------------------------------------------------------
*/
#ifndef ANALYZE
void
irmp_init (void)
{
#if defined(PIC_CCS) || defined(PIC_C18) // PIC: do nothing
#elif defined (ARM_STM32_HAL) // STM32 with Hal Library: do nothing
#elif defined (ARM_STM32) // STM32
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOx clock enable */
# if defined (ARM_STM32L1XX)
RCC_AHBPeriphClockCmd(IRMP_PORT_RCC, ENABLE);
# elif defined (ARM_STM32F10X)
RCC_APB2PeriphClockCmd(IRMP_PORT_RCC, ENABLE);
# elif defined (ARM_STM32F30X)
RCC_AHBPeriphClockCmd(IRMP_PORT_RCC, ENABLE);
# elif defined (ARM_STM32F4XX)
RCC_AHB1PeriphClockCmd(IRMP_PORT_RCC, ENABLE);
# endif
/* GPIO Configuration */
GPIO_InitStructure.GPIO_Pin = IRMP_BIT;
# if defined (ARM_STM32L1XX) || defined (ARM_STM32F4XX)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
# elif defined (ARM_STM32F10X)
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
# elif defined (ARM_STM32F30X)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
# endif
GPIO_Init(IRMP_PORT, &GPIO_InitStructure);
#elif defined(STELLARIS_ARM_CORTEX_M4)
// Enable the GPIO port
ROM_SysCtlPeripheralEnable(IRMP_PORT_PERIPH);
// Set as an input
ROM_GPIODirModeSet(IRMP_PORT_BASE, IRMP_PORT_PIN, GPIO_DIR_MODE_IN);
ROM_GPIOPadConfigSet(IRMP_PORT_BASE, IRMP_PORT_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
#elif defined(__SDCC_stm8) // STM8
IRMP_GPIO_STRUCT->DDR &= ~(1<<IRMP_BIT); // pin is input
IRMP_GPIO_STRUCT->CR1 |= (1<<IRMP_BIT); // activate pullup
#elif defined (TEENSY_ARM_CORTEX_M4) // TEENSY
pinMode(IRMP_PIN, INPUT);
#elif defined(__xtensa__) // ESP8266
pinMode(IRMP_BIT_NUMBER, INPUT);
// select pin function
# if (IRMP_BIT_NUMBER == 12)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);
// doesn't work for me:
// # elif (IRMP_BIT_NUMBER == 13)
// PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U , FUNC_GPIO13);
# else
# warning Please add PIN_FUNC_SELECT when necessary.
# endif
GPIO_DIS_OUTPUT(IRMP_BIT_NUMBER);
#elif defined(__MBED__)
gpio_init_in_ex(&gpioIRin, IRMP_PIN, IRMP_PINMODE); // initialize input for IR diode
#elif defined(_CHIBIOS_HAL_)
// ChibiOS HAL automatically initializes all pins according to the board config file, no need to repeat here
#else // AVR
IRMP_PORT &= ~(1<<IRMP_BIT); // deactivate pullup
IRMP_DDR &= ~(1<<IRMP_BIT); // set pin to input
#endif
#if IRMP_LOGGING == 1
irmp_uart_init ();
#endif
}
#endif
/*---------------------------------------------------------------------------------------------------------------------------------------------------
* Get IRMP data
* @details gets decoded IRMP data
* @param pointer in order to store IRMP data
* @return TRUE: successful, FALSE: failed
*---------------------------------------------------------------------------------------------------------------------------------------------------
*/
uint_fast8_t
irmp_get_data (IRMP_DATA * irmp_data_p)
{
uint_fast8_t rtc = FALSE;
#if IRMP_SUPPORT_MERLIN_PROTOCOL == 1
uint_fast8_t cmd_len = 0;
#endif
if (irmp_ir_detected)
{
switch (irmp_protocol)
{
#if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
case IRMP_SAMSUNG_PROTOCOL:
if ((irmp_command >> 8) == (~irmp_command & 0x00FF))
{
irmp_command &= 0xff;
irmp_command |= irmp_id << 8;
rtc = TRUE;
}
break;
#if IRMP_SUPPORT_SAMSUNG48_PROTOCOL == 1
case IRMP_SAMSUNG48_PROTOCOL:
irmp_command = (irmp_command & 0x00FF) | ((irmp_id & 0x00FF) << 8);
rtc = TRUE;
break;
#endif
#endif
#if IRMP_SUPPORT_NEC_PROTOCOL == 1
case IRMP_NEC_PROTOCOL:
if ((irmp_command >> 8) == (~irmp_command & 0x00FF))
{
irmp_command &= 0xff;
rtc = TRUE;
}
else if (irmp_address == 0x87EE)
{
ANALYZE_PRINT ("Switching to APPLE protocol\n");
irmp_protocol = IRMP_APPLE_PROTOCOL;
irmp_address = (irmp_command & 0xFF00) >> 8;
irmp_command &= 0x00FF;
rtc = TRUE;
}
else
{
ANALYZE_PRINT ("Switching to ONKYO protocol\n");
irmp_protocol = IRMP_ONKYO_PROTOCOL;
rtc = TRUE;
}
break;
#endif
#if IRMP_SUPPORT_NEC_PROTOCOL == 1
case IRMP_VINCENT_PROTOCOL:
if ((irmp_command >> 8) == (irmp_command & 0x00FF))
{
irmp_command &= 0xff;
rtc = TRUE;
}
break;
#endif
#if IRMP_SUPPORT_BOSE_PROTOCOL == 1
case IRMP_BOSE_PROTOCOL:
if ((irmp_command >> 8) == (~irmp_command & 0x00FF))
{
irmp_command &= 0xff;
rtc = TRUE;
}
break;
#endif
#if IRMP_SUPPORT_MERLIN_PROTOCOL == 1
case IRMP_MERLIN_PROTOCOL:
if (irmp_bit == 10)
{
rtc = TRUE;
}
else if (irmp_bit >= 19 && ((irmp_bit - 3) % 8 == 0))
{
if (((irmp_command >> 1) & 1) != (irmp_command & 1))
{
irmp_command >>= 1;
irmp_command |= ((irmp_address & 1) << (irmp_bit - 12));
irmp_address >>= 1;
cmd_len = (irmp_bit - 11) >> 3;
rtc = TRUE;
}
}
break;
#endif
#if IRMP_SUPPORT_SIEMENS_OR_RUWIDO_PROTOCOL == 1
case IRMP_SIEMENS_PROTOCOL:
case IRMP_RUWIDO_PROTOCOL:
if (((irmp_command >> 1) & 0x0001) == (~irmp_command & 0x0001))
{
irmp_command >>= 1;
rtc = TRUE;
}
break;
#endif
#if IRMP_SUPPORT_KATHREIN_PROTOCOL == 1
case IRMP_KATHREIN_PROTOCOL:
if (irmp_command != 0x0000)
{
rtc = TRUE;
}
break;
#endif
#if IRMP_SUPPORT_RC5_PROTOCOL == 1
case IRMP_RC5_PROTOCOL:
irmp_address &= ~0x20; // clear toggle bit
rtc = TRUE;
break;
#endif
#if IRMP_SUPPORT_S100_PROTOCOL == 1
case IRMP_S100_PROTOCOL:
irmp_address &= ~0x20; // clear toggle bit
rtc = TRUE;
break;
#endif
#if IRMP_SUPPORT_IR60_PROTOCOL == 1
case IRMP_IR60_PROTOCOL:
if (irmp_command != 0x007d) // 0x007d (== 62<<1 + 1) is start instruction frame
{
rtc = TRUE;
}
else
{
ANALYZE_PRINT("Info IR60: got start instruction frame\n");
}
break;
#endif
#if IRMP_SUPPORT_RCCAR_PROTOCOL == 1
case IRMP_RCCAR_PROTOCOL:
// frame in irmp_data:
// Bit 12 11 10 9 8 7 6 5 4 3 2 1 0
// V D7 D6 D5 D4 D3 D2 D1 D0 A1 A0 C1 C0 // 10 9 8 7 6 5 4 3 2 1 0
irmp_address = (irmp_command & 0x000C) >> 2; // addr: 0 0 0 0 0 0 0 0 0 A1 A0
irmp_command = ((irmp_command & 0x1000) >> 2) | // V-Bit: V 0 0 0 0 0 0 0 0 0 0
((irmp_command & 0x0003) << 8) | // C-Bits: 0 C1 C0 0 0 0 0 0 0 0 0
((irmp_command & 0x0FF0) >> 4); // D-Bits: D7 D6 D5 D4 D3 D2 D1 D0
rtc = TRUE; // Summe: V C1 C0 D7 D6 D5 D4 D3 D2 D1 D0
break;
#endif
#if IRMP_SUPPORT_NETBOX_PROTOCOL == 1 // squeeze code to 8 bit, upper bit indicates release-key
case IRMP_NETBOX_PROTOCOL:
if (irmp_command & 0x1000) // last bit set?
{
if ((irmp_command & 0x1f) == 0x15) // key pressed: 101 01 (LSB)
{
irmp_command >>= 5;
irmp_command &= 0x7F;
rtc = TRUE;
}
else if ((irmp_command & 0x1f) == 0x10) // key released: 000 01 (LSB)
{
irmp_command >>= 5;
irmp_command |= 0x80;
rtc = TRUE;
}
else
{
ANALYZE_PRINT("error NETBOX: bit6/7 must be 0/1\n");
}
}
else
{
ANALYZE_PRINT("error NETBOX: last bit not set\n");
}
break;
#endif
#if IRMP_SUPPORT_LEGO_PROTOCOL == 1
case IRMP_LEGO_PROTOCOL:
{
uint_fast8_t crc = 0x0F ^ ((irmp_command & 0xF000) >> 12) ^ ((irmp_command & 0x0F00) >> 8) ^ ((irmp_command & 0x00F0) >> 4);
if ((irmp_command & 0x000F) == crc)
{
irmp_command >>= 4;
rtc = TRUE;
}
else
{
ANALYZE_PRINT ("CRC error in LEGO protocol\n");
// rtc = TRUE; // don't accept codes with CRC errors
}
break;
}
#endif
#if IRMP_SUPPORT_METZ_PROTOCOL == 1
case IRMP_METZ_PROTOCOL:
irmp_address &= ~0x40; // clear toggle bit
if (((~irmp_address) & 0x07) == (irmp_address >> 3) && ((~irmp_command) & 0x3f) == (irmp_command >> 6))
{
irmp_address >>= 3;
irmp_command >>= 6;
rtc = TRUE;
}
break;
#endif
default:
{
rtc = TRUE;
break;
}
}
if (rtc)
{
irmp_data_p->protocol = irmp_protocol;
irmp_data_p->address = irmp_address;
irmp_data_p->command = irmp_command;
irmp_data_p->flags = irmp_flags;
#if IRMP_SUPPORT_MERLIN_PROTOCOL == 1
irmp_data_p->flags |= cmd_len;
#endif
}
else
{
irmp_protocol = IRMP_UNKNOWN_PROTOCOL;
}
irmp_command = 0; // don't reset irmp_protocol here, needed for detection of NEC & JVC repetition frames!
irmp_address = 0;
irmp_flags = 0;
irmp_ir_detected = FALSE;
}
return rtc;
}
#if IRMP_USE_CALLBACK == 1
void
irmp_set_callback_ptr (void (*cb)(uint_fast8_t))
{
irmp_callback_ptr = cb;
}
#endif // IRMP_USE_CALLBACK == 1
// these statics must not be volatile, because they are only used by irmp_store_bit(), which is called by irmp_ISR()
static uint_fast16_t irmp_tmp_address; // ir address
#if IRMP_32_BIT == 1
static uint_fast32_t irmp_tmp_command; // ir command
#else
static uint_fast16_t irmp_tmp_command; // ir command
#endif
#if (IRMP_SUPPORT_RC5_PROTOCOL == 1 && (IRMP_SUPPORT_FDC_PROTOCOL == 1 || IRMP_SUPPORT_RCCAR_PROTOCOL == 1)) || IRMP_SUPPORT_NEC42_PROTOCOL == 1
static uint_fast16_t irmp_tmp_address2; // ir address
static uint_fast16_t irmp_tmp_command2; // ir command
#endif
#if IRMP_SUPPORT_LGAIR_PROTOCOL == 1
static uint_fast16_t irmp_lgair_address; // ir address
static uint_fast16_t irmp_lgair_command; // ir command
#endif
#if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
static uint_fast16_t irmp_tmp_id; // ir id (only SAMSUNG)
#endif
#if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1
static uint8_t xor_check[6]; // check kaseikyo "parity" bits
static uint_fast8_t genre2; // save genre2 bits here, later copied to MSB in flags
#endif
#if IRMP_SUPPORT_ORTEK_PROTOCOL == 1
static uint_fast8_t parity; // number of '1' of the first 14 bits, check if even.
#endif
#if IRMP_SUPPORT_MITSU_HEAVY_PROTOCOL == 1
static uint_fast8_t check; // number of '1' of the first 14 bits, check if even.
static uint_fast8_t mitsu_parity; // number of '1' of the first 14 bits, check if even.
#endif
/*---------------------------------------------------------------------------------------------------------------------------------------------------
* store bit
* @details store bit in temp address or temp command
* @param value to store: 0 or 1
*---------------------------------------------------------------------------------------------------------------------------------------------------
*/
// verhindert, dass irmp_store_bit() inline compiliert wird:
// static void irmp_store_bit (uint_fast8_t) __attribute__ ((noinline));
static void
irmp_store_bit (uint_fast8_t value)
{
#if IRMP_SUPPORT_ACP24_PROTOCOL == 1
if (irmp_param.protocol == IRMP_ACP24_PROTOCOL) // squeeze 64 bits into 16 bits:
{
if (value)
{
// ACP24-Frame:
// 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890123456789
// N VVMMM ? ??? t vmA x y TTTT
//
// irmp_data_p->command:
//
// 5432109876543210
// NAVVvMMMmtxyTTTT
switch (irmp_bit)
{
case 0: irmp_tmp_command |= (1<<15); break; // N
case 2: irmp_tmp_command |= (1<<13); break; // V
case 3: irmp_tmp_command |= (1<<12); break; // V
case 4: irmp_tmp_command |= (1<<10); break; // M
case 5: irmp_tmp_command |= (1<< 9); break; // M
case 6: irmp_tmp_command |= (1<< 8); break; // M
case 20: irmp_tmp_command |= (1<< 6); break; // t
case 22: irmp_tmp_command |= (1<<11); break; // v
case 23: irmp_tmp_command |= (1<< 7); break; // m
case 24: irmp_tmp_command |= (1<<14); break; // A
case 26: irmp_tmp_command |= (1<< 5); break; // x
case 44: irmp_tmp_command |= (1<< 4); break; // y
case 66: irmp_tmp_command |= (1<< 3); break; // T
case 67: irmp_tmp_command |= (1<< 2); break; // T
case 68: irmp_tmp_command |= (1<< 1); break; // T
case 69: irmp_tmp_command |= (1<< 0); break; // T
}
}
}
else
#endif // IRMP_SUPPORT_ACP24_PROTOCOL
#if IRMP_SUPPORT_ORTEK_PROTOCOL == 1
if (irmp_param.protocol == IRMP_ORTEK_PROTOCOL)
{
if (irmp_bit < 14)
{
if (value)
{
parity++;
}
}
else if (irmp_bit == 14)
{
if (value) // value == 1: even parity
{
if (parity & 0x01)
{
parity = PARITY_CHECK_FAILED;
}
else
{
parity = PARITY_CHECK_OK;
}
}
else
{
if (parity & 0x01) // value == 0: odd parity
{
parity = PARITY_CHECK_OK;
}
else
{
parity = PARITY_CHECK_FAILED;
}
}
}
}
else
#endif
{
;
}
#if IRMP_SUPPORT_GRUNDIG_NOKIA_IR60_PROTOCOL == 1
if (irmp_bit == 0 && irmp_param.protocol == IRMP_GRUNDIG_PROTOCOL)
{
first_bit = value;
}
else
#endif
if (irmp_bit >= irmp_param.address_offset && irmp_bit < irmp_param.address_end)
{
if (irmp_param.lsb_first)
{
irmp_tmp_address |= (((uint_fast16_t) (value)) << (irmp_bit - irmp_param.address_offset)); // CV wants cast
}
else
{
irmp_tmp_address <<= 1;
irmp_tmp_address |= value;
}
}
else if (irmp_bit >= irmp_param.command_offset && irmp_bit < irmp_param.command_end)
{
if (irmp_param.lsb_first)
{
#if IRMP_SUPPORT_SAMSUNG48_PROTOCOL == 1
if (irmp_param.protocol == IRMP_SAMSUNG48_PROTOCOL && irmp_bit >= 32)
{
irmp_tmp_id |= (((uint_fast16_t) (value)) << (irmp_bit - 32)); // CV wants cast
}
else
#endif
{
irmp_tmp_command |= (((uint_fast16_t) (value)) << (irmp_bit - irmp_param.command_offset)); // CV wants cast
}
}
else
{
irmp_tmp_command <<= 1;
irmp_tmp_command |= value;
}
}
#if IRMP_SUPPORT_LGAIR_PROTOCOL == 1
if (irmp_param.protocol == IRMP_NEC_PROTOCOL || irmp_param.protocol == IRMP_NEC42_PROTOCOL)
{
if (irmp_bit < 8)
{
irmp_lgair_address <<= 1; // LGAIR uses MSB
irmp_lgair_address |= value;
}
else if (irmp_bit < 24)
{
irmp_lgair_command <<= 1; // LGAIR uses MSB
irmp_lgair_command |= value;
}
}
// NO else!
#endif
#if IRMP_SUPPORT_NEC42_PROTOCOL == 1
if (irmp_param.protocol == IRMP_NEC42_PROTOCOL && irmp_bit >= 13 && irmp_bit < 26)
{
irmp_tmp_address2 |= (((uint_fast16_t) (value)) << (irmp_bit - 13)); // CV wants cast
}
else
#endif
#if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
if (irmp_param.protocol == IRMP_SAMSUNG_PROTOCOL && irmp_bit >= SAMSUNG_ID_OFFSET && irmp_bit < SAMSUNG_ID_OFFSET + SAMSUNG_ID_LEN)
{
irmp_tmp_id |= (((uint_fast16_t) (value)) << (irmp_bit - SAMSUNG_ID_OFFSET)); // store with LSB first
}
else
#endif
#if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1
if (irmp_param.protocol == IRMP_KASEIKYO_PROTOCOL)
{
if (irmp_bit >= 20 && irmp_bit < 24)
{
irmp_tmp_command |= (((uint_fast16_t) (value)) << (irmp_bit - 8)); // store 4 system bits (genre 1) in upper nibble with LSB first
}
else if (irmp_bit >= 24 && irmp_bit < 28)
{
genre2 |= (((uint_fast8_t) (value)) << (irmp_bit - 20)); // store 4 system bits (genre 2) in upper nibble with LSB first
}
if (irmp_bit < KASEIKYO_COMPLETE_DATA_LEN)
{
if (value)
{
xor_check[irmp_bit / 8] |= 1 << (irmp_bit % 8);
}
else
{
xor_check[irmp_bit / 8] &= ~(1 << (irmp_bit % 8));
}
}
}
else
#endif
#if IRMP_SUPPORT_MITSU_HEAVY_PROTOCOL == 1
if (irmp_param.protocol == IRMP_MITSU_HEAVY_PROTOCOL) // squeeze 64 bits into 16 bits:
{
if (irmp_bit == 72 )
{ // irmp_tmp_address, irmp_tmp_command received: check parity & compress
mitsu_parity = PARITY_CHECK_OK;
check = irmp_tmp_address >> 8; // inverted upper byte == lower byte?
check = ~ check;
if (check == (irmp_tmp_address & 0xFF))
{ // ok:
irmp_tmp_address <<= 8; // throw away upper byte
}
else
{
mitsu_parity = PARITY_CHECK_FAILED;
}
check = irmp_tmp_command >> 8; // inverted upper byte == lower byte?
check = ~ check;
if (check == (irmp_tmp_command & 0xFF))
{ // ok: pack together
irmp_tmp_address |= irmp_tmp_command & 0xFF; // byte 1, byte2 in irmp_tmp_address, irmp_tmp_command can be used for byte 3
}
else
{
mitsu_parity = PARITY_CHECK_FAILED;
}
irmp_tmp_command = 0;
}
if (irmp_bit >= 72 )
{ // receive 3. word in irmp_tmp_command
irmp_tmp_command <<= 1;
irmp_tmp_command |= value;
}
}
else
#endif // IRMP_SUPPORT_MITSU_HEAVY_PROTOCOL
{
;
}
irmp_bit++;
}
/*---------------------------------------------------------------------------------------------------------------------------------------------------
* store bit
* @details store bit in temp address or temp command
* @param value to store: 0 or 1
*---------------------------------------------------------------------------------------------------------------------------------------------------
*/
#if IRMP_SUPPORT_RC5_PROTOCOL == 1 && (IRMP_SUPPORT_FDC_PROTOCOL == 1 || IRMP_SUPPORT_RCCAR_PROTOCOL == 1)
static void
irmp_store_bit2 (uint_fast8_t value)
{
uint_fast8_t irmp_bit2;
if (irmp_param.protocol)
{
irmp_bit2 = irmp_bit - 2;
}
else
{
irmp_bit2 = irmp_bit - 1;
}
if (irmp_bit2 >= irmp_param2.address_offset && irmp_bit2 < irmp_param2.address_end)
{
irmp_tmp_address2 |= (((uint_fast16_t) (value)) << (irmp_bit2 - irmp_param2.address_offset)); // CV wants cast
}
else if (irmp_bit2 >= irmp_param2.command_offset && irmp_bit2 < irmp_param2.command_end)
{
irmp_tmp_command2 |= (((uint_fast16_t) (value)) << (irmp_bit2 - irmp_param2.command_offset)); // CV wants cast
}
}
#endif // IRMP_SUPPORT_RC5_PROTOCOL == 1 && (IRMP_SUPPORT_FDC_PROTOCOL == 1 || IRMP_SUPPORT_RCCAR_PROTOCOL == 1)
#ifdef ANALYZE
static uint32_t s_curSample = 0;
static uint32_t s_startBitSample = 0;
#endif
/*---------------------------------------------------------------------------------------------------------------------------------------------------
* ISR routine
* @details ISR routine, called 10000 times per second
*---------------------------------------------------------------------------------------------------------------------------------------------------
*/
uint_fast8_t
irmp_ISR (void)
{
static uint_fast8_t irmp_start_bit_detected; // flag: start bit detected
static uint_fast8_t wait_for_space; // flag: wait for data bit space
static uint_fast8_t wait_for_start_space; // flag: wait for start bit space
static uint_fast8_t irmp_pulse_time; // count bit time for pulse
static PAUSE_LEN irmp_pause_time; // count bit time for pause
static uint_fast16_t last_irmp_address = 0xFFFF; // save last irmp address to recognize key repetition
#if IRMP_32_BIT == 1
static uint_fast32_t last_irmp_command = 0xFFFFFFFF; // save last irmp command to recognize key repetition
#else
static uint_fast16_t last_irmp_command = 0xFFFF; // save last irmp command to recognize key repetition
#endif
static uint_fast16_t key_repetition_len; // SIRCS repeats frame 2-5 times with 45 ms pause
static uint_fast8_t repetition_frame_number;
#if IRMP_SUPPORT_DENON_PROTOCOL == 1
static uint_fast16_t last_irmp_denon_command; // save last irmp command to recognize DENON frame repetition
static uint_fast16_t denon_repetition_len = 0xFFFF; // denon repetition len of 2nd auto generated frame
#endif
#if IRMP_SUPPORT_RC5_PROTOCOL == 1 || IRMP_SUPPORT_S100_PROTOCOL == 1
static uint_fast8_t rc5_cmd_bit6; // bit 6 of RC5 command is the inverted 2nd start bit
#endif
#if IRMP_SUPPORT_MANCHESTER == 1
static PAUSE_LEN last_pause; // last pause value
#endif
#if IRMP_SUPPORT_MANCHESTER == 1 || IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL == 1
static uint_fast8_t last_value; // last bit value
#endif
#if IRMP_SUPPORT_RCII_PROTOCOL == 1
static uint_fast8_t waiting_for_2nd_pulse = 0;
#endif
uint_fast8_t irmp_input; // input value
#ifdef ANALYZE
#if 0 // only for test
static uint_fast8_t last_irmp_start_bit_detected = 0xFF;
static uint_fast8_t last_irmp_pulse_time = 0xFF;
if (last_irmp_start_bit_detected != irmp_start_bit_detected || last_irmp_pulse_time != irmp_pulse_time)
{
last_irmp_start_bit_detected = irmp_start_bit_detected;
last_irmp_pulse_time = irmp_pulse_time;
printf ("%d %d %d\n", time_counter, irmp_start_bit_detected, irmp_pulse_time);
}
#endif // 0
time_counter++;
printf("%d: %i - %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n"
, (int)time_counter
, (int)IRMP_PIN
, (int)irmp_start_bit_detected
, (int)wait_for_space
, (int)wait_for_start_space
, (int)irmp_pulse_time
, (int)irmp_pause_time
, (int)last_irmp_address
, (int)last_irmp_command
, (int)key_repetition_len
, (int)repetition_frame_number
, (int)last_irmp_denon_command
, (int)last_pause
, (int)last_value
, (int)denon_repetition_len
, (int)rc5_cmd_bit6
);
#endif // ANALYZE
#if defined(__SDCC_stm8)
irmp_input = input(IRMP_GPIO_STRUCT->IDR)
#elif defined(__MBED__)
//irmp_input = inputPin;
irmp_input = gpio_read (&gpioIRin);
#else
irmp_input = input(IRMP_PIN);
#endif
#if IRMP_USE_CALLBACK == 1
if (irmp_callback_ptr)
{
static uint_fast8_t last_inverted_input;
if (last_inverted_input != !irmp_input)
{
(*irmp_callback_ptr) (! irmp_input);
last_inverted_input = !irmp_input;
}
}
#endif // IRMP_USE_CALLBACK == 1
irmp_log(irmp_input); // log ir signal, if IRMP_LOGGING defined
if (! irmp_ir_detected) // ir code already detected?
{ // no...
if (! irmp_start_bit_detected) // start bit detected?
{ // no...
if (! irmp_input) // receiving burst?
{ // yes...
// irmp_busy_flag = TRUE;
#ifdef ANALYZE
if (! irmp_pulse_time)
{
s_startBitSample = s_curSample;
ANALYZE_PRINTF_1("%8.3fms [starting pulse]\n", (float) (time_counter * 1000) / F_INTERRUPTS);
}
#endif // ANALYZE
irmp_pulse_time++; // increment counter
}
else
{ // no...
if (irmp_pulse_time) // it's dark....
{ // set flags for counting the time of darkness...
irmp_start_bit_detected = 1;
wait_for_start_space = 1;
wait_for_space = 0;
irmp_tmp_command = 0;
irmp_tmp_address = 0;
#if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1
genre2 = 0;
#endif
#if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
irmp_tmp_id = 0;
#endif
#if IRMP_SUPPORT_RC5_PROTOCOL == 1 && (IRMP_SUPPORT_FDC_PROTOCOL == 1 || IRMP_SUPPORT_RCCAR_PROTOCOL == 1) || IRMP_SUPPORT_NEC42_PROTOCOL == 1
irmp_tmp_command2 = 0;
irmp_tmp_address2 = 0;
#endif
#if IRMP_SUPPORT_LGAIR_PROTOCOL == 1
irmp_lgair_command = 0;
irmp_lgair_address = 0;
#endif
irmp_bit = 0xff;
irmp_pause_time = 1; // 1st pause: set to 1, not to 0!
#if IRMP_SUPPORT_RC5_PROTOCOL == 1 || IRMP_SUPPORT_S100_PROTOCOL == 1
rc5_cmd_bit6 = 0; // fm 2010-03-07: bugfix: reset it after incomplete RC5 frame!
#endif
}
else
{
if (key_repetition_len < 0xFFFF) // avoid overflow of counter
{
key_repetition_len++;
#if IRMP_SUPPORT_DENON_PROTOCOL == 1
if (denon_repetition_len < 0xFFFF) // avoid overflow of counter
{
denon_repetition_len++;
if (denon_repetition_len >= DENON_AUTO_REPETITION_PAUSE_LEN && last_irmp_denon_command != 0)
{
ANALYZE_PRINTF_1 ("%8.3fms warning: did not receive inverted command repetition\n",
(float) (time_counter * 1000) / F_INTERRUPTS);
last_irmp_denon_command = 0;
denon_repetition_len = 0xFFFF;
}
}
#endif // IRMP_SUPPORT_DENON_PROTOCOL == 1
}
}
}
}
else
{
if (wait_for_start_space) // we have received start bit...
{ // ...and are counting the time of darkness
if (irmp_input) // still dark?
{ // yes
irmp_pause_time++; // increment counter
#if IRMP_SUPPORT_NIKON_PROTOCOL == 1
if (((irmp_pulse_time < NIKON_START_BIT_PULSE_LEN_MIN || irmp_pulse_time > NIKON_START_BIT_PULSE_LEN_MAX) && irmp_pause_time > IRMP_TIMEOUT_LEN) ||
irmp_pause_time > IRMP_TIMEOUT_NIKON_LEN)
#else
if (irmp_pause_time > IRMP_TIMEOUT_LEN) // timeout?
#endif
{ // yes...
#if IRMP_SUPPORT_JVC_PROTOCOL == 1
if (irmp_protocol == IRMP_JVC_PROTOCOL) // don't show eror if JVC protocol, irmp_pulse_time has been set below!
{
;
}
else
#endif // IRMP_SUPPORT_JVC_PROTOCOL == 1
{
ANALYZE_PRINTF_3 ("%8.3fms error 1: pause after start bit pulse %d too long: %d\n", (float) (time_counter * 1000) / F_INTERRUPTS, irmp_pulse_time, irmp_pause_time);
ANALYZE_ONLY_NORMAL_PUTCHAR ('\n');
}
irmp_start_bit_detected = 0; // reset flags, let's wait for another start bit
irmp_pulse_time = 0;
irmp_pause_time = 0;
}
}
else
{ // receiving first data pulse!
IRMP_PARAMETER * irmp_param_p;
irmp_param_p = (IRMP_PARAMETER *) 0;
#if IRMP_SUPPORT_RC5_PROTOCOL == 1 && (IRMP_SUPPORT_FDC_PROTOCOL == 1 || IRMP_SUPPORT_RCCAR_PROTOCOL == 1)
irmp_param2.protocol = 0;
#endif
ANALYZE_PRINTF_3 ("%8.3fms [start-bit: pulse = %2d, pause = %2d]\n", (float) (time_counter * 1000) / F_INTERRUPTS, irmp_pulse_time, irmp_pause_time);
#if IRMP_SUPPORT_SIRCS_PROTOCOL == 1
if (irmp_pulse_time >= SIRCS_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= SIRCS_START_BIT_PULSE_LEN_MAX &&
irmp_pause_time >= SIRCS_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= SIRCS_START_BIT_PAUSE_LEN_MAX)
{ // it's SIRCS
ANALYZE_PRINTF_4 ("protocol = SIRCS, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
SIRCS_START_BIT_PULSE_LEN_MIN, SIRCS_START_BIT_PULSE_LEN_MAX,
SIRCS_START_BIT_PAUSE_LEN_MIN, SIRCS_START_BIT_PAUSE_LEN_MAX);
irmp_param_p = (IRMP_PARAMETER *) &sircs_param;
}
else
#endif // IRMP_SUPPORT_SIRCS_PROTOCOL == 1
#if IRMP_SUPPORT_JVC_PROTOCOL == 1
if (irmp_protocol == IRMP_JVC_PROTOCOL && // last protocol was JVC, awaiting repeat frame
irmp_pulse_time >= JVC_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= JVC_START_BIT_PULSE_LEN_MAX &&
irmp_pause_time >= JVC_REPEAT_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= JVC_REPEAT_START_BIT_PAUSE_LEN_MAX)
{
ANALYZE_PRINTF_4 ("protocol = NEC or JVC (type 1) repeat frame, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
JVC_START_BIT_PULSE_LEN_MIN, JVC_START_BIT_PULSE_LEN_MAX,
JVC_REPEAT_START_BIT_PAUSE_LEN_MIN, JVC_REPEAT_START_BIT_PAUSE_LEN_MAX);
irmp_param_p = (IRMP_PARAMETER *) &nec_param;
}
else
#endif // IRMP_SUPPORT_JVC_PROTOCOL == 1
#if IRMP_SUPPORT_NEC_PROTOCOL == 1
if (irmp_pulse_time >= NEC_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= NEC_START_BIT_PULSE_LEN_MAX &&