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.

76 lines
2.2 KiB

  1. /*
  2. * Copyright 2018-2023 Jack Humbert <jack.humb@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "matrix.h"
  18. #include "wait.h"
  19. /* matrix state(1:on, 0:off) */
  20. static pin_t matrix_row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  21. static pin_t matrix_col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  22. static matrix_row_t matrix_inverted[MATRIX_COLS];
  23. void matrix_init_custom(void) {
  24. // actual matrix setup - cols
  25. for (int i = 0; i < MATRIX_COLS; i++) {
  26. setPinOutput(matrix_col_pins[i]);
  27. writePinLow(matrix_col_pins[i]);
  28. }
  29. // rows
  30. for (int i = 0; i < MATRIX_ROWS; i++) {
  31. setPinInputLow(matrix_row_pins[i]);
  32. }
  33. }
  34. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  35. bool changed = false;
  36. // actual matrix
  37. for (int col = 0; col < MATRIX_COLS; col++) {
  38. matrix_row_t data = 0;
  39. // strobe col
  40. writePinHigh(matrix_col_pins[col]);
  41. // need wait to settle pin state
  42. wait_us(20);
  43. // read row data
  44. for (int row = 0; row < MATRIX_ROWS; row++) {
  45. data |= (readPin(matrix_row_pins[row]) << row);
  46. }
  47. // unstrobe col
  48. writePinLow(matrix_col_pins[col]);
  49. if (matrix_inverted[col] != data) {
  50. matrix_inverted[col] = data;
  51. }
  52. }
  53. for (int row = 0; row < MATRIX_ROWS; row++) {
  54. matrix_row_t old = current_matrix[row];
  55. current_matrix[row] = 0;
  56. for (int col = 0; col < MATRIX_COLS; col++) {
  57. current_matrix[row] |= ((matrix_inverted[col] & (1 << row) ? 1 : 0) << col);
  58. }
  59. changed |= old != current_matrix[row];
  60. }
  61. return changed;
  62. }