logo

qmk_firmware

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

matrix.c (3610B)


  1. /*
  2. Copyright 2012-2019 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 "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 = 0x07;
  35. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data, 1, 20);
  36. // Set Pull-up
  37. i2c_write_register((PORT_EXPANDER_ADDRESS << 1), 0x06, &send_data, 1, 20);
  38. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  39. if ( (x > 0) && (x < 12) ) {
  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. wait_us(30);
  60. // For each col...
  61. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  62. uint8_t pin_state;
  63. // Select the col pin to read (active low)
  64. switch (col_index) {
  65. case 0 :
  66. i2c_read_register((PORT_EXPANDER_ADDRESS << 1), 0x09, &pin_state, 1, 20);
  67. pin_state = pin_state & 0x01;
  68. break;
  69. case 12 :
  70. i2c_read_register((PORT_EXPANDER_ADDRESS << 1), 0x09, &pin_state, 1, 20);
  71. pin_state = pin_state & (1 << 2);
  72. break;
  73. case 13 :
  74. i2c_read_register((PORT_EXPANDER_ADDRESS << 1), 0x09, &pin_state, 1, 20);
  75. pin_state = pin_state & (1 << 1);
  76. break;
  77. default :
  78. pin_state = gpio_read_pin(col_pins[col_index]);
  79. }
  80. // Populate the matrix row with the state of the col pin
  81. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  82. }
  83. // Unselect row
  84. unselect_row(current_row);
  85. return (last_row_value != current_matrix[current_row]);
  86. }
  87. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  88. bool matrix_has_changed = false;
  89. // Set row, read cols
  90. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  91. matrix_has_changed |= read_cols_on_row(current_matrix, current_row);
  92. }
  93. return matrix_has_changed;
  94. }