diff options
Diffstat (limited to 'keyboard/planck/matrix.c')
| -rw-r--r-- | keyboard/planck/matrix.c | 299 |
1 files changed, 0 insertions, 299 deletions
diff --git a/keyboard/planck/matrix.c b/keyboard/planck/matrix.c deleted file mode 100644 index 12a158543..000000000 --- a/keyboard/planck/matrix.c +++ /dev/null | |||
| @@ -1,299 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako | ||
| 3 | Generated by planckkeyboard.com (2014 Jack Humbert) | ||
| 4 | |||
| 5 | This program is free software: you can redistribute it and/or modify | ||
| 6 | it under the terms of the GNU General Public License as published by | ||
| 7 | the Free Software Foundation, either version 2 of the License, or | ||
| 8 | (at your option) any later version. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | */ | ||
| 18 | |||
| 19 | /* | ||
| 20 | * scan matrix | ||
| 21 | */ | ||
| 22 | #include <stdint.h> | ||
| 23 | #include <stdbool.h> | ||
| 24 | #include <avr/io.h> | ||
| 25 | #include <util/delay.h> | ||
| 26 | #include "print.h" | ||
| 27 | #include "debug.h" | ||
| 28 | #include "util.h" | ||
| 29 | #include "matrix.h" | ||
| 30 | |||
| 31 | #ifndef DEBOUNCE | ||
| 32 | # define DEBOUNCE 10 | ||
| 33 | #endif | ||
| 34 | static uint8_t debouncing = DEBOUNCE; | ||
| 35 | |||
| 36 | /* matrix state(1:on, 0:off) */ | ||
| 37 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 38 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 39 | |||
| 40 | #if DIODE_DIRECTION == ROW2COL | ||
| 41 | static matrix_row_t matrix_reversed[MATRIX_COLS]; | ||
| 42 | static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS]; | ||
| 43 | #endif | ||
| 44 | |||
| 45 | static matrix_row_t read_cols(void); | ||
| 46 | static void init_cols(void); | ||
| 47 | static void unselect_rows(void); | ||
| 48 | static void select_row(uint8_t row); | ||
| 49 | |||
| 50 | inline | ||
| 51 | uint8_t matrix_rows(void) | ||
| 52 | { | ||
| 53 | return MATRIX_ROWS; | ||
| 54 | } | ||
| 55 | |||
| 56 | inline | ||
| 57 | uint8_t matrix_cols(void) | ||
| 58 | { | ||
| 59 | return MATRIX_COLS; | ||
| 60 | } | ||
| 61 | |||
| 62 | void matrix_init(void) | ||
| 63 | { | ||
| 64 | // To use PORTF disable JTAG with writing JTD bit twice within four cycles. | ||
| 65 | MCUCR |= (1<<JTD); | ||
| 66 | MCUCR |= (1<<JTD); | ||
| 67 | |||
| 68 | #ifdef BACKLIGHT_ENABLE | ||
| 69 | backlight_init_ports(); | ||
| 70 | #endif | ||
| 71 | |||
| 72 | // Turn status LED on | ||
| 73 | DDRE |= (1<<6); | ||
| 74 | PORTE |= (1<<6); | ||
| 75 | |||
| 76 | // initialize row and col | ||
| 77 | unselect_rows(); | ||
| 78 | init_cols(); | ||
| 79 | |||
| 80 | // initialize matrix state: all keys off | ||
| 81 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 82 | matrix[i] = 0; | ||
| 83 | matrix_debouncing[i] = 0; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | uint8_t matrix_scan(void) | ||
| 89 | { | ||
| 90 | #if DIODE_DIRECTION == COL2ROW | ||
| 91 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 92 | select_row(i); | ||
| 93 | _delay_us(30); // without this wait read unstable value. | ||
| 94 | matrix_row_t cols = read_cols(); | ||
| 95 | if (matrix_debouncing[i] != cols) { | ||
| 96 | matrix_debouncing[i] = cols; | ||
| 97 | if (debouncing) { | ||
| 98 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
| 99 | } | ||
| 100 | debouncing = DEBOUNCE; | ||
| 101 | } | ||
| 102 | unselect_rows(); | ||
| 103 | } | ||
| 104 | |||
| 105 | if (debouncing) { | ||
| 106 | if (--debouncing) { | ||
| 107 | _delay_ms(1); | ||
| 108 | } else { | ||
| 109 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 110 | matrix[i] = matrix_debouncing[i]; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | #else | ||
| 115 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | ||
| 116 | select_row(i); | ||
| 117 | _delay_us(30); // without this wait read unstable value. | ||
| 118 | matrix_row_t rows = read_cols(); | ||
| 119 | if (matrix_reversed_debouncing[i] != rows) { | ||
| 120 | matrix_reversed_debouncing[i] = rows; | ||
| 121 | if (debouncing) { | ||
| 122 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
| 123 | } | ||
| 124 | debouncing = DEBOUNCE; | ||
| 125 | } | ||
| 126 | unselect_rows(); | ||
| 127 | } | ||
| 128 | |||
| 129 | if (debouncing) { | ||
| 130 | if (--debouncing) { | ||
| 131 | _delay_ms(1); | ||
| 132 | } else { | ||
| 133 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | ||
| 134 | matrix_reversed[i] = matrix_reversed_debouncing[i]; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | for (uint8_t y = 0; y < MATRIX_ROWS; y++) { | ||
| 139 | matrix_row_t row = 0; | ||
| 140 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
| 141 | row |= ((matrix_reversed[x] & (1<<y)) >> y) << x; | ||
| 142 | } | ||
| 143 | matrix[y] = row; | ||
| 144 | } | ||
| 145 | #endif | ||
| 146 | |||
| 147 | return 1; | ||
| 148 | } | ||
| 149 | |||
| 150 | bool matrix_is_modified(void) | ||
| 151 | { | ||
| 152 | if (debouncing) return false; | ||
| 153 | return true; | ||
| 154 | } | ||
| 155 | |||
| 156 | inline | ||
| 157 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 158 | { | ||
| 159 | return (matrix[row] & ((matrix_row_t)1<col)); | ||
| 160 | } | ||
| 161 | |||
| 162 | inline | ||
| 163 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 164 | { | ||
| 165 | return matrix[row]; | ||
| 166 | } | ||
| 167 | |||
| 168 | void matrix_print(void) | ||
| 169 | { | ||
| 170 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 171 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 172 | phex(row); print(": "); | ||
| 173 | pbin_reverse16(matrix_get_row(row)); | ||
| 174 | print("\n"); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | uint8_t matrix_key_count(void) | ||
| 179 | { | ||
| 180 | uint8_t count = 0; | ||
| 181 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 182 | count += bitpop16(matrix[i]); | ||
| 183 | } | ||
| 184 | return count; | ||
| 185 | } | ||
| 186 | |||
| 187 | static void init_cols(void) | ||
| 188 | { | ||
| 189 | int B = 0, C = 0, D = 0, E = 0, F = 0; | ||
| 190 | |||
| 191 | #if DIODE_DIRECTION == COL2ROW | ||
| 192 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
| 193 | int col = COLS[x]; | ||
| 194 | #else | ||
| 195 | for(int x = 0; x < MATRIX_ROWS; x++) { | ||
| 196 | int col = ROWS[x]; | ||
| 197 | #endif | ||
| 198 | if ((col & 0xF0) == 0x20) { | ||
| 199 | B |= (1<<(col & 0x0F)); | ||
| 200 | } else if ((col & 0xF0) == 0x30) { | ||
| 201 | C |= (1<<(col & 0x0F)); | ||
| 202 | } else if ((col & 0xF0) == 0x40) { | ||
| 203 | D |= (1<<(col & 0x0F)); | ||
| 204 | } else if ((col & 0xF0) == 0x50) { | ||
| 205 | E |= (1<<(col & 0x0F)); | ||
| 206 | } else if ((col & 0xF0) == 0x60) { | ||
| 207 | F |= (1<<(col & 0x0F)); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | DDRB &= ~(B); PORTB |= (B); | ||
| 211 | DDRC &= ~(C); PORTC |= (C); | ||
| 212 | DDRD &= ~(D); PORTD |= (D); | ||
| 213 | DDRE &= ~(E); PORTE |= (E); | ||
| 214 | DDRF &= ~(F); PORTF |= (F); | ||
| 215 | } | ||
| 216 | |||
| 217 | static matrix_row_t read_cols(void) | ||
| 218 | { | ||
| 219 | matrix_row_t result = 0; | ||
| 220 | |||
| 221 | #if DIODE_DIRECTION == COL2ROW | ||
| 222 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
| 223 | int col = COLS[x]; | ||
| 224 | #else | ||
| 225 | for(int x = 0; x < MATRIX_ROWS; x++) { | ||
| 226 | int col = ROWS[x]; | ||
| 227 | #endif | ||
| 228 | |||
| 229 | if ((col & 0xF0) == 0x20) { | ||
| 230 | result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 231 | } else if ((col & 0xF0) == 0x30) { | ||
| 232 | result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 233 | } else if ((col & 0xF0) == 0x40) { | ||
| 234 | result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 235 | } else if ((col & 0xF0) == 0x50) { | ||
| 236 | result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 237 | } else if ((col & 0xF0) == 0x60) { | ||
| 238 | result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | return result; | ||
| 242 | } | ||
| 243 | |||
| 244 | static void unselect_rows(void) | ||
| 245 | { | ||
| 246 | int B = 0, C = 0, D = 0, E = 0, F = 0; | ||
| 247 | |||
| 248 | #if DIODE_DIRECTION == COL2ROW | ||
| 249 | for(int x = 0; x < MATRIX_ROWS; x++) { | ||
| 250 | int row = ROWS[x]; | ||
| 251 | #else | ||
| 252 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
| 253 | int row = COLS[x]; | ||
| 254 | #endif | ||
| 255 | if ((row & 0xF0) == 0x20) { | ||
| 256 | B |= (1<<(row & 0x0F)); | ||
| 257 | } else if ((row & 0xF0) == 0x30) { | ||
| 258 | C |= (1<<(row & 0x0F)); | ||
| 259 | } else if ((row & 0xF0) == 0x40) { | ||
| 260 | D |= (1<<(row & 0x0F)); | ||
| 261 | } else if ((row & 0xF0) == 0x50) { | ||
| 262 | E |= (1<<(row & 0x0F)); | ||
| 263 | } else if ((row & 0xF0) == 0x60) { | ||
| 264 | F |= (1<<(row & 0x0F)); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | DDRB &= ~(B); PORTB |= (B); | ||
| 268 | DDRC &= ~(C); PORTC |= (C); | ||
| 269 | DDRD &= ~(D); PORTD |= (D); | ||
| 270 | DDRE &= ~(E); PORTE |= (E); | ||
| 271 | DDRF &= ~(F); PORTF |= (F); | ||
| 272 | } | ||
| 273 | |||
| 274 | static void select_row(uint8_t row) | ||
| 275 | { | ||
| 276 | |||
| 277 | #if DIODE_DIRECTION == COL2ROW | ||
| 278 | int row_pin = ROWS[row]; | ||
| 279 | #else | ||
| 280 | int row_pin = COLS[row]; | ||
| 281 | #endif | ||
| 282 | |||
| 283 | if ((row_pin & 0xF0) == 0x20) { | ||
| 284 | DDRB |= (1<<(row_pin & 0x0F)); | ||
| 285 | PORTB &= ~(1<<(row_pin & 0x0F)); | ||
| 286 | } else if ((row_pin & 0xF0) == 0x30) { | ||
| 287 | DDRC |= (1<<(row_pin & 0x0F)); | ||
| 288 | PORTC &= ~(1<<(row_pin & 0x0F)); | ||
| 289 | } else if ((row_pin & 0xF0) == 0x40) { | ||
| 290 | DDRD |= (1<<(row_pin & 0x0F)); | ||
| 291 | PORTD &= ~(1<<(row_pin & 0x0F)); | ||
| 292 | } else if ((row_pin & 0xF0) == 0x50) { | ||
| 293 | DDRE |= (1<<(row_pin & 0x0F)); | ||
| 294 | PORTE &= ~(1<<(row_pin & 0x0F)); | ||
| 295 | } else if ((row_pin & 0xF0) == 0x60) { | ||
| 296 | DDRF |= (1<<(row_pin & 0x0F)); | ||
| 297 | PORTF &= ~(1<<(row_pin & 0x0F)); | ||
| 298 | } | ||
| 299 | } \ No newline at end of file | ||
