logo

qmk_firmware

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

matrix.c (2304B)


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