
#define INPUT_POLL_COUNT  4

/* array of bit flags for input filter */
unsigned char input_filter[INPUT_POLL_COUNT];
unsigned char input_status;              /* current filtered input state */
unsigned char input_previous;            /* previous filtered input state */
unsigned char input_true;                /* saved input true transition */
unsigned char input_false;               /* saved input false transition */

unsigned char code input_xlat[8] = {0x01,0x02,0x04,0x08, /* xlat bit number to mask */
                                    0x10,0x20,0x40,0x80};
unsigned char code input_nxlt[8] = {0xFE,0xFD,0xFB,0xF7, /* xlat bit number to ~mask */
                                    0xEF,0xDF,0xBF,0x7F};

void p1_input_poll(void)
{
    unsigned char input_tmp;
    unsigned char input_or;
    unsigned char input_cur;
    unsigned char i;

    input_cur = P1;                            /* read current input state */

    /* loop to shift filter up by one position and perform */
    /* bit wise equal comparison */
    input_or = 0;                              /* this holds 1 for bits that are */  
    for(i=0; i<INPUT_POLL_COUNT; i++)          /* changing in span of filter table */
    {
        input_or |= input_cur ^ input_filter[i];  /* adjacent pair != so set or */
        input_tmp = input_filter[i];              /* swap so to slide table up */
        input_filter[i] = input_cur;
        input_cur = input_tmp;
    }

    /* produce bit pattern for current stable input data */
    /* if a input goes true its previous false state data is */
    /* automatically cleared and as well if a input goes false */
    /* its previous true going status is cleared */
                 /* no chg where toggles  */   /* mask present status where stable */
    input_status = (input_status & input_or) | (input_filter[0] & ~input_or);

    input_tmp = input_status & (input_status ^ input_previous);
    input_true |= input_tmp;
    input_false &= ~input_tmp;

    input_tmp = input_previous & (input_status ^ input_previous);
    input_false |= input_tmp;
    input_true &= ~input_tmp;

    input_previous = input_status;
}
