logo

qmk_firmware

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

matrix.c (3165B)


  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. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  17. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  18. /* Columns 0 - 3
  19. * col / pin:
  20. * 0: C7
  21. * 1: C6
  22. * 2: B6
  23. * 3: B5
  24. */
  25. static void unselect_cols(void) {
  26. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  27. gpio_set_pin_output(col_pins[col]);
  28. gpio_write_pin_low(col_pins[col]);
  29. }
  30. }
  31. static void select_col(uint8_t col) {
  32. gpio_write_pin_high(col_pins[col]);
  33. }
  34. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  35. bool matrix_changed = false;
  36. // Select col and wait for col selecton to stabilize
  37. select_col(current_col);
  38. wait_us(30);
  39. // row:0 , col:0 FN key is DIRECT_PIN
  40. if (current_col == 0) {
  41. matrix_row_t last_row_value = current_matrix[0];
  42. if (gpio_read_pin(row_pins[0]) == 0) {
  43. // Pin LO, set col bit
  44. current_matrix[0] |= (1 << current_col);
  45. } else {
  46. // Pin HI, clear col bit
  47. current_matrix[0] &= ~(1 << current_col);
  48. }
  49. // Determine if the matrix changed state
  50. if ((last_row_value != current_matrix[0]) && !(matrix_changed)) {
  51. matrix_changed = true;
  52. }
  53. }
  54. // other row use MATRIX
  55. for (uint8_t row_index = 1; row_index < MATRIX_ROWS; row_index++) {
  56. matrix_row_t last_row_value = current_matrix[row_index];
  57. if (gpio_read_pin(row_pins[row_index]) == 0) {
  58. // Pin HI, clear col bit
  59. current_matrix[row_index] &= ~(1 << current_col);
  60. } else {
  61. // Pin LO, set col bit
  62. current_matrix[row_index] |= (1 << current_col);
  63. }
  64. // Determine if the matrix changed state
  65. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  66. matrix_changed = true;
  67. }
  68. }
  69. // Unselect cols
  70. unselect_cols();
  71. return matrix_changed;
  72. }
  73. void matrix_init_custom(void) {
  74. // initialize hardware and global matrix state here
  75. unselect_cols();
  76. // initialize key pins
  77. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  78. gpio_set_pin_input_high(row_pins[row_index]);
  79. }
  80. }
  81. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  82. bool changed = false;
  83. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  84. changed |= read_rows_on_col(current_matrix, current_col);
  85. }
  86. return changed;
  87. }