You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

  1. /*
  2. * Copyright 2020 Richard Titmuss (richard.titmuss@gmail.com)
  3. * Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include QMK_KEYBOARD_H
  19. #include "mcp23018.h"
  20. #define SPLIT_MATRIX_COLS (MATRIX_COLS / 2)
  21. #define SECONDARY_ROW_OFFSET (MATRIX_ROWS / 2)
  22. typedef uint16_t mcp23018_pin_t;
  23. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  24. static const pin_t col_pins[SPLIT_MATRIX_COLS] = MATRIX_COL_PINS;
  25. static const mcp23018_pin_t secondary_row_pins[MATRIX_ROWS] = SECONDARY_ROW_PINS;
  26. static const mcp23018_pin_t secondary_col_pins[SPLIT_MATRIX_COLS] = SECONDARY_COL_PINS;
  27. static void select_row(uint8_t row) {
  28. setPinOutput(row_pins[row]);
  29. writePinLow(row_pins[row]);
  30. }
  31. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  32. static void unselect_rows(void) {
  33. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  34. setPinInputHigh(row_pins[x]);
  35. }
  36. }
  37. static void select_secondary_row(uint8_t row) {
  38. uint8_t gpioa = 0xFF & ~secondary_row_pins[row];
  39. mcp23018_writeReg(GPIOA, &gpioa, 1);
  40. }
  41. static void init_pins(void) {
  42. unselect_rows();
  43. for (uint8_t x = 0; x < SPLIT_MATRIX_COLS; x++) {
  44. setPinInputHigh(col_pins[x]);
  45. }
  46. }
  47. static matrix_row_t read_cols(void) {
  48. matrix_row_t state = 0;
  49. // For each col...
  50. for (uint8_t col_index = 0; col_index < SPLIT_MATRIX_COLS; col_index++) {
  51. // Select the col pin to read (active low)
  52. uint8_t pin_state = readPin(col_pins[col_index]);
  53. // Populate the matrix row with the state of the col pin
  54. state |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  55. }
  56. return state;
  57. }
  58. static matrix_row_t read_secondary_cols(void) {
  59. matrix_row_t state = 0;
  60. uint8_t mcp23018_pin_state[2];
  61. if (mcp23018_readReg(GPIOA, mcp23018_pin_state, 2)) {
  62. return 0;
  63. }
  64. uint16_t pins = mcp23018_pin_state[0] | (mcp23018_pin_state[1] << 8);
  65. for (uint8_t col_index = 0; col_index < SPLIT_MATRIX_COLS; col_index++) {
  66. uint16_t pin_state = pins & (secondary_col_pins[col_index]);
  67. state |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  68. }
  69. return state;
  70. }
  71. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  72. matrix_row_t last_row_value = current_matrix[current_row];
  73. select_row(current_row);
  74. select_secondary_row(current_row);
  75. current_matrix[current_row] = read_cols() | (read_secondary_cols() << 6);
  76. unselect_row(current_row);
  77. return (last_row_value != current_matrix[current_row]);
  78. }
  79. void matrix_init_custom(void) { init_pins(); }
  80. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  81. bool changed = false;
  82. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  83. changed |= read_cols_on_row(current_matrix, current_row);
  84. }
  85. return changed;
  86. }