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.

70 lines
2.3 KiB

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