matrix.c (2214B)
1 /* 2 Copyright 2012 Jun Wako 3 Copyright 2014 Jack Humbert 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include "matrix.h" 20 #include "uart.h" 21 22 #define UART_MATRIX_RESPONSE_TIMEOUT 10000 23 24 void matrix_init_custom(void) { 25 uart_init(500000); 26 } 27 28 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 29 uint32_t timeout = 0; 30 bool changed = false; 31 32 //the s character requests the RF remote slave to send the matrix information 33 uart_write('s'); 34 35 //trust the external keystates, erase the last set of data 36 uint8_t uart_data[11] = {0}; 37 38 //there are 10 bytes corresponding to 1w columns, and an end byte 39 for (uint8_t i = 0; i < 11; i++) { 40 //wait for the serial data, timeout if it's been too long 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] << 5; 61 if (current_matrix[i] != current_row) { 62 changed = true; 63 } 64 current_matrix[i] = current_row; 65 } 66 } 67 68 return changed; 69 }