
/* Capture the negitive edges and update the queue with the number
 * received. */
void zk_timer2_capture_isr() __interrupt 7
{
        static long int ovf;    /* Timer overflow counter */
        static uint16_t word;
        static uint8_t bits_recvd; /* count the number of bits received. */
        idata char word_buf[17] = "----------------";/* a buffer to print on LCD*/

        uint16_t timer_val;     /* hold the timer capture value */
        uint32_t lag_edge;
        static uint32_t lead_edge; /* the previous negitive edge needs
                                    * to be preserved. */
        int32_t diff;

        if (TF2 == 1) {         /* check overflow */
                ovf++;
                TF2 = 0;
                P3_4 = !P3_4;   //for debugging, buzzer beeps
        }
        if (EXF2 == 1) {
                /* Capture Interrupt is not disabled! So keep ISR
                 * small! */
                timer_val = RCAP2H;
                timer_val <<= 8;
                timer_val |= RCAP2L;
                EXF2 = 0;       /* clear the flags */

                P1_4 = !P1_4; //for debugging, led toggles

                lag_edge = timer_val;

                if (lead_edge == 0) { /* for the first pulse only */
                        lead_edge = lag_edge;
                        ovf = 0;
                }
                else {
                        diff =  lag_edge - lead_edge + (ovf * 0xFFFF);
                        /* Consider the timer overflow */
                        ovf = 0;

                        if (diff <= CLK_6_1MS) /* received 0 */ {
                                word |= 0x0000;
                                word_buf[bits_recvd] = '0';
                                /* check each bit received, for
                                 * debugging only. */
                                bits_recvd++;
                        }
                        else {
                                if (diff <= CLK_6_2MS) { /* received 1 */
                                        word |= 0x8000;
                                        word_buf[bits_recvd] = '1';
                                }
                                else
                                        word_buf[bits_recvd] = 'X';     /* FIXME:This should
                                                                         * not happen. */
                                bits_recvd++;
                        }

                        word >>= 1;

                        if (bits_recvd == (MAX_NUM_BITS - 1)) {
                                /* 15 -bits received. Update the buffer. */
                                lead_edge = 0;
                                bits_recvd = 0;
                                word = word >> 1;
                                /* update the buffer here. */
                                lcd_printl(0, word_buf);
                                sprintf(word_buf, "received %d bits.", bits_recvd);
                                lcd_printl(1, word_buf);
                                word = 0;
                        }
                        else
                                lead_edge = lag_edge;
                }
        }
}
