logo

qmk_firmware

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

matrix.c (2324B)


  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 "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] << 7;
  52. if (current_matrix[i] != current_row) {
  53. changed = true;
  54. }
  55. current_matrix[i] = current_row;
  56. }
  57. }
  58. return changed;
  59. }