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