matrix.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. Copyright 2019 worthlessowl
  3. based on work by:
  4. Jun Wako <wakojun@gmail.com>
  5. Cole Markham <cole@ccmcomputing.net>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * scan matrix
  19. */
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include "wait.h"
  23. #include "print.h"
  24. #include "debug.h"
  25. #include "util.h"
  26. #include "matrix.h"
  27. #include "timer.h"
  28. #if (MATRIX_COLS <= 8)
  29. # define print_matrix_header() print("\nr/c 01234567\n")
  30. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  31. #elif (MATRIX_COLS <= 16)
  32. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  33. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  34. #elif (MATRIX_COLS <= 32)
  35. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  36. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  37. #endif
  38. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  39. static const uint8_t col_select_pins[3] = MATRIX_COL_SELECT_PINS;
  40. static const uint8_t dat_pin = MATRIX_COL_DATA_PIN;
  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]; // raw values
  44. /* 2d array containing binary representation of its index */
  45. static const uint8_t num_in_binary[8][3] = {
  46. {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1},
  47. };
  48. static void select_col_analog(uint8_t col);
  49. static void mux_pin_control(const uint8_t binary[]);
  50. void debounce_init(void);
  51. void debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed);
  52. __attribute__((weak)) void matrix_init_user(void) {}
  53. __attribute__((weak)) void matrix_scan_user(void) {}
  54. __attribute__((weak)) void matrix_init_kb(void) {
  55. matrix_init_user();
  56. }
  57. __attribute__((weak)) void matrix_scan_kb(void) {
  58. matrix_scan_user();
  59. }
  60. inline uint8_t matrix_rows(void) {
  61. return MATRIX_ROWS;
  62. }
  63. inline uint8_t matrix_cols(void) {
  64. return MATRIX_COLS;
  65. }
  66. inline bool matrix_is_on(uint8_t row, uint8_t col) {
  67. return (matrix[row] & ((matrix_row_t)1 << col));
  68. }
  69. inline matrix_row_t matrix_get_row(uint8_t row) {
  70. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  71. // switch blocker installed and the switch is always pressed.
  72. #ifdef MATRIX_MASKED
  73. return matrix[row] & matrix_mask[row];
  74. #else
  75. return matrix[row];
  76. #endif
  77. }
  78. void matrix_print(void) {
  79. print_matrix_header();
  80. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  81. print_hex8(row);
  82. print(": ");
  83. print_matrix_row(row);
  84. print("\n");
  85. }
  86. }
  87. // uses standard row code
  88. static void select_row(uint8_t row) {
  89. gpio_set_pin_output(row_pins[row]);
  90. gpio_write_pin_low(row_pins[row]);
  91. }
  92. static void unselect_row(uint8_t row) {
  93. gpio_set_pin_input_high(row_pins[row]);
  94. }
  95. static void unselect_rows(void) {
  96. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  97. gpio_set_pin_input_high(row_pins[x]);
  98. }
  99. }
  100. static void init_pins(void) { // still need some fixing, this might not work
  101. unselect_rows(); // with the loop
  102. /*
  103. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  104. gpio_set_pin_input_high(col_pins[x]);
  105. }
  106. */
  107. gpio_set_pin_input_high(dat_pin);
  108. }
  109. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  110. // Store last value of row prior to reading
  111. matrix_row_t last_row_value = current_matrix[current_row];
  112. // Clear data in matrix row
  113. current_matrix[current_row] = 0;
  114. // Select row and wait for row selecton to stabilize
  115. select_row(current_row);
  116. wait_us(30);
  117. // For each col...
  118. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  119. // Select the col pin to read (active low)
  120. select_col_analog(col_index);
  121. wait_us(30);
  122. uint8_t pin_state = gpio_read_pin(dat_pin);
  123. // Populate the matrix row with the state of the col pin
  124. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  125. }
  126. // Unselect row
  127. unselect_row(current_row);
  128. return (last_row_value != current_matrix[current_row]);
  129. }
  130. void matrix_init(void) {
  131. // initialize key pins
  132. init_pins();
  133. // initialize matrix state: all keys off
  134. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  135. raw_matrix[i] = 0;
  136. matrix[i] = 0;
  137. }
  138. debounce_init();
  139. matrix_init_kb();
  140. gpio_set_pin_input(D5);
  141. gpio_set_pin_input(B0);
  142. }
  143. // modified for per col read matrix scan
  144. uint8_t matrix_scan(void) {
  145. bool changed = false;
  146. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  147. changed |= read_cols_on_row(raw_matrix, current_row);
  148. }
  149. debounce(raw_matrix, matrix, changed);
  150. matrix_scan_kb();
  151. return (uint8_t)changed;
  152. }
  153. /*
  154. uint8_t matrix_scan(void)
  155. {
  156. bool changed = false;
  157. #if (DIODE_DIRECTION == COL2ROW)
  158. // Set row, read cols
  159. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  160. changed |= read_cols_on_row(raw_matrix, current_row);
  161. }
  162. #endif
  163. debounce(raw_matrix, matrix, changed);
  164. matrix_scan_kb();
  165. return (uint8_t)changed;
  166. }
  167. */
  168. static void select_col_analog(uint8_t col) {
  169. switch (col) {
  170. case 0:
  171. mux_pin_control(num_in_binary[0]);
  172. break;
  173. case 1:
  174. mux_pin_control(num_in_binary[1]);
  175. break;
  176. case 2:
  177. mux_pin_control(num_in_binary[2]);
  178. break;
  179. case 3:
  180. mux_pin_control(num_in_binary[3]);
  181. break;
  182. case 4:
  183. mux_pin_control(num_in_binary[4]);
  184. break;
  185. case 5:
  186. mux_pin_control(num_in_binary[5]);
  187. break;
  188. case 6:
  189. mux_pin_control(num_in_binary[6]);
  190. break;
  191. case 7:
  192. mux_pin_control(num_in_binary[7]);
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. static void mux_pin_control(const uint8_t binary[]) {
  199. // set pin0
  200. gpio_set_pin_output(col_select_pins[0]);
  201. if (binary[2] == 0) {
  202. gpio_write_pin_low(col_select_pins[0]);
  203. } else {
  204. gpio_write_pin_high(col_select_pins[0]);
  205. }
  206. // set pin1
  207. gpio_set_pin_output(col_select_pins[1]);
  208. if (binary[1] == 0) {
  209. gpio_write_pin_low(col_select_pins[1]);
  210. } else {
  211. gpio_write_pin_high(col_select_pins[1]);
  212. }
  213. // set pin2
  214. gpio_set_pin_output(col_select_pins[2]);
  215. if (binary[0] == 0) {
  216. gpio_write_pin_low(col_select_pins[2]);
  217. } else {
  218. gpio_write_pin_high(col_select_pins[2]);
  219. }
  220. }