logo

qmk_firmware

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

matrix.c (3994B)


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