- add eepromdata.h
[moodlight.git] / ircontrol.c
index 7ae665a41b439f89f296d4fc0508ae1363554a38..16ff7b0a13d4c8f9215c667eee177e9d2164b411 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: ircontrol.c,v 1.2 2010/06/27 22:20:39 simimeie Exp $
+/* $Id: ircontrol.c,v 1.6 2010/07/10 07:36:28 simimeie Exp $
  * Functions for the infrared receiver
  *
  * The infrared receiver is connected to PB0 / PCINT0.
  * that equals around 7100 cpu cycles at 8 MHz. */
 #define RC5HALFLENINCYCLES ((CPUFREQ * 889UL) / 1000000UL)
 
-/* For NEC, we start with a 9000 us pulse, then 4500 us silence.
+/* Source for most of this was the following nice page with illustrations
+ * and all that: http://www.sbprojects.com/knowledge/ir/nec.htm
+ * For NEC, we start with a 9000 us pulse, then 4500 us silence.
  * Then the bits follow:
- * a 0 is a 560 us pulse followed by 1690 us of silence.
- * a 1 is a 560 us pulse followed by  560 us of silence.
+ * a 1 is a 560 us pulse followed by 1690 us of silence (=2250 us total).
+ * a 0 is a 560 us pulse followed by  560 us of silence (=1120 us total).
  * These values equal the following cpu cycle counts:
  *  9000 us = 72000 cc, 4500 us = 36000 cc, 560 us = 4480, 1690 us = 13520 cc
+ * When the key stays pressed, it is not resubmitted, but instead a special
+ * "repeat" code is sent. That is: 9000 us pulse, 2250 us silence, 560 us
+ * pulse.
  */
 #define NECSTARTLEN1 ((CPUFREQ *   9UL) / 1000UL)
 #define NECSTARTLEN2 ((CPUFREQ *  45UL) / 10000UL)
 #define NECPULSELEN  ((CPUFREQ *  56UL) / 100000UL)
-#define NECONELEN    ((CPUFREQ * 112UL) / 100000UL)
-#define NECZEROLEN   ((CPUFREQ * 225UL) / 100000UL)
+#define NECZEROLEN   ((CPUFREQ * 112UL) / 100000UL)
+#define NECONELEN    ((CPUFREQ * 225UL) / 100000UL)
+#define NECREPEATLEN ((CPUFREQ * 225UL) / 100000UL)
 
 /* the NEC code contains 4 bytes, sent with LSB first:
  * 0+1 are either the "extended address" or "address and inverted address".
 
 static struct timestamp last0irqts;
 static struct timestamp last1irqts;
-static uint8_t lastpin = 0;
+static uint8_t lastpin = 0xff;
 static uint8_t codebytes[4];
 static uint8_t curcodebit = 0xff;
+static uint8_t lastcommand = 0xff;
+static uint8_t repeatcommand = 0xff;
+static uint16_t repeatticks = 0;
+/* Repeat after this many ticks (70 = 0.5s) */
+#define REPEATAFTERTICKS 100
 
 /* some example codes
 root@moodlight# !NSB! 11111111 00001000 11011111 00100000  (r)
@@ -60,7 +71,7 @@ ISR(PCINT0_vect) {
   uint32_t ts1diff; /* distance from last 1 */
   uint32_t ts0diff; /* distance from last 0 */
   
-  v = PINB & _BV(PB0);
+  v = PINB & _BV(0);
   if (v == lastpin) {  /* No change visible - spurious interrupt */
     return;
   }
@@ -89,6 +100,9 @@ ISR(PCINT0_vect) {
           console_printpgm_P(PSTR("!CRC!"));
         } else {
           /* Successful decode! */
+          lastcommand = codebytes[2];
+          repeatcommand = codebytes[2];
+          repeatticks = curirqts.ticks;
           console_printpgm_P(PSTR(" DEC>"));
           console_printhex8(codebytes[0]);
           console_printhex8(codebytes[1]);
@@ -120,6 +134,17 @@ ISR(PCINT0_vect) {
           curcodebit = 0;
           codebytes[0] = codebytes[1] = codebytes[2] = codebytes[3] = 0;
         }
+    } else if ((ts0diff >= (( 8 * NECREPEATLEN) / 10))
+            && (ts0diff <= ((12 * NECREPEATLEN) / 10))) {
+        if (curcodebit == 0xfe) {
+          console_printpgm_P(PSTR(".REP."));
+          if ((curirqts.ticks - repeatticks) > REPEATAFTERTICKS) {
+            if ((repeatcommand == 0x00) || (repeatcommand == 0x01)) {
+              /* Only the up/down arrows are allowed to be repeated */
+              lastcommand = repeatcommand;
+            }
+          }
+        }
     }
     last1irqts = curirqts;
   }
@@ -161,9 +186,17 @@ ISR(PCINT0_vect) {
 void ircontrol_init(void)
 {
   /* Activate pullup */
-  PORTB |= _BV(PB0);
+  PORTB |= _BV(0);
   /* enable PCINT0 */
   PCICR |= _BV(PCIE0);
   /* Enable pin change interrupt 0 (=PB0) in pcint0 */
   PCMSK0 |= _BV(PCINT0);
 }
+
+uint8_t ircontrol_getlastcommand(void)
+{
+  uint8_t res;
+  res = lastcommand;
+  lastcommand = 0xff;
+  return res;
+}
This page took 0.035086 seconds and 4 git commands to generate.