logo

qmk_firmware

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

matrix.c (2788B)


  1. /*
  2. Copyright 2012-2020 Jun Wako, Jack Humbert, Yiancar, Takeshi Nishio
  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 "matrix.h"
  15. #define ROW_SHIFTER ((uint16_t)1)
  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. static void select_row(uint8_t row) {
  19. gpio_set_pin_output(row_pins[row]);
  20. gpio_write_pin_low(row_pins[row]);
  21. }
  22. static void unselect_row(uint8_t row) {
  23. gpio_set_pin_input_high(row_pins[row]);
  24. }
  25. static void unselect_rows(void) {
  26. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  27. gpio_set_pin_input_high(row_pins[x]);
  28. }
  29. }
  30. static void init_pins(void) {
  31. unselect_rows();
  32. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  33. gpio_set_pin_input_high(col_pins[x]);
  34. }
  35. }
  36. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  37. // Store last value of row prior to reading
  38. matrix_row_t last_row_value = current_matrix[current_row];
  39. // Clear data in matrix row
  40. current_matrix[current_row] = 0;
  41. // Select row and wait for row selecton to stabilize
  42. select_row(current_row);
  43. matrix_io_delay();
  44. // For each col...
  45. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  46. // skip reading when index equals (= pin itself)
  47. if (col_index != current_row) {
  48. // Check col pin pin_state
  49. if (gpio_read_pin(col_pins[col_index]) == 0) {
  50. // Pin LO, set col bit
  51. current_matrix[current_row] |= (ROW_SHIFTER << col_index);
  52. } else {
  53. // Pin HI, clear col bit
  54. current_matrix[current_row] &= ~(ROW_SHIFTER << col_index);
  55. }
  56. }
  57. }
  58. // Unselect row
  59. unselect_row(current_row);
  60. return (last_row_value != current_matrix[current_row]);
  61. }
  62. void matrix_init_custom(void) {
  63. // initialize key pins
  64. init_pins();
  65. }
  66. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  67. bool changed = false;
  68. // Set row, read cols
  69. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  70. changed |= read_cols_on_row(current_matrix, current_row);
  71. }
  72. return changed;
  73. }