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.

115 lines
3.5 KiB

  1. /*
  2. Copyright 2019 Alex Ong
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "wait.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "debounce.h"
  20. #include "quantum.h"
  21. /* matrix state(1:on, 0:off) */
  22. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  23. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  24. // matrix code
  25. // super fast read_cols code.
  26. static inline matrix_row_t read_cols(void) {
  27. return (PINC & (1 << 6) ? 0 : (1UL << 0)) |
  28. (PIND & (1 << 7) ? 0 : (1UL << 1)) |
  29. (PINE & (1 << 6) ? 0 : (1UL << 2)) |
  30. (PINB & (1 << 4) ? 0 : (1UL << 3)) |
  31. (PINB & (1 << 5) ? 0 : (1UL << 4)) |
  32. (PINB & (1 << 6) ? 0 : (1UL << 5)) |
  33. (PINB & (1 << 2) ? 0 : (1UL << 6)) |
  34. (PINB & (1 << 3) ? 0 : (1UL << 7)) |
  35. (PINB & (1 << 1) ? 0 : (1UL << 8)) |
  36. (PINF & (1 << 7) ? 0 : (1UL << 9)) |
  37. (PINF & (1 << 6) ? 0 : (1UL << 10)) |
  38. (PINF & (1 << 5) ? 0 : (1UL << 11)) |
  39. (PINF & (1 << 4) ? 0 : (1UL << 12));
  40. }
  41. static void unselect_rows(void) {
  42. DDRD &= ~0b00011111;
  43. PORTD &= ~0b00011111;
  44. }
  45. static void select_row(uint8_t row) {
  46. switch (row) {
  47. case 0:
  48. DDRD |= (1 << 3);
  49. PORTD &= ~(1 << 3);
  50. break;
  51. case 1:
  52. DDRD |= (1 << 2);
  53. PORTD &= ~(1 << 2);
  54. break;
  55. case 2:
  56. DDRD |= (1 << 1);
  57. PORTD &= ~(1 << 1);
  58. break;
  59. case 3:
  60. DDRD |= (1 << 0);
  61. PORTD &= ~(1 << 0);
  62. break;
  63. case 4:
  64. DDRD |= (1 << 4);
  65. PORTD &= ~(1 << 4);
  66. break;
  67. }
  68. }
  69. static void init_pins(void) {
  70. DDRC &= ~(1 << 6);
  71. PORTC |= (1 << 6);
  72. DDRD &= ~(1 << 7);
  73. PORTD |= (1 << 7);
  74. DDRE &= ~(1 << 6);
  75. PORTE |= (1 << 6);
  76. DDRB &= ~(1 << 4 | 1 << 5 | 1 << 6 | 1 << 2 | 1 << 3 | 1 << 1);
  77. PORTB |= (1 << 4 | 1 << 5 | 1 << 6 | 1 << 2 | 1 << 3 | 1 << 1);
  78. DDRF &= ~(1 << 7 | 1 << 6 | 1 << 5 | 1 << 4);
  79. PORTF |= (1 << 7 | 1 << 6 | 1 << 5 | 1 << 4);
  80. }
  81. // Only need to init the pins. Debounce / raw matrix are initialized already for us.
  82. void matrix_init_custom(void) {
  83. // initialize key pins
  84. init_pins();
  85. }
  86. // Only need to scan the result into current_matrix, and return changed.
  87. uint8_t matrix_scan_custom(matrix_row_t current_matrix[]) {
  88. bool changed = false;
  89. // Set row, read cols
  90. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  91. select_row(current_row);
  92. matrix_output_unselect_delay(current_row, changed);
  93. matrix_row_t cols = read_cols();
  94. changed |= (current_matrix[current_row] != cols);
  95. current_matrix[current_row] = cols;
  96. unselect_rows();
  97. //this internally calls matrix_io_delay()
  98. matrix_output_unselect_delay(current_row, changed);
  99. }
  100. return changed;
  101. }