qmk_firmware

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

matrix.c (6894B)


      1 /*
      2 Copyright 2012 Jun Wako
      3 Copyright 2014 Jack Humbert
      4 Copyright 2017 Priyadi Iman Nurcahyo
      5 
      6 This program is free software: you can redistribute it and/or modify
      7 it under the terms of the GNU General Public License as published by
      8 the Free Software Foundation, either version 2 of the License, or
      9 (at your option) any later version.
     10 
     11 This program is distributed in the hope that it will be useful,
     12 but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 GNU General Public License for more details.
     15 
     16 You should have received a copy of the GNU General Public License
     17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 #include <stdint.h>
     20 #include <stdbool.h>
     21 #if defined(__AVR__)
     22 #include <avr/io.h>
     23 #endif
     24 #include "wait.h"
     25 #include "print.h"
     26 #include "debug.h"
     27 #include "util.h"
     28 #include "matrix.h"
     29 #include "timer.h"
     30 
     31 
     32 /* Set 0 if debouncing isn't needed */
     33 
     34 #ifndef DEBOUNCE
     35 #   define DEBOUNCE 5
     36 #endif
     37 
     38 #if (DEBOUNCE > 0)
     39     static uint16_t debouncing_time;
     40     static bool debouncing = false;
     41 #endif
     42 
     43 #if (MATRIX_COLS <= 8)
     44 #    define print_matrix_header()  print("\nr/c 01234567\n")
     45 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
     46 #    define ROW_SHIFTER ((uint8_t)1)
     47 #elif (MATRIX_COLS <= 16)
     48 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
     49 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
     50 #    define ROW_SHIFTER ((uint16_t)1)
     51 #elif (MATRIX_COLS <= 32)
     52 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
     53 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
     54 #    define ROW_SHIFTER  ((uint32_t)1)
     55 #endif
     56 
     57 #ifdef MATRIX_MASKED
     58     extern const matrix_row_t matrix_mask[];
     59 #endif
     60 
     61 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
     62 static const uint8_t tp_pins[3] = TRACKPOINT_PINS;
     63 
     64 /* matrix state(1:on, 0:off) */
     65 static matrix_row_t matrix[MATRIX_ROWS];
     66 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
     67 
     68 static void init_cols(void);
     69 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
     70 static void unselect_rows(void);
     71 static void select_row(uint8_t row);
     72 static void unselect_row(uint8_t row);
     73 
     74 
     75 __attribute__ ((weak))
     76 void matrix_init_kb(void) {
     77     matrix_init_user();
     78 }
     79 
     80 __attribute__ ((weak))
     81 void matrix_scan_kb(void) {
     82     matrix_scan_user();
     83 }
     84 
     85 __attribute__ ((weak))
     86 void matrix_init_user(void) {
     87 }
     88 
     89 __attribute__ ((weak))
     90 void matrix_scan_user(void) {
     91 }
     92 
     93 inline
     94 uint8_t matrix_rows(void) {
     95     return MATRIX_ROWS;
     96 }
     97 
     98 inline
     99 uint8_t matrix_cols(void) {
    100     return MATRIX_COLS;
    101 }
    102 
    103 void matrix_init(void) {
    104     // initialize row and col
    105     unselect_rows();
    106     init_cols();
    107 
    108     // initialize matrix state: all keys off
    109     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
    110         matrix[i] = 0;
    111         matrix_debouncing[i] = 0;
    112     }
    113 
    114     matrix_init_kb();
    115 }
    116 
    117 uint8_t matrix_scan(void)
    118 {
    119     // Set row, read cols
    120     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
    121 #       if (DEBOUNCE > 0)
    122             bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
    123 
    124             if (matrix_changed) {
    125                 debouncing = true;
    126                 debouncing_time = timer_read();
    127             }
    128 
    129 #       else
    130             read_cols_on_row(matrix, current_row);
    131 #       endif
    132 
    133     }
    134 
    135 #   if (DEBOUNCE > 0)
    136         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
    137             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
    138                 matrix[i] = matrix_debouncing[i];
    139             }
    140             debouncing = false;
    141         }
    142 #   endif
    143 
    144     matrix_scan_kb();
    145     return 1;
    146 }
    147 
    148 inline
    149 bool matrix_is_on(uint8_t row, uint8_t col)
    150 
    151 {
    152     return (matrix[row] & ((matrix_row_t)1<<col));
    153 }
    154 
    155 inline
    156 matrix_row_t matrix_get_row(uint8_t row)
    157 {
    158     // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
    159     // switch blocker installed and the switch is always pressed.
    160 #ifdef MATRIX_MASKED
    161     return matrix[row] & matrix_mask[row];
    162 #else
    163     return matrix[row];
    164 #endif
    165 }
    166 
    167 void matrix_print(void)
    168 {
    169     print_matrix_header();
    170 
    171     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
    172         print_hex8(row); print(": ");
    173         print_matrix_row(row);
    174         print("\n");
    175     }
    176 }
    177 
    178 #define ROW_MASK 0b11100000
    179 
    180 static const uint8_t row_bit[MATRIX_ROWS] = {
    181   //  76543210
    182     0b00000000,
    183     0b00100000,
    184     0b01000000,
    185     0b01100000,
    186     0b10000000,
    187     0b10100000,
    188     0b11000000,
    189     0b11100000,
    190 };
    191 
    192 static void init_cols(void)
    193 {
    194     // columns
    195     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
    196         uint8_t pin = col_pins[x];
    197         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    198         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    199     }
    200 
    201     // rows
    202     DDRF  |=  ROW_MASK;
    203     PORTF &= ~ROW_MASK;
    204 
    205     // trackpoint
    206     for(uint8_t x = 0; x < 3; x++) {
    207         uint8_t pin = tp_pins[x];
    208         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    209         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    210     }
    211 }
    212 
    213 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
    214     {
    215         // Store last value of row prior to reading
    216         matrix_row_t last_row_value = current_matrix[current_row];
    217 
    218         // Clear data in matrix row
    219         current_matrix[current_row] = 0;
    220 
    221         // special case for trackpoint
    222         if (current_row == 8) {
    223             for(uint8_t tp_index = 0; tp_index < 3; tp_index++) {
    224 
    225                 // Select the TP pin to read (active low)
    226                 uint8_t pin = tp_pins[tp_index];
    227                 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
    228 
    229                 // Populate the matrix row with the state of the col pin
    230                 current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << tp_index);
    231             }
    232             return (last_row_value != current_matrix[current_row]);
    233         }
    234 
    235         // Select row and wait for row selecton to stabilize
    236         select_row(current_row);
    237         _delay_us(5);       // without this wait it won't read stable value.
    238         // wait_us(50);
    239 
    240         // For each col...
    241         for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
    242 
    243             // Select the col pin to read (active low)
    244             uint8_t pin = col_pins[col_index];
    245             uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
    246 
    247             // Populate the matrix row with the state of the col pin
    248             current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
    249         }
    250 
    251         // Unselect row
    252         unselect_row(current_row);
    253 
    254         return (last_row_value != current_matrix[current_row]);
    255 }
    256 
    257 static void select_row(uint8_t row)
    258 {
    259     PORTF = row_bit[row] | (PORTF & ~ROW_MASK);
    260 }
    261 
    262 static void unselect_row(uint8_t row)
    263 {
    264 }
    265 
    266 static void unselect_rows(void)
    267 {
    268 }