qmk_firmware

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

matrix.c (3743B)


      1 /*
      2 Copyright 2012-2020 Jun Wako, Jack Humbert, Yiancar-Designs
      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 "matrix.h"
     18 #include "wait.h"
     19 #include "i2c_master.h"
     20 
     21 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
     22 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
     23 
     24 static void unselect_rows(void) {
     25     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
     26         gpio_set_pin_input_high(row_pins[x]);
     27     }
     28 }
     29 
     30 static void select_row(uint8_t row) {
     31     gpio_set_pin_output(row_pins[row]);
     32     gpio_write_pin_low(row_pins[row]);
     33 }
     34 
     35 static void unselect_row(uint8_t row) {
     36     gpio_set_pin_input_high(row_pins[row]);
     37 }
     38 
     39 static void init_pins(void) {
     40     unselect_rows();
     41     // Set I/O
     42     uint8_t send_data = 0x1F;
     43     i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data, 1, 20);
     44     // Set Pull-up
     45     i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x06, &send_data, 1, 20);
     46 
     47     for (uint8_t x = 0; x < MATRIX_COLS; x++) {
     48         if ( x < 10 ) {
     49             gpio_set_pin_input_high(col_pins[x]);
     50         }
     51     }
     52 }
     53 
     54 void matrix_init_custom(void) {
     55     // TODO: initialize hardware here
     56     // Initialize I2C
     57     i2c_init();
     58 
     59     // initialize key pins
     60     init_pins();
     61     wait_ms(50);
     62 }
     63 
     64 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
     65     // Store last value of row prior to reading
     66     matrix_row_t last_row_value = current_matrix[current_row];
     67 
     68     // Clear data in matrix row
     69     current_matrix[current_row] = 0;
     70 
     71     // Select row and wait for row selecton to stabilize
     72     select_row(current_row);
     73     matrix_io_delay();
     74 
     75     uint8_t port_expander_col_buffer;
     76     i2c_read_register((PORT_EXPANDER_ADDRESS << 1), 0x09, &port_expander_col_buffer, 1, 20);
     77 
     78     // For each col...
     79     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
     80         uint8_t pin_state;
     81         // Select the col pin to read (active low)
     82         switch (col_index) {
     83             case 10 :
     84                 pin_state = port_expander_col_buffer & (1 << 0);
     85                 break;
     86             case 11 :
     87                 pin_state = port_expander_col_buffer & (1 << 1);
     88                 break;
     89             case 12 :
     90                 pin_state = port_expander_col_buffer & (1 << 2);
     91                 break;
     92             case 13 :
     93                 pin_state = port_expander_col_buffer & (1 << 3);
     94                 break;
     95             case 14 :
     96                 pin_state = port_expander_col_buffer & (1 << 4);
     97                 break;
     98             default :
     99                 pin_state = gpio_read_pin(col_pins[col_index]);
    100         }
    101 
    102         // Populate the matrix row with the state of the col pin
    103         current_matrix[current_row] |=  pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
    104     }
    105 
    106     // Unselect row
    107     unselect_row(current_row);
    108 
    109     return (last_row_value != current_matrix[current_row]);
    110 }
    111 
    112 bool matrix_scan_custom(matrix_row_t current_matrix[]) {
    113     bool matrix_has_changed = false;
    114 
    115     // Set row, read cols
    116     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
    117         matrix_has_changed |= read_cols_on_row(current_matrix, current_row);
    118     }
    119 
    120     return matrix_has_changed;
    121 }