rows to col option, maybe

This commit is contained in:
Jack Humbert 2015-09-13 22:10:01 -04:00
parent b89a9cdc91
commit 5bb7ef0012
3 changed files with 69 additions and 2 deletions

View File

@ -36,6 +36,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define COLS (int []){ F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7 }
#define ROWS (int []){ D0, D5, B5, B6 }
/* COL2ROW or ROW2COL */
#define DIODE_DIRECTION COL2ROW
/* define if matrix has ghost */
//#define MATRIX_HAS_GHOST

View File

@ -42,7 +42,8 @@
#define F6 0x66
#define F7 0x67
#define COL2ROW 0x0
#define ROW2COL 0x1

View File

@ -37,6 +37,11 @@ static uint8_t debouncing = DEBOUNCE;
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];
#if DIODE_DIRECTION == ROW2COL
static matrix_row_t matrix_reversed[MATRIX_COLS];
static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
#endif
static matrix_row_t read_cols(void);
static void init_cols(void);
static void unselect_rows(void);
@ -80,6 +85,7 @@ void matrix_init(void)
uint8_t matrix_scan(void)
{
#if DIODE_DIRECTION == COL2ROW
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
select_row(i);
_delay_us(30); // without this wait read unstable value.
@ -103,6 +109,38 @@ uint8_t matrix_scan(void)
}
}
}
#else
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
select_row(i);
_delay_us(30); // without this wait read unstable value.
matrix_row_t rows = read_cols();
if (matrix_reversed_debouncing[i] != rows) {
matrix_reversed_debouncing[i] = rows;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCE;
}
unselect_rows();
}
if (debouncing) {
if (--debouncing) {
_delay_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
matrix_reversed[i] = matrix_reversed_debouncing[i];
}
}
}
for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
matrix_row_t row = 0;
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
}
matrix[y] = row;
}
#endif
return 1;
}
@ -147,8 +185,14 @@ uint8_t matrix_key_count(void)
static void init_cols(void)
{
int B = 0, C = 0, D = 0, E = 0, F = 0;
#if DIODE_DIRECTION == COL2ROW
for(int x = 0; x < MATRIX_COLS; x++) {
int col = COLS[x];
#else
for(int x = 0; x < MATRIX_ROWS; x++) {
int col = ROWS[x];
#endif
if ((col & 0xF0) == 0x20) {
B |= (1<<(col & 0x0F));
} else if ((col & 0xF0) == 0x30) {
@ -171,8 +215,15 @@ static void init_cols(void)
static matrix_row_t read_cols(void)
{
matrix_row_t result = 0;
#if DIODE_DIRECTION == COL2ROW
for(int x = 0; x < MATRIX_COLS; x++) {
int col = COLS[x];
#else
for(int x = 0; x < MATRIX_ROWS; x++) {
int col = ROWS[x];
#endif
if ((col & 0xF0) == 0x20) {
result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
} else if ((col & 0xF0) == 0x30) {
@ -191,8 +242,14 @@ static matrix_row_t read_cols(void)
static void unselect_rows(void)
{
int B = 0, C = 0, D = 0, E = 0, F = 0;
#if DIODE_DIRECTION == COL2ROW
for(int x = 0; x < MATRIX_ROWS; x++) {
int row = ROWS[x];
#else
for(int x = 0; x < MATRIX_COLS; x++) {
int row = COLS[x];
#endif
if ((row & 0xF0) == 0x20) {
B |= (1<<(row & 0x0F));
} else if ((row & 0xF0) == 0x30) {
@ -214,7 +271,13 @@ static void unselect_rows(void)
static void select_row(uint8_t row)
{
#if DIODE_DIRECTION == COL2ROW
int row_pin = ROWS[row];
#else
int row_pin = COLS[row];
#endif
if ((row_pin & 0xF0) == 0x20) {
DDRB |= (1<<(row_pin & 0x0F));
PORTB &= ~(1<<(row_pin & 0x0F));