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_API #531

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
1 change: 1 addition & 0 deletions keyboard/alps64/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ MOUSEKEY_ENABLE ?= yes # Mouse keys(+4700)
EXTRAKEY_ENABLE ?= yes # Audio control and System control(+450)
CONSOLE_ENABLE ?= yes # Console for debug(+400)
COMMAND_ENABLE ?= yes # Commands for debug and configuration
DEBOUNCE_HASU ?= yes # Default debounce algorithm
#SLEEP_LED_ENABLE ?= yes # Breathing sleep LED during USB suspend
#NKRO_ENABLE ?= yes # USB Nkey Rollover
#ACTIONMAP_ENABLE ?= yes # Use 16bit action codes in keymap instead of 8bit keycodes
Expand Down
26 changes: 1 addition & 25 deletions keyboard/alps64/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "matrix.h"


#ifndef DEBOUNCE
# define DEBOUNCE 5
#endif
static uint8_t debouncing = DEBOUNCE;

/* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];

static matrix_row_t read_cols(void);
static void init_cols(void);
Expand All @@ -56,7 +50,6 @@ void matrix_init(void)
// initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
matrix[i] = 0;
matrix_debouncing[i] = 0;
}

//debug
Expand All @@ -71,27 +64,10 @@ uint8_t matrix_scan(void)
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
select_row(i);
_delay_us(30); // without this wait read unstable value.
matrix_row_t cols = read_cols();
if (matrix_debouncing[i] != cols) {
matrix_debouncing[i] = cols;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCE;
}
matrix[i] = read_cols();
unselect_rows();
}

if (debouncing) {
if (--debouncing) {
_delay_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
matrix[i] = matrix_debouncing[i];
}
}
}

return 1;
}

Expand Down
1 change: 1 addition & 0 deletions keyboard/fc660c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ EXTRAKEY_ENABLE ?= yes # Audio control and System control
CONSOLE_ENABLE ?= yes # Console for debug
COMMAND_ENABLE ?= yes # Commands for debug and configuration
NKRO_ENABLE ?= yes # USB Nkey Rollover
DEBOUNCE_HASU ?= yes # Default debounce algorithm
#ACTIONMAP_ENABLE ?= yes # Use 16bit actionmap instead of 8bit keymap
UNIMAP_ENABLE ?= yes # Universal keymap
KEYMAP_SECTION_ENABLE ?= yes # fixed address keymap for keymap editor
Expand Down
1 change: 1 addition & 0 deletions keyboard/gh60/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ CONSOLE_ENABLE = yes # Console for debug(+400)
COMMAND_ENABLE = yes # Commands for debug and configuration
#SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend
NKRO_ENABLE = yes # USB Nkey Rollover
DEBOUNCE_HASU = yes # use default debounce algorithm.


# Optimize size but this may cause error "relocation truncated to fit"
Expand Down
1 change: 1 addition & 0 deletions keyboard/gh60/Makefile.pjrc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ CONSOLE_ENABLE = yes # Console for debug
COMMAND_ENABLE = yes # Commands for debug and configuration
SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend
NKRO_ENABLE = yes # USB Nkey Rollover(+500)
DEBOUNCE_HASU = yes # Default debounce algorithm
#PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support


Expand Down
28 changes: 1 addition & 27 deletions keyboard/gh60/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "matrix.h"


#ifndef DEBOUNCE
# define DEBOUNCE 5
#endif
static uint8_t debouncing = DEBOUNCE;

/* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];

static matrix_row_t read_cols(void);
static void init_cols(void);
static void unselect_rows(void);
static void select_row(uint8_t row);


void matrix_init(void)
{
// initialize row and col
Expand All @@ -52,7 +45,6 @@ void matrix_init(void)
// initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
matrix[i] = 0;
matrix_debouncing[i] = 0;
}
}

Expand All @@ -61,27 +53,9 @@ uint8_t matrix_scan(void)
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
select_row(i);
_delay_us(30); // without this wait read unstable value.
matrix_row_t cols = read_cols();
if (matrix_debouncing[i] != cols) {
matrix_debouncing[i] = cols;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCE;
}
matrix[i] = read_cols();
unselect_rows();
}

if (debouncing) {
if (--debouncing) {
_delay_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
matrix[i] = matrix_debouncing[i];
}
}
}

return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion keyboard/hbkb/Makefile.lufa → keyboard/hbkb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ MOUSEKEY_ENABLE = yes # Mouse keys
EXTRAKEY_ENABLE = yes # Audio control and System control
CONSOLE_ENABLE = yes # Console for debug
COMMAND_ENABLE = yes # Commands for debug and configuration

DEBOUNCE_HASU = yes # default debounce algorithm

# Boot Section Size in bytes
# Teensy halfKay 512
Expand Down
2 changes: 1 addition & 1 deletion keyboard/hbkb/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define MATRIX_HAS_GHOST

/* Set 0 if need no debouncing */
#define DEBOUNCE 5
#define DEBOUNCE 10

/* legacy keymap support */
#define USE_LEGACY_KEYMAP
Expand Down
25 changes: 1 addition & 24 deletions keyboard/hbkb/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
* COL: PD0-7
* ROW: PB0-7, PF4-7
*/
#ifndef DEBOUNCE
# define DEBOUNCE 10
#endif
static uint8_t debouncing = DEBOUNCE;

/* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];

static matrix_row_t read_cols(void);
static void unselect_rows(void);
Expand All @@ -62,7 +57,6 @@ void matrix_init(void)
// initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
matrix[i] = 0;
matrix_debouncing[i] = 0;
}
}

Expand All @@ -71,27 +65,10 @@ uint8_t matrix_scan(void)
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
select_row(i);
_delay_us(30); // without this wait read unstable value.
matrix_row_t cols = read_cols();
if (matrix_debouncing[i] != cols) {
matrix_debouncing[i] = cols;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCE;
}
matrix[i] = read_cols();
unselect_rows();
}

if (debouncing) {
if (--debouncing) {
_delay_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
matrix[i] = matrix_debouncing[i];
}
}
}

return 1;
}

Expand Down
10 changes: 10 additions & 0 deletions tmk_core/common.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
COMMON_DIR = common
DEBOUNCE = $(COMMON_DIR)/debounce

SRC += $(COMMON_DIR)/host.c \
$(COMMON_DIR)/debounce.c \
$(COMMON_DIR)/keyboard.c \
$(COMMON_DIR)/matrix.c \
$(COMMON_DIR)/action.c \
Expand All @@ -18,6 +21,12 @@ SRC += $(COMMON_DIR)/host.c \


# Option modules

# Debounce - if it's implemented in matrix.c, choose debounce_none
ifeq (yes,$(strip $(DEBOUNCE_HASU)))
SRC += $(DEBOUNCE)/debounce_hasu.c
endif

ifeq (yes,$(strip $(UNIMAP_ENABLE)))
SRC += $(COMMON_DIR)/unimap.c
OPT_DEFS += -DUNIMAP_ENABLE
Expand Down Expand Up @@ -94,6 +103,7 @@ ifeq (yes,$(strip $(KEYMAP_SECTION_ENABLE)))
endif
endif


# Version string
VERSION := $(shell (git describe --always --dirty || echo 'unknown') 2> /dev/null)
OPT_DEFS += -DVERSION=$(VERSION)
Expand Down
26 changes: 26 additions & 0 deletions tmk_core/common/debounce.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2017 Alex Ong<[email protected]>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "debounce.h"
#include "matrix.h"

//Default implementation - no debouncing
__attribute__((weak)) void matrix_debounce(void) {}
__attribute__((weak)) matrix_row_t matrix_debounce_get_row(uint8_t row)
{
return matrix_get_row(row);
}
21 changes: 21 additions & 0 deletions tmk_core/common/debounce.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef DEBOUNCE_H
#define DEBOUNCE_H

#include <stdbool.h>
#include <stdint.h>
#include "matrix.h"

#ifdef __cplusplus
extern "C" {
#endif
/* call this every keyboard_task to debounce the matrix*/
void matrix_debounce(void);

/* matrix state on row */
matrix_row_t matrix_debounce_get_row(uint8_t row);

#ifdef __cplusplus
}
#endif

#endif
41 changes: 41 additions & 0 deletions tmk_core/common/debounce/debounce_hasu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Basic global debounce algorithm.
When no state changes have occured for DEBOUNCE milliseconds, we push the state.
*/

#include "debounce.h"
#include "matrix.h"
#include "timer.h"

#ifndef DEBOUNCE
#define DEBOUNCE 5
#endif

//Default implementation - no debouncing
static matrix_row_t matrix_debounced[MATRIX_ROWS];
static bool debouncing = false;
static uint16_t debouncing_time;

void matrix_debounce(void) {
for (uint8_t r = 0; r < MATRIX_ROWS; r++)
{
matrix_row_t raw = matrix_get_row(r);
if (raw != matrix_debounced[r])
{
debouncing = true;
debouncing_time = timer_read();
}
}

if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
for (int i = 0; i < MATRIX_ROWS; i++) {
matrix_debounced[i] = matrix_get_row(i);
}
debouncing = false;
}
}

matrix_row_t matrix_debounce_get_row(uint8_t row)
{
return matrix_debounced[row];
}
Loading