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.

93 lines
2.7 KiB

  1. /* Copyright 2021 xyzz
  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 "util.h"
  17. #include "matrix.h"
  18. #include "debounce.h"
  19. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  20. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  21. static void select_col(uint8_t col) {
  22. setPinOutput(col_pins[col]);
  23. writePinHigh(col_pins[col]);
  24. }
  25. static void unselect_col(uint8_t col) { setPinInputLow(col_pins[col]); }
  26. static void unselect_cols(void) {
  27. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  28. setPinInputLow(col_pins[x]);
  29. }
  30. }
  31. static void init_pins(void) {
  32. unselect_cols();
  33. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  34. setPinInputLow(row_pins[x]);
  35. }
  36. }
  37. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  38. bool matrix_changed = false;
  39. // Select col and wait for col selecton to stabilize
  40. select_col(current_col);
  41. matrix_io_delay();
  42. // For each row...
  43. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  44. // Store last value of row prior to reading
  45. matrix_row_t last_row_value = current_matrix[row_index];
  46. matrix_row_t current_row_value = last_row_value;
  47. // Check row pin state
  48. if (readPin(row_pins[row_index]) != 0) {
  49. // Pin LO, set col bit
  50. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  51. } else {
  52. // Pin HI, clear col bit
  53. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  54. }
  55. // Determine if the matrix changed state
  56. if ((last_row_value != current_row_value)) {
  57. matrix_changed |= true;
  58. current_matrix[row_index] = current_row_value;
  59. }
  60. }
  61. // Unselect col
  62. unselect_col(current_col);
  63. return matrix_changed;
  64. }
  65. void matrix_init_custom(void) {
  66. // initialize key pins
  67. init_pins();
  68. }
  69. uint8_t matrix_scan_custom(matrix_row_t current_matrix[]) {
  70. bool changed = false;
  71. // Set col, read rows
  72. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  73. changed |= read_rows_on_col(current_matrix, current_col);
  74. }
  75. return (uint8_t)changed;
  76. }