📄 ui_onboard.c
字号:
g_sUISwitches[ulIdx].pfnPress();
}
//
// Reset the hold count for this button.
//
g_pulUIHoldCount[ulIdx] = 0;
}
else
{
//
// The switch was just released. If there is a hold defined,
// the switch was held for less than the hold time, and there
// is a press function to call then call it now.
//
if(g_sUISwitches[ulIdx].pfnPress &&
(g_sUISwitches[ulIdx].ulHoldTime != 0) &&
(g_pulUIHoldCount[ulIdx] < g_sUISwitches[ulIdx].ulHoldTime))
{
g_sUISwitches[ulIdx].pfnPress();
}
//
// If there is a release function to call then call it now.
//
if(g_sUISwitches[ulIdx].pfnRelease)
{
g_sUISwitches[ulIdx].pfnRelease();
}
}
}
//
// See if there is a hold defined for this switch and the switch is
// pressed.
//
if((g_sUISwitches[ulIdx].ulHoldTime != 0) &&
(!(g_ulUIOnboardSwitches & (1 << g_sUISwitches[ulIdx].ucBit))))
{
//
// Increment the hold count if it is not maxed out.
//
if(g_pulUIHoldCount[ulIdx] < 0xffffffff)
{
g_pulUIHoldCount[ulIdx]++;
}
//
// If the hold count has reached the hold time for this switch and
// there is a hold function then call it now.
//
if((g_pulUIHoldCount[ulIdx] == g_sUISwitches[ulIdx].ulHoldTime) &&
g_sUISwitches[ulIdx].pfnHold)
{
g_sUISwitches[ulIdx].pfnHold();
}
}
}
}
//*****************************************************************************
//
//! Filters the value of a potentiometer.
//!
//! \param ulValue is the current sample for the potentiometer.
//!
//! This function performs filtering on the sampled value of a potentiometer.
//! First, a single pole IIR low pass filter is applied to the raw sampled
//! value. Then, the filtered value is examined to determine when the
//! potentiometer is being turned and when it is not. When the potentiometer
//! is not being turned (and variations in the value are therefore the result
//! of noise in the system), a constant value is returned instead of the
//! filtered value. When the potentiometer is being turned, the filtered value
//! is returned unmodified.
//!
//! This second filtering step eliminates the flutter when the potentiometer is
//! not being turned so that processes that are driven from its value (such as
//! a motor position) do not result in the motor jiggling back and forth to the
//! potentiometer flutter. The downside to this filtering is a larger turn
//! of the potentiometer being required before the output value changes.
//!
//! \return Returns the filtered potentiometer value.
//
//*****************************************************************************
unsigned long
UIOnboardPotentiometerFilter(unsigned long ulValue)
{
//
// Pass the potentiometer value through a single pole IIR low pass filter
// (with a coefficient of 0.75).
//
g_ulUIOnboardPotValue = ((g_ulUIOnboardPotValue * 3) + ulValue) / 4;
//
// Now, pass the potentiometer value through a stable value detector.
// Start by seeing if the value is stable or changing.
//
if(g_ulUIOnboardFilteredPotValue == 0xffffffff)
{
//
// The potenetiometer value is changing. If the present value is less
// than the current minimum then save it as the new minimum.
//
if(g_ulUIOnboardPotValue < g_ulUIOnboardPotMin)
{
g_ulUIOnboardPotMin = g_ulUIOnboardPotValue;
}
//
// If the present value is greater than the current maximum then save
// it as the new maximum.
//
if(g_ulUIOnboardPotValue > g_ulUIOnboardPotMax)
{
g_ulUIOnboardPotMax = g_ulUIOnboardPotValue;
}
//
// Add the present value into the accumulator.
//
g_ulUIOnboardPotSum += g_ulUIOnboardPotValue;
//
// Increment the count of samples in the accumulator.
//
g_ulUIOnboardPotCount++;
//
// See if there are sixteen samples in the accumulator.
//
if(g_ulUIOnboardPotCount == 16)
{
//
// If the range from minimum to maximum for the last sixteen
// samples is less than ten then the potentiometer value has become
// stable.
//
if((g_ulUIOnboardPotMax - g_ulUIOnboardPotMin) < 10)
{
//
// Compute the stable potentiometer value based on the average
// of the previous sixteen samples.
//
g_ulUIOnboardFilteredPotValue = g_ulUIOnboardPotSum / 16;
}
//
// Reset the minimum, maximum, accumulator, and sample count
// variables.
//
g_ulUIOnboardPotMin = 0xffffffff;
g_ulUIOnboardPotMax = 0;
g_ulUIOnboardPotSum = 0;
g_ulUIOnboardPotCount = 0;
}
}
else
{
//
// The potentiometer value is stable. If the present value is
// different than the stable value by more than ten then the value
// is considered to be changing.
//
if(((g_ulUIOnboardFilteredPotValue < g_ulUIOnboardPotValue) &&
((g_ulUIOnboardPotValue - g_ulUIOnboardFilteredPotValue) > 10)) ||
((g_ulUIOnboardFilteredPotValue > g_ulUIOnboardPotValue) &&
((g_ulUIOnboardFilteredPotValue - g_ulUIOnboardPotValue) > 10)))
{
g_ulUIOnboardFilteredPotValue = 0xffffffff;
}
}
//
// If the potentiometer value is stable then return the averaged value. If
// it is changing then return the output of the low pass filter.
//
return((g_ulUIOnboardFilteredPotValue == 0xffffffff) ?
g_ulUIOnboardPotValue : g_ulUIOnboardFilteredPotValue);
}
//*****************************************************************************
//
//! Initializes the on-board user interface elements.
//!
//! \param ulSwitches is the initial state of the switches.
//! \param ulPotentiometer is the initial state of the potentiometer.
//!
//! This function initializes the internal state of the on-board user interface
//! handlers. The initial state of the switches are used to avoid spurious
//! switch presses/releases, and the initial state of the potentiometer is used
//! to make the filtered potentiometer value track more accurately when first
//! starting (after a short period of time it will track correctly regardless
//! of the initial state).
//!
//! \return None.
//
//*****************************************************************************
void
UIOnboardInit(unsigned long ulSwitches, unsigned long ulPotentiometer)
{
//
// Set the default debounced switch state based on the provided switch
// value.
//
g_ulUIOnboardSwitches = ulSwitches;
//
// Set the default filtered value of the potentiometer based on the
// provided value.
//
g_ulUIOnboardPotValue = ulPotentiometer;
//
// Initialize the internal state of the stable value detector.
//
g_ulUIOnboardFilteredPotValue = 0xffffffff;
g_ulUIOnboardPotMin = 0xffffffff;
g_ulUIOnboardPotMax = 0;
g_ulUIOnboardPotSum = 0;
g_ulUIOnboardPotCount = 0;
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -