matrix.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  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. #include <stdint.h>
  16. #include <stdbool.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #include <avr/wdt.h>
  20. #include <avr/interrupt.h>
  21. #include <util/delay.h>
  22. #endif
  23. #include "wait.h"
  24. #include "print.h"
  25. #include "debug.h"
  26. #include "util.h"
  27. #include "matrix.h"
  28. #include "timer.h"
  29. #include "i2c_slave.h"
  30. #include "lufa.h"
  31. #include "quantum.h"
  32. #define SLAVE_I2C_ADDRESS 0x32
  33. /* Set 0 if debouncing isn't needed */
  34. #ifndef DEBOUNCE
  35. # define DEBOUNCE 5
  36. #endif
  37. #if (DEBOUNCE > 0)
  38. static uint16_t debouncing_time;
  39. static bool debouncing = false;
  40. #endif
  41. #if (MATRIX_COLS <= 8)
  42. # define print_matrix_header() print("\nr/c 01234567\n")
  43. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  44. # define matrix_bitpop(i) bitpop(matrix[i])
  45. # define ROW_SHIFTER ((uint8_t)1)
  46. #elif (MATRIX_COLS <= 16)
  47. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  48. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  49. # define matrix_bitpop(i) bitpop16(matrix[i])
  50. # define ROW_SHIFTER ((uint16_t)1)
  51. #elif (MATRIX_COLS <= 32)
  52. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  53. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  54. # define matrix_bitpop(i) bitpop32(matrix[i])
  55. # define ROW_SHIFTER ((uint32_t)1)
  56. #endif
  57. #ifdef MATRIX_MASKED
  58. extern const matrix_row_t matrix_mask[];
  59. #endif
  60. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  61. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  62. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  63. #endif
  64. /* matrix state(1:on, 0:off) */
  65. static matrix_row_t matrix[MATRIX_ROWS];
  66. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  67. #if (DIODE_DIRECTION == COL2ROW)
  68. static void init_cols(void);
  69. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  70. static void unselect_rows(void);
  71. static void select_row(uint8_t row);
  72. static void unselect_row(uint8_t row);
  73. #elif (DIODE_DIRECTION == ROW2COL)
  74. static void init_rows(void);
  75. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  76. static void unselect_cols(void);
  77. static void unselect_col(uint8_t col);
  78. static void select_col(uint8_t col);
  79. #endif
  80. __attribute__ ((weak))
  81. void matrix_init_quantum(void) {
  82. matrix_init_kb();
  83. }
  84. __attribute__ ((weak))
  85. void matrix_scan_quantum(void) {
  86. matrix_scan_kb();
  87. }
  88. __attribute__ ((weak))
  89. void matrix_init_kb(void) {
  90. matrix_init_user();
  91. }
  92. __attribute__ ((weak))
  93. void matrix_scan_kb(void) {
  94. matrix_scan_user();
  95. }
  96. __attribute__ ((weak))
  97. void matrix_init_user(void) {
  98. }
  99. __attribute__ ((weak))
  100. void matrix_scan_user(void) {
  101. }
  102. inline
  103. uint8_t matrix_rows(void) {
  104. return MATRIX_ROWS;
  105. }
  106. inline
  107. uint8_t matrix_cols(void) {
  108. return MATRIX_COLS;
  109. }
  110. void matrix_init(void) {
  111. // initialize row and col
  112. #if (DIODE_DIRECTION == COL2ROW)
  113. unselect_rows();
  114. init_cols();
  115. #elif (DIODE_DIRECTION == ROW2COL)
  116. unselect_cols();
  117. init_rows();
  118. #endif
  119. // initialize matrix state: all keys off
  120. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  121. matrix[i] = 0;
  122. matrix_debouncing[i] = 0;
  123. }
  124. matrix_init_quantum();
  125. }
  126. uint8_t matrix_scan(void)
  127. {
  128. #if (DIODE_DIRECTION == COL2ROW)
  129. // Set row, read cols
  130. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  131. # if (DEBOUNCE > 0)
  132. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  133. if (matrix_changed) {
  134. debouncing = true;
  135. debouncing_time = timer_read();
  136. }
  137. # else
  138. read_cols_on_row(matrix, current_row);
  139. # endif
  140. }
  141. #elif (DIODE_DIRECTION == ROW2COL)
  142. // Set col, read rows
  143. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  144. # if (DEBOUNCE > 0)
  145. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  146. if (matrix_changed) {
  147. debouncing = true;
  148. debouncing_time = timer_read();
  149. }
  150. # else
  151. read_rows_on_col(matrix, current_col);
  152. # endif
  153. }
  154. #endif
  155. # if (DEBOUNCE > 0)
  156. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  157. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  158. matrix[i] = matrix_debouncing[i];
  159. }
  160. debouncing = false;
  161. }
  162. # endif
  163. i2c_slave_reg[1] = 0x55;
  164. for (uint8_t i = 0; i < MATRIX_ROWS; i++){
  165. i2c_slave_reg[i+2] = matrix[i]; //send matrix over i2c
  166. }
  167. matrix_scan_quantum();
  168. return 1;
  169. }
  170. bool matrix_is_modified(void)
  171. {
  172. #if (DEBOUNCE > 0)
  173. if (debouncing) return false;
  174. #endif
  175. return true;
  176. }
  177. inline
  178. bool matrix_is_on(uint8_t row, uint8_t col)
  179. {
  180. return (matrix[row] & ((matrix_row_t)1<<col));
  181. }
  182. inline
  183. matrix_row_t matrix_get_row(uint8_t row)
  184. {
  185. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  186. // switch blocker installed and the switch is always pressed.
  187. #ifdef MATRIX_MASKED
  188. return matrix[row] & matrix_mask[row];
  189. #else
  190. return matrix[row];
  191. #endif
  192. }
  193. void matrix_print(void)
  194. {
  195. print_matrix_header();
  196. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  197. print_hex8(row); print(": ");
  198. print_matrix_row(row);
  199. print("\n");
  200. }
  201. }
  202. uint8_t matrix_key_count(void)
  203. {
  204. uint8_t count = 0;
  205. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  206. count += matrix_bitpop(i);
  207. }
  208. return count;
  209. }
  210. #if (DIODE_DIRECTION == COL2ROW)
  211. static void init_cols(void)
  212. {
  213. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  214. uint8_t pin = col_pins[x];
  215. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  216. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  217. }
  218. }
  219. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  220. {
  221. // Store last value of row prior to reading
  222. matrix_row_t last_row_value = current_matrix[current_row];
  223. // Clear data in matrix row
  224. current_matrix[current_row] = 0;
  225. // Select row and wait for row selecton to stabilize
  226. select_row(current_row);
  227. wait_us(30);
  228. // For each col...
  229. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  230. // Select the col pin to read (active low)
  231. uint8_t pin = col_pins[col_index];
  232. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  233. // Populate the matrix row with the state of the col pin
  234. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  235. }
  236. // Unselect row
  237. unselect_row(current_row);
  238. return (last_row_value != current_matrix[current_row]);
  239. }
  240. static void select_row(uint8_t row)
  241. {
  242. uint8_t pin = row_pins[row];
  243. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  244. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  245. }
  246. static void unselect_row(uint8_t row)
  247. {
  248. uint8_t pin = row_pins[row];
  249. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  250. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  251. }
  252. static void unselect_rows(void)
  253. {
  254. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  255. uint8_t pin = row_pins[x];
  256. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  257. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  258. }
  259. }
  260. #elif (DIODE_DIRECTION == ROW2COL)
  261. static void init_rows(void)
  262. {
  263. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  264. uint8_t pin = row_pins[x];
  265. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  266. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  267. }
  268. }
  269. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  270. {
  271. bool matrix_changed = false;
  272. // Select col and wait for col selecton to stabilize
  273. select_col(current_col);
  274. wait_us(30);
  275. // For each row...
  276. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  277. {
  278. // Store last value of row prior to reading
  279. matrix_row_t last_row_value = current_matrix[row_index];
  280. // Check row pin state
  281. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  282. {
  283. // Pin LO, set col bit
  284. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  285. }
  286. else
  287. {
  288. // Pin HI, clear col bit
  289. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  290. }
  291. // Determine if the matrix changed state
  292. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  293. {
  294. matrix_changed = true;
  295. }
  296. }
  297. // Unselect col
  298. unselect_col(current_col);
  299. return matrix_changed;
  300. }
  301. static void select_col(uint8_t col)
  302. {
  303. uint8_t pin = col_pins[col];
  304. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  305. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  306. }
  307. static void unselect_col(uint8_t col)
  308. {
  309. uint8_t pin = col_pins[col];
  310. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  311. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  312. }
  313. static void unselect_cols(void)
  314. {
  315. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  316. uint8_t pin = col_pins[x];
  317. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  318. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  319. }
  320. }
  321. #endif
  322. //this replases tmk code
  323. void matrix_setup(void){
  324. i2c_slave_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c
  325. sei(); //enable interupts
  326. }