logo

qmk_firmware

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

matrix.c (3158B)


  1. /* Copyright 2019
  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 "pca9555.h"
  18. // PCA9555 slave addresses
  19. #define IC1 0x20
  20. #define IC2 0x21
  21. //_____Utility funcs___________________________________________________________
  22. static void init_pins(void) {
  23. // init all rows - IC1 port0 input
  24. pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT); // same as initial state
  25. // init all cols high - IC2 all input
  26. pca9555_set_config(IC1, PCA9555_PORT1, ALL_INPUT); // same as initial state
  27. pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT); // same as initial state
  28. pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT); // same as initial state
  29. pca9555_set_output(IC1, PCA9555_PORT0, ALL_LOW);
  30. }
  31. static void select_row(uint8_t row) {
  32. // For the Southpaw Ext 65% pins 1-6 are used for the rows
  33. uint8_t pin = row;
  34. uint8_t mask = 2 << pin;
  35. pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT & (~mask));
  36. }
  37. static uint32_t read_cols(void) {
  38. //Read column inputs. Pins 13-31 are used. Split across both ICs but they are sequential
  39. uint8_t state_1 = 0;
  40. uint8_t state_2 = 0;
  41. uint8_t state_3 = 0;
  42. pca9555_read_pins(IC1, PCA9555_PORT1, &state_1);
  43. pca9555_read_pins(IC2, PCA9555_PORT0, &state_2);
  44. pca9555_read_pins(IC2, PCA9555_PORT1, &state_3);
  45. uint32_t state = ((((uint32_t)state_3 & 0b01111111) << 12) | ((uint32_t)state_2 << 4) | (((uint32_t)state_1 & 0b11110000) >> 4));
  46. return ~state;
  47. }
  48. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  49. // Store last value of row prior to reading
  50. matrix_row_t last_row_value = current_matrix[current_row];
  51. // Clear data in matrix row
  52. current_matrix[current_row] = 0;
  53. // Select row and wait for row selection to stabilize
  54. select_row(current_row);
  55. // Skip the wait_us(30); as i2c is slow enough to debounce the io changes
  56. current_matrix[current_row] = read_cols();
  57. // No need to Unselect row as the next `select_row` will blank everything
  58. return (last_row_value != current_matrix[current_row]);
  59. }
  60. //_____CUSTOM MATRIX IMPLEMENTATION____________________________________________________
  61. void matrix_init_custom(void) {
  62. pca9555_init(IC1);
  63. pca9555_init(IC2);
  64. init_pins();
  65. }
  66. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  67. bool changed = false;
  68. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  69. changed |= read_cols_on_row(current_matrix, current_row);
  70. }
  71. return changed;
  72. }