qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

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<<7) ? 0 : ((matrix_row_t)1<<0)) |
     30         (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) |
     31         (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
     32         (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
     33         (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
     34         (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
     35         (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
     36         (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7))
     37     );
     38 }
     39 
     40 static void select_row(uint8_t row) {
     41     switch (row) {
     42         case  0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
     43         case  1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
     44         case  2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
     45         case  3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
     46         case  4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
     47         case  5: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
     48         case  6: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
     49         case  7: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
     50         case  8: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
     51         case  9: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
     52         case 10: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
     53         case 11: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
     54         case 12: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
     55         case 13: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
     56         case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
     57         case 15: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
     58         case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
     59         case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
     60     }
     61 }
     62 
     63 void matrix_init_custom(void) {
     64     /* Row output pins */
     65     DDRD  |=  0b01111011;
     66     /* Column input pins */
     67     DDRC  &= ~0b10000000;
     68     DDRB  &= ~0b01111111;
     69     PORTC |=  0b10000000;
     70     PORTB |=  0b01111111;
     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 }