matrix.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2018 James Laird-Wah
  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 "i2c_master.h"
  18. #include <string.h>
  19. #include "model01.h"
  20. /* If no key events have occurred, the scanners will time out on reads.
  21. * So we don't want to be too permissive here. */
  22. #define I2C_TIMEOUT 10
  23. static matrix_row_t rows[MATRIX_ROWS];
  24. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  25. // user-defined overridable functions
  26. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  27. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  28. __attribute__((weak)) void matrix_init_user(void) {}
  29. __attribute__((weak)) void matrix_scan_user(void) {}
  30. // helper functions
  31. inline
  32. uint8_t matrix_rows(void) {
  33. return MATRIX_ROWS;
  34. }
  35. inline
  36. uint8_t matrix_cols(void) {
  37. return MATRIX_COLS;
  38. }
  39. static int i2c_read_hand(int hand) {
  40. uint8_t buf[5];
  41. i2c_status_t ret = i2c_receive(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
  42. if (ret != I2C_STATUS_SUCCESS)
  43. return 1;
  44. if (buf[0] != TWI_REPLY_KEYDATA)
  45. return 2;
  46. int start_row = hand ? ROWS_PER_HAND : 0;
  47. uint8_t *out = &rows[start_row];
  48. memcpy(out, &buf[1], 4);
  49. return 0;
  50. }
  51. static int i2c_set_keyscan_interval(int hand, int delay) {
  52. uint8_t buf[] = {TWI_CMD_KEYSCAN_INTERVAL, delay};
  53. i2c_status_t ret = i2c_transmit(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
  54. return ret;
  55. }
  56. void matrix_init(void) {
  57. /* Ensure scanner power is on - else right hand will not work */
  58. DDRC |= _BV(7);
  59. PORTC |= _BV(7);
  60. i2c_init();
  61. i2c_set_keyscan_interval(LEFT, 2);
  62. i2c_set_keyscan_interval(RIGHT, 2);
  63. memset(rows, 0, sizeof(rows));
  64. matrix_init_quantum();
  65. }
  66. uint8_t matrix_scan(void) {
  67. uint8_t ret = 0;
  68. ret |= i2c_read_hand(LEFT);
  69. ret |= i2c_read_hand(RIGHT);
  70. matrix_scan_quantum();
  71. return ret;
  72. }
  73. inline
  74. matrix_row_t matrix_get_row(uint8_t row) {
  75. return rows[row];
  76. }
  77. void matrix_print(void) {
  78. print("\nr/c 0123456789ABCDEF\n");
  79. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  80. print_hex8(row); print(": ");
  81. print_bin_reverse16(matrix_get_row(row));
  82. print("\n");
  83. }
  84. }
  85. /* vim: set ts=2 sw=2 et: */