diff options
| author | tmk <nobody@nowhere> | 2012-06-08 00:37:46 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2012-06-08 13:32:38 +0900 |
| commit | afb08462085132acf7a1962522952f7dbf064519 (patch) | |
| tree | bc60f28142bc36d882f5930191ba8da98a27c47c /keyboard/hhkb/matrix.c | |
| parent | 63d82fcaeb78d0764f39667b937262ed4a692c17 (diff) | |
| download | qmk_firmware-afb08462085132acf7a1962522952f7dbf064519.tar.gz qmk_firmware-afb08462085132acf7a1962522952f7dbf064519.zip | |
Made directories for keyboard and converter projects.
Diffstat (limited to 'keyboard/hhkb/matrix.c')
| -rw-r--r-- | keyboard/hhkb/matrix.c | 294 |
1 files changed, 294 insertions, 0 deletions
diff --git a/keyboard/hhkb/matrix.c b/keyboard/hhkb/matrix.c new file mode 100644 index 000000000..350066b90 --- /dev/null +++ b/keyboard/hhkb/matrix.c | |||
| @@ -0,0 +1,294 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2011 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* | ||
| 19 | * scan matrix | ||
| 20 | */ | ||
| 21 | #include <stdint.h> | ||
| 22 | #include <stdbool.h> | ||
| 23 | #include <avr/io.h> | ||
| 24 | #include <avr/interrupt.h> | ||
| 25 | #include <util/delay.h> | ||
| 26 | #include "print.h" | ||
| 27 | #include "util.h" | ||
| 28 | #include "timer.h" | ||
| 29 | #include "matrix.h" | ||
| 30 | |||
| 31 | |||
| 32 | // Timer resolution check | ||
| 33 | #if (1000000/TIMER_RAW_FREQ > 20) | ||
| 34 | # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB." | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #if (MATRIX_COLS > 16) | ||
| 38 | # error "MATRIX_COLS must not exceed 16" | ||
| 39 | #endif | ||
| 40 | #if (MATRIX_ROWS > 255) | ||
| 41 | # error "MATRIX_ROWS must not exceed 255" | ||
| 42 | #endif | ||
| 43 | |||
| 44 | |||
| 45 | // matrix state buffer(1:on, 0:off) | ||
| 46 | #if (MATRIX_COLS <= 8) | ||
| 47 | static uint8_t *matrix; | ||
| 48 | static uint8_t *matrix_prev; | ||
| 49 | static uint8_t _matrix0[MATRIX_ROWS]; | ||
| 50 | static uint8_t _matrix1[MATRIX_ROWS]; | ||
| 51 | #else | ||
| 52 | static uint16_t *matrix; | ||
| 53 | static uint16_t *matrix_prev; | ||
| 54 | static uint16_t _matrix0[MATRIX_ROWS]; | ||
| 55 | static uint16_t _matrix1[MATRIX_ROWS]; | ||
| 56 | #endif | ||
| 57 | |||
| 58 | // HHKB has no ghost and no bounce. | ||
| 59 | #ifdef MATRIX_HAS_GHOST | ||
| 60 | static bool matrix_has_ghost_in_row(uint8_t row); | ||
| 61 | #endif | ||
| 62 | |||
| 63 | |||
| 64 | // Matrix I/O ports | ||
| 65 | // | ||
| 66 | // row: HC4051[A,B,C] selects scan row0-7 | ||
| 67 | // col: LS145[A,B,C,D] selects scan col0-7 and enable(D) | ||
| 68 | // key: on: 0/off: 1 | ||
| 69 | // prev: unknown: output previous key state(negated)? | ||
| 70 | |||
| 71 | #ifdef HOST_PJRC | ||
| 72 | // Ports for Teensy | ||
| 73 | // row: PB0-2 | ||
| 74 | // col: PB3-5,6 | ||
| 75 | // key: PE6(pull-uped) | ||
| 76 | // prev: PE7 | ||
| 77 | #define KEY_INIT() do { \ | ||
| 78 | DDRB |= 0x7F; \ | ||
| 79 | DDRE |= (1<<7); \ | ||
| 80 | DDRE &= ~(1<<6); \ | ||
| 81 | PORTE |= (1<<6); \ | ||
| 82 | } while (0) | ||
| 83 | #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \ | ||
| 84 | (((COL) & 0x07)<<3) | \ | ||
| 85 | ((ROW) & 0x07)) | ||
| 86 | #define KEY_ENABLE() (PORTB &= ~(1<<6)) | ||
| 87 | #define KEY_UNABLE() (PORTB |= (1<<6)) | ||
| 88 | #define KEY_STATE() (PINE & (1<<6)) | ||
| 89 | #define KEY_PREV_ON() (PORTE |= (1<<7)) | ||
| 90 | #define KEY_PREV_OFF() (PORTE &= ~(1<<7)) | ||
| 91 | #define KEY_POWER_ON() | ||
| 92 | #define KEY_POWER_OFF() | ||
| 93 | #else | ||
| 94 | // Ports for V-USB | ||
| 95 | // key: PB0(pull-uped) | ||
| 96 | // prev: PB1 | ||
| 97 | // row: PB2-4 | ||
| 98 | // col: PC0-2,3 | ||
| 99 | // power: PB5(Low:on/Hi-z:off) | ||
| 100 | #define KEY_INIT() do { \ | ||
| 101 | DDRB |= 0x3E; \ | ||
| 102 | DDRB &= ~(1<<0); \ | ||
| 103 | PORTB |= 1<<0; \ | ||
| 104 | DDRC |= 0x0F; \ | ||
| 105 | KEY_UNABLE(); \ | ||
| 106 | KEY_PREV_OFF(); \ | ||
| 107 | } while (0) | ||
| 108 | #define KEY_SELECT(ROW, COL) do { \ | ||
| 109 | PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \ | ||
| 110 | PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \ | ||
| 111 | } while (0) | ||
| 112 | #define KEY_ENABLE() (PORTC &= ~(1<<3)) | ||
| 113 | #define KEY_UNABLE() (PORTC |= (1<<3)) | ||
| 114 | #define KEY_STATE() (PINB & (1<<0)) | ||
| 115 | #define KEY_PREV_ON() (PORTB |= (1<<1)) | ||
| 116 | #define KEY_PREV_OFF() (PORTB &= ~(1<<1)) | ||
| 117 | // Power supply switching | ||
| 118 | #define KEY_POWER_ON() do { \ | ||
| 119 | KEY_INIT(); \ | ||
| 120 | PORTB &= ~(1<<5); \ | ||
| 121 | _delay_us(200); \ | ||
| 122 | } while (0) | ||
| 123 | #define KEY_POWER_OFF() do { \ | ||
| 124 | DDRB &= ~0x3F; \ | ||
| 125 | PORTB &= ~0x3F; \ | ||
| 126 | DDRC &= ~0x0F; \ | ||
| 127 | PORTC &= ~0x0F; \ | ||
| 128 | } while (0) | ||
| 129 | #endif | ||
| 130 | |||
| 131 | |||
| 132 | inline | ||
| 133 | uint8_t matrix_rows(void) | ||
| 134 | { | ||
| 135 | return MATRIX_ROWS; | ||
| 136 | } | ||
| 137 | |||
| 138 | inline | ||
| 139 | uint8_t matrix_cols(void) | ||
| 140 | { | ||
| 141 | return MATRIX_COLS; | ||
| 142 | } | ||
| 143 | |||
| 144 | void matrix_init(void) | ||
| 145 | { | ||
| 146 | KEY_INIT(); | ||
| 147 | |||
| 148 | // initialize matrix state: all keys off | ||
| 149 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00; | ||
| 150 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00; | ||
| 151 | matrix = _matrix0; | ||
| 152 | matrix_prev = _matrix1; | ||
| 153 | } | ||
| 154 | |||
| 155 | uint8_t matrix_scan(void) | ||
| 156 | { | ||
| 157 | uint8_t *tmp; | ||
| 158 | |||
| 159 | tmp = matrix_prev; | ||
| 160 | matrix_prev = matrix; | ||
| 161 | matrix = tmp; | ||
| 162 | |||
| 163 | KEY_POWER_ON(); | ||
| 164 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 165 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | ||
| 166 | KEY_SELECT(row, col); | ||
| 167 | _delay_us(40); | ||
| 168 | |||
| 169 | // Not sure this is needed. This just emulates HHKB controller's behaviour. | ||
| 170 | if (matrix_prev[row] & (1<<col)) { | ||
| 171 | KEY_PREV_ON(); | ||
| 172 | } | ||
| 173 | _delay_us(7); | ||
| 174 | |||
| 175 | // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE. | ||
| 176 | // If V-USB interrupts in this section we could lose 40us or so | ||
| 177 | // and would read invalid value from KEY_STATE. | ||
| 178 | uint8_t last = TIMER_RAW; | ||
| 179 | |||
| 180 | KEY_ENABLE(); | ||
| 181 | // Wait for KEY_STATE outputs its value. | ||
| 182 | // 1us was ok on one HHKB, but not worked on another. | ||
| 183 | _delay_us(10); | ||
| 184 | if (KEY_STATE()) { | ||
| 185 | matrix[row] &= ~(1<<col); | ||
| 186 | } else { | ||
| 187 | matrix[row] |= (1<<col); | ||
| 188 | } | ||
| 189 | |||
| 190 | // Ignore if this code region execution time elapses more than 20us. | ||
| 191 | if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) { | ||
| 192 | matrix[row] = matrix_prev[row]; | ||
| 193 | } | ||
| 194 | |||
| 195 | KEY_PREV_OFF(); | ||
| 196 | KEY_UNABLE(); | ||
| 197 | // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE. | ||
| 198 | // This takes 25us or more to make sure KEY_STATE returns to idle state. | ||
| 199 | _delay_us(150); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | KEY_POWER_OFF(); | ||
| 203 | return 1; | ||
| 204 | } | ||
| 205 | |||
| 206 | bool matrix_is_modified(void) | ||
| 207 | { | ||
| 208 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 209 | if (matrix[i] != matrix_prev[i]) | ||
| 210 | return true; | ||
| 211 | } | ||
| 212 | return false; | ||
| 213 | } | ||
| 214 | |||
| 215 | inline | ||
| 216 | bool matrix_has_ghost(void) | ||
| 217 | { | ||
| 218 | #ifdef MATRIX_HAS_GHOST | ||
| 219 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 220 | if (matrix_has_ghost_in_row(i)) | ||
| 221 | return true; | ||
| 222 | } | ||
| 223 | #endif | ||
| 224 | return false; | ||
| 225 | } | ||
| 226 | |||
| 227 | inline | ||
| 228 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 229 | { | ||
| 230 | return (matrix[row] & (1<<col)); | ||
| 231 | } | ||
| 232 | |||
| 233 | inline | ||
| 234 | #if (MATRIX_COLS <= 8) | ||
| 235 | uint8_t matrix_get_row(uint8_t row) | ||
| 236 | #else | ||
| 237 | uint16_t matrix_get_row(uint8_t row) | ||
| 238 | #endif | ||
| 239 | { | ||
| 240 | return matrix[row]; | ||
| 241 | } | ||
| 242 | |||
| 243 | void matrix_print(void) | ||
| 244 | { | ||
| 245 | #if (MATRIX_COLS <= 8) | ||
| 246 | print("\nr/c 01234567\n"); | ||
| 247 | #else | ||
| 248 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 249 | #endif | ||
| 250 | for (uint8_t row = 0; row < matrix_rows(); row++) { | ||
| 251 | phex(row); print(": "); | ||
| 252 | #if (MATRIX_COLS <= 8) | ||
| 253 | pbin_reverse(matrix_get_row(row)); | ||
| 254 | #else | ||
| 255 | pbin_reverse16(matrix_get_row(row)); | ||
| 256 | #endif | ||
| 257 | #ifdef MATRIX_HAS_GHOST | ||
| 258 | if (matrix_has_ghost_in_row(row)) { | ||
| 259 | print(" <ghost"); | ||
| 260 | } | ||
| 261 | #endif | ||
| 262 | print("\n"); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | uint8_t matrix_key_count(void) | ||
| 267 | { | ||
| 268 | uint8_t count = 0; | ||
| 269 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 270 | #if (MATRIX_COLS <= 8) | ||
| 271 | count += bitpop(matrix[i]); | ||
| 272 | #else | ||
| 273 | count += bitpop16(matrix[i]); | ||
| 274 | #endif | ||
| 275 | } | ||
| 276 | return count; | ||
| 277 | } | ||
| 278 | |||
| 279 | #ifdef MATRIX_HAS_GHOST | ||
| 280 | inline | ||
| 281 | static bool matrix_has_ghost_in_row(uint8_t row) | ||
| 282 | { | ||
| 283 | // no ghost exists in case less than 2 keys on | ||
| 284 | if (((matrix[row] - 1) & matrix[row]) == 0) | ||
| 285 | return false; | ||
| 286 | |||
| 287 | // ghost exists in case same state as other row | ||
| 288 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 289 | if (i != row && (matrix[i] & matrix[row]) == matrix[row]) | ||
| 290 | return true; | ||
| 291 | } | ||
| 292 | return false; | ||
| 293 | } | ||
| 294 | #endif | ||
