diff options
Diffstat (limited to 'keyboards/kinesis/alvicstep/matrix.c')
| -rw-r--r-- | keyboards/kinesis/alvicstep/matrix.c | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/keyboards/kinesis/alvicstep/matrix.c b/keyboards/kinesis/alvicstep/matrix.c new file mode 100644 index 000000000..cb0d5ad7d --- /dev/null +++ b/keyboards/kinesis/alvicstep/matrix.c | |||
| @@ -0,0 +1,228 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2014 Warren Janssens <warren.janssens@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 "action_layer.h" | ||
| 26 | #include "print.h" | ||
| 27 | #include "debug.h" | ||
| 28 | #include "util.h" | ||
| 29 | #include "matrix.h" | ||
| 30 | #include "led.h" | ||
| 31 | #include "config.h" | ||
| 32 | |||
| 33 | #ifndef DEBOUNCE | ||
| 34 | # define DEBOUNCE 5 | ||
| 35 | #endif | ||
| 36 | static uint8_t debouncing = DEBOUNCE; | ||
| 37 | |||
| 38 | /* matrix state(1:on, 0:off) */ | ||
| 39 | static uint8_t matrix[MATRIX_ROWS]; | ||
| 40 | static uint8_t matrix_debouncing[MATRIX_ROWS]; | ||
| 41 | |||
| 42 | static matrix_row_t read_row(uint8_t row); | ||
| 43 | static void unselect_rows(void); | ||
| 44 | static void select_rows(uint8_t row); | ||
| 45 | |||
| 46 | __attribute__ ((weak)) | ||
| 47 | void matrix_init_quantum(void) { | ||
| 48 | matrix_init_kb(); | ||
| 49 | } | ||
| 50 | |||
| 51 | __attribute__ ((weak)) | ||
| 52 | void matrix_scan_quantum(void) { | ||
| 53 | matrix_scan_kb(); | ||
| 54 | } | ||
| 55 | |||
| 56 | __attribute__ ((weak)) | ||
| 57 | void matrix_init_kb(void) { | ||
| 58 | matrix_init_user(); | ||
| 59 | } | ||
| 60 | |||
| 61 | __attribute__ ((weak)) | ||
| 62 | void matrix_scan_kb(void) { | ||
| 63 | matrix_scan_user(); | ||
| 64 | } | ||
| 65 | |||
| 66 | __attribute__ ((weak)) | ||
| 67 | void matrix_init_user(void) { | ||
| 68 | } | ||
| 69 | |||
| 70 | __attribute__ ((weak)) | ||
| 71 | void matrix_scan_user(void) { | ||
| 72 | } | ||
| 73 | |||
| 74 | inline | ||
| 75 | uint8_t matrix_rows(void) | ||
| 76 | { | ||
| 77 | return MATRIX_ROWS; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | inline | ||
| 82 | uint8_t matrix_cols(void) | ||
| 83 | { | ||
| 84 | return MATRIX_COLS; | ||
| 85 | } | ||
| 86 | |||
| 87 | void matrix_init(void) | ||
| 88 | { | ||
| 89 | //debug_enable = true; | ||
| 90 | |||
| 91 | //dprint("matrix_init"); dprintln(); | ||
| 92 | // output high (leds) | ||
| 93 | DDRD = 0xFF; | ||
| 94 | PORTD = 0xFF; | ||
| 95 | |||
| 96 | // output low (multiplexers) | ||
| 97 | DDRF = 0xFF; | ||
| 98 | PORTF = 0x00; | ||
| 99 | |||
| 100 | // input with pullup (matrix) | ||
| 101 | DDRB = 0x00; | ||
| 102 | PORTB = 0xFF; | ||
| 103 | |||
| 104 | // input with pullup (program and keypad buttons) | ||
| 105 | DDRC = 0x00; | ||
| 106 | PORTC = 0xFF; | ||
| 107 | |||
| 108 | // initialize row and col | ||
| 109 | unselect_rows(); | ||
| 110 | |||
| 111 | // initialize matrix state: all keys off | ||
| 112 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 113 | matrix[i] = 0; | ||
| 114 | matrix_debouncing[i] = 0; | ||
| 115 | } | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | uint8_t matrix_scan(void) | ||
| 120 | { | ||
| 121 | |||
| 122 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 123 | select_rows(i); | ||
| 124 | uint8_t row = read_row(i); | ||
| 125 | if (matrix_debouncing[i] != row) { | ||
| 126 | matrix_debouncing[i] = row; | ||
| 127 | if (debouncing) { | ||
| 128 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
| 129 | } | ||
| 130 | debouncing = DEBOUNCE; | ||
| 131 | } | ||
| 132 | unselect_rows(); | ||
| 133 | } | ||
| 134 | |||
| 135 | if (debouncing) { | ||
| 136 | if (--debouncing) { | ||
| 137 | _delay_ms(1); | ||
| 138 | } else { | ||
| 139 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 140 | matrix[i] = matrix_debouncing[i]; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | matrix_scan_quantum(); | ||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | |||
| 148 | bool matrix_is_modified(void) | ||
| 149 | { | ||
| 150 | if (debouncing) return false; | ||
| 151 | return true; | ||
| 152 | } | ||
| 153 | |||
| 154 | inline | ||
| 155 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 156 | { | ||
| 157 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
| 158 | } | ||
| 159 | |||
| 160 | inline | ||
| 161 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 162 | { | ||
| 163 | return matrix[row]; | ||
| 164 | } | ||
| 165 | |||
| 166 | void matrix_print(void) | ||
| 167 | { | ||
| 168 | print("\nr/c 01234567\n"); | ||
| 169 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 170 | phex(row); print(": "); | ||
| 171 | pbin_reverse(matrix_get_row(row)); | ||
| 172 | print("\n"); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | uint8_t matrix_key_count(void) | ||
| 177 | { | ||
| 178 | uint8_t count = 0; | ||
| 179 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 180 | count += bitpop16(matrix[i]); | ||
| 181 | } | ||
| 182 | return count; | ||
| 183 | } | ||
| 184 | |||
| 185 | static matrix_row_t read_row(uint8_t row) | ||
| 186 | { | ||
| 187 | _delay_us(30); // without this wait read unstable value. | ||
| 188 | |||
| 189 | //keypad and program buttons | ||
| 190 | if (row == 12) | ||
| 191 | { | ||
| 192 | return ~(PINC | 0b00111111); | ||
| 193 | } | ||
| 194 | return ~PINB; | ||
| 195 | } | ||
| 196 | |||
| 197 | static void unselect_rows(void) | ||
| 198 | { | ||
| 199 | // set A,B,C,G to 0 (F4 - F7) | ||
| 200 | PORTF &= 0x0F; | ||
| 201 | } | ||
| 202 | |||
| 203 | static void select_rows(uint8_t row) | ||
| 204 | { | ||
| 205 | // set A,B,C,G to row value | ||
| 206 | PORTF |= row << 4; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | /* Row pin configuration | ||
| 211 | PF0 A | ||
| 212 | PF1 B | ||
| 213 | PF2 C | ||
| 214 | PF3 G 0 = U4, 1 = U5 | ||
| 215 | |||
| 216 | 4y0 4y1 4y2 4y3 4y4 4y5 4y6 4y7 5y0 5y1 5y2 5y3 5y4 5y5 5y6 5y7 | ||
| 217 | r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 | ||
| 218 | PB0 21 c1 f6 f8 f7 5 4 3 2 1 =+ | ||
| 219 | PB1 22 c2 f3 f5 f4 t r e w q TAB | ||
| 220 | PB2 23 c3 ESC f2 f1 g f d s a CL | ||
| 221 | PB3 24 c4 f9 f11 f10 b v c x z LS UP DN [{ ]} | ||
| 222 | PB4 25 c5 f12 SL PS RT LT §± `~ 6 7 8 9 0 -_ | ||
| 223 | PB5 26 c6 PB PGM KPD y u i o p \ | ||
| 224 | PB6 27 c7 LC DL BS RC EN SP h j k l ;: '" | ||
| 225 | PB7 28 c8 RA PU PD n m ,< .> /? RS | ||
| 226 | */ | ||
| 227 | |||
| 228 | |||
