]> cloudbase.mooo.com Git - irmp.git/blobdiff - irmp.c
Version 1.7.2: added timeout for NEC repetition frames
[irmp.git] / irmp.c
diff --git a/irmp.c b/irmp.c
index 99439d6029a49ea20ca6fdea91df16ba61b80c1c..861836c4b1298e178d315ded08337679fa400a6c 100644 (file)
--- a/irmp.c
+++ b/irmp.c
@@ -3,7 +3,7 @@
  *\r
  * Copyright (c) 2009-2010 Frank Meyer - frank(at)fli4l.de\r
  *\r
- * $Id: irmp.c,v 1.73 2010/06/26 18:13:11 fm Exp $\r
+ * $Id: irmp.c,v 1.75 2010/07/01 09:00:16 fm Exp $\r
  *\r
  * ATMEGA88 @ 8 MHz\r
  *\r
@@ -352,7 +352,7 @@ typedef uint8_t     PAUSE_LEN;
 #endif\r
 \r
 #define IRMP_TIMEOUT_LEN                        (PAUSE_LEN)(F_INTERRUPTS * IRMP_TIMEOUT_TIME + 0.5)\r
-#define IRMP_KEY_REPETITION_LEN                 (uint16_t)(F_INTERRUPTS * 150.0e-3 + 0.5)  // autodetect key repetition within 150 msec\r
+#define IRMP_KEY_REPETITION_LEN                 (uint16_t)(F_INTERRUPTS * 150.0e-3 + 0.5)           // autodetect key repetition within 150 msec\r
 \r
 #define MIN_TOLERANCE_00                        1.0                           // -0%\r
 #define MAX_TOLERANCE_00                        1.0                           // +0%\r
@@ -404,6 +404,13 @@ typedef uint8_t     PAUSE_LEN;
 #define NEC_1_PAUSE_LEN_MAX                     ((uint8_t)(F_INTERRUPTS * NEC_1_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)\r
 #define NEC_0_PAUSE_LEN_MIN                     ((uint8_t)(F_INTERRUPTS * NEC_0_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)\r
 #define NEC_0_PAUSE_LEN_MAX                     ((uint8_t)(F_INTERRUPTS * NEC_0_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)\r
+// autodetect nec repetition frame within 50 msec:\r
+// NEC seems to send the first repetition frame after 40ms, further repetition frames after 100 ms\r
+#if 0\r
+#define NEC_FRAME_REPEAT_PAUSE_LEN_MAX          (uint16_t)(F_INTERRUPTS * NEC_FRAME_REPEAT_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5)\r
+#else\r
+#define NEC_FRAME_REPEAT_PAUSE_LEN_MAX          (uint16_t)(F_INTERRUPTS * 100.0e-3 * MAX_TOLERANCE_20 + 0.5)\r
+#endif\r
 \r
 #define SAMSUNG_START_BIT_PULSE_LEN_MIN         ((uint8_t)(F_INTERRUPTS * SAMSUNG_START_BIT_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)\r
 #define SAMSUNG_START_BIT_PULSE_LEN_MAX         ((uint8_t)(F_INTERRUPTS * SAMSUNG_START_BIT_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)\r
@@ -630,9 +637,9 @@ irmp_uart_init (void)
     UART0_UBRRL = UBRRL_VALUE;\r
 \r
 #if USE_2X\r
-    UART0_UCSRA = (1<<U2X);\r
+    UART0_UCSRA |= (1<<U2X);\r
 #else\r
-    UART0_UCSRA = 0;\r
+    UART0_UCSRA &= ~(1<<U2X);\r
 #endif\r
 \r
     UART0_UCSRC = UART0_UCSZ1_BIT_VALUE | UART0_UCSZ0_BIT_VALUE | UART0_URSEL_BIT_VALUE;\r
@@ -1460,7 +1467,10 @@ irmp_ISR (void)
                 }\r
                 else\r
                 {\r
-                    repetition_counter++;\r
+                    if (repetition_counter < 0xFFFF)                            // avoid overflow of counter\r
+                    {\r
+                        repetition_counter++;\r
+                    }\r
                 }\r
             }\r
         }\r
@@ -2391,9 +2401,20 @@ irmp_ISR (void)
 #if IRMP_SUPPORT_NEC_PROTOCOL == 1\r
                         if (irmp_param.protocol == IRMP_NEC_PROTOCOL && irmp_bit == 0)  // repetition frame\r
                         {\r
-                            irmp_tmp_address = last_irmp_address;                   // address is last address\r
-                            irmp_tmp_command = last_irmp_command;                   // command is last command\r
-                            irmp_flags |= IRMP_FLAG_REPETITION;\r
+                            if (repetition_counter < NEC_FRAME_REPEAT_PAUSE_LEN_MAX)\r
+                            {\r
+                                ANALYZE_PRINTF ("Detected NEC repetition frame, repetition_counter = %d\n", repetition_counter);\r
+                                irmp_tmp_address = last_irmp_address;                   // address is last address\r
+                                irmp_tmp_command = last_irmp_command;                   // command is last command\r
+                                irmp_flags |= IRMP_FLAG_REPETITION;\r
+                                repetition_counter = 0;\r
+                            }\r
+                            else\r
+                            {\r
+                                ANALYZE_PRINTF ("Detected NEC repetition frame, ignoring it: timeout occured, repetition_counter = %d > %d\n",\r
+                                                repetition_counter, NEC_FRAME_REPEAT_PAUSE_LEN_MAX);\r
+                                irmp_ir_detected = FALSE;\r
+                            }\r
                         }\r
 #endif // IRMP_SUPPORT_NEC_PROTOCOL\r
                         irmp_protocol = irmp_param.protocol;\r