matrix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright 2017-2019 Mathias Andersson <wraul@dbox.se>
  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 "print.h"
  18. #include "debug.h"
  19. #include "util.h"
  20. #include "matrix.h"
  21. #include "debounce.h"
  22. #include "quantum.h"
  23. #if (MATRIX_COLS <= 8)
  24. # define print_matrix_header() print("\nr/c 01234567\n")
  25. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  26. # define ROW_SHIFTER ((uint8_t)1)
  27. #elif (MATRIX_COLS <= 16)
  28. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  29. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  30. # define ROW_SHIFTER ((uint16_t)1)
  31. #elif (MATRIX_COLS <= 32)
  32. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  33. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  34. # define ROW_SHIFTER ((uint32_t)1)
  35. #endif
  36. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  37. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  38. /* matrix state(1:on, 0:off) */
  39. static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  40. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  41. __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
  42. __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
  43. __attribute__((weak)) void matrix_init_user(void) {}
  44. __attribute__((weak)) void matrix_scan_user(void) {}
  45. inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  46. inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
  47. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  48. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  49. void matrix_print(void) {
  50. print_matrix_header();
  51. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  52. print_hex8(row);
  53. print(": ");
  54. print_matrix_row(row);
  55. print("\n");
  56. }
  57. }
  58. /* Columns 0 - 15
  59. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  60. * col / pin: PB6 PC6 PC7 PF1 PF0
  61. * 0: 0 1 0 0 0
  62. * 1: 0 1 0 0 1
  63. * 2: 0 1 0 1 0
  64. * 3: 0 1 0 1 1
  65. * 4: 0 1 1 0 0
  66. * 5: 0 1 1 0 1
  67. * 6: 0 1 1 1 0
  68. * 7: 0 1 1 1 1
  69. * 8: 1 0 0 0 0
  70. * 9: 1 0 0 0 1
  71. * 10: 1 0 0 1 0
  72. * 11: 1 0 0 1 1
  73. * 12: 1 0 1 0 0
  74. * 13: 1 0 1 0 1
  75. * 14: 1 0 1 1 0
  76. * 15: 1 0 1 1 1
  77. *
  78. * col: 16
  79. * pin: PB5
  80. */
  81. static void unselect_cols(void) {
  82. for (uint8_t x = 0; x < 6; x++) {
  83. setPinOutput(col_pins[x]);
  84. writePinLow(col_pins[x]);
  85. }
  86. }
  87. static void select_col(uint8_t col) {
  88. if (col < 16) {
  89. uint8_t c = col + 8;
  90. writePin(B6, c & 0b10000);
  91. writePin(C6, c & 0b01000);
  92. writePin(C7, c & 0b00100);
  93. writePin(F1, c & 0b00010);
  94. writePin(F0, c & 0b00001);
  95. } else {
  96. writePinHigh(B5);
  97. }
  98. }
  99. /* Row pin configuration
  100. * row: 0 1 2 3 4 5
  101. * pin: D0 D1 D2 D3 D5 B7
  102. *
  103. * Caps lock uses its own pin E2
  104. */
  105. static void init_pins(void) {
  106. unselect_cols();
  107. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  108. setPinInputHigh(row_pins[x]);
  109. }
  110. setPinInputHigh(E2);
  111. }
  112. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  113. bool matrix_changed = false;
  114. // Select col and wait for col selecton to stabilize
  115. select_col(current_col);
  116. wait_us(30);
  117. // For each row...
  118. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  119. // Store last value of row prior to reading
  120. matrix_row_t last_row_value = current_matrix[row_index];
  121. // Check row pin state
  122. // Use the otherwise unused row: 3, col: 0 for caps lock
  123. if (row_index == 3 && current_col == 0) {
  124. if (readPin(E2) == 0) {
  125. // Pin LO, set col bit
  126. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  127. } else {
  128. // Pin HI, clear col bit
  129. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  130. }
  131. } else {
  132. if (readPin(row_pins[row_index]) == 0) {
  133. // Pin HI, clear col bit
  134. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  135. } else {
  136. // Pin LO, set col bit
  137. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  138. }
  139. }
  140. // Determine if the matrix changed state
  141. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  142. matrix_changed = true;
  143. }
  144. }
  145. // Unselect cols
  146. unselect_cols();
  147. return matrix_changed;
  148. }
  149. void matrix_init(void) {
  150. // initialize key pins
  151. init_pins();
  152. // initialize matrix state: all keys off
  153. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  154. raw_matrix[i] = 0;
  155. matrix[i] = 0;
  156. }
  157. debounce_init(MATRIX_ROWS);
  158. matrix_init_kb();
  159. }
  160. uint8_t matrix_scan(void) {
  161. bool changed = false;
  162. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  163. changed |= read_rows_on_col(raw_matrix, current_col);
  164. }
  165. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  166. matrix_scan_kb();
  167. return (uint8_t)changed;
  168. }