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.

299 lines
7.1 KiB

  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 "config.h"
  28. #include "timer.h"
  29. #if (MATRIX_COLS <= 8)
  30. # define print_matrix_header() print("\nr/c 01234567\n")
  31. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  32. # define ROW_SHIFTER ((uint8_t)1)
  33. #elif (MATRIX_COLS <= 16)
  34. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  35. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  36. # define ROW_SHIFTER ((uint16_t)1)
  37. #elif (MATRIX_COLS <= 32)
  38. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  39. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  40. # define ROW_SHIFTER ((uint32_t)1)
  41. #endif
  42. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  43. static const uint8_t col_select_pins[3] = MATRIX_COL_SELECT_PINS;
  44. static const uint8_t dat_pin = MATRIX_COL_DATA_PIN;
  45. /* matrix state(1:on, 0:off) */
  46. static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
  47. static matrix_row_t matrix[MATRIX_ROWS]; //raw values
  48. /* 2d array containing binary representation of its index */
  49. static const uint8_t num_in_binary[8][3] = {
  50. {0, 0, 0},
  51. {0, 0, 1},
  52. {0, 1, 0},
  53. {0, 1, 1},
  54. {1, 0, 0},
  55. {1, 0, 1},
  56. {1, 1, 0},
  57. {1, 1, 1},
  58. };
  59. static void select_col_analog(uint8_t col);
  60. static void mux_pin_control(const uint8_t binary[]);
  61. void debounce_init(uint8_t num_rows);
  62. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
  63. __attribute__ ((weak))
  64. void matrix_init_user(void) {}
  65. __attribute__ ((weak))
  66. void matrix_scan_user(void) {}
  67. __attribute__ ((weak))
  68. void matrix_init_kb(void) {
  69. matrix_init_user();
  70. }
  71. __attribute__ ((weak))
  72. void matrix_scan_kb(void) {
  73. matrix_scan_user();
  74. }
  75. inline
  76. uint8_t matrix_rows(void)
  77. {
  78. return MATRIX_ROWS;
  79. }
  80. inline
  81. uint8_t matrix_cols(void)
  82. {
  83. return MATRIX_COLS;
  84. }
  85. inline
  86. bool matrix_is_on(uint8_t row, uint8_t col)
  87. {
  88. return (matrix[row] & ((matrix_row_t)1<<col));
  89. }
  90. inline
  91. matrix_row_t matrix_get_row(uint8_t row)
  92. {
  93. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  94. // switch blocker installed and the switch is always pressed.
  95. #ifdef MATRIX_MASKED
  96. return matrix[row] & matrix_mask[row];
  97. #else
  98. return matrix[row];
  99. #endif
  100. }
  101. void matrix_print(void)
  102. {
  103. print_matrix_header();
  104. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  105. print_hex8(row); print(": ");
  106. print_matrix_row(row);
  107. print("\n");
  108. }
  109. }
  110. // uses standard row code
  111. static void select_row(uint8_t row)
  112. {
  113. setPinOutput(row_pins[row]);
  114. writePinLow(row_pins[row]);
  115. }
  116. static void unselect_row(uint8_t row)
  117. {
  118. setPinInputHigh(row_pins[row]);
  119. }
  120. static void unselect_rows(void)
  121. {
  122. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  123. setPinInputHigh(row_pins[x]);
  124. }
  125. }
  126. static void init_pins(void) { // still need some fixing, this might not work
  127. unselect_rows(); // with the loop
  128. /*
  129. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  130. setPinInputHigh(col_pins[x]);
  131. }
  132. */
  133. setPinInputHigh(dat_pin);
  134. }
  135. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  136. {
  137. // Store last value of row prior to reading
  138. matrix_row_t last_row_value = current_matrix[current_row];
  139. // Clear data in matrix row
  140. current_matrix[current_row] = 0;
  141. // Select row and wait for row selecton to stabilize
  142. select_row(current_row);
  143. wait_us(30);
  144. // For each col...
  145. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  146. // Select the col pin to read (active low)
  147. select_col_analog(col_index);
  148. wait_us(30);
  149. uint8_t pin_state = gpio_read_pin(dat_pin);
  150. // Populate the matrix row with the state of the col pin
  151. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  152. }
  153. // Unselect row
  154. unselect_row(current_row);
  155. return (last_row_value != current_matrix[current_row]);
  156. }
  157. void matrix_init(void) {
  158. // initialize key pins
  159. init_pins();
  160. // initialize matrix state: all keys off
  161. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  162. raw_matrix[i] = 0;
  163. matrix[i] = 0;
  164. }
  165. debounce_init(MATRIX_ROWS);
  166. matrix_init_kb();
  167. setPinInput(D5);
  168. setPinInput(B0);
  169. }
  170. // modified for per col read matrix scan
  171. uint8_t matrix_scan(void)
  172. {
  173. bool changed = false;
  174. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  175. changed |= read_cols_on_row(raw_matrix, current_row);
  176. }
  177. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  178. matrix_scan_kb();
  179. return (uint8_t)changed;
  180. }
  181. /*
  182. uint8_t matrix_scan(void)
  183. {
  184. bool changed = false;
  185. #if (DIODE_DIRECTION == COL2ROW)
  186. // Set row, read cols
  187. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  188. changed |= read_cols_on_row(raw_matrix, current_row);
  189. }
  190. #endif
  191. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  192. matrix_scan_kb();
  193. return (uint8_t)changed;
  194. }
  195. */
  196. static void select_col_analog(uint8_t col) {
  197. switch(col) {
  198. case 0:
  199. mux_pin_control(num_in_binary[0]);
  200. break;
  201. case 1:
  202. mux_pin_control(num_in_binary[1]);
  203. break;
  204. case 2:
  205. mux_pin_control(num_in_binary[2]);
  206. break;
  207. case 3:
  208. mux_pin_control(num_in_binary[3]);
  209. break;
  210. case 4:
  211. mux_pin_control(num_in_binary[4]);
  212. break;
  213. case 5:
  214. mux_pin_control(num_in_binary[5]);
  215. break;
  216. case 6:
  217. mux_pin_control(num_in_binary[6]);
  218. break;
  219. case 7:
  220. mux_pin_control(num_in_binary[7]);
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. static void mux_pin_control(const uint8_t binary[]) {
  227. // set pin0
  228. setPinOutput(col_select_pins[0]);
  229. if(binary[2] == 0) {
  230. writePinLow(col_select_pins[0]);
  231. }
  232. else {
  233. writePinHigh(col_select_pins[0]);
  234. }
  235. // set pin1
  236. setPinOutput(col_select_pins[1]);
  237. if(binary[1] == 0) {
  238. writePinLow(col_select_pins[1]);
  239. }
  240. else {
  241. writePinHigh(col_select_pins[1]);
  242. }
  243. // set pin2
  244. setPinOutput(col_select_pins[2]);
  245. if(binary[0] == 0) {
  246. writePinLow(col_select_pins[2]);
  247. }
  248. else {
  249. writePinHigh(col_select_pins[2]);
  250. }
  251. }