From c8494e1042cc779b2b44e6acebe110526ce8b1a5 Mon Sep 17 00:00:00 2001 From: Evan Sailer Date: Wed, 24 May 2017 19:45:01 -0500 Subject: [PATCH] flipped matrix --- keyboard/bananasplit/matrix.c | 193 +++++++++++++++++++++++----------- 1 file changed, 133 insertions(+), 60 deletions(-) diff --git a/keyboard/bananasplit/matrix.c b/keyboard/bananasplit/matrix.c index ac8aa2e4..4060bffb 100644 --- a/keyboard/bananasplit/matrix.c +++ b/keyboard/bananasplit/matrix.c @@ -37,6 +37,9 @@ static uint8_t debouncing = DEBOUNCE; static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t matrix_debouncing[MATRIX_ROWS]; +static matrix_row_t matrix_reversed[MATRIX_COLS]; +static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS]; + static matrix_row_t read_cols(void); static void init_cols(void); static void unselect_rows(void); @@ -74,12 +77,12 @@ void matrix_init(void) uint8_t matrix_scan(void) { - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + for (uint8_t i = 0; i < MATRIX_COLS; 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; + 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"); } @@ -92,12 +95,20 @@ uint8_t matrix_scan(void) if (--debouncing) { _delay_ms(1); } else { - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - matrix[i] = matrix_debouncing[i]; + 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) << x; + } + matrix[y] = row; + } + return 1; } @@ -129,15 +140,6 @@ void matrix_print(void) } } -uint8_t matrix_key_count(void) -{ - uint8_t count = 0; - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - count += bitpop16(matrix[i]); - } - return count; -} - /* Column pin configuration * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.A) @@ -146,32 +148,39 @@ uint8_t matrix_key_count(void) static void init_cols(void) { // Input with pull-up(DDR:0, PORT:1) - DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5); - PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5); - DDRD &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7); - PORTD |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7); - DDRB &= ~(1<<1 | 1<<3); - PORTB |= (1<<1 | 1<<3); + // DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5); + // PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5); + // DDRD &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7); + // PORTD |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7); + // DDRB &= ~(1<<1 | 1<<3); + // PORTB |= (1<<1 | 1<<3); + DDRB &= ~(1<<0 | 1<<2 | 1<<4 | 1<<5 | 1<<6); + PORTB |= (1<<0 | 1<<2 | 1<<4 | 1<<5 | 1<<6); } static matrix_row_t read_cols(void) { - return (PINF&(1<<5) ? 0 : (1<<0)) | - (PINB&(1<<1) ? 0 : (1<<1)) | - (PINF&(1<<0) ? 0 : (1<<2)) | - (PINF&(1<<1) ? 0 : (1<<3)) | - (PINF&(1<<4) ? 0 : (1<<4)) | - (PINB&(1<<3) ? 0 : (1<<5)) | - (PIND&(1<<7) ? 0 : (1<<6)) | - (PIND&(1<<6) ? 0 : (1<<7)) | - (PIND&(1<<4) ? 0 : (1<<8)) | - (PIND&(1<<5) ? 0 : (1<<9)) | - (PIND&(1<<3) ? 0 : (1<<10)) | - (PIND&(1<<2) ? 0 : (1<<11)) | - (PIND&(1<<1) ? 0 : (1<<12)) | - (PIND&(1<<0) ? 0 : (1<<13)); + // return (PINF&(1<<5) ? 0 : (1<<0)) | + // (PINB&(1<<1) ? 0 : (1<<1)) | + // (PINF&(1<<0) ? 0 : (1<<2)) | + // (PINF&(1<<1) ? 0 : (1<<3)) | + // (PINF&(1<<4) ? 0 : (1<<4)) | + // (PINB&(1<<3) ? 0 : (1<<5)) | + // (PIND&(1<<7) ? 0 : (1<<6)) | + // (PIND&(1<<6) ? 0 : (1<<7)) | + // (PIND&(1<<4) ? 0 : (1<<8)) | + // (PIND&(1<<5) ? 0 : (1<<9)) | + // (PIND&(1<<3) ? 0 : (1<<10)) | + // (PIND&(1<<2) ? 0 : (1<<11)) | + // (PIND&(1<<1) ? 0 : (1<<12)) | + // (PIND&(1<<0) ? 0 : (1<<13)); + return (PINB&(1<<0) ? 0 : (1<<0)) | + (PINB&(1<<2) ? 0 : (1<<1)) | + (PINB&(1<<4) ? 0 : (1<<2)) | + (PINB&(1<<5) ? 0 : (1<<3)) | + (PINB&(1<<6) ? 0 : (1<<4)); } /* Row pin configuration @@ -180,35 +189,99 @@ uint8_t matrix_key_count(void) */ static void unselect_rows(void) { - // Hi-Z(DDR:0, PORT:0) to unselect - DDRB &= ~0b01110101; - PORTB &= ~0b01110101; + // // Hi-Z(DDR:0, PORT:0) to unselect + // DDRB &= ~0b01110101; + // PORTB &= ~0b01110101; + DDRB &= ~0b00001010; + PORTB &= ~0b00001010; + DDRD &= ~0b11111111; + PORTD &= ~0b11111111; + DDRF &= ~0b00110011; + PORTF &= ~0b00110011; } static void select_row(uint8_t row) { // Output low(DDR:1, PORT:0) to select - switch (row) { - case 0: - DDRB |= (1<<0); - PORTB &= ~(1<<0); - break; - case 1: - DDRB |= (1<<2); - PORTB &= ~(1<<2); - break; - case 2: - DDRB |= (1<<4); - PORTB &= ~(1<<4); - break; - case 3: - DDRB |= (1<<5); - PORTB &= ~(1<<5); - break; - case 4: - DDRB |= (1<<6); - PORTB &= ~(1<<6); - break; - } + // switch (row) { + // case 0: + // DDRB |= (1<<0); + // PORTB &= ~(1<<0); + // break; + // case 1: + // DDRB |= (1<<2); + // PORTB &= ~(1<<2); + // break; + // case 2: + // DDRB |= (1<<4); + // PORTB &= ~(1<<4); + // break; + // case 3: + // DDRB |= (1<<5); + // PORTB &= ~(1<<5); + // break; + // case 4: + // DDRB |= (1<<6); + // PORTB &= ~(1<<6); + // break; + // } + + switch (row) { + case 0: + DDRF |= (1<<5); + PORTF &= ~(1<<5); + break; + case 1: + DDRB |= (1<<1); + PORTB &= ~(1<<1); + break; + case 2: + DDRF |= (1<<0); + PORTF &= ~(1<<0); + break; + case 3: + DDRF |= (1<<1); + PORTF &= ~(1<<1); + break; + case 4: + DDRF |= (1<<4); + PORTF &= ~(1<<4); + break; + case 5: + DDRB |= (1<<3); + PORTB &= ~(1<<3); + break; + case 6: + DDRD |= (1<<7); + PORTD &= ~(1<<7); + break; + case 7: + DDRD |= (1<<6); + PORTD &= ~(1<<6); + break; + case 8: + DDRD |= (1<<4); + PORTD &= ~(1<<4); + break; + case 9: + DDRD |= (1<<5); + PORTD &= ~(1<<5); + break; + case 10: + DDRD |= (1<<3); + PORTD &= ~(1<<3); + break; + case 11: + DDRD |= (1<<2); + PORTD &= ~(1<<2); + break; + case 12: + DDRD |= (1<<1); + PORTD &= ~(1<<1); + break; + case 13: + DDRD |= (1<<0); + PORTD &= ~(1<<0); + } } -- 2.39.3