diff --git a/campapp/main.c b/campapp/main.c index fdba72ae..aac09d61 100644 --- a/campapp/main.c +++ b/campapp/main.c @@ -107,6 +107,10 @@ void night_tick(void){ } }; }; + //Debounce rutine runs every 10 systick + EVERY(10,0){ + inputDebounce(); + }; return; } diff --git a/ccccmaze/main.c b/ccccmaze/main.c index 4a70b2fa..b8730610 100644 --- a/ccccmaze/main.c +++ b/ccccmaze/main.c @@ -19,8 +19,18 @@ #include "main.gen" +#define EVERY(x,y) if((ctr+y)%(x/SYSTICKSPEED)==0) +void debounce_tick(){ + static int ctr; + ctr++; + EVERY(10,0){ + inputDebounce(); + }; + +} void sys_tick_handler(void){ incTimer(); + debounce_tick(); generated_tick(); }; diff --git a/l0dables/EXPORTS b/l0dables/EXPORTS index 4d60aa7a..e25e2b35 100644 --- a/l0dables/EXPORTS +++ b/l0dables/EXPORTS @@ -3,6 +3,8 @@ getInput getInputRaw getInputWait getInputWaitRelease +getInputChange +inputDebounce gpio_clear gpio_set gpio_toggle diff --git a/r0ketlib/keyin.c b/r0ketlib/keyin.c index 558a6ba9..b07a9a1f 100644 --- a/r0ketlib/keyin.c +++ b/r0ketlib/keyin.c @@ -5,6 +5,8 @@ #define FUNC (SCU_GPIO_PDN) +static volatile uint8_t buttonState=BTN_NONE; +static volatile uint8_t buttonChange=0; static char isTurned; @@ -83,18 +85,9 @@ uint8_t getInputRaw(void) { uint8_t getInput(void) { - uint8_t key = BTN_NONE; - key=getInputRaw(); - /* XXX: we should probably debounce the joystick. - Any ideas how to do this properly? - For now wait for any release. - */ - if(key != BTN_NONE) - while(key==getInputRaw()) - ; + return buttonState; - return key; } @@ -160,3 +153,22 @@ void getInputWaitRelease(void) { work_queue(); delayms_queue(10); /* Delay a little more to debounce */ } + +uint8_t getInputChange(void){ + return buttonChange; +} + +//debounce with a vertical counter +void inputDebounce(void){ + uint8_t input = getInputRaw(); + static uint8_t cnt1, cnt0; + uint8_t change; + + change = input ^ buttonState; + cnt1 = (cnt1 ^ cnt0) & change; + cnt0 = (~cnt0) & change; + + buttonChange = (cnt1 & cnt0); + buttonState ^= buttonChange; +} + diff --git a/r0ketlib/keyin.h b/r0ketlib/keyin.h index dc1f1544..a7be4479 100644 --- a/r0ketlib/keyin.h +++ b/r0ketlib/keyin.h @@ -12,13 +12,15 @@ #define BTN_ENTER (1<<4) void keySetRotation(char doit); -uint8_t getInputRaw(void); -uint8_t getInput(void); +uint8_t getInputRaw(void); //Returns the raw GPIO values +uint8_t getInput(void); //Returns debounced values void inputInit(void); -uint8_t getInputWait(void); -uint8_t getInputWaitTimeout(int timeout); -uint8_t getInputWaitRepeat(void); -void getInputWaitRelease(void); +uint8_t getInputWait(void); //Waits for key press +uint8_t getInputWaitTimeout(int timeout); //Waits timeout ms for key press +uint8_t getInputWaitRepeat(void);//Repeats the same key while holding down +void getInputWaitRelease(void); //Waits for any key +uint8_t getInputChange(void); //Returns 1 for 10ms on an edge +void inputDebounce(void); //Does the actual debounce, runs in system tick /* XXX: probably should be in central header file */ #define KEY_DOWN_PIN (PB_0) diff --git a/rfapp/main.c b/rfapp/main.c index 3a9ebec8..8c7b2b5a 100644 --- a/rfapp/main.c +++ b/rfapp/main.c @@ -47,6 +47,10 @@ void tick_batteryLED(void){ }; }; + //Debounce inputs + EVERY(10,0){ + inputDebounce(); + }; return; } diff --git a/testapp/key.c b/testapp/key.c new file mode 100644 index 00000000..ca1c4639 --- /dev/null +++ b/testapp/key.c @@ -0,0 +1,207 @@ +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/***************************************************************************/ + +//# MENU key keyInputRaw +void keyTestRaw(void) { + lcdClear(); + lcdPrintln("getInputRaw()"); + lcdPrintln("Press left to exit"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdSetCrsr(0, 70); + lcdPrint("Down"); + lcdDisplay(); + + int countEnter; + int countUp; + int countDown; + + while( getInputRaw() != BTN_LEFT) { + if (getInputRaw() == BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + if (getInputRaw() == BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + + if (getInputRaw() == BTN_DOWN){ + lcdSetCrsr(50, 70); + countDown ++; + lcdPrintInt(countDown); + lcdDisplay(); + } + + } + +} + +//# MENU key keyInputRaw_edge +//Counts on rising edge of keyInputRaw +void keyTestRawEdge(void) { + lcdClear(); + lcdPrintln("getInputRaw()"); + lcdPrintln("with edge detection"); + lcdPrintln("Press left to exit"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdSetCrsr(0, 70); + lcdPrint("Down"); + lcdDisplay(); + + int countEnter; + int countUp; + int countDown; + + uint8_t lastInput = BTN_NONE; + uint8_t currentInput = BTN_NONE; + + while( getInputRaw() != BTN_LEFT) { + currentInput = getInputRaw(); + if (currentInput == BTN_UP && lastInput != BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + if (currentInput == BTN_ENTER && lastInput != BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + + if (currentInput == BTN_DOWN && lastInput != BTN_DOWN){ + lcdSetCrsr(50, 50); + countDown ++; + lcdPrintInt(countDown); + lcdDisplay(); + } + lastInput = currentInput; + } + +} + +//# MENU key keyInput +//Counts on getInputChange and getInput +void keyTestDebounce(void) { + lcdClear(); + lcdPrintln("Press enter to increse count"); + lcdPrintln("with getInput"); + lcdPrintln("Press left to exit"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdSetCrsr(0, 70); + lcdPrint("Down"); + lcdDisplay(); + + int countEnter; + int countUp; + int countDown; + + uint8_t changeInput = BTN_NONE; + uint8_t currentInput = BTN_NONE; + + while( getInputRaw() != BTN_LEFT) { + currentInput = getInput(); + changeInput = getInputChange(); + if (currentInput == BTN_UP && changeInput == BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + if (currentInput == BTN_ENTER && changeInput == BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + if (currentInput == BTN_DOWN && changeInput == BTN_DOWN){ + lcdSetCrsr(50, 70); + countDown ++; + lcdPrintInt(countDown); + lcdDisplay(); + } + + } + +} + +//# MENU key WaitRepeat +void keyTestWaitRepeat(void) { + lcdClear(); + lcdPrintln("Press enter to increse count"); + lcdPrintln("with getInputWaitRepeat()"); + lcdPrintln("Press left to exit"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdSetCrsr(0, 70); + lcdPrint("Down"); + lcdDisplay(); + + int countEnter = 0; + int countUp = 0; + int countDown = 0; + + while( getInputRaw() != BTN_LEFT) { + if (getInputWaitRepeat() == BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + + if (getInputWaitRepeat() == BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + + if (getInputWaitRepeat() == BTN_DOWN){ + lcdSetCrsr(50, 70); + countDown ++; + lcdPrintInt(countDown); + lcdDisplay(); + } + + } + +} + diff --git a/testapp/main.c b/testapp/main.c index 38563df5..8d764a8e 100644 --- a/testapp/main.c +++ b/testapp/main.c @@ -19,8 +19,18 @@ #include "main.gen" +#define EVERY(x,y) if((ctr+y)%(x/SYSTICKSPEED)==0) +void debounce_tick(){ + static int ctr; + ctr++; + EVERY(10,0){ + inputDebounce(); + }; + +} void sys_tick_handler(void){ incTimer(); + debounce_tick(); generated_tick(); };