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.

111 lines
3.1 KiB

  1. /*
  2. Copyright 2021 talsu <talsu84@gmail.com>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "wait.h"
  15. #include "matrix.h"
  16. #include "quantum.h"
  17. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  18. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  19. /* Columns 0 - 3
  20. * col / pin:
  21. * 0: C7
  22. * 1: C6
  23. * 2: B6
  24. * 3: B5
  25. */
  26. static void unselect_cols(void) {
  27. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  28. setPinOutput(col_pins[col]);
  29. writePinLow(col_pins[col]);
  30. }
  31. }
  32. static void select_col(uint8_t col) {
  33. writePinHigh(col_pins[col]);
  34. }
  35. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  36. bool matrix_changed = false;
  37. // Select col and wait for col selecton to stabilize
  38. select_col(current_col);
  39. wait_us(30);
  40. // row:0 , col:0 FN key is DIRECT_PIN
  41. if (current_col == 0) {
  42. matrix_row_t last_row_value = current_matrix[0];
  43. if (readPin(row_pins[0]) == 0) {
  44. // Pin LO, set col bit
  45. current_matrix[0] |= (1 << current_col);
  46. } else {
  47. // Pin HI, clear col bit
  48. current_matrix[0] &= ~(1 << current_col);
  49. }
  50. // Determine if the matrix changed state
  51. if ((last_row_value != current_matrix[0]) && !(matrix_changed)) {
  52. matrix_changed = true;
  53. }
  54. }
  55. // other row use MATRIX
  56. for (uint8_t row_index = 1; row_index < MATRIX_ROWS; row_index++) {
  57. matrix_row_t last_row_value = current_matrix[row_index];
  58. if (readPin(row_pins[row_index]) == 0) {
  59. // Pin HI, clear col bit
  60. current_matrix[row_index] &= ~(1 << current_col);
  61. } else {
  62. // Pin LO, set col bit
  63. current_matrix[row_index] |= (1 << current_col);
  64. }
  65. // Determine if the matrix changed state
  66. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  67. matrix_changed = true;
  68. }
  69. }
  70. // Unselect cols
  71. unselect_cols();
  72. return matrix_changed;
  73. }
  74. void matrix_init_custom(void) {
  75. // initialize hardware and global matrix state here
  76. unselect_cols();
  77. // initialize key pins
  78. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  79. setPinInputHigh(row_pins[row_index]);
  80. }
  81. }
  82. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  83. bool changed = false;
  84. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  85. changed |= read_rows_on_col(current_matrix, current_col);
  86. }
  87. return changed;
  88. }