qmk_firmware

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

matrix.c (3648B)


      1 /*
      2   Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
      3   Copyright 2016 Daniel Svensson <dsvensson@gmail.com>
      4 
      5   This program is free software: you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as published by
      7   the Free Software Foundation, either version 2 of the License, or
      8   (at your option) any later version.
      9 
     10   This program is distributed in the hope that it will be useful,
     11   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public License
     16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 */
     18 
     19 #include "matrix.h"
     20 
     21 matrix_row_t read_rows(void) {
     22     return
     23         (PINB & (1 << 1) ? 0 : ((matrix_row_t)1 << 0)) |
     24         (PINC & (1 << 2) ? 0 : ((matrix_row_t)1 << 1)) |
     25         (PINB & (1 << 6) ? 0 : ((matrix_row_t)1 << 2)) |
     26         (PINB & (1 << 4) ? 0 : ((matrix_row_t)1 << 3)) |
     27         (PINB & (1 << 3) ? 0 : ((matrix_row_t)1 << 4)) |
     28         (PINB & (1 << 2) ? 0 : ((matrix_row_t)1 << 5)) |
     29         (PINB & (1 << 0) ? 0 : ((matrix_row_t)1 << 6)) |
     30         (PINB & (1 << 5) ? 0 : ((matrix_row_t)1 << 7));
     31 }
     32 
     33 void select_col(uint8_t col) {
     34     switch (col) {
     35         case  0: PORTD = (PORTD & ~0b01111110) | 0b01100010; break;
     36         case  1: PORTD = (PORTD & ~0b01111110) | 0b01101000; break;
     37         case  2: PORTD = (PORTD & ~0b01111110) | 0b01101100; break;
     38         case  3: PORTD = (PORTD & ~0b01111110) | 0b01110000; break;
     39         case  4: PORTD = (PORTD & ~0b01111110) | 0b01111000; break;
     40         case  5: PORTD = (PORTD & ~0b01111110) | 0b01100000; break;
     41         case  6: PORTD = (PORTD & ~0b01111110) | 0b01110100; break;
     42         case  7: PORTD = (PORTD & ~0b01111110) | 0b01100100; break;
     43         case  8: PORTD = (PORTD & ~0b01111110) | 0b01111100; break;
     44         case  9: PORTD = (PORTD & ~0b01111110) | 0b01101010; break;
     45         case 10: PORTD = (PORTD & ~0b01111110) | 0b00110110; break;
     46         case 11: PORTD = (PORTD & ~0b01111110) | 0b00010110; break;
     47         case 12: PORTD = (PORTD & ~0b01111110) | 0b01001110; break;
     48         case 13: PORTD = (PORTD & ~0b01111110) | 0b00111110; break;
     49         case 14: PORTD = (PORTD & ~0b01111110) | 0b00011110; break;
     50         case 15: PORTD = (PORTD & ~0b01111110) | 0b01000110; break;
     51         case 16: PORTD = (PORTD & ~0b01111110) | 0b00100110; break;
     52         case 17: PORTD = (PORTD & ~0b01111110) | 0b00101110; break;
     53     }
     54 }
     55 
     56 void matrix_init_custom(void) {
     57     /* Column output pins */
     58     gpio_set_pin_output(D1);
     59     gpio_set_pin_output(D2);
     60     gpio_set_pin_output(D3);
     61     gpio_set_pin_output(D4);
     62     gpio_set_pin_output(D5);
     63     gpio_set_pin_output(D6);
     64 
     65     /* Row input pins */
     66     gpio_write_pin_high(B0);
     67     gpio_write_pin_high(B1);
     68     gpio_write_pin_high(B2);
     69     gpio_write_pin_high(B3);
     70     gpio_write_pin_high(B4);
     71     gpio_write_pin_high(B5);
     72     gpio_write_pin_high(B6);
     73     gpio_write_pin_high(C2);
     74 }
     75 
     76 bool matrix_scan_custom(matrix_row_t current_matrix[]) {
     77     bool changed = false;
     78 
     79     for (uint8_t col = 0; col < MATRIX_COLS; col++) {
     80         select_col(col);
     81         matrix_io_delay();
     82         matrix_row_t rows = read_rows();
     83 
     84         for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     85             bool prev_bit = current_matrix[row] & ((matrix_row_t)1 << col);
     86             bool curr_bit = rows & (1 << row);
     87 
     88             if (prev_bit != curr_bit) {
     89                 current_matrix[row] ^= (matrix_row_t)1 << col;
     90                 changed = true;
     91             }
     92         }
     93     }
     94 
     95     return changed;
     96 }