在TI的例程里经常看到关于按键值得读取的程序都大同小异,但是我就看不懂那一段程序到底是什么意思?为什么会那么写。哪位高手给我解读下好么?程序如下:
ucData = GPIOPinRead(BUTTONS_GPIO_BASE, ALL_BUTTONS);
//
// Determine the buttons that are in a different state than the debounced
// state.
//
ucDelta = ucData ^g_ucButtonStates;
//
// Increment the clocks by one.
//
g_ucDebounceClockA ^= g_ucDebounceClockB; g_ucDebounceClockB = ~g_ucDebounceClockB;
// Reset the clocks corresponding to switches that have not changed state.
//
g_ucDebounceClockA &= ucDelta;
g_ucDebounceClockB &= ucDelta;
//
// Get the new debounced switch state.
//
g_ucButtonStates &= g_ucDebounceClockA | g_ucDebounceClockB;
g_ucButtonStates |= (~(g_ucDebounceClockA | g_ucDebounceClockB)) & ucData;
//
// Determine the switches that just changed debounced state.
//
ucDelta ^= (g_ucDebounceClockA | g_ucDebounceClockB);
//
// Remember the delta state in our global variable.
//
g_ucButtonDelta = ucDelta;
//
// Now consider auto-repeats.
//
ucRepeat = 0;
//
// Loop through the buttons.
//
for(ulLoop = 0; ulLoop < NUM_BUTTONS; ulLoop++)
{
//
// We only ever send auto-repeats for buttons that are in the pressed
// state so check for this first.
//
if((g_ucButtonStates & g_psButtonInfo[ulLoop].ucBtn) == 0)
{
//
// First, check for any buttons that have just been pressed. We
// need to initialize the auto-repeat counter for these.
//
if(ucDelta & g_psButtonInfo[ulLoop].ucBtn)
{
//
// The button has just been pressed so set up for the initial
// delay prior to repeating.
//
g_psButtonInfo[ulLoop].ucCount =
g_psButtonInfo[ulLoop].ucInitialCount;
}
//
// Now determine if the button needs to send an auto-repeat. This
// will be necessary if auto-repeat is enabled (ucRepeatCount not
// set to 0) and the button counter has reached 0.
//
if((g_psButtonInfo[ulLoop].ucCount == 0) &&
(g_psButtonInfo[ulLoop].ucRepeatCount != 0))
{
//
// Set the auto-repeat flag for this button.
//
ucRepeat |= g_psButtonInfo[ulLoop].ucBtn;
//
// Reset the button counter for the next auto-repeat interval.
//
g_psButtonInfo[ulLoop].ucCount =
g_psButtonInfo[ulLoop].ucRepeatCount;
}
//
// Decrement the button counter.
//
g_psButtonInfo[ulLoop].ucCount--;
}
}
//
// Update our global with the auto-repeat state.
//
g_ucButtonRepeat = ucRepeat;
//
// Pass the returned button information back to the caller.
//
*pucDelta = ucDelta;
*pucRepeat = ucRepeat;
return(g_ucButtonStates); |