diff options
| author | Mathias Andersson <wraul@dbox.se> | 2013-04-28 20:03:12 +0200 |
|---|---|---|
| committer | Mathias Andersson <wraul@dbox.se> | 2013-05-01 15:33:48 +0200 |
| commit | 2d31fcf187ab3a334d39d55c622d8b5bd75a487e (patch) | |
| tree | f69d20c86ae439b4438650c3ac3bd11ee5f7bc59 | |
| parent | 71fabf72e04bbcc3d3d758bb1d20f95804edefc8 (diff) | |
| download | qmk_firmware-2d31fcf187ab3a334d39d55c622d8b5bd75a487e.tar.gz qmk_firmware-2d31fcf187ab3a334d39d55c622d8b5bd75a487e.zip | |
Change Phantom matrix scan to be similar to GH60.
| -rw-r--r-- | keyboard/phantom/matrix.c | 219 |
1 files changed, 125 insertions, 94 deletions
diff --git a/keyboard/phantom/matrix.c b/keyboard/phantom/matrix.c index 92db6817c..e9606ef5d 100644 --- a/keyboard/phantom/matrix.c +++ b/keyboard/phantom/matrix.c | |||
| @@ -27,91 +27,10 @@ static uint8_t debouncing = DEBOUNCE; | |||
| 27 | static matrix_row_t matrix[MATRIX_ROWS]; | 27 | static matrix_row_t matrix[MATRIX_ROWS]; |
| 28 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | 28 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; |
| 29 | 29 | ||
| 30 | 30 | static uint8_t read_rows(void); | |
| 31 | #define _DDRA (uint8_t *const)&DDRA | 31 | static void init_rows(void); |
| 32 | #define _DDRB (uint8_t *const)&DDRB | 32 | static void unselect_cols(void); |
| 33 | #define _DDRC (uint8_t *const)&DDRC | 33 | static void select_col(uint8_t col); |
| 34 | #define _DDRD (uint8_t *const)&DDRD | ||
| 35 | #define _DDRE (uint8_t *const)&DDRE | ||
| 36 | #define _DDRF (uint8_t *const)&DDRF | ||
| 37 | |||
| 38 | #define _PINA (uint8_t *const)&PINA | ||
| 39 | #define _PINB (uint8_t *const)&PINB | ||
| 40 | #define _PINC (uint8_t *const)&PINC | ||
| 41 | #define _PIND (uint8_t *const)&PIND | ||
| 42 | #define _PINE (uint8_t *const)&PINE | ||
| 43 | #define _PINF (uint8_t *const)&PINF | ||
| 44 | |||
| 45 | #define _PORTA (uint8_t *const)&PORTA | ||
| 46 | #define _PORTB (uint8_t *const)&PORTB | ||
| 47 | #define _PORTC (uint8_t *const)&PORTC | ||
| 48 | #define _PORTD (uint8_t *const)&PORTD | ||
| 49 | #define _PORTE (uint8_t *const)&PORTE | ||
| 50 | #define _PORTF (uint8_t *const)&PORTF | ||
| 51 | |||
| 52 | #define _BIT0 0x01 | ||
| 53 | #define _BIT1 0x02 | ||
| 54 | #define _BIT2 0x04 | ||
| 55 | #define _BIT3 0x08 | ||
| 56 | #define _BIT4 0x10 | ||
| 57 | #define _BIT5 0x20 | ||
| 58 | #define _BIT6 0x40 | ||
| 59 | #define _BIT7 0x80 | ||
| 60 | |||
| 61 | /* Specifies the ports and pin numbers for the rows */ | ||
| 62 | static | ||
| 63 | uint8_t *const row_ddr[MATRIX_ROWS] = {_DDRB, _DDRB, _DDRB, _DDRB, _DDRB, _DDRB}; | ||
| 64 | |||
| 65 | static | ||
| 66 | uint8_t *const row_port[MATRIX_ROWS] = {_PORTB, _PORTB, _PORTB, _PORTB, _PORTB, _PORTB}; | ||
| 67 | |||
| 68 | static | ||
| 69 | uint8_t *const row_pin[MATRIX_ROWS] = {_PINB, _PINB, _PINB, _PINB, _PINB, _PINB}; | ||
| 70 | |||
| 71 | static | ||
| 72 | const uint8_t row_bit[MATRIX_ROWS] = { _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5}; | ||
| 73 | |||
| 74 | /* Specifies the ports and pin numbers for the columns */ | ||
| 75 | static | ||
| 76 | uint8_t *const col_ddr[MATRIX_COLS] = { _DDRD, _DDRC, _DDRC, _DDRD, _DDRD, _DDRE, | ||
| 77 | _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, | ||
| 78 | _DDRD, _DDRD, _DDRD, _DDRD, _DDRD}; | ||
| 79 | |||
| 80 | static | ||
| 81 | uint8_t *const col_port[MATRIX_COLS] = {_PORTD, _PORTC, _PORTC, _PORTD, _PORTD, _PORTE, | ||
| 82 | _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, | ||
| 83 | _PORTD, _PORTD, _PORTD, _PORTD, _PORTD}; | ||
| 84 | |||
| 85 | static | ||
| 86 | const uint8_t col_bit[MATRIX_COLS] = { _BIT5, _BIT7, _BIT6, _BIT4, _BIT0, _BIT6, | ||
| 87 | _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7, | ||
| 88 | _BIT7, _BIT6, _BIT1, _BIT2, _BIT3}; | ||
| 89 | |||
| 90 | static | ||
| 91 | inline void pull_column(int col) { | ||
| 92 | *col_port[col] &= ~col_bit[col]; | ||
| 93 | } | ||
| 94 | |||
| 95 | static | ||
| 96 | inline void release_column(int col) { | ||
| 97 | *col_port[col] |= col_bit[col]; | ||
| 98 | } | ||
| 99 | |||
| 100 | /* PORTB is set as input with pull-up resistors | ||
| 101 | PORTC,D,E,F are set to high output */ | ||
| 102 | |||
| 103 | static | ||
| 104 | void setup_io_pins(void) { | ||
| 105 | uint8_t row, col; | ||
| 106 | for(row = 0; row < MATRIX_ROWS; row++) { | ||
| 107 | *row_ddr[row] &= ~row_bit[row]; | ||
| 108 | *row_port[row] |= row_bit[row]; | ||
| 109 | } | ||
| 110 | for(col = 0; col < MATRIX_COLS; col++) { | ||
| 111 | *col_ddr[col] |= col_bit[col]; | ||
| 112 | *col_port[col] |= col_bit[col]; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | 34 | ||
| 116 | /* LEDs are on output compare pins OC1B OC1C | 35 | /* LEDs are on output compare pins OC1B OC1C |
| 117 | This activates fast PWM mode on them. | 36 | This activates fast PWM mode on them. |
| @@ -158,7 +77,8 @@ void matrix_init(void) | |||
| 158 | MCUCR |= (1<<JTD); | 77 | MCUCR |= (1<<JTD); |
| 159 | 78 | ||
| 160 | // initialize row and col | 79 | // initialize row and col |
| 161 | setup_io_pins(); | 80 | unselect_cols(); |
| 81 | init_rows(); | ||
| 162 | setup_leds(); | 82 | setup_leds(); |
| 163 | 83 | ||
| 164 | // initialize matrix state: all keys off | 84 | // initialize matrix state: all keys off |
| @@ -171,11 +91,12 @@ void matrix_init(void) | |||
| 171 | uint8_t matrix_scan(void) | 91 | uint8_t matrix_scan(void) |
| 172 | { | 92 | { |
| 173 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16 | 93 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16 |
| 174 | pull_column(col); // output hi on theline | 94 | select_col(col); |
| 175 | _delay_us(3); // without this wait it won't read stable value. | 95 | _delay_us(3); // without this wait it won't read stable value. |
| 96 | uint8_t rows = read_rows(); | ||
| 176 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5 | 97 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5 |
| 177 | bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col); | 98 | bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col); |
| 178 | bool curr_bit = !(*row_pin[row] & row_bit[row]); | 99 | bool curr_bit = rows & (1<<row); |
| 179 | if (prev_bit != curr_bit) { | 100 | if (prev_bit != curr_bit) { |
| 180 | matrix_debouncing[row] ^= ((matrix_row_t)1<<col); | 101 | matrix_debouncing[row] ^= ((matrix_row_t)1<<col); |
| 181 | if (debouncing) { | 102 | if (debouncing) { |
| @@ -184,7 +105,7 @@ uint8_t matrix_scan(void) | |||
| 184 | debouncing = DEBOUNCE; | 105 | debouncing = DEBOUNCE; |
| 185 | } | 106 | } |
| 186 | } | 107 | } |
| 187 | release_column(col); | 108 | unselect_cols(); |
| 188 | } | 109 | } |
| 189 | 110 | ||
| 190 | if (debouncing) { | 111 | if (debouncing) { |
| @@ -202,7 +123,7 @@ uint8_t matrix_scan(void) | |||
| 202 | 123 | ||
| 203 | bool matrix_is_modified(void) | 124 | bool matrix_is_modified(void) |
| 204 | { | 125 | { |
| 205 | // NOTE: no longer used | 126 | if (debouncing) return false; |
| 206 | return true; | 127 | return true; |
| 207 | } | 128 | } |
| 208 | 129 | ||
| @@ -232,10 +153,120 @@ uint8_t matrix_key_count(void) | |||
| 232 | { | 153 | { |
| 233 | uint8_t count = 0; | 154 | uint8_t count = 0; |
| 234 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 155 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { |
| 235 | for (uint8_t j = 0; j < MATRIX_COLS; j++) { | 156 | count += bitpop32(matrix[i]); |
| 236 | if (matrix_is_on(i, j)) | ||
| 237 | count++; | ||
| 238 | } | ||
| 239 | } | 157 | } |
| 240 | return count; | 158 | return count; |
| 241 | } | 159 | } |
| 160 | |||
| 161 | /* Row pin configuration | ||
| 162 | * row: 0 1 2 3 4 5 | ||
| 163 | * pin: B0 B1 B2 B3 B4 B5 | ||
| 164 | */ | ||
| 165 | static void init_rows(void) | ||
| 166 | { | ||
| 167 | // Input with pull-up(DDR:0, PORT:1) | ||
| 168 | DDRB &= ~0b00111111; | ||
| 169 | PORTB |= 0b00111111; | ||
| 170 | } | ||
| 171 | |||
| 172 | static uint8_t read_rows(void) | ||
| 173 | { | ||
| 174 | return (PINB&(1<<0) ? 0 : (1<<0)) | | ||
| 175 | (PINB&(1<<1) ? 0 : (1<<1)) | | ||
| 176 | (PINB&(1<<2) ? 0 : (1<<2)) | | ||
| 177 | (PINB&(1<<3) ? 0 : (1<<3)) | | ||
| 178 | (PINB&(1<<4) ? 0 : (1<<4)) | | ||
| 179 | (PINB&(1<<5) ? 0 : (1<<5)); | ||
| 180 | } | ||
| 181 | |||
| 182 | /* Column pin configuration | ||
| 183 | * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ||
| 184 | * pin: D5 C7 C6 D4 D0 E6 F0 F1 F4 F5 F6 F7 D7 D6 D1 D2 D3 | ||
| 185 | */ | ||
| 186 | static void unselect_cols(void) | ||
| 187 | { | ||
| 188 | // Hi-Z(DDR:0, PORT:0) to unselect | ||
| 189 | DDRC |= 0b11000000; // PC: 7 6 | ||
| 190 | PORTC |= 0b11000000; | ||
| 191 | DDRD |= 0b11111111; // PD: 7 6 5 4 3 2 1 0 | ||
| 192 | PORTD |= 0b11111111; | ||
| 193 | DDRE |= 0b01000000; // PE: 6 | ||
| 194 | PORTE |= 0b01000000; | ||
| 195 | DDRF |= 0b11110011; // PF: 7 6 5 4 1 0 | ||
| 196 | PORTF |= 0b11110011; | ||
| 197 | } | ||
| 198 | |||
| 199 | static void select_col(uint8_t col) | ||
| 200 | { | ||
| 201 | // Output low(DDR:1, PORT:0) to select | ||
| 202 | switch (col) { | ||
| 203 | case 0: | ||
| 204 | DDRD |= (1<<5); | ||
| 205 | PORTD &= ~(1<<5); | ||
| 206 | break; | ||
| 207 | case 1: | ||
| 208 | DDRC |= (1<<7); | ||
| 209 | PORTC &= ~(1<<7); | ||
| 210 | break; | ||
| 211 | case 2: | ||
| 212 | DDRC |= (1<<6); | ||
| 213 | PORTC &= ~(1<<6); | ||
| 214 | break; | ||
| 215 | case 3: | ||
| 216 | DDRD |= (1<<4); | ||
| 217 | PORTD &= ~(1<<4); | ||
| 218 | break; | ||
| 219 | case 4: | ||
| 220 | DDRD |= (1<<0); | ||
| 221 | PORTD &= ~(1<<0); | ||
| 222 | break; | ||
| 223 | case 5: | ||
| 224 | DDRE |= (1<<6); | ||
| 225 | PORTE &= ~(1<<6); | ||
| 226 | break; | ||
| 227 | case 6: | ||
| 228 | DDRF |= (1<<0); | ||
| 229 | PORTF &= ~(1<<0); | ||
| 230 | break; | ||
| 231 | case 7: | ||
| 232 | DDRF |= (1<<1); | ||
| 233 | PORTF &= ~(1<<1); | ||
| 234 | break; | ||
| 235 | case 8: | ||
| 236 | DDRF |= (1<<4); | ||
| 237 | PORTF &= ~(1<<4); | ||
| 238 | break; | ||
| 239 | case 9: | ||
| 240 | DDRF |= (1<<5); | ||
| 241 | PORTF &= ~(1<<5); | ||
| 242 | break; | ||
| 243 | case 10: | ||
| 244 | DDRF |= (1<<6); | ||
| 245 | PORTF &= ~(1<<6); | ||
| 246 | break; | ||
| 247 | case 11: | ||
| 248 | DDRF |= (1<<7); | ||
| 249 | PORTF &= ~(1<<7); | ||
| 250 | break; | ||
| 251 | case 12: | ||
| 252 | DDRD |= (1<<7); | ||
| 253 | PORTD &= ~(1<<7); | ||
| 254 | break; | ||
| 255 | case 13: | ||
| 256 | DDRD |= (1<<6); | ||
| 257 | PORTD &= ~(1<<6); | ||
| 258 | break; | ||
| 259 | case 14: | ||
| 260 | DDRD |= (1<<1); | ||
| 261 | PORTD &= ~(1<<1); | ||
| 262 | break; | ||
| 263 | case 15: | ||
| 264 | DDRD |= (1<<2); | ||
| 265 | PORTD &= ~(1<<2); | ||
| 266 | break; | ||
| 267 | case 16: | ||
| 268 | DDRD |= (1<<3); | ||
| 269 | PORTD &= ~(1<<3); | ||
| 270 | break; | ||
| 271 | } | ||
| 272 | } | ||
