Skip to content

Commit 4f99459

Browse files
committed
Added debouncing
1 parent 8292f25 commit 4f99459

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Please refer to the [MK32 Wiki page](https://github.com/Galzai/MK32/wiki) for in
1818

1919
## To-do:
2020
- Plugin API support (in progress).
21-
- Proper debouncing (Temporerly using delays).
2221
- More than 2 pads (currently supports 2 pads).
2322
- Connection switching.
2423
- Modify keymap via webserver (in progress) .

components/r_encoder/r_encoder.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,9 @@ void r_encoder_setup(void){
164164
#endif
165165

166166
}
167-
168167
//Check encoder state, currently defined for Vol +/= and mute
169168
uint8_t r_encoder_state(void){
170-
uint8_t EncoderState=0x00;
169+
uint8_t EncoderState = 0x00;
171170
int16_t EncoderCount;
172171
pcnt_get_counter_value(PCNT_UNIT_0, &EncoderCount);
173172
if(EncoderCount>PastEncoderCount){

main/keyboard_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define SPLIT_MASTER // undefine if keyboard is not split and master
2323
//#define SLAVE // undefine if keyboard is master
2424

25+
#define DEBOUNCE 5 //debounce time in ms
26+
2527
//Define matrix
2628
#define KEYPADS 2 // intended in order to create a Multiple keypad split boards
2729
#define MATRIX_ROWS 4

main/matrix.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ const gpio_num_t MATRIX_COLS_PINS[]={GPIO_NUM_13,GPIO_NUM_14,GPIO_NUM_15,GPIO_NU
3737

3838
// matrix states
3939
uint8_t MATRIX_STATE[MATRIX_ROWS][MATRIX_COLS]={0};
40+
uint8_t PREV_MATRIX_STATE[MATRIX_ROWS][MATRIX_COLS]={0};
4041
uint8_t SLAVE_MATRIX_STATE[MATRIX_ROWS][MATRIX_COLS]={0};
4142

43+
44+
uint32_t lastDebounceTime = 0;
45+
4246
uint8_t (*matrix_states[])[MATRIX_ROWS][MATRIX_COLS]={
4347
&MATRIX_STATE,
4448
&SLAVE_MATRIX_STATE,
4549
};
4650

51+
//used for debouncing
52+
static uint32_t millis() {
53+
return esp_timer_get_time() / 1000;
54+
}
55+
4756
// deinitializing rtc matrix pins on deep sleep wake up
4857
void rtc_matrix_deinit(void){
4958

@@ -147,7 +156,8 @@ void matrix_setup(void){
147156
#endif
148157
}
149158

150-
159+
uint8_t curState = 0;
160+
uint32_t DEBOUNCE_MATRIX[MATRIX_ROWS][MATRIX_COLS]={0};
151161
// Scanning the matrix for input
152162
void scan_matrix(void){
153163
#ifdef COL2ROW
@@ -156,11 +166,20 @@ void scan_matrix(void){
156166
gpio_set_level(MATRIX_COLS_PINS[col], 1);
157167
for(uint8_t row=0; row <MATRIX_ROWS; row++){
158168

159-
MATRIX_STATE[row][col]=gpio_get_level(MATRIX_ROWS_PINS[row]) ;
160-
}
161-
gpio_set_level(MATRIX_COLS_PINS[col], 0);
169+
curState = gpio_get_level(MATRIX_ROWS_PINS[row]) ;
170+
if( PREV_MATRIX_STATE[row][col] != curState){
171+
DEBOUNCE_MATRIX[row][col] = millis();
172+
}
173+
PREV_MATRIX_STATE[row][col] = curState;
174+
if( (millis() - DEBOUNCE_MATRIX[row][col]) > DEBOUNCE){
162175

176+
if( MATRIX_STATE[row][col] != curState){
177+
MATRIX_STATE[row][col] = curState ;
178+
}
163179

180+
}
181+
}
182+
gpio_set_level(MATRIX_COLS_PINS[col], 0);
164183
}
165184

166185

@@ -171,13 +190,26 @@ void scan_matrix(void){
171190
gpio_set_level(MATRIX_ROWS_PINS[row], 0);
172191

173192
for(uint8_t col=0; col <MATRIX_COLS; col++){
174-
MATRIX_STATE[row][col]=gpio_get_level(MATRIX_COLS_PINS[col]) ;
193+
194+
curState = gpio_get_level(MATRIX_ROWS_PINS[row]) ;
195+
if( PREV_MATRIX_STATE[row][col] != curState){
196+
DEBOUNCE_MATRIX[row][col] = millis();
197+
}
198+
PREV_MATRIX_STATE[row][col] = curState;
199+
if( (millis() - DEBOUNCE_MATRIX[row][col]) > DEBOUNCE){
200+
201+
if( MATRIX_STATE[row][col] != curState){
202+
MATRIX_STATE[row][col] = curState ;
203+
}
204+
205+
}
175206
}
176207
gpio_set_level(MATRIX_ROWS_PINS[row], 0);
177208
}
178209
#endif
179210

180211

212+
181213
}
182214

183215
#endif

main/mk32_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ extern "C" void key_reports(void *pvParameters)
123123
DEEP_SLEEP = false;
124124
memcpy(past_report,report_state, sizeof past_report );
125125
xQueueSend(keyboard_q,(void*)&report_state, (TickType_t) 0);
126-
vTaskDelay(15/portTICK_PERIOD_MS);
126+
//vTaskDelay(5/portTICK_PERIOD_MS);
127127
}
128128

129129
}
@@ -174,7 +174,7 @@ extern "C" void slave_scan(void *pvParameters){
174174
memcpy(&PAST_MATRIX, &MATRIX_STATE, sizeof MATRIX_STATE );
175175

176176
xQueueSend(espnow_matrix_send_q,(void*)&MATRIX_STATE, (TickType_t) 0);
177-
vTaskDelay(15/portTICK_PERIOD_MS);
177+
//vTaskDelay(5/portTICK_PERIOD_MS);
178178
}
179179
}
180180
}

0 commit comments

Comments
 (0)