logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (9530B)


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