logo

qmk_firmware

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

matrix.c (3964B)


  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 <string.h>
  18. #include "split_util.h"
  19. #include "wait.h"
  20. #define VIRT_COLS_PER_HAND 1
  21. #define PHYS_COLS_PER_HAND (MATRIX_COLS - VIRT_COLS_PER_HAND)
  22. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  23. #define COL_SHIFTER ((uint32_t)1)
  24. #define EXT_PIN_ROW 2
  25. #define EXT_PIN_COL 8
  26. // Row & column pins
  27. static uint8_t row_pins_left[MATRIX_ROWS] = MATRIX_ROW_PINS;
  28. static uint8_t col_pins_left[MATRIX_MUX_COLS] = MATRIX_COL_MUX_PINS;
  29. static uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  30. static uint8_t col_pins_right[MATRIX_MUX_COLS] = MATRIX_COL_MUX_PINS_RIGHT;
  31. static uint8_t* row_pins = row_pins_left;
  32. static uint8_t* col_pins = col_pins_left;
  33. // Internal functions
  34. static void init_pins(void) {
  35. // Set cols to outputs, low
  36. for (uint8_t pin = 0; pin < MATRIX_MUX_COLS; pin++) {
  37. gpio_set_pin_output(col_pins[pin]);
  38. }
  39. // Unselect cols
  40. for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
  41. gpio_write_pin_low(col_pins[bit]);
  42. }
  43. // Set rows to input, pullup
  44. for (uint8_t pin = 0; pin < ROWS_PER_HAND; pin++) {
  45. gpio_set_pin_input_high(row_pins[pin]);
  46. }
  47. // Set extended pin (only on right side)
  48. if (!isLeftHand) {
  49. // Set extended pin to input, pullup
  50. gpio_set_pin_input_high(MATRIX_EXT_PIN_RIGHT);
  51. }
  52. }
  53. static void select_col(uint8_t col) {
  54. // Drive demux with correct column address
  55. for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
  56. uint8_t state = (col & (0b1 << bit)) >> bit;
  57. gpio_write_pin(col_pins[bit], !state);
  58. }
  59. }
  60. static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  61. select_col(current_col % PHYS_COLS_PER_HAND);
  62. wait_us(5);
  63. // Read each row sequentially
  64. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  65. if (gpio_read_pin(row_pins[row_index]) == 0) {
  66. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  67. } else {
  68. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  69. }
  70. }
  71. }
  72. static void read_ext_pin(matrix_row_t current_matrix[]) {
  73. // Read the state of the extended matrix pin
  74. if (!isLeftHand) {
  75. if (gpio_read_pin(MATRIX_EXT_PIN_RIGHT) == 0) {
  76. current_matrix[EXT_PIN_ROW] |= (COL_SHIFTER << EXT_PIN_COL);
  77. } else {
  78. current_matrix[EXT_PIN_ROW] &= ~(COL_SHIFTER << EXT_PIN_COL);
  79. }
  80. }
  81. }
  82. void matrix_init_custom(void) {
  83. if (!isLeftHand) {
  84. row_pins = row_pins_right;
  85. col_pins = col_pins_right;
  86. }
  87. init_pins();
  88. }
  89. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  90. matrix_row_t last_matrix[MATRIX_ROWS];
  91. memcpy(last_matrix, current_matrix, sizeof(last_matrix));
  92. #ifdef DEBUG_SLOW_MATRIX
  93. // Slow for debugging
  94. wait_ms(1000);
  95. #endif
  96. // Set col, read rows
  97. for (uint8_t current_col = 0; current_col < PHYS_COLS_PER_HAND; current_col++) {
  98. read_rows_on_col(current_matrix, current_col);
  99. }
  100. // Read extended pin and store in matrix
  101. read_ext_pin(current_matrix);
  102. // Check if the matrix changed
  103. bool changed = memcmp(last_matrix, current_matrix, sizeof(last_matrix)) != 0;
  104. return changed;
  105. }