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.

92 lines
3.2 KiB

  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 cols high - IC2 all input
  24. pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT); // same as initial state
  25. pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT); // same as initial state
  26. // init all rows - IC1 port0 input
  27. pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT); // same as initial state
  28. pca9555_set_output(IC1, PCA9555_PORT0, ALL_LOW);
  29. }
  30. static void select_row(uint8_t row) {
  31. // For the XD84 all rows are on the same IC and port
  32. // so its safe enough to assume here row == pin
  33. uint8_t pin = row;
  34. uint8_t mask = 1 << pin;
  35. // we configure output once in init, as pca9555 remembers state when flipping between input/output
  36. // pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH & (~mask));
  37. pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT & (~mask));
  38. }
  39. static uint16_t read_cols(void) {
  40. // uint16_t state_1 = pca9555_read_pins(IC2, PCA9555_PORT0);
  41. // uint16_t state_2 = pca9555_read_pins(IC2, PCA9555_PORT1);
  42. uint16_t state = 0;
  43. pca9555_read_pins_all(IC2, &state);
  44. // For the XD84 all cols are on the same IC and mapped sequentially
  45. // while this technically gives 16 column reads,
  46. // the 16th column can never be set so is safely ignored
  47. // return ~((state_2 << 8) | state_1);
  48. return ~state;
  49. }
  50. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  51. // Store last value of row prior to reading
  52. matrix_row_t last_row_value = current_matrix[current_row];
  53. // Clear data in matrix row
  54. current_matrix[current_row] = 0;
  55. // Select row and wait for row selection to stabilize
  56. select_row(current_row);
  57. // Skip the wait_us(30); as i2c is slow enough to debounce the io changes
  58. current_matrix[current_row] = read_cols();
  59. // No need to Unselect row as the next `select_row` will blank everything
  60. return (last_row_value != current_matrix[current_row]);
  61. }
  62. //_____CUSTOM MATRIX IMPLEMENTATION____________________________________________________
  63. void matrix_init_custom(void) {
  64. pca9555_init(IC1);
  65. pca9555_init(IC2);
  66. init_pins();
  67. }
  68. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  69. bool changed = false;
  70. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  71. changed |= read_cols_on_row(current_matrix, current_row);
  72. }
  73. return changed;
  74. }