logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (2214B)


  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "matrix.h"
  16. #include "uart.h"
  17. #define UART_MATRIX_RESPONSE_TIMEOUT 10000
  18. void matrix_init_custom(void) {
  19. uart_init(500000);
  20. }
  21. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  22. uint32_t timeout = 0;
  23. bool changed = false;
  24. //the s character requests the RF remote slave to send the matrix information
  25. uart_write('s');
  26. //trust the external keystates, erase the last set of data
  27. uint8_t uart_data[11] = {0};
  28. //there are 10 bytes corresponding to 1w columns, and an end byte
  29. for (uint8_t i = 0; i < 11; i++) {
  30. //wait for the serial data, timeout if it's been too long
  31. while (!uart_available()) {
  32. timeout++;
  33. if (timeout > UART_MATRIX_RESPONSE_TIMEOUT) {
  34. break;
  35. }
  36. }
  37. if (timeout < UART_MATRIX_RESPONSE_TIMEOUT) {
  38. uart_data[i] = uart_read();
  39. } else {
  40. uart_data[i] = 0x00;
  41. }
  42. }
  43. //check for the end packet, the key state bytes use the LSBs, so 0xE0
  44. //will only show up here if the correct bytes were recieved
  45. if (uart_data[10] == 0xE0) {
  46. //shifting and transferring the keystates to the QMK matrix variable
  47. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  48. matrix_row_t current_row = (uint16_t) uart_data[i * 2] | (uint16_t) uart_data[i * 2 + 1] << 5;
  49. if (current_matrix[i] != current_row) {
  50. changed = true;
  51. }
  52. current_matrix[i] = current_row;
  53. }
  54. }
  55. return changed;
  56. }