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.

119 lines
3.2 KiB

  1. /* Copyright 2020 sekigon-gonnoc
  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 "grs_70ec.h"
  17. #include "ec_switch_matrix.h"
  18. #include "matrix.h"
  19. #include "debug.h"
  20. #include "split_util.h"
  21. #include "transport.h"
  22. #include "debounce.h"
  23. #ifndef LOW_THRESHOLD
  24. # define LOW_THRESHOLD 200
  25. #endif
  26. #ifndef HIGH_THRESHOLD
  27. # define HIGH_THRESHOLD 300
  28. #endif
  29. #define ERROR_DISCONNECT_COUNT 20
  30. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  31. /* matrix state(1:on, 0:off) */
  32. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  33. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  34. // row offsets for each hand
  35. uint8_t thisHand, thatHand;
  36. // user-defined overridable functions
  37. __attribute__((weak)) void matrix_slave_scan_user(void) {}
  38. void matrix_init_custom(void) {
  39. split_pre_init();
  40. ecsm_config_t ecsm_config = {.low_threshold = LOW_THRESHOLD, .high_threshold = HIGH_THRESHOLD};
  41. ecsm_init(&ecsm_config);
  42. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  43. thatHand = ROWS_PER_HAND - thisHand;
  44. split_post_init();
  45. }
  46. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  47. bool updated = ecsm_matrix_scan(current_matrix);
  48. static int cnt = 0;
  49. if (cnt++ == 300) {
  50. cnt = 0;
  51. ecsm_print_matrix();
  52. print("\n");
  53. }
  54. return updated;
  55. }
  56. bool matrix_post_scan(void) {
  57. bool changed = false;
  58. if (is_keyboard_master()) {
  59. static uint8_t error_count;
  60. matrix_row_t slave_matrix[ROWS_PER_HAND] = {0};
  61. if (!transport_master(matrix + thatHand, slave_matrix)) {
  62. error_count++;
  63. if (error_count > ERROR_DISCONNECT_COUNT) {
  64. // reset other half if disconnected
  65. dprintf("Error: disconnect split half\n");
  66. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  67. matrix[thatHand + i] = 0;
  68. slave_matrix[i] = 0;
  69. }
  70. changed = true;
  71. }
  72. } else {
  73. error_count = 0;
  74. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  75. if (matrix[thatHand + i] != slave_matrix[i]) {
  76. matrix[thatHand + i] = slave_matrix[i];
  77. changed = true;
  78. }
  79. }
  80. }
  81. matrix_scan_quantum();
  82. } else {
  83. transport_slave(matrix + thatHand, matrix + thisHand);
  84. matrix_slave_scan_user();
  85. }
  86. return changed;
  87. }
  88. uint8_t matrix_scan(void) {
  89. bool changed = matrix_scan_custom(raw_matrix) || matrix_post_scan();
  90. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
  91. return changed;
  92. }