Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce #122

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions campapp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ void night_tick(void){
}
};
};
//Debounce rutine runs every 10 systick
EVERY(10,0){
inputDebounce();
};

return;
}
Expand Down
10 changes: 10 additions & 0 deletions ccccmaze/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down
2 changes: 2 additions & 0 deletions l0dables/EXPORTS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ getInput
getInputRaw
getInputWait
getInputWaitRelease
getInputChange
inputDebounce
gpio_clear
gpio_set
gpio_toggle
Expand Down
32 changes: 22 additions & 10 deletions r0ketlib/keyin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;
}

14 changes: 8 additions & 6 deletions r0ketlib/keyin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions rfapp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ void tick_batteryLED(void){
};
};

//Debounce inputs
EVERY(10,0){
inputDebounce();
};
return;
}

Expand Down
207 changes: 207 additions & 0 deletions testapp/key.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#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("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();
}

}

}

10 changes: 10 additions & 0 deletions testapp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down