qmk_firmware

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

matrix.c (4418B)


      1 /*
      2 Copyright 2012-2023 Jun Wako <wakojun@gmail.com> Kyrremann <kyrremann@gmail.com>
      3 This program is free software: you can redistribute it and/or modify
      4 it under the terms of the GNU General Public License as published by
      5 the Free Software Foundation, either version 2 of the License, or
      6 (at your option) any later version.
      7 This program is distributed in the hope that it will be useful,
      8 but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 GNU General Public License for more details.
     11 You should have received a copy of the GNU General Public License
     12 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     13 
     14  Copied from here: https://github.com/e3w2q/qmk_firmware/blob/762fe3e0a7cbea768245a75520f06ff5a2f00b9f/keyboards/2x3test/matrix.c
     15 */
     16 
     17 /*
     18  * scan matrix
     19  */
     20 #include <stdint.h>
     21 #include <stdbool.h>
     22 #include "wait.h"
     23 #include "util.h"
     24 #include "matrix.h"
     25 #include "quantum.h"
     26 
     27 #define ROW_SHIFTER ((uint16_t)1)
     28 
     29 static const pin_t row_pins[] = MATRIX_ROW_PINS;
     30 static const pin_t col_pins[] = MATRIX_COL_PINS;
     31 
     32 static void select_row(uint8_t row) {
     33     gpio_set_pin_output(row_pins[row]);
     34     gpio_write_pin_low(row_pins[row]);
     35 }
     36 
     37 static void unselect_row(uint8_t row) {
     38     gpio_set_pin_input_high(row_pins[row]);
     39 }
     40 
     41 static void unselect_rows(void) {
     42     for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
     43         gpio_set_pin_input_high(row_pins[x]);
     44     }
     45 }
     46 
     47 static void select_col(uint8_t col) {
     48     gpio_set_pin_output(col_pins[col]);
     49     gpio_write_pin_low(col_pins[col]);
     50 }
     51 
     52 static void unselect_col(uint8_t col) {
     53     gpio_set_pin_input_high(col_pins[col]);
     54 }
     55 
     56 static void unselect_cols(void) {
     57     for (uint8_t x = 0; x < MATRIX_COLS/2; x++) {
     58         gpio_set_pin_input_high(col_pins[x*2]);
     59     }
     60 }
     61 
     62 static void init_pins(void) {
     63     unselect_cols();
     64     unselect_rows();
     65 }
     66 
     67 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
     68     // Store last value of row prior to reading
     69     matrix_row_t last_row_value = current_matrix[current_row];
     70 
     71     // Select row and wait for row selection to stabilize
     72     select_row(current_row);
     73     wait_us(30);
     74 
     75     // For each col...
     76     for (uint8_t col_index = 0; col_index < MATRIX_COLS / 2; col_index++) {
     77         uint16_t column_index_bitmask = ROW_SHIFTER << ((col_index * 2) + 1);
     78         // Check row pin state
     79         if (gpio_read_pin(col_pins[col_index*2])) {
     80             // Pin HI, clear col bit
     81             current_matrix[current_row] &= ~column_index_bitmask;
     82         } else {
     83             // Pin LO, set col bit
     84             current_matrix[current_row] |= column_index_bitmask;
     85         }
     86     }
     87 
     88     // Unselect row
     89     unselect_row(current_row);
     90 
     91     return (last_row_value != current_matrix[current_row]);
     92 }
     93 
     94 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
     95     bool matrix_changed = false;
     96 
     97     // Select col and wait for col selection to stabilize
     98     select_col(current_col*2);
     99     wait_us(30);
    100 
    101     // For each row...
    102     for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
    103         // Store last value of row prior to reading
    104         matrix_row_t last_row_value = current_matrix[row_index];
    105 
    106         uint16_t column_index_bitmask = ROW_SHIFTER << (current_col * 2);
    107         // Check row pin state
    108         if (gpio_read_pin(row_pins[row_index])) {
    109             // Pin HI, clear col bit
    110             current_matrix[row_index] &= ~column_index_bitmask;
    111         } else {
    112             // Pin LO, set col bit
    113             current_matrix[row_index] |= column_index_bitmask;
    114         }
    115 
    116         // Determine if the matrix changed state
    117         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
    118                    matrix_changed = true;
    119         }
    120     }
    121 
    122     // Unselect col
    123     unselect_col(current_col*2);
    124 
    125     return matrix_changed;
    126 }
    127 
    128 
    129 void matrix_init_custom(void) {
    130     // initialize key pins
    131     init_pins();
    132 }
    133 
    134 bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    135 
    136     bool changed = false;
    137 
    138     // Set row, read cols
    139     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
    140         changed |= read_cols_on_row(current_matrix, current_row);
    141     }
    142     // Set col, read rows
    143     for (uint8_t current_col = 0; current_col < MATRIX_COLS/2; current_col++) {
    144         changed |= read_rows_on_col(current_matrix, current_col);
    145     }
    146 
    147     return changed;
    148 }