qmk_firmware

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

matrix.c (2324B)


      1 /* Copyright 2017 Mattia Dal Ben
      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 
     17 #include "matrix.h"
     18 #include "uart.h"
     19 
     20 #define UART_MATRIX_RESPONSE_TIMEOUT 10000
     21 
     22 void matrix_init_custom(void) {
     23     uart_init(1000000);
     24 }
     25 
     26 bool matrix_scan_custom(matrix_row_t current_matrix[]) {
     27     uint32_t timeout = 0;
     28     bool changed = false;
     29 
     30     //the s character requests the RF slave to send the matrix
     31     uart_write('s');
     32 
     33     //trust the external keystates entirely, erase the last data
     34     uint8_t uart_data[11] = {0};
     35 
     36     //there are 10 bytes corresponding to 10 columns, and then an end byte
     37     for (uint8_t i = 0; i < 11; i++) {
     38         //wait for the serial data, timeout if it's been too long
     39         //this only happened in testing with a loose wire, but does no
     40         //harm to leave it in here
     41         while (!uart_available()) {
     42             timeout++;
     43             if (timeout > UART_MATRIX_RESPONSE_TIMEOUT) {
     44                 break;
     45             }
     46         }
     47 
     48         if (timeout < UART_MATRIX_RESPONSE_TIMEOUT) {
     49             uart_data[i] = uart_read();
     50         } else {
     51             uart_data[i] = 0x00;
     52         }
     53     }
     54 
     55     //check for the end packet, the key state bytes use the LSBs, so 0xE0
     56     //will only show up here if the correct bytes were recieved
     57     if (uart_data[10] == 0xE0) {
     58         //shifting and transferring the keystates to the QMK matrix variable
     59         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
     60             matrix_row_t current_row = (uint16_t) uart_data[i * 2] | (uint16_t) uart_data[i * 2 + 1] << 7;
     61             if (current_matrix[i] != current_row) {
     62                 changed = true;
     63             }
     64             current_matrix[i] = current_row;
     65         }
     66     }
     67 
     68     return changed;
     69 }