]> cloudbase.mooo.com Git - irmp.git/blobdiff - irmp.c
Version 1.7.3: better support of Kaseikyo and Apple remote controls
[irmp.git] / irmp.c
diff --git a/irmp.c b/irmp.c
index 7f478f076d7145fa58e1251db702b4c73a3c0612..4077f54fd83e7c6a047c895b714fe9872c02e045 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.64 2010/06/25 09:04:43 fm Exp $\r
+ * $Id: irmp.c,v 1.79 2010/08/28 22:14:56 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
@@ -432,9 +439,9 @@ typedef uint8_t     PAUSE_LEN;
 #define KASEIKYO_START_BIT_PAUSE_LEN_MIN        ((uint8_t)(F_INTERRUPTS * KASEIKYO_START_BIT_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)\r
 #define KASEIKYO_START_BIT_PAUSE_LEN_MAX        ((uint8_t)(F_INTERRUPTS * KASEIKYO_START_BIT_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)\r
 #define KASEIKYO_PULSE_LEN_MIN                  ((uint8_t)(F_INTERRUPTS * KASEIKYO_PULSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)\r
-#define KASEIKYO_PULSE_LEN_MAX                  ((uint8_t)(F_INTERRUPTS * KASEIKYO_PULSE_TIME * MAX_TOLERANCE_60 + 0.5) + 1)\r
-#define KASEIKYO_1_PAUSE_LEN_MIN                ((uint8_t)(F_INTERRUPTS * KASEIKYO_1_PAUSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)\r
-#define KASEIKYO_1_PAUSE_LEN_MAX                ((uint8_t)(F_INTERRUPTS * KASEIKYO_1_PAUSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)\r
+#define KASEIKYO_PULSE_LEN_MAX                  ((uint8_t)(F_INTERRUPTS * KASEIKYO_PULSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)\r
+#define KASEIKYO_1_PAUSE_LEN_MIN                ((uint8_t)(F_INTERRUPTS * KASEIKYO_1_PAUSE_TIME * MIN_TOLERANCE_30 + 0.5) - 1)\r
+#define KASEIKYO_1_PAUSE_LEN_MAX                ((uint8_t)(F_INTERRUPTS * KASEIKYO_1_PAUSE_TIME * MAX_TOLERANCE_30 + 0.5) + 1)\r
 #define KASEIKYO_0_PAUSE_LEN_MIN                ((uint8_t)(F_INTERRUPTS * KASEIKYO_0_PAUSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)\r
 #define KASEIKYO_0_PAUSE_LEN_MAX                ((uint8_t)(F_INTERRUPTS * KASEIKYO_0_PAUSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)\r
 \r
@@ -559,6 +566,19 @@ typedef uint8_t     PAUSE_LEN;
 #define RCCAR_0_PAUSE_LEN_MIN                   ((uint8_t)(F_INTERRUPTS * RCCAR_0_PAUSE_TIME * MIN_TOLERANCE_30 + 0.5) - 1)\r
 #define RCCAR_0_PAUSE_LEN_MAX                   ((uint8_t)(F_INTERRUPTS * RCCAR_0_PAUSE_TIME * MAX_TOLERANCE_30 + 0.5) + 1)\r
 \r
+#define JVC_START_BIT_PULSE_LEN_MIN             ((uint8_t)(F_INTERRUPTS * JVC_START_BIT_PULSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)\r
+#define JVC_START_BIT_PULSE_LEN_MAX             ((uint8_t)(F_INTERRUPTS * JVC_START_BIT_PULSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)\r
+#define JVC_REPEAT_START_BIT_PAUSE_LEN_MIN      ((uint8_t)(F_INTERRUPTS * (JVC_FRAME_REPEAT_PAUSE_TIME - IRMP_TIMEOUT_TIME) * MIN_TOLERANCE_40 + 0.5) - 1)  // HACK!\r
+#define JVC_REPEAT_START_BIT_PAUSE_LEN_MAX      ((uint8_t)(F_INTERRUPTS * (JVC_FRAME_REPEAT_PAUSE_TIME - IRMP_TIMEOUT_TIME) * MAX_TOLERANCE_40 + 0.5) - 1)  // HACK!\r
+#define JVC_PULSE_LEN_MIN                       ((uint8_t)(F_INTERRUPTS * JVC_PULSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)\r
+#define JVC_PULSE_LEN_MAX                       ((uint8_t)(F_INTERRUPTS * JVC_PULSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)\r
+#define JVC_1_PAUSE_LEN_MIN                     ((uint8_t)(F_INTERRUPTS * JVC_1_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)\r
+#define JVC_1_PAUSE_LEN_MAX                     ((uint8_t)(F_INTERRUPTS * JVC_1_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)\r
+#define JVC_0_PAUSE_LEN_MIN                     ((uint8_t)(F_INTERRUPTS * JVC_0_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)\r
+#define JVC_0_PAUSE_LEN_MAX                     ((uint8_t)(F_INTERRUPTS * JVC_0_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)\r
+// autodetect JVC repetition frame within 50 msec:\r
+#define JVC_FRAME_REPEAT_PAUSE_LEN_MAX          (uint16_t)(F_INTERRUPTS * JVC_FRAME_REPEAT_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5)\r
+\r
 #define AUTO_FRAME_REPETITION_LEN               (uint16_t)(F_INTERRUPTS * AUTO_FRAME_REPETITION_TIME + 0.5)       // use uint16_t!\r
 \r
 #ifdef ANALYZE\r
@@ -630,9 +650,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
@@ -1250,11 +1270,12 @@ irmp_get_data (IRMP_DATA * irmp_data_p)
                     irmp_command &= 0xff;\r
                     rtc = TRUE;\r
                 }\r
-                else if ((irmp_command & 0xFF00) == 0xD100)\r
+                else if (irmp_address == 0x87EE)\r
                 {\r
                     ANALYZE_PRINTF ("Switching to APPLE protocol\n");\r
                     irmp_protocol = IRMP_APPLE_PROTOCOL;\r
-                    irmp_command &= 0xff;\r
+                    irmp_address = (irmp_command & 0xFF00) >> 8;\r
+                    irmp_command &= 0x00FF;\r
                     rtc = TRUE;\r
                 }\r
                 break;\r
@@ -1293,19 +1314,22 @@ irmp_get_data (IRMP_DATA * irmp_data_p)
 }\r
 \r
 // these statics must not be volatile, because they are only used by irmp_store_bit(), which is called by irmp_ISR()\r
-static uint16_t   irmp_tmp_address;                                                         // ir address\r
-static uint16_t   irmp_tmp_command;                                                         // ir command\r
+static uint16_t irmp_tmp_address;                                                       // ir address\r
+static uint16_t irmp_tmp_command;                                                       // ir command\r
 \r
 #if IRMP_SUPPORT_RC5_PROTOCOL == 1 && (IRMP_SUPPORT_FDC_PROTOCOL == 1 || IRMP_SUPPORT_RCCAR_PROTOCOL == 1)\r
-static uint16_t   irmp_tmp_address2;                                                         // ir address\r
-static uint16_t   irmp_tmp_command2;                                                         // ir command\r
+static uint16_t irmp_tmp_address2;                                                      // ir address\r
+static uint16_t irmp_tmp_command2;                                                      // ir command\r
 #endif\r
 \r
 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1\r
-static uint16_t   irmp_tmp_id;                                                              // ir id (only SAMSUNG)\r
+static uint16_t irmp_tmp_id;                                                            // ir id (only SAMSUNG)\r
+#endif\r
+#if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1\r
+static uint8_t  xor_check[6];                                                           // check kaseikyo "parity" bits\r
 #endif\r
 \r
-static uint8_t    irmp_bit;                                                                 // current bit position\r
+static uint8_t  irmp_bit;                                                               // current bit position\r
 \r
 /*---------------------------------------------------------------------------------------------------------------------------------------------------\r
  *  store bit\r
@@ -1319,6 +1343,7 @@ static uint8_t    irmp_bit;
 static void\r
 irmp_store_bit (uint8_t value)\r
 {\r
+\r
     if (irmp_bit >= irmp_param.address_offset && irmp_bit < irmp_param.address_end)\r
     {\r
         if (irmp_param.lsb_first)\r
@@ -1343,12 +1368,34 @@ irmp_store_bit (uint8_t value)
             irmp_tmp_command |= value;\r
         }\r
     }\r
+\r
 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1\r
     else if (irmp_param.protocol == IRMP_SAMSUNG_PROTOCOL && irmp_bit >= SAMSUNG_ID_OFFSET && irmp_bit < SAMSUNG_ID_OFFSET + SAMSUNG_ID_LEN)\r
     {\r
         irmp_tmp_id |= (((uint16_t) (value)) << (irmp_bit - SAMSUNG_ID_OFFSET));                    // store with LSB first\r
     }\r
 #endif\r
+\r
+#if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1\r
+    else if (irmp_param.protocol == IRMP_KASEIKYO_PROTOCOL && irmp_bit >= 20 && irmp_bit < 24)\r
+    {\r
+        irmp_tmp_command |= (((uint16_t) (value)) << (irmp_bit - 8));                   // store 4 system bits in upper nibble with LSB first\r
+    }\r
+\r
+    if (irmp_param.protocol == IRMP_KASEIKYO_PROTOCOL && irmp_bit < KASEIKYO_COMPLETE_DATA_LEN)\r
+    {\r
+        if (value)\r
+        {\r
+            xor_check[irmp_bit / 8] |= 1 << (irmp_bit % 8);\r
+        }\r
+        else\r
+        {\r
+            xor_check[irmp_bit / 8] &= ~(1 << (irmp_bit % 8));\r
+        }\r
+    }\r
+\r
+#endif\r
+\r
     irmp_bit++;\r
 }\r
 \r
@@ -1460,7 +1507,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
@@ -1503,6 +1553,19 @@ irmp_ISR (void)
                     else\r
 #endif // IRMP_SUPPORT_SIRCS_PROTOCOL == 1\r
 \r
+#if IRMP_SUPPORT_JVC_PROTOCOL == 1\r
+                    if (irmp_protocol == IRMP_JVC_PROTOCOL &&                                                       // last protocol was JVC, awaiting repeat frame\r
+                        irmp_pulse_time >= JVC_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= JVC_START_BIT_PULSE_LEN_MAX &&\r
+                        irmp_pause_time >= JVC_REPEAT_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= JVC_REPEAT_START_BIT_PAUSE_LEN_MAX)\r
+                    {\r
+                        ANALYZE_PRINTF ("protocol = NEC or JVC repeat frame, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",\r
+                                        JVC_START_BIT_PULSE_LEN_MIN, JVC_START_BIT_PULSE_LEN_MAX,\r
+                                        JVC_REPEAT_START_BIT_PAUSE_LEN_MIN, JVC_REPEAT_START_BIT_PAUSE_LEN_MAX);\r
+                        irmp_param_p = (IRMP_PARAMETER *) &nec_param;                                               // tricky: use nec parameters\r
+                    }\r
+                    else\r
+#endif // IRMP_SUPPORT_JVC_PROTOCOL == 1\r
+\r
 #if IRMP_SUPPORT_NEC_PROTOCOL == 1\r
                     if (irmp_pulse_time >= NEC_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= NEC_START_BIT_PULSE_LEN_MAX &&\r
                         irmp_pause_time >= NEC_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= NEC_START_BIT_PAUSE_LEN_MAX)\r
@@ -1957,6 +2020,18 @@ irmp_ISR (void)
                             {\r
                                 irmp_bit++;\r
                             }\r
+#if IRMP_SUPPORT_JVC_PROTOCOL == 1\r
+                            else if (irmp_param.protocol == IRMP_NEC_PROTOCOL && (irmp_bit == 16 || irmp_bit == 17))      // it was a JVC stop bit\r
+                            {\r
+                                ANALYZE_PRINTF ("Switching to JVC protocol\n");\r
+                                irmp_param.stop_bit     = TRUE;                                     // set flag\r
+                                irmp_param.protocol     = IRMP_JVC_PROTOCOL;                        // switch protocol\r
+                                irmp_param.complete_len = irmp_bit;                                 // patch length: 16 or 17\r
+                                irmp_tmp_command        = (irmp_tmp_address >> 4);                  // set command: upper 12 bits are command bits\r
+                                irmp_tmp_address        = irmp_tmp_address & 0x000F;                // lower 4 bits are address bits\r
+                                irmp_start_bit_detected = 1;                                        // tricky: don't wait for another start bit...\r
+                            }\r
+#endif // IRMP_SUPPORT_JVC_PROTOCOL == 1\r
                             else\r
                             {\r
                                 ANALYZE_PRINTF ("error 2: pause %d after data bit %d too long\n", irmp_pause_time, irmp_bit);\r
@@ -2323,6 +2398,17 @@ irmp_ISR (void)
                 else\r
 #endif\r
 \r
+#if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1\r
+                // if KASEIKYO protocol and the code will be repeated within 50 ms, we will ignore 2nd repetition frame\r
+                if (irmp_param.protocol == IRMP_KASEIKYO_PROTOCOL && repetition_frame_number == 1)\r
+                {\r
+                    ANALYZE_PRINTF ("code skipped: KASEIKYO auto repetition frame #%d, counter = %d, auto repetition len = %d\n",\r
+                                    repetition_frame_number + 1, repetition_counter, AUTO_FRAME_REPETITION_LEN);\r
+                    repetition_counter = 0;\r
+                }\r
+                else\r
+#endif\r
+\r
 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1\r
                 // if SAMSUNG32 protocol and the code will be repeated within 50 ms, we will ignore every 2nd frame\r
                 if (irmp_param.protocol == IRMP_SAMSUNG32_PROTOCOL && (repetition_frame_number & 0x01))\r
@@ -2391,11 +2477,48 @@ 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
+\r
+#if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1\r
+                        if (irmp_param.protocol == IRMP_KASEIKYO_PROTOCOL)\r
+                        {\r
+                            uint8_t xor;\r
+                            // ANALYZE_PRINTF ("0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",\r
+                            //                 xor_check[0], xor_check[1], xor_check[2], xor_check[3], xor_check[4], xor_check[5]);\r
+\r
+                            xor = (xor_check[0] & 0x0F) ^ ((xor_check[0] & 0xF0) >> 4) ^ (xor_check[1] & 0x0F) ^ ((xor_check[1] & 0xF0) >> 4);\r
+\r
+                            if (xor != (xor_check[2] & 0x0F))\r
+                            {\r
+                                ANALYZE_PRINTF ("error 4: wrong XOR check for customer id: 0x%1x 0x%1x\n", xor, xor_check[2] & 0x0F);\r
+                                irmp_ir_detected = FALSE;\r
+                            }\r
+\r
+                            xor = xor_check[2] ^ xor_check[3] ^ xor_check[4];\r
+\r
+                            if (xor != xor_check[5])\r
+                            {\r
+                                ANALYZE_PRINTF ("error 4: wrong XOR check for data bits: 0x%02x 0x%02x\n", xor, xor_check[5]);\r
+                                irmp_ir_detected = FALSE;\r
+                            }\r
+                        }\r
+#endif // IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1\r
+\r
                         irmp_protocol = irmp_param.protocol;\r
 \r
 #if IRMP_SUPPORT_FDC_PROTOCOL == 1\r
@@ -2456,6 +2579,13 @@ irmp_ISR (void)
                 irmp_tmp_command        = 0;\r
                 irmp_pulse_time         = 0;\r
                 irmp_pause_time         = 0;\r
+\r
+#if IRMP_SUPPORT_JVC_PROTOCOL == 1\r
+                if (irmp_protocol == IRMP_JVC_PROTOCOL)                             // the stop bit of JVC frame is also start bit of next frame\r
+                {                                                                   // set pulse time here!\r
+                    irmp_pulse_time = ((uint8_t)(F_INTERRUPTS * JVC_START_BIT_PULSE_TIME));\r
+                }\r
+#endif // IRMP_SUPPORT_JVC_PROTOCOL == 1\r
             }\r
         }\r
     }\r