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.

220 lines
6.5 KiB

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