qmk_firmware

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

matrix.c (6172B)


      1 /*
      2 Copyright 2017-2019 Mathias Andersson <wraul@dbox.se>
      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 #include "wait.h"
     18 #include "print.h"
     19 #include "debug.h"
     20 #include "util.h"
     21 #include "matrix.h"
     22 #include "debounce.h"
     23 
     24 #if (MATRIX_COLS <= 8)
     25 #    define print_matrix_header() print("\nr/c 01234567\n")
     26 #    define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
     27 #    define ROW_SHIFTER ((uint8_t)1)
     28 #elif (MATRIX_COLS <= 16)
     29 #    define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
     30 #    define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
     31 #    define ROW_SHIFTER ((uint16_t)1)
     32 #elif (MATRIX_COLS <= 32)
     33 #    define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
     34 #    define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
     35 #    define ROW_SHIFTER ((uint32_t)1)
     36 #endif
     37 
     38 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
     39 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
     40 
     41 /* matrix state(1:on, 0:off) */
     42 static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
     43 static matrix_row_t matrix[MATRIX_ROWS];     // debounced values
     44 
     45 __attribute__((weak)) void matrix_init_kb(void) {
     46     matrix_init_user();
     47 }
     48 
     49 __attribute__((weak)) void matrix_scan_kb(void) {
     50     matrix_scan_user();
     51 }
     52 
     53 __attribute__((weak)) void matrix_init_user(void) {}
     54 
     55 __attribute__((weak)) void matrix_scan_user(void) {}
     56 
     57 inline uint8_t matrix_rows(void) {
     58     return MATRIX_ROWS;
     59 }
     60 
     61 inline uint8_t matrix_cols(void) {
     62     return MATRIX_COLS;
     63 }
     64 
     65 inline bool matrix_is_on(uint8_t row, uint8_t col) {
     66     return (matrix[row] & ((matrix_row_t)1 << col));
     67 }
     68 
     69 inline matrix_row_t matrix_get_row(uint8_t row) {
     70     return matrix[row];
     71 }
     72 
     73 void matrix_print(void) {
     74     print_matrix_header();
     75 
     76     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     77         print_hex8(row);
     78         print(": ");
     79         print_matrix_row(row);
     80         print("\n");
     81     }
     82 }
     83 
     84 /* Columns 0 - 15
     85  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
     86  * col / pin:    PB6  PC6  PC7  PF1  PF0
     87  * 0:             0    1    0    0    0
     88  * 1:             0    1    0    0    1
     89  * 2:             0    1    0    1    0
     90  * 3:             0    1    0    1    1
     91  * 4:             0    1    1    0    0
     92  * 5:             0    1    1    0    1
     93  * 6:             0    1    1    1    0
     94  * 7:             0    1    1    1    1
     95  * 8:             1    0    0    0    0
     96  * 9:             1    0    0    0    1
     97  * 10:            1    0    0    1    0
     98  * 11:            1    0    0    1    1
     99  * 12:            1    0    1    0    0
    100  * 13:            1    0    1    0    1
    101  * 14:            1    0    1    1    0
    102  * 15:            1    0    1    1    1
    103  *
    104  * col: 16
    105  * pin: PB5
    106  */
    107 static void unselect_cols(void) {
    108     for (uint8_t x = 0; x < 6; x++) {
    109         gpio_set_pin_output(col_pins[x]);
    110         gpio_write_pin_low(col_pins[x]);
    111     }
    112 }
    113 
    114 static void select_col(uint8_t col) {
    115     if (col < 16) {
    116         uint8_t c = col + 8;
    117 
    118         gpio_write_pin(B6, c & 0b10000);
    119         gpio_write_pin(C6, c & 0b01000);
    120         gpio_write_pin(C7, c & 0b00100);
    121         gpio_write_pin(F1, c & 0b00010);
    122         gpio_write_pin(F0, c & 0b00001);
    123     } else {
    124         gpio_write_pin_high(B5);
    125     }
    126 }
    127 
    128 /* Row pin configuration
    129  * row: 0   1   2   3   4   5
    130  * pin: D0  D1  D2  D3  D5  B7
    131  *
    132  * Caps lock uses its own pin E2
    133  */
    134 static void init_pins(void) {
    135     unselect_cols();
    136     for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
    137         gpio_set_pin_input_high(row_pins[x]);
    138     }
    139 
    140     gpio_set_pin_input_high(E2);
    141 }
    142 
    143 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
    144     bool matrix_changed = false;
    145 
    146     // Select col and wait for col selecton to stabilize
    147     select_col(current_col);
    148     wait_us(30);
    149 
    150     // For each row...
    151     for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
    152         // Store last value of row prior to reading
    153         matrix_row_t last_row_value = current_matrix[row_index];
    154 
    155         // Check row pin state
    156         // Use the otherwise unused row: 3, col: 0 for caps lock
    157         if (row_index == 3 && current_col == 0) {
    158             if (gpio_read_pin(E2) == 0) {
    159                 // Pin LO, set col bit
    160                 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
    161             } else {
    162                 // Pin HI, clear col bit
    163                 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
    164             }
    165         } else {
    166             if (gpio_read_pin(row_pins[row_index]) == 0) {
    167                 // Pin HI, clear col bit
    168                 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
    169             } else {
    170                 // Pin LO, set col bit
    171                 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
    172             }
    173         }
    174 
    175         // Determine if the matrix changed state
    176         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
    177             matrix_changed = true;
    178         }
    179     }
    180 
    181     // Unselect cols
    182     unselect_cols();
    183 
    184     return matrix_changed;
    185 }
    186 
    187 void matrix_init(void) {
    188     // initialize key pins
    189     init_pins();
    190 
    191     // initialize matrix state: all keys off
    192     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
    193         raw_matrix[i] = 0;
    194         matrix[i]     = 0;
    195     }
    196 
    197     debounce_init();
    198 
    199     matrix_init_kb();
    200 }
    201 
    202 uint8_t matrix_scan(void) {
    203     bool changed = false;
    204 
    205     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
    206         changed |= read_rows_on_col(raw_matrix, current_col);
    207     }
    208 
    209     debounce(raw_matrix, matrix, changed);
    210 
    211     matrix_scan_kb();
    212 
    213     return (uint8_t)changed;
    214 }