matrix.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright 2020 Jay Greco
  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 "quantum.h"
  17. #define COL_SHIFTER ((uint32_t)1)
  18. // Column pins
  19. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  20. static const uint8_t col_pins[MATRIX_MUX_COLS] = MATRIX_COL_MUX_PINS;
  21. // Internal functions
  22. static void init_pins(void) {
  23. // Set cols to outputs, low
  24. for (uint8_t pin = 0; pin < MATRIX_MUX_COLS; pin++) {
  25. setPinOutput(col_pins[pin]);
  26. }
  27. // Unselect cols
  28. for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
  29. writePinLow(col_pins[bit]);
  30. }
  31. // Set rows to input, pullup
  32. for (uint8_t pin = 0; pin < MATRIX_ROWS; pin++) {
  33. setPinInputHigh(row_pins[pin]);
  34. }
  35. }
  36. static void select_col(uint8_t col)
  37. {
  38. for (uint8_t bit = 0; bit < MATRIX_MUX_COLS; bit++) {
  39. uint8_t state = (col & (0b1 << bit)) >> bit;
  40. writePin(col_pins[bit], state);
  41. }
  42. }
  43. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  44. {
  45. bool matrix_changed = false;
  46. select_col(current_col);
  47. wait_us(5);
  48. // Read each row sequentially
  49. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  50. {
  51. matrix_row_t last_row_value = current_matrix[row_index];
  52. if (!readPin(row_pins[row_index]))
  53. {
  54. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  55. }
  56. else
  57. {
  58. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  59. }
  60. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  61. {
  62. matrix_changed = true;
  63. }
  64. }
  65. return matrix_changed;
  66. }
  67. // Matrix scan functions
  68. void matrix_init_custom(void) {
  69. init_pins();
  70. }
  71. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  72. bool changed = false;
  73. //Set col, read rows
  74. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  75. changed |= read_rows_on_col(current_matrix, current_col);
  76. }
  77. return (uint8_t)changed;
  78. }