matrix.c (2304B)
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(1000000); 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 slave to send the matrix 33 uart_write('s'); 34 35 //trust the external keystates entirely, erase the last data 36 uint8_t uart_data[13] = {0}; 37 38 //there are 12 bytes corresponding to 12 columns, and an end byte 39 for (uint8_t i = 0; i < 13; i++) { 40 //wait for the serial data, timeout if it's been too long 41 //this only happened in testing with a loose wire, but does no 42 //harm to leave it in here 43 while (!uart_available()) { 44 timeout++; 45 if (timeout > UART_MATRIX_RESPONSE_TIMEOUT) { 46 break; 47 } 48 } 49 50 if (timeout < UART_MATRIX_RESPONSE_TIMEOUT) { 51 uart_data[i] = uart_read(); 52 } else { 53 uart_data[i] = 0x00; 54 } 55 } 56 57 //check for the end packet, the key state bytes use the LSBs, so 0xE0 58 //will only show up here if the correct bytes were recieved 59 if (uart_data[11] == 0xE0) { 60 //shifting and transferring the keystates to the QMK matrix variable 61 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 62 matrix_row_t current_row = (uint16_t) uart_data[i * 2] | (uint16_t) uart_data[i * 2 + 1] << 6; 63 if (current_matrix[i] != current_row) { 64 changed = true; 65 } 66 current_matrix[i] = current_row; 67 } 68 } 69 70 return changed; 71 }