Created
April 6, 2014 20:18
-
-
Save rosterloh/10011040 to your computer and use it in GitHub Desktop.
Simple input debouncing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CHECK_MSEC 5 // Read hardware every 5 msec | |
#define PRESS_MSEC 10 // Stable time before registering pressed | |
#define RELEASE_MSEC 100 // Stable time before registering released | |
// This function reads the key state from the hardware. | |
extern bool_t RawKeyPressed(); | |
// This holds the debounced state of the key. | |
bool_t DebouncedKeyPress = false; | |
// Service routine called every CHECK_MSEC to | |
// debounce both edges | |
void DebounceSwitch1(bool_t *Key_changed, bool_t *Key_pressed) | |
{ | |
static uint8_t Count = RELEASE_MSEC / CHECK_MSEC; | |
bool_t RawState; | |
*Key_changed = false; | |
*Key_pressed = DebouncedKeyPress; | |
RawState = RawKeyPressed(); | |
if (RawState == DebouncedKeyPress) { | |
// Set the timer which allows a change from current state. | |
if (DebouncedKeyPress) Count = RELEASE_MSEC / CHECK_MSEC; | |
else Count = PRESS_MSEC / CHECK_MSEC; | |
} else { | |
// Key has changed - wait for new state to become stable. | |
if (--Count == 0) { | |
// Timer expired - accept the change. | |
DebouncedKeyPress = RawState; | |
*Key_changed=true; | |
*Key_pressed=DebouncedKeyPress; | |
// And reset the timer. | |
if (DebouncedKeyPress) Count = RELEASE_MSEC / CHECK_MSEC; | |
else Count = PRESS_MSEC / CHECK_MSEC; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment