Browse Source

add debouncing into macway/matrix.c.

tags/v1.9
tmk 12 years ago
parent
commit
6284b147c2
2 changed files with 28 additions and 7 deletions
  1. 2
    0
      macway/config.h
  2. 26
    7
      macway/matrix.c

+ 2
- 0
macway/config.h View File

#define MATRIX_COLS 8 #define MATRIX_COLS 8
/* define if matrix has ghost */ /* define if matrix has ghost */
#define MATRIX_HAS_GHOST #define MATRIX_HAS_GHOST
/* Set 0 if need no debouncing */
#define DEBOUNCE 5




/* key combination for command */ /* key combination for command */

+ 26
- 7
macway/matrix.c View File

#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include "print.h" #include "print.h"
#include "debug.h"
#include "util.h" #include "util.h"
#include "matrix.h" #include "matrix.h"


#endif #endif




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

// matrix state buffer(1:on, 0:off) // matrix state buffer(1:on, 0:off)
#if (MATRIX_COLS <= 8) #if (MATRIX_COLS <= 8)
static uint8_t *matrix; static uint8_t *matrix;


uint8_t matrix_scan(void) uint8_t matrix_scan(void)
{ {
uint8_t *tmp;
tmp = matrix_prev;
matrix_prev = matrix;
matrix = tmp;
if (!debouncing) {
uint8_t *tmp = matrix_prev;
matrix_prev = matrix;
matrix = tmp;
}


for (uint8_t i = 0; i < MATRIX_ROWS; i++) { for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
unselect_rows(); unselect_rows();
select_row(i); select_row(i);
_delay_us(30); // without this wait read unstable value. _delay_us(30); // without this wait read unstable value.
matrix[i] = ~read_col();
if (matrix[i] != (uint8_t)~read_col()) {
matrix[i] = (uint8_t)~read_col();
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); print("\n");
}
debouncing = DEBOUNCE;
}
} }
unselect_rows(); unselect_rows();

if (debouncing) {
debouncing--;
}

return 1; return 1;
} }


bool matrix_is_modified(void) bool matrix_is_modified(void)
{ {
if (debouncing) return false;
for (uint8_t i = 0; i < MATRIX_ROWS; i++) { for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
if (matrix[i] != matrix_prev[i])
if (matrix[i] != matrix_prev[i]) {
return true; return true;
}
} }
return false; return false;
} }

Loading…
Cancel
Save