matrix.c 9.2 KB

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