logo

qmk_firmware

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

matrix.c (4312B)


  1. /*
  2. Copyright 2021 Matthew Dias
  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 "matrix.h"
  15. #include "wait.h"
  16. #include "i2c_master.h"
  17. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  18. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  19. static void unselect_rows(void) {
  20. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  21. gpio_set_pin_input_high(row_pins[x]);
  22. }
  23. }
  24. static void select_row(uint8_t row) {
  25. gpio_set_pin_output(row_pins[row]);
  26. gpio_write_pin_low(row_pins[row]);
  27. }
  28. static void unselect_row(uint8_t row) {
  29. gpio_set_pin_input_high(row_pins[row]);
  30. }
  31. static void init_pins(void) {
  32. unselect_rows();
  33. // Set I/O
  34. uint8_t send_data[2] = { 0xFF, 0x03};
  35. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data[0], 2, 20);
  36. // Set Pull-up
  37. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x0C, &send_data[0], 2, 20);
  38. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  39. if ( x < 8 ) {
  40. gpio_set_pin_input_high(col_pins[x]);
  41. }
  42. }
  43. }
  44. void matrix_init_custom(void) {
  45. // TODO: initialize hardware here
  46. // Initialize I2C
  47. i2c_init();
  48. // initialize key pins
  49. init_pins();
  50. wait_ms(50);
  51. }
  52. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  53. // Store last value of row prior to reading
  54. matrix_row_t last_row_value = current_matrix[current_row];
  55. // Clear data in matrix row
  56. current_matrix[current_row] = 0;
  57. // Select row and wait for row selecton to stabilize
  58. select_row(current_row);
  59. matrix_io_delay();
  60. uint8_t port_expander_col_buffer[2];
  61. i2c_read_register((PORT_EXPANDER_ADDRESS << 1), 0x12, &port_expander_col_buffer[0], 2, 20);
  62. // For each col...
  63. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  64. uint8_t pin_state;
  65. // Select the col pin to read (active low)
  66. switch (col_index) {
  67. case 8 :
  68. pin_state = port_expander_col_buffer[0] & (1 << 0);
  69. break;
  70. case 9 :
  71. pin_state = port_expander_col_buffer[0] & (1 << 1);
  72. break;
  73. case 10 :
  74. pin_state = port_expander_col_buffer[0] & (1 << 2);
  75. break;
  76. case 11 :
  77. pin_state = port_expander_col_buffer[0] & (1 << 3);
  78. break;
  79. case 12 :
  80. pin_state = port_expander_col_buffer[0] & (1 << 4);
  81. break;
  82. case 13 :
  83. pin_state = port_expander_col_buffer[0] & (1 << 5);
  84. break;
  85. case 14 :
  86. pin_state = port_expander_col_buffer[0] & (1 << 6);
  87. break;
  88. case 15 :
  89. pin_state = port_expander_col_buffer[0] & (1 << 7);
  90. break;
  91. case 16 :
  92. pin_state = port_expander_col_buffer[1] & (1 << 0);
  93. break;
  94. case 17 :
  95. pin_state = port_expander_col_buffer[1] & (1 << 1);
  96. break;
  97. default :
  98. pin_state = gpio_read_pin(col_pins[col_index]);
  99. }
  100. // Populate the matrix row with the state of the col pin
  101. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  102. }
  103. // Unselect row
  104. unselect_row(current_row);
  105. return (last_row_value != current_matrix[current_row]);
  106. }
  107. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  108. bool matrix_has_changed = false;
  109. // Set row, read cols
  110. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  111. matrix_has_changed |= read_cols_on_row(current_matrix, current_row);
  112. }
  113. return matrix_has_changed;
  114. }