matrix.c 3.9 KB

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