qmk_firmware

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

matrix.c (3147B)


      1 /* Copyright 2019
      2  *
      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  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  */
     16 #include "matrix.h"
     17 #include "pca9555.h"
     18 #include "wait.h"
     19 
     20 // PCA9555 slave addresses
     21 #define IC1 0x20
     22 #define IC2 0x21
     23 
     24 //_____Utility funcs___________________________________________________________
     25 
     26 static void init_pins(void) {
     27   // init all cols high - IC2 all input
     28   pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT);//same as initial state
     29   pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT);//same as initial state
     30   pca9555_set_config(IC1, PCA9555_PORT1, ALL_INPUT);//same as initial state
     31 
     32   // init all rows - IC1 port0 input
     33   pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT);//same as initial state
     34 }
     35 
     36 static void select_row(uint8_t row) {
     37   // For the XD96 all rows are on the same IC and port
     38   // so its safe enough to assume here row == pin
     39   uint8_t pin = row;
     40   uint8_t mask = 1 << pin;
     41 
     42   pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH & (~mask));
     43   pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT & (~mask));
     44 }
     45 
     46 static uint32_t read_cols(void) {
     47   uint8_t state_1 = 0;
     48   uint8_t state_2 = 0;
     49   uint8_t state_3 = 0;
     50   pca9555_read_pins(IC2, PCA9555_PORT0, &state_1);
     51   pca9555_read_pins(IC2, PCA9555_PORT1, &state_2);
     52   pca9555_read_pins(IC1, PCA9555_PORT1, &state_3);
     53 
     54   // For the XD96 the pins are mapped to port expanders as follows:
     55   //   all 8 pins port 0 IC2, first 6 pins port 1 IC2, first 4 pins port 1 IC1
     56   uint32_t state = ((((uint32_t)state_3 & 0b00001111) << 14) | (((uint32_t)state_2 & 0b00111111) << 8) | (uint32_t)state_1);
     57   return (~state) & 0b111111111111111111;
     58 }
     59 
     60 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
     61   // Store last value of row prior to reading
     62   matrix_row_t last_row_value = current_matrix[current_row];
     63 
     64   // Clear data in matrix row
     65   current_matrix[current_row] = 0;
     66 
     67   // Select row and wait for row selection to stabilize
     68   select_row(current_row);
     69   wait_us(30);
     70 
     71   current_matrix[current_row] = read_cols();
     72 
     73   // No need to Unselect row as the next `select_row` will blank everything
     74 
     75   return (last_row_value != current_matrix[current_row]);
     76 }
     77 
     78 //_____CUSTOM MATRIX IMPLEMENTATION____________________________________________________
     79 
     80 void matrix_init_custom(void) {
     81   pca9555_init(IC1);
     82   pca9555_init(IC2);
     83 
     84   init_pins();
     85 }
     86 
     87 bool matrix_scan_custom(matrix_row_t current_matrix[]) {
     88   bool changed = false;
     89   for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
     90     changed |= read_cols_on_row(current_matrix, current_row);
     91   }
     92   return changed;
     93 }