qmk_firmware

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

matrix.c (1796B)


      1 // Copyright 2023 QMK
      2 // SPDX-License-Identifier: GPL-2.0-or-later
      3 
      4 #include "matrix.h"
      5 
      6 static matrix_row_t read_row(uint8_t row) {
      7     matrix_io_delay();  // without this wait read unstable value.
      8 
      9     // keypad and program buttons
     10     if (row == 12) {
     11         return ~(gpio_read_pin(B4) | (gpio_read_pin(B5) << 1) | 0b11111100);
     12     }
     13 
     14     return ~(gpio_read_pin(B6) | gpio_read_pin(B2) << 1 | gpio_read_pin(B3) << 2 | gpio_read_pin(B1) << 3 | gpio_read_pin(F7) << 4 | gpio_read_pin(F6) << 5 | gpio_read_pin(F5) << 6 | gpio_read_pin(F4) << 7);
     15 }
     16 
     17 static void unselect_rows(void) {
     18     // set A,B,C,G to 0
     19     PORTD &= 0xF0;
     20 }
     21 
     22 static void select_rows(uint8_t row) {
     23     // set A,B,C,G to row value
     24     PORTD |= (0x0F & row);
     25 }
     26 
     27 void matrix_init_custom(void) {
     28     // output low (multiplexers)
     29     gpio_set_pin_output(D0);
     30     gpio_set_pin_output(D1);
     31     gpio_set_pin_output(D2);
     32     gpio_set_pin_output(D3);
     33 
     34     // input with pullup (matrix)
     35     gpio_set_pin_input_high(B6);
     36     gpio_set_pin_input_high(B2);
     37     gpio_set_pin_input_high(B3);
     38     gpio_set_pin_input_high(B1);
     39     gpio_set_pin_input_high(F7);
     40     gpio_set_pin_input_high(F6);
     41     gpio_set_pin_input_high(F5);
     42     gpio_set_pin_input_high(F4);
     43 
     44     // input with pullup (program and keypad buttons)
     45     gpio_set_pin_input_high(B4);
     46     gpio_set_pin_input_high(B5);
     47 
     48     // initialize row and col
     49     unselect_rows();
     50 }
     51 
     52 bool matrix_scan_custom(matrix_row_t current_matrix[]) {
     53     bool matrix_has_changed = false;
     54 
     55     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
     56         select_rows(i);
     57         matrix_row_t row = read_row(i);
     58         unselect_rows();
     59         bool row_has_changed = current_matrix[i] != row;
     60         matrix_has_changed |= row_has_changed;
     61         current_matrix[i] = row;
     62     }
     63 
     64     return matrix_has_changed;
     65 }