matrix.c 7.1 KB

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