logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (6174B)


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