matrix.c (3347B)
1 /* 2 Copyright 2017 Gabriel Young <gabeplaysdrums@live.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 #include <util/delay.h> 19 #include "matrix.h" 20 21 22 static matrix_row_t scan_col(void) { 23 // Each of the 8 columns is read off pins as below 24 // 7 6 5 4 3 2 1 0 25 // ,--,--,--,--,--,--,--,--, 26 // |B0|B3|B2|B1|B6|B4|B5|C7| 27 // `--`--`--`--`--`--`--`--` 28 return ( 29 (PINC&(1<<2) ? 0 : ((matrix_row_t)1<<0)) | 30 (PIND&(1<<0) ? 0 : ((matrix_row_t)1<<1)) | 31 (PIND&(1<<1) ? 0 : ((matrix_row_t)1<<2)) | 32 (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<3)) | 33 (PIND&(1<<5) ? 0 : ((matrix_row_t)1<<4)) | 34 (PIND&(1<<4) ? 0 : ((matrix_row_t)1<<5)) | 35 (PIND&(1<<2) ? 0 : ((matrix_row_t)1<<6)) | 36 (PIND&(1<<6) ? 0 : ((matrix_row_t)1<<7)) 37 ); 38 } 39 40 static void select_row(uint8_t row) { 41 switch (row) { 42 case 0: PORTB = (PORTB & ~0b01111110) | 0b00111010; break; 43 case 1: PORTB = (PORTB & ~0b01111110) | 0b01011000; break; 44 case 2: PORTB = (PORTB & ~0b01111110) | 0b01110000; break; 45 case 3: PORTB = (PORTB & ~0b01111110) | 0b01101110; break; 46 case 4: PORTB = (PORTB & ~0b01111110) | 0b01101100; break; 47 case 5: PORTB = (PORTB & ~0b01111110) | 0b01101010; break; 48 case 6: PORTB = (PORTB & ~0b01111110) | 0b01101000; break; 49 case 7: PORTB = (PORTB & ~0b01111110) | 0b01100100; break; 50 case 8: PORTB = (PORTB & ~0b01111110) | 0b01100000; break; 51 case 9: PORTB = (PORTB & ~0b01111110) | 0b01100010; break; 52 case 10: PORTB = (PORTB & ~0b01111110) | 0b00011010; break; 53 case 11: PORTB = (PORTB & ~0b01111110) | 0b00011000; break; 54 case 12: PORTB = (PORTB & ~0b01111110) | 0b00111100; break; 55 case 13: PORTB = (PORTB & ~0b01111110) | 0b01100110; break; 56 case 14: PORTB = (PORTB & ~0b01111110) | 0b00111000; break; 57 case 15: PORTB = (PORTB & ~0b01111110) | 0b01110010; break; 58 case 16: PORTB = (PORTB & ~0b01111110) | 0b00011110; break; 59 case 17: PORTB = (PORTB & ~0b01111110) | 0b00111110; break; 60 } 61 } 62 63 void matrix_init_custom(void) { 64 /* Column output pins */ 65 DDRB |= 0b01111110; 66 /* Row input pins */ 67 DDRC &= ~0b10000100; 68 DDRD &= ~0b01110111; 69 PORTC |= 0b10000100; 70 PORTD |= 0b01110111; 71 } 72 73 // matrix is 18 uint8_t. 74 // we select the row (one of 18), then read the column 75 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 76 bool has_changed = false; 77 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 78 matrix_row_t orig = current_matrix[row]; 79 select_row(row); 80 _delay_us(3); 81 current_matrix[row] = scan_col(); 82 has_changed |= (orig != current_matrix[row]); 83 } 84 85 return has_changed; 86 }