logo

qmk_firmware

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

matrix.c (2705B)


  1. /* Copyright 2021 Jay Greco
  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 "matrix.h"
  17. #include "wait.h"
  18. #define COL_SHIFTER ((uint32_t)1)
  19. // Column pins
  20. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  21. static const uint8_t col_pins[MATRIX_MUX_COLS] = MATRIX_COL_MUX_PINS;
  22. // Internal functions
  23. static void init_pins(void) {
  24. // Set cols to outputs, low
  25. for (uint8_t pin = 0; pin < MATRIX_MUX_COLS; pin++) {
  26. gpio_set_pin_output(col_pins[pin]);
  27. }
  28. // Unselect cols
  29. for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
  30. gpio_write_pin_low(col_pins[bit]);
  31. }
  32. // Set rows to input, pullup
  33. for (uint8_t pin = 0; pin < MATRIX_ROWS; pin++) {
  34. gpio_set_pin_input_high(row_pins[pin]);
  35. }
  36. }
  37. static void select_col(uint8_t col)
  38. {
  39. for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
  40. uint8_t state = (col & (0b1 << bit)) >> bit;
  41. gpio_write_pin(col_pins[bit], state);
  42. }
  43. }
  44. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  45. {
  46. bool matrix_changed = false;
  47. select_col(current_col);
  48. wait_us(5);
  49. // Read each row sequentially
  50. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  51. {
  52. matrix_row_t last_row_value = current_matrix[row_index];
  53. if (!gpio_read_pin(row_pins[row_index]))
  54. {
  55. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  56. }
  57. else
  58. {
  59. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  60. }
  61. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  62. {
  63. matrix_changed = true;
  64. }
  65. }
  66. return matrix_changed;
  67. }
  68. // Matrix scan functions
  69. void matrix_init_custom(void) {
  70. init_pins();
  71. }
  72. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  73. bool changed = false;
  74. //Set col, read rows
  75. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  76. changed |= read_rows_on_col(current_matrix, current_col);
  77. }
  78. return (uint8_t)changed;
  79. }