matrix.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. Copyright 2017 Cole Markham <cole@ccmcomputing.net>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #if defined(__AVR__)
  21. #include <avr/io.h>
  22. #include "pincontrol.h"
  23. #endif
  24. #include "wait.h"
  25. #include "print.h"
  26. #include "debug.h"
  27. #include "util.h"
  28. #include "matrix.h"
  29. #include "config.h"
  30. #include "timer.h"
  31. //#include "audio.h"
  32. #ifndef DEBOUNCING_DELAY
  33. # define DEBOUNCING_DELAY 5
  34. #endif
  35. #if (DEBOUNCING_DELAY > 0)
  36. static uint16_t debouncing_time;
  37. static bool debouncing = false;
  38. #endif
  39. #if (MATRIX_COLS <= 8)
  40. # define print_matrix_header() print("\nr/c 01234567\n")
  41. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  42. # define matrix_bitpop(i) bitpop(matrix[i])
  43. # define ROW_SHIFTER ((uint8_t)1)
  44. #elif (MATRIX_COLS <= 16)
  45. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  46. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  47. # define matrix_bitpop(i) bitpop16(matrix[i])
  48. # define ROW_SHIFTER ((uint16_t)1)
  49. #elif (MATRIX_COLS <= 32)
  50. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  51. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  52. # define matrix_bitpop(i) bitpop32(matrix[i])
  53. # define ROW_SHIFTER ((uint32_t)1)
  54. #endif
  55. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  56. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  57. static const uint8_t col_pins[4] = MATRIX_COL_PINS;
  58. static const uint8_t xcol_pins[MATRIX_COLS - 16] = MATRIX_XCOL_PINS;
  59. //
  60. //float init_song[][2] = SONG(QWERTY_SOUND);
  61. /* matrix state(1:on, 0:off) */
  62. static matrix_row_t matrix[MATRIX_ROWS];
  63. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  64. static void init_rows(void);
  65. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  66. static void unselect_cols(void);
  67. static void init_cols(void);
  68. static void select_col(uint8_t col);
  69. //static void demux_enable(bool enabled);
  70. __attribute__ ((weak))
  71. void matrix_init_quantum(void) {
  72. matrix_init_kb();
  73. }
  74. __attribute__ ((weak))
  75. void matrix_scan_quantum(void) {
  76. matrix_scan_kb();
  77. }
  78. __attribute__ ((weak))
  79. void matrix_init_kb(void) {
  80. matrix_init_user();
  81. }
  82. __attribute__ ((weak))
  83. void matrix_scan_kb(void) {
  84. matrix_scan_user();
  85. }
  86. __attribute__ ((weak))
  87. void matrix_init_user(void) {
  88. }
  89. __attribute__ ((weak))
  90. void matrix_scan_user(void) {
  91. }
  92. inline
  93. uint8_t matrix_rows(void)
  94. {
  95. return MATRIX_ROWS;
  96. }
  97. inline
  98. uint8_t matrix_cols(void)
  99. {
  100. return MATRIX_COLS;
  101. }
  102. void matrix_init(void)
  103. {
  104. debug_enable = true;
  105. debug_matrix = true;
  106. debug_mouse = true;
  107. dprintf("matrix init");
  108. // initialize row and col
  109. init_cols();
  110. init_rows();
  111. // PLAY_NOTE_ARRAY(init_song, false, 0);
  112. // initialize matrix state: all keys off
  113. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  114. matrix[i] = 0;
  115. matrix_debouncing[i] = 0;
  116. }
  117. matrix_init_quantum();
  118. }
  119. uint8_t _matrix_scan(void)
  120. {
  121. // Set col, read rows
  122. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  123. # if (DEBOUNCING_DELAY > 0)
  124. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  125. if (matrix_changed) {
  126. debouncing = true;
  127. debouncing_time = timer_read();
  128. }
  129. # else
  130. read_rows_on_col(matrix, current_col);
  131. # endif
  132. }
  133. // Unselect cols
  134. unselect_cols();
  135. # if (DEBOUNCING_DELAY > 0)
  136. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  137. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  138. matrix[i] = matrix_debouncing[i];
  139. }
  140. debouncing = false;
  141. }
  142. # endif
  143. return 1;
  144. }
  145. uint8_t matrix_scan(void)
  146. {
  147. uint8_t ret = _matrix_scan();
  148. matrix_scan_quantum();
  149. return ret;
  150. }
  151. bool matrix_is_modified(void)
  152. {
  153. if (debouncing) return false;
  154. return true;
  155. }
  156. inline
  157. bool matrix_is_on(uint8_t row, uint8_t col)
  158. {
  159. return (matrix[row] & ((matrix_row_t)1<<col));
  160. }
  161. inline
  162. matrix_row_t matrix_get_row(uint8_t row)
  163. {
  164. return matrix[row];
  165. }
  166. void matrix_print(void)
  167. {
  168. print("\nr/c 0123456789ABCDEF\n");
  169. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  170. phex(row); print(": ");
  171. pbin_reverse16(matrix_get_row(row));
  172. print("\n");
  173. }
  174. }
  175. uint8_t matrix_key_count(void)
  176. {
  177. uint8_t count = 0;
  178. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  179. count += bitpop16(matrix[i]);
  180. }
  181. return count;
  182. }
  183. static void init_rows(void)
  184. {
  185. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  186. uint8_t pin = row_pins[x];
  187. pinMode(pin, PinDirectionInput);
  188. digitalWrite(pin, PinLevelHigh);
  189. }
  190. }
  191. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  192. {
  193. bool matrix_changed = false;
  194. // Select col and wait for col selection to stabilize
  195. select_col(current_col);
  196. wait_us(30);
  197. // wait_ms(1000);
  198. // PLAY_NOTE_ARRAY(init_song, false, 0);
  199. // For each row...
  200. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  201. {
  202. // Store last value of row prior to reading
  203. matrix_row_t last_row_value = current_matrix[row_index];
  204. // Check row pin state
  205. uint8_t pin = row_pins[row_index];
  206. if (digitalRead(pin) == 0)
  207. {
  208. // Pin LO, set col bit
  209. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  210. }
  211. else
  212. {
  213. // Pin HI, clear col bit
  214. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  215. }
  216. // Determine if the matrix changed state
  217. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  218. {
  219. matrix_changed = true;
  220. }
  221. }
  222. return matrix_changed;
  223. }
  224. static void select_col(uint8_t col)
  225. {
  226. for (uint8_t y = 16; y < MATRIX_COLS; y++) {
  227. uint8_t pin = xcol_pins[y-16];
  228. if (y == col) {
  229. digitalWrite(pin, PinLevelLow);
  230. } else {
  231. digitalWrite(pin, PinLevelHigh);
  232. }
  233. }
  234. if (col >= 16) {
  235. // demux_enable(false);
  236. // digitalWrite(MATRIX_EN_PIN, PinLevelHigh);
  237. } else {
  238. for(uint8_t x = 0; x < 4; x++) {
  239. uint8_t pin = col_pins[x];
  240. pinMode(pin, PinDirectionOutput);
  241. digitalWrite(pin, (col >> x) & 0x1);
  242. }
  243. // demux_enable(true);
  244. digitalWrite(MATRIX_EN_PIN, PinLevelLow);
  245. }
  246. }
  247. static void init_cols(void) {
  248. for (uint8_t y = 16; y < MATRIX_COLS; y++) {
  249. uint8_t pin = xcol_pins[y-16];
  250. pinMode(pin, PinDirectionOutput);
  251. }
  252. for(uint8_t x = 0; x < 4; x++) {
  253. uint8_t pin = col_pins[x];
  254. pinMode(pin, PinDirectionOutput);
  255. }
  256. pinMode(MATRIX_EN_PIN, PinDirectionOutput);
  257. // digitalWrite(MATRIX_EN_PIN, PinLevelHigh);
  258. unselect_cols();
  259. }
  260. static void unselect_cols(void)
  261. {
  262. for (uint8_t y = 16; y < MATRIX_COLS; y++) {
  263. uint8_t pin = xcol_pins[y-16];
  264. digitalWrite(pin, PinLevelHigh);
  265. }
  266. // demux_enable(false);
  267. // digitalWrite(MATRIX_EN_PIN, PinLevelHigh);
  268. }
  269. //static void demux_enable(uint8_t state)
  270. //{
  271. // if (enabled){
  272. // digitalWrite(F7, PinLevelLow);
  273. // } else {
  274. // digitalWrite(F7, PinLevelHigh);
  275. // }
  276. //}