You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB

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