From 7680f9df5fb649d9a14a25f23a5be5239a30ada4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=A5gen?= <johannes@wagen.no> Date: Sun, 20 Sep 2015 22:13:39 +0200 Subject: [PATCH 1/7] Added debounce to getInput --- r0ketlib/keyin.c | 32 ++++++++++++++++++++++---------- r0ketlib/keyin.h | 2 ++ 2 files changed, 24 insertions(+), 10 deletions(-) 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..ad1b780f 100644 --- a/r0ketlib/keyin.h +++ b/r0ketlib/keyin.h @@ -19,6 +19,8 @@ uint8_t getInputWait(void); uint8_t getInputWaitTimeout(int timeout); uint8_t getInputWaitRepeat(void); void getInputWaitRelease(void); +uint8_t getInputChange(void); +void inputDebounce(void); /* XXX: probably should be in central header file */ #define KEY_DOWN_PIN (PB_0) From ba3485cb1f75f72c8152059bbc731f709ece58d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=A5gen?= <johannes@wagen.no> Date: Sun, 20 Sep 2015 23:07:45 +0200 Subject: [PATCH 2/7] Added debounce functions to export and system tick rutine --- campapp/main.c | 4 ++++ l0dables/EXPORTS | 2 ++ rfapp/main.c | 4 ++++ 3 files changed, 10 insertions(+) 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/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/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; } From 0ab8579edea7be400c33afd43ec7fb45fc5bb095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=A5gen?= <johannes@wagen.no> Date: Sun, 20 Sep 2015 23:51:08 +0200 Subject: [PATCH 3/7] Made a test program for the buttons --- testapp/key.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 testapp/key.c diff --git a/testapp/key.c b/testapp/key.c new file mode 100644 index 00000000..9162d388 --- /dev/null +++ b/testapp/key.c @@ -0,0 +1,95 @@ +#include <string.h> + +#include <r0ketlib/display.h> +#include <r0ketlib/print.h> + +#include <fatfs/ff.h> +#include <r0ketlib/select.h> +#include <r0ketlib/execute.h> +#include <r0ketlib/config.h> +#include <r0ketlib/render.h> +#include <r0ketlib/fs_util.h> +#include <r0ketlib/keyin.h> +#include <r0ketlib/fonts.h> +#include <r0ketlib/fonts/smallfonts.h> +#include <r0ketlib/stringin.h> +#include <r0ketlib/colorin.h> + +#include <string.h> + +/***************************************************************************/ + +//# MENU key keyInputRaw +void keyTestRaw(void) { + lcdClear(); + lcdPrintln("Press enter to increse count"); + lcdPrintln("with getInputRaw()"); + lcdPrintln("exit with left"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdDisplay(); + + int countEnter; + int countUp; + + while( getInputRaw() != BTN_LEFT) { + if (getInputRaw() == BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + + if (getInputRaw() == BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + + } + +} + +//# MENU key keyInputRaw_edge +//Counts on rising edge of keyInputRaw +void keyTestRawEdge(void) { + lcdClear(); + lcdPrintln("Press enter to increse count"); + lcdPrintln("with getInputRaw()"); + lcdPrintln("exit with left"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdDisplay(); + + int countEnter; + int countUp; + + uint8_t lastInput = BTN_NONE; + uint8_t currentInput = BTN_NONE; + + while( getInputRaw() != BTN_LEFT) { + currentInput = getInputRaw(); + if (currentInput == BTN_ENTER && lastInput != BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + + if (currentInput == BTN_UP && lastInput != BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + lastInput = currentInput; + } + +} From 672c51daf1d8b788ec97c37231c52e03a26ff6bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=A5gen?= <johannes@wagen.no> Date: Mon, 21 Sep 2015 00:34:38 +0200 Subject: [PATCH 4/7] added debounce rutine to system tick of testapp/main.c and expanded key test app --- r0ketlib/keyin.h | 11 ++++--- testapp/key.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ testapp/main.c | 6 ++++ 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/r0ketlib/keyin.h b/r0ketlib/keyin.h index ad1b780f..1ad6740f 100644 --- a/r0ketlib/keyin.h +++ b/r0ketlib/keyin.h @@ -12,15 +12,18 @@ #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); +//Waits for input uint8_t getInputWait(void); +//Waits timeout ms for input uint8_t getInputWaitTimeout(int timeout); uint8_t getInputWaitRepeat(void); void getInputWaitRelease(void); -uint8_t getInputChange(void); -void inputDebounce(void); +uint8_t getInputChange(void); //Returns 1 for 10ms on an edge +//Does the actual debounce, runs in system tick +void inputDebounce(void); /* XXX: probably should be in central header file */ #define KEY_DOWN_PIN (PB_0) diff --git a/testapp/key.c b/testapp/key.c index 9162d388..9d1f7d43 100644 --- a/testapp/key.c +++ b/testapp/key.c @@ -93,3 +93,78 @@ void keyTestRawEdge(void) { } } + +//# MENU key keyInput +//Counts on getInputChange and getInput +void keyTestDebounce(void) { + lcdClear(); + lcdPrintln("Press enter to increse count"); + lcdPrintln("with getInput"); + lcdPrintln("exit with left"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdDisplay(); + + int countEnter; + int countUp; + + uint8_t changeInput = BTN_NONE; + uint8_t currentInput = BTN_NONE; + + while( getInputRaw() != BTN_LEFT) { + currentInput = getInput(); + changeInput = getInputChange(); + if (currentInput == BTN_ENTER && changeInput == BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + + if (currentInput == BTN_UP && changeInput == BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + } + +} + +//# MENU key WaitRepeat +void keyTestWaitRepeat(void) { + lcdClear(); + lcdPrintln("Press enter to increse count"); + lcdPrintln("with getInputWaitRepeat()"); + lcdPrintln("exit with left"); + lcdNl(); + lcdSetCrsr(0, 50); + lcdPrint("Up"); + lcdSetCrsr(0, 60); + lcdPrint("Enter"); + lcdDisplay(); + + int countEnter; + int countUp; + + while( getInputRaw() != BTN_LEFT) { + if (getInputWaitRepeat == BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + + if (getInputWaitRepeat() == BTN_UP){ + lcdSetCrsr(50, 50); + countUp ++; + lcdPrintInt(countUp); + lcdDisplay(); + } + + } + +} diff --git a/testapp/main.c b/testapp/main.c index 38563df5..a955fc21 100644 --- a/testapp/main.c +++ b/testapp/main.c @@ -19,8 +19,14 @@ #include "main.gen" +#define EVERY(x,y) if((ctr+y)%(x/SYSTICKSPEED)==0) void sys_tick_handler(void){ + static int ctr; + ctr++; incTimer(); + EVERY(10,0){ + inputDebounce(); + }; generated_tick(); }; From f9c8628a1755154ada5aecef1e624f19e52023bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=A5gen?= <johannes@wagen.no> Date: Mon, 21 Sep 2015 01:16:09 +0200 Subject: [PATCH 5/7] Added discriptions in keyin.h --- r0ketlib/keyin.h | 13 +++++-------- testapp/key.c | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/r0ketlib/keyin.h b/r0ketlib/keyin.h index 1ad6740f..b604e6ed 100644 --- a/r0ketlib/keyin.h +++ b/r0ketlib/keyin.h @@ -15,15 +15,12 @@ void keySetRotation(char doit); uint8_t getInputRaw(void); //Returns the raw GPIO values uint8_t getInput(void); //Returns debounced values void inputInit(void); -//Waits for input -uint8_t getInputWait(void); -//Waits timeout ms for input -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 until key is relesed uint8_t getInputChange(void); //Returns 1 for 10ms on an edge -//Does the actual debounce, runs in system tick -void inputDebounce(void); +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/testapp/key.c b/testapp/key.c index 9d1f7d43..6f191e77 100644 --- a/testapp/key.c +++ b/testapp/key.c @@ -145,13 +145,16 @@ void keyTestWaitRepeat(void) { lcdPrint("Up"); lcdSetCrsr(0, 60); lcdPrint("Enter"); + lcdSetCrsr(0, 70); + lcdPrint("Down"); lcdDisplay(); - int countEnter; - int countUp; + int countEnter = 0; + int countUp = 0; + int countDown = 0; while( getInputRaw() != BTN_LEFT) { - if (getInputWaitRepeat == BTN_ENTER){ + if (getInputWaitRepeat() == BTN_ENTER){ lcdSetCrsr(50, 60); countEnter ++; lcdPrintInt(countEnter); @@ -165,6 +168,13 @@ void keyTestWaitRepeat(void) { lcdDisplay(); } + if (getInputWaitRepeat() == BTN_DOWN){ + lcdSetCrsr(50, 70); + countDown ++; + lcdPrintInt(countDown); + lcdDisplay(); + } + } } From ea3454334a75585a13acb722d3a8bf9156a99e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=A5gen?= <johannes@wagen.no> Date: Mon, 21 Sep 2015 02:13:43 +0200 Subject: [PATCH 6/7] Clean up of test app --- r0ketlib/keyin.h | 2 +- testapp/key.c | 81 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/r0ketlib/keyin.h b/r0ketlib/keyin.h index b604e6ed..a7be4479 100644 --- a/r0ketlib/keyin.h +++ b/r0ketlib/keyin.h @@ -18,7 +18,7 @@ void inputInit(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 until key is relesed +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 diff --git a/testapp/key.c b/testapp/key.c index 6f191e77..ca1c4639 100644 --- a/testapp/key.c +++ b/testapp/key.c @@ -22,20 +22,28 @@ //# MENU key keyInputRaw void keyTestRaw(void) { lcdClear(); - lcdPrintln("Press enter to increse count"); - lcdPrintln("with getInputRaw()"); - lcdPrintln("exit with left"); + 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 ++; @@ -43,10 +51,10 @@ void keyTestRaw(void) { lcdDisplay(); } - if (getInputRaw() == BTN_UP){ - lcdSetCrsr(50, 50); - countUp ++; - lcdPrintInt(countUp); + if (getInputRaw() == BTN_DOWN){ + lcdSetCrsr(50, 70); + countDown ++; + lcdPrintInt(countDown); lcdDisplay(); } @@ -58,24 +66,33 @@ void keyTestRaw(void) { //Counts on rising edge of keyInputRaw void keyTestRawEdge(void) { lcdClear(); - lcdPrintln("Press enter to increse count"); - lcdPrintln("with getInputRaw()"); - lcdPrintln("exit with left"); + 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 ++; @@ -83,10 +100,10 @@ void keyTestRawEdge(void) { lcdDisplay(); } - if (currentInput == BTN_UP && lastInput != BTN_UP){ + if (currentInput == BTN_DOWN && lastInput != BTN_DOWN){ lcdSetCrsr(50, 50); - countUp ++; - lcdPrintInt(countUp); + countDown ++; + lcdPrintInt(countDown); lcdDisplay(); } lastInput = currentInput; @@ -100,16 +117,19 @@ void keyTestDebounce(void) { lcdClear(); lcdPrintln("Press enter to increse count"); lcdPrintln("with getInput"); - lcdPrintln("exit with left"); + 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; @@ -117,19 +137,25 @@ void keyTestDebounce(void) { 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_UP && changeInput == BTN_UP){ - lcdSetCrsr(50, 50); - countUp ++; - lcdPrintInt(countUp); + if (currentInput == BTN_DOWN && changeInput == BTN_DOWN){ + lcdSetCrsr(50, 70); + countDown ++; + lcdPrintInt(countDown); lcdDisplay(); } + } } @@ -139,7 +165,7 @@ void keyTestWaitRepeat(void) { lcdClear(); lcdPrintln("Press enter to increse count"); lcdPrintln("with getInputWaitRepeat()"); - lcdPrintln("exit with left"); + lcdPrintln("Press left to exit"); lcdNl(); lcdSetCrsr(0, 50); lcdPrint("Up"); @@ -154,13 +180,6 @@ void keyTestWaitRepeat(void) { int countDown = 0; while( getInputRaw() != BTN_LEFT) { - if (getInputWaitRepeat() == BTN_ENTER){ - lcdSetCrsr(50, 60); - countEnter ++; - lcdPrintInt(countEnter); - lcdDisplay(); - } - if (getInputWaitRepeat() == BTN_UP){ lcdSetCrsr(50, 50); countUp ++; @@ -168,6 +187,13 @@ void keyTestWaitRepeat(void) { lcdDisplay(); } + if (getInputWaitRepeat() == BTN_ENTER){ + lcdSetCrsr(50, 60); + countEnter ++; + lcdPrintInt(countEnter); + lcdDisplay(); + } + if (getInputWaitRepeat() == BTN_DOWN){ lcdSetCrsr(50, 70); countDown ++; @@ -178,3 +204,4 @@ void keyTestWaitRepeat(void) { } } + From aa01b87f94d6e527e20bb64d4284e1802e1d9479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=A5gen?= <johannes@wagen.no> Date: Mon, 21 Sep 2015 02:23:20 +0200 Subject: [PATCH 7/7] Cleaned up debounce in system tick --- ccccmaze/main.c | 10 ++++++++++ testapp/main.c | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) 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/testapp/main.c b/testapp/main.c index a955fc21..8d764a8e 100644 --- a/testapp/main.c +++ b/testapp/main.c @@ -20,13 +20,17 @@ #include "main.gen" #define EVERY(x,y) if((ctr+y)%(x/SYSTICKSPEED)==0) -void sys_tick_handler(void){ +void debounce_tick(){ static int ctr; ctr++; - incTimer(); EVERY(10,0){ inputDebounce(); }; + +} +void sys_tick_handler(void){ + incTimer(); + debounce_tick(); generated_tick(); };