diff options
| author | tmk <nobody@nowhere> | 2012-12-27 02:41:32 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2012-12-27 02:41:32 +0900 |
| commit | c009f4643c17084b8f632b3292e0c7962b324ae5 (patch) | |
| tree | 10490ca64f2ee8cbe6d5f6531c954826f2c4756e /keyboard/gh60/matrix.c | |
| parent | 6ebe99f37c15c80c36d942e581794dc0cf3b68a4 (diff) | |
| download | qmk_firmware-c009f4643c17084b8f632b3292e0c7962b324ae5.tar.gz qmk_firmware-c009f4643c17084b8f632b3292e0c7962b324ae5.zip | |
Added Initial files for gh60.
Diffstat (limited to 'keyboard/gh60/matrix.c')
| -rw-r--r-- | keyboard/gh60/matrix.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/keyboard/gh60/matrix.c b/keyboard/gh60/matrix.c new file mode 100644 index 000000000..4c18a6191 --- /dev/null +++ b/keyboard/gh60/matrix.c | |||
| @@ -0,0 +1,225 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 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 <util/delay.h> | ||
| 25 | #include "print.h" | ||
| 26 | #include "debug.h" | ||
| 27 | #include "util.h" | ||
| 28 | #include "matrix.h" | ||
| 29 | |||
| 30 | |||
| 31 | #ifndef DEBOUNCE | ||
| 32 | # define DEBOUNCE 5 | ||
| 33 | #endif | ||
| 34 | static uint8_t debouncing = DEBOUNCE; | ||
| 35 | |||
| 36 | // matrix state buffer(1:on, 0:off) | ||
| 37 | static uint16_t *matrix; | ||
| 38 | static uint16_t *matrix_prev; | ||
| 39 | static uint16_t _matrix0[MATRIX_ROWS]; | ||
| 40 | static uint16_t _matrix1[MATRIX_ROWS]; | ||
| 41 | |||
| 42 | static uint16_t read_cols(void); | ||
| 43 | static void init_cols(void); | ||
| 44 | static void unselect_rows(void); | ||
| 45 | static void select_row(uint8_t row); | ||
| 46 | |||
| 47 | |||
| 48 | inline | ||
| 49 | uint8_t matrix_rows(void) | ||
| 50 | { | ||
| 51 | return MATRIX_ROWS; | ||
| 52 | } | ||
| 53 | |||
| 54 | inline | ||
| 55 | uint8_t matrix_cols(void) | ||
| 56 | { | ||
| 57 | return MATRIX_COLS; | ||
| 58 | } | ||
| 59 | |||
| 60 | void matrix_init(void) | ||
| 61 | { | ||
| 62 | // initialize row and col | ||
| 63 | unselect_rows(); | ||
| 64 | init_cols(); | ||
| 65 | |||
| 66 | // initialize matrix state: all keys off | ||
| 67 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0; | ||
| 68 | for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0; | ||
| 69 | matrix = _matrix0; | ||
| 70 | matrix_prev = _matrix1; | ||
| 71 | } | ||
| 72 | |||
| 73 | uint8_t matrix_scan(void) | ||
| 74 | { | ||
| 75 | if (!debouncing) { | ||
| 76 | uint16_t *tmp = matrix_prev; | ||
| 77 | matrix_prev = matrix; | ||
| 78 | matrix = tmp; | ||
| 79 | } | ||
| 80 | |||
| 81 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 82 | unselect_rows(); | ||
| 83 | select_row(i); | ||
| 84 | _delay_us(1); // without this wait read unstable value. | ||
| 85 | uint16_t cols = ~read_cols(); | ||
| 86 | if (matrix[i] != cols) { | ||
| 87 | matrix[i] = cols; | ||
| 88 | if (debouncing) { | ||
| 89 | debug("bounce!: "); debug_hex(debouncing); print("\n"); | ||
| 90 | } | ||
| 91 | debouncing = DEBOUNCE; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | unselect_rows(); | ||
| 95 | |||
| 96 | if (debouncing) { | ||
| 97 | debouncing--; | ||
| 98 | _delay_ms(1); // improved affect on bouncing | ||
| 99 | } | ||
| 100 | |||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | |||
| 104 | bool matrix_is_modified(void) | ||
| 105 | { | ||
| 106 | if (debouncing) return false; | ||
| 107 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 108 | if (matrix[i] != matrix_prev[i]) { | ||
| 109 | return true; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | return false; | ||
| 113 | } | ||
| 114 | |||
| 115 | inline | ||
| 116 | bool matrix_has_ghost(void) | ||
| 117 | { | ||
| 118 | return false; | ||
| 119 | } | ||
| 120 | |||
| 121 | inline | ||
| 122 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 123 | { | ||
| 124 | return (matrix[row] & (1<<col)); | ||
| 125 | } | ||
| 126 | |||
| 127 | inline | ||
| 128 | uint16_t matrix_get_row(uint8_t row) | ||
| 129 | { | ||
| 130 | return matrix[row]; | ||
| 131 | } | ||
| 132 | |||
| 133 | void matrix_print(void) | ||
| 134 | { | ||
| 135 | print("\nr/c 01234567\n"); | ||
| 136 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 137 | phex(row); print(": "); | ||
| 138 | pbin_reverse16(matrix_get_row(row)); | ||
| 139 | print("\n"); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | uint8_t matrix_key_count(void) | ||
| 144 | { | ||
| 145 | uint8_t count = 0; | ||
| 146 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 147 | count += bitpop16(matrix[i]); | ||
| 148 | } | ||
| 149 | return count; | ||
| 150 | } | ||
| 151 | |||
| 152 | /* Column pin configuration | ||
| 153 | * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 | ||
| 154 | * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 | ||
| 155 | */ | ||
| 156 | static void init_cols(void) | ||
| 157 | { | ||
| 158 | // Input with pull-up(DDR:0, PORT:1) | ||
| 159 | DDRF &= ~(1<<0 | 1<<1); | ||
| 160 | PORTF |= (1<<0 | 1<<1); | ||
| 161 | DDRE &= ~(1<<6); | ||
| 162 | PORTE |= (1<<6); | ||
| 163 | DDRD &= ~(1<<7 | 1<<6 | 1<<4); | ||
| 164 | PORTD |= (1<<7 | 1<<6 | 1<<4); | ||
| 165 | DDRC &= ~(1<<7 | 1<<6); | ||
| 166 | PORTC |= (1<<7 | 1<<6); | ||
| 167 | DDRB &= ~(1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0); | ||
| 168 | PORTB |= (1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0); | ||
| 169 | } | ||
| 170 | |||
| 171 | static uint16_t read_cols(void) | ||
| 172 | { | ||
| 173 | return (PINF&(1<<0) ? (1<<0) : 0) | | ||
| 174 | (PINF&(1<<1) ? (1<<1) : 0) | | ||
| 175 | (PINE&(1<<6) ? (1<<2) : 0) | | ||
| 176 | (PINC&(1<<7) ? (1<<3) : 0) | | ||
| 177 | (PINC&(1<<6) ? (1<<4) : 0) | | ||
| 178 | (PINB&(1<<6) ? (1<<5) : 0) | | ||
| 179 | (PIND&(1<<4) ? (1<<6) : 0) | | ||
| 180 | (PINB&(1<<1) ? (1<<7) : 0) | | ||
| 181 | (PINB&(1<<0) ? (1<<8) : 0) | | ||
| 182 | (PINB&(1<<5) ? (1<<9) : 0) | | ||
| 183 | (PINB&(1<<4) ? (1<<10) : 0) | | ||
| 184 | (PIND&(1<<7) ? (1<<11) : 0) | | ||
| 185 | (PIND&(1<<6) ? (1<<12) : 0) | | ||
| 186 | (PINB&(1<<3) ? (1<<13) : 0); | ||
| 187 | } | ||
| 188 | |||
| 189 | /* Row pin configuration | ||
| 190 | * row: 0 1 2 3 4 | ||
| 191 | * pin: D0 D1 D2 D3 D5 | ||
| 192 | */ | ||
| 193 | static void unselect_rows(void) | ||
| 194 | { | ||
| 195 | // Hi-Z(DDR:0, PORT:0) to unselect | ||
| 196 | DDRD &= ~0b00101111; | ||
| 197 | PORTD &= ~0b00101111; | ||
| 198 | } | ||
| 199 | |||
| 200 | static void select_row(uint8_t row) | ||
| 201 | { | ||
| 202 | // Output low(DDR:1, PORT:0) to select | ||
| 203 | switch (row) { | ||
| 204 | case 0: | ||
| 205 | DDRD |= (1<<0); | ||
| 206 | PORTD &= ~(1<<0); | ||
| 207 | break; | ||
| 208 | case 1: | ||
| 209 | DDRD |= (1<<1); | ||
| 210 | PORTD &= ~(1<<1); | ||
| 211 | break; | ||
| 212 | case 2: | ||
| 213 | DDRD |= (1<<2); | ||
| 214 | PORTD &= ~(1<<2); | ||
| 215 | break; | ||
| 216 | case 3: | ||
| 217 | DDRD |= (1<<3); | ||
| 218 | PORTD &= ~(1<<3); | ||
| 219 | break; | ||
| 220 | case 4: | ||
| 221 | DDRD |= (1<<5); | ||
| 222 | PORTD &= ~(1<<5); | ||
| 223 | break; | ||
| 224 | } | ||
| 225 | } | ||
