matrix.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "protocol/serial.h"
  18. void matrix_init_custom(void) {
  19. serial_init();
  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. SERIAL_UART_DATA = 's';
  26. //trust the external keystates entirely, erase the last data
  27. uint8_t uart_data[13] = {0};
  28. //there are 12 bytes corresponding to 12 columns, and an end byte
  29. for (uint8_t i = 0; i < 13; 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 (!SERIAL_UART_RXD_PRESENT) {
  34. timeout++;
  35. if (timeout > 10000) {
  36. break;
  37. }
  38. }
  39. uart_data[i] = SERIAL_UART_DATA;
  40. }
  41. //check for the end packet, the key state bytes use the LSBs, so 0xE0
  42. //will only show up here if the correct bytes were recieved
  43. if (uart_data[11] == 0xE0) {
  44. //shifting and transferring the keystates to the QMK matrix variable
  45. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  46. matrix_row_t current_row = (uint16_t) uart_data[i * 2] | (uint16_t) uart_data[i * 2 + 1] << 6;
  47. if (current_matrix[i] != current_row) {
  48. changed = true;
  49. }
  50. current_matrix[i] = current_row;
  51. }
  52. }
  53. return changed;
  54. }