Permalink
Browse files

Debounce.

Reformat - codelite does something strange with tabs...
  • Loading branch information...
1 parent d579ddf commit 8122a8782ee6e991b568fe9f7ee125da956fbefb @netmaniac committed Jun 4, 2011
Showing with 44 additions and 31 deletions.
  1. +25 −13 NettigoKeypad.cpp
  2. +19 −18 NettigoKeypad.h
View
@@ -13,35 +13,47 @@ NG_Keypad::NG_Keypad(void) {
};
int NG_Keypad::key_pressed(int rd) {
- if (rd < 250) return RIGHT;
- if (rd < 590) return UP;
- if (rd < 749) return DOWN;
- if (rd < 792) return LEFT;
- if (rd < 912) return SELECT;
- return NONE;
+ static int ret;
+
+ if (rd < 250) { ret = RIGHT;}
+ else if (rd < 590) { ret = UP; }
+ else if (rd < 749) { ret = DOWN; }
+ else if (rd < 792) { ret = LEFT; }
+ else if (rd < 912) { ret = SELECT; }
+ else { ret = NONE; }
+ //no change since last time or timeout for debouncing not passed - do nothing
+ if (ret == lastKey || millis() - lastKeyTime < debounceDelay) {
+ return NONE;
+ } else {
+ lastKey = ret;
+ lastKeyTime = millis();
+ return ret;
+ }
+
};
-void NG_Keypad::check_handlers(byte rd) {
- if (_functions[key_pressed(rd)] != NULL)
+void NG_Keypad::check_handlers(int rd) {
+ int key = key_pressed(rd);
+ if (_functions[key] != NULL)
{
- _functions[key_pressed(rd)]();
+ _functions[key]();
};
return;
}
-int NG_Keypad::register_handler( byte key, void (*userF)(void) ) {
+int NG_Keypad::register_handler( int key, void (*userF)(void) ) {
if(key >= NG_KEYPAD_SIZE)
return -1;
- _functions[key] = userF;
+ _functions[key] = userF;
};
unsigned int NG_Keypad::getDebounceDelay( void ){
- return debounceDelay;
+ return debounceDelay;
};
void NG_Keypad::setDebounceDelay (unsigned int d){
- debounceDelay = d;
+ debounceDelay = d;
};
View
@@ -2,17 +2,18 @@
//Published on MIT licence
// (c) 2011 Nettigo
#include "WProgram.h"
+
#define NG_KEYPAD_SIZE 6
class NG_Keypad
{
public:
- static const byte NONE = 0;
- static const byte SELECT = 1;
- static const byte LEFT = 2;
- static const byte DOWN = 3;
- static const byte UP = 4;
- static const byte RIGHT = 5;
+ static const int NONE = 0;
+ static const int SELECT = 1;
+ static const int LEFT = 2;
+ static const int DOWN = 3;
+ static const int UP = 4;
+ static const int RIGHT = 5;
NG_Keypad(void);
@@ -24,26 +25,26 @@ class NG_Keypad
// takes two args
// key - which key should be used
// userF - which function should be called when key pressed
- int register_handler( byte key, void (*userF)(void) );
+ int register_handler( int key, void (*userF)(void) );
//Check for handlers to be called. Takes as argument analogRead result.
- void check_handlers(byte rd);
+ void check_handlers(int rd);
- //return current debounce delay
- unsigned int getDebounceDelay( void );
+ //return current debounce delay
+ unsigned int getDebounceDelay( void );
- //set new debounce delay
- void setDebounceDelay (unsigned int);
+ //set new debounce delay
+ void setDebounceDelay (unsigned int);
- private:
+ private:
void (*_functions[NG_KEYPAD_SIZE])(void);
- //default debounce timeout
- unsigned int debounceDelay;
+ //default debounce timeout
+ unsigned int debounceDelay;
- //what last key was pressed and when (millis)
- byte lastKey;
- unsigned long lastKeyTime;
+ //what last key was pressed and when (millis)
+ int lastKey;
+ unsigned long lastKeyTime;
};

0 comments on commit 8122a87

Please sign in to comment.