logo

qmk_firmware

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

matrix.c (9571B)


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