+ uint8_t v;
+ struct timestamp curirqts;
+ uint32_t ts1diff; /* distance from last 1 */
+ uint32_t ts0diff; /* distance from last 0 */
+
+ v = PINB & _BV(PB0);
+ if (v == lastpin) { /* No change visible - spurious interrupt */
+ return;
+ }
+ curirqts = gettimestamp_noirq();
+ ts1diff = ((uint32_t)curirqts.ticks << 16) + curirqts.partticks;
+ ts0diff = ts1diff;
+ ts1diff -= ((uint32_t)last1irqts.ticks << 16) + last1irqts.partticks;
+ ts0diff -= ((uint32_t)last0irqts.ticks << 16) + last0irqts.partticks;
+ if (v) { /* Infrared just went away! */
+ if ((ts1diff >= (( 8 * NECSTARTLEN1) / 10))
+ && (ts1diff <= ((12 * NECSTARTLEN1) / 10))) {
+ /* NEC start bit */
+ /* console_printpgm_P(PSTR("!NSB!")); */
+ curcodebit = 0xfe; /* Wait for second part of start sequence */
+ } else {
+ if (curcodebit <= 32) { /* We're in a decoding attempt, so */
+ /* Check pulse length */
+ if ((ts1diff < (( 8 * NECPULSELEN) / 10))
+ || (ts1diff > ((12 * NECPULSELEN) / 10))) {
+ /* WRONG */
+ curcodebit = 0xff;
+ }
+ }
+ if (curcodebit == 32) {
+ if (codebytes[2] != (codebytes[3] ^ 0xff)) {
+ console_printpgm_P(PSTR("!CRC!"));
+ } else {
+ /* Successful decode! */
+ console_printpgm_P(PSTR(" DEC>"));
+ console_printhex8(codebytes[0]);
+ console_printhex8(codebytes[1]);
+ console_printhex8(codebytes[2]);
+ console_printhex8(codebytes[3]);
+ }
+ }
+ }
+ last0irqts = curirqts;
+ } else { /* Infrared went on */
+ if ((ts1diff >= (( 8 * NECZEROLEN) / 10))
+ && (ts1diff <= ((12 * NECZEROLEN) / 10))) {
+ /* console_printpgm_P(PSTR("0")); */
+ if (curcodebit < 32) {
+ curcodebit++;
+ }
+ } else if ((ts1diff >= (( 8 * NECONELEN) / 10))
+ && (ts1diff <= ((12 * NECONELEN) / 10))) {
+ /* console_printpgm_P(PSTR("1")); */
+ if (curcodebit < 32) {
+ codebytes[curcodebit >> 3] |= (1 << (curcodebit & 0x07));
+ curcodebit++;
+ } else {
+ curcodebit = 0xff;
+ }
+ } else if ((ts0diff >= (( 8 * NECSTARTLEN2) / 10))
+ && (ts0diff <= ((12 * NECSTARTLEN2) / 10))) {
+ if (curcodebit == 0xfe) { /* voila, correct start sequence */
+ curcodebit = 0;
+ codebytes[0] = codebytes[1] = codebytes[2] = codebytes[3] = 0;
+ }
+ }
+ last1irqts = curirqts;
+ }
+#if 0
+ console_printpgm_P(PSTR("!"));
+ console_printhex8(tsdiff >> 24);
+ console_printhex8(tsdiff >> 16);
+ console_printhex8(tsdiff >> 8);
+ console_printhex8(tsdiff >> 0);
+ console_printpgm_P(PSTR("!"));
+#endif
+#if 0
+ if (tsdiff > ((24 * HALFRC5LENINCYCLES) / 10)) {
+ /* Start of new transmission */
+ console_printpgm_P(PSTR("!1["));
+ console_printhex8(v);
+ lastbit = 1;
+ } else if (tsdiff > ((15 * HALFRC5LENINCYCLES) / 10)) {
+ /* Different bit than last time */
+ lastbit = !lastbit;
+ if (lastbit) {
+ console_printpgm_P(PSTR("1"));
+ } else {
+ console_printpgm_P(PSTR("0"));
+ }
+ } else if ((tsdiff < ((15 * HALFRC5LENINCYCLES) / 10))
+ && (tsdiff > (( 5 * HALFRC5LENINCYCLES) / 10))) {
+ /* Same bit as last time */
+ if (lastbit) {
+ console_printpgm_P(PSTR("1"));
+ } else {
+ console_printpgm_P(PSTR("0"));
+ }