matrix.c 5.9 KB

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