logo

qmk_firmware

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

matrix.c (3680B)


  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  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 "util.h"
  16. #include "matrix.h"
  17. // Encoder things
  18. #define ENC_SW F7
  19. static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row);
  20. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  21. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  22. /* matrix state(1:on, 0:off) */
  23. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  24. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  25. static void select_row(uint8_t row) {
  26. gpio_set_pin_output(row_pins[row]);
  27. gpio_write_pin_low(row_pins[row]);
  28. }
  29. static void unselect_row(uint8_t row) { gpio_set_pin_input_high(row_pins[row]); }
  30. static void unselect_rows(void) {
  31. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  32. gpio_set_pin_input_high(row_pins[x]);
  33. }
  34. }
  35. static void init_pins(void) {
  36. unselect_rows();
  37. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  38. gpio_set_pin_input_high(col_pins[x]);
  39. }
  40. }
  41. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  42. // Store last value of row prior to reading
  43. matrix_row_t last_row_value = current_matrix[current_row];
  44. // Clear data in matrix row
  45. current_matrix[current_row] = 0;
  46. // Select row and wait for row selecton to stabilize
  47. select_row(current_row);
  48. wait_us(30);
  49. // For each col...
  50. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  51. // Select the col pin to read (active low)
  52. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  53. // Populate the matrix row with the state of the col pin
  54. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  55. }
  56. // Unselect row
  57. unselect_row(current_row);
  58. return (last_row_value != current_matrix[current_row]);
  59. }
  60. void matrix_init_custom(void) {
  61. // initialize key pins
  62. gpio_set_pin_input(ENC_SW);
  63. init_pins();
  64. }
  65. bool matrix_scan_custom(void) {
  66. bool changed = false;
  67. // Set row, read cols
  68. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  69. changed |= read_cols_on_row(raw_matrix, current_row);
  70. }
  71. // Read encoder switches, already debounced
  72. changed |= read_encoder_switches(matrix, 2);
  73. return changed;
  74. }
  75. static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row) {
  76. // Store last value of row prior to reading
  77. matrix_row_t last_row_value = current_matrix[current_row];
  78. // Clear data in matrix row
  79. current_matrix[current_row] = 0;
  80. // Debounce the encoder buttons using a shift register
  81. static uint8_t btn_1_array;
  82. bool btn_1_pressed = 0;
  83. btn_1_array <<= 1;
  84. btn_1_array |= gpio_read_pin(ENC_SW);
  85. (btn_1_array == 0b11111111) ? (btn_1_pressed = 1) : (btn_1_pressed = 0);
  86. // Populate the matrix row with the state of the encoder
  87. current_matrix[current_row] |= btn_1_pressed ? (1 << 0) : 0;
  88. return (last_row_value != current_matrix[current_row]);
  89. }