logo

qmk_firmware

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

matrix.c (7179B)


  1. /*
  2. Copyright 2012-2017 Jun Wako, Jack Humbert
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #if defined(__AVR__)
  17. #include <avr/io.h>
  18. #endif
  19. #include "wait.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #include "timer.h"
  25. #include "sx60.h"
  26. /* Set 0 if debouncing isn't needed */
  27. #ifndef DEBOUNCE
  28. # define DEBOUNCE 5
  29. #endif
  30. #if (DEBOUNCE > 0)
  31. static uint16_t debouncing_time;
  32. static bool debouncing = false;
  33. #endif
  34. #if (MATRIX_COLS <= 8)
  35. # define print_matrix_header() print("\nr/c 01234567\n")
  36. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  37. # define ROW_SHIFTER ((uint8_t)1)
  38. #elif (MATRIX_COLS <= 16)
  39. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  40. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  41. # define ROW_SHIFTER ((uint16_t)1)
  42. #elif (MATRIX_COLS <= 32)
  43. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  44. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  45. # define ROW_SHIFTER ((uint32_t)1)
  46. #endif
  47. #ifdef MATRIX_MASKED
  48. extern const matrix_row_t matrix_mask[];
  49. #endif
  50. static const uint8_t col_pins[ATMEGA_COLS] = MATRIX_COL_PINS;
  51. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  52. /* matrix state(1:on, 0:off) */
  53. static matrix_row_t matrix[MATRIX_ROWS];
  54. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  55. static uint8_t mcp23018_reset_loop;
  56. static void init_cols(void);
  57. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  58. static void unselect_rows(void);
  59. static void select_row(uint8_t row);
  60. __attribute__ ((weak))
  61. void matrix_init_kb(void) {
  62. matrix_init_user();
  63. }
  64. __attribute__ ((weak))
  65. void matrix_scan_kb(void) {
  66. matrix_scan_user();
  67. }
  68. __attribute__ ((weak))
  69. void matrix_init_user(void) {
  70. }
  71. __attribute__ ((weak))
  72. void matrix_scan_user(void) {
  73. }
  74. inline
  75. uint8_t matrix_rows(void) {
  76. return MATRIX_ROWS;
  77. }
  78. inline
  79. uint8_t matrix_cols(void) {
  80. return MATRIX_COLS;
  81. }
  82. void matrix_init(void) {
  83. mcp23018_status = true;
  84. /* initialize row and col */
  85. unselect_rows();
  86. init_cols();
  87. /* initialize matrix state: all keys off */
  88. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  89. matrix[i] = 0;
  90. matrix_debouncing[i] = 0;
  91. }
  92. matrix_init_kb();
  93. }
  94. uint8_t matrix_scan(void)
  95. {
  96. if (mcp23018_status) {
  97. /* if there was an error */
  98. if (++mcp23018_reset_loop == 0) {
  99. /* since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  100. this will be approx bit more frequent than once per second */
  101. print("trying to reset mcp23018\n");
  102. mcp23018_status = init_mcp23018();
  103. if (mcp23018_status) {
  104. print("left side not responding\n");
  105. } else {
  106. print("left side attached\n");
  107. }
  108. }
  109. }
  110. /* Set row, read cols */
  111. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  112. # if (DEBOUNCE > 0)
  113. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  114. if (matrix_changed) {
  115. debouncing = true;
  116. debouncing_time = timer_read();
  117. }
  118. # else
  119. read_cols_on_row(matrix, current_row);
  120. # endif
  121. }
  122. # if (DEBOUNCE > 0)
  123. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  124. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  125. matrix[i] = matrix_debouncing[i];
  126. }
  127. debouncing = false;
  128. }
  129. # endif
  130. matrix_scan_kb();
  131. return 1;
  132. }
  133. inline
  134. bool matrix_is_on(uint8_t row, uint8_t col)
  135. {
  136. return (matrix[row] & ((matrix_row_t)1<<col));
  137. }
  138. inline
  139. matrix_row_t matrix_get_row(uint8_t row)
  140. {
  141. /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  142. switch blocker installed and the switch is always pressed. */
  143. #ifdef MATRIX_MASKED
  144. return matrix[row] & matrix_mask[row];
  145. #else
  146. return matrix[row];
  147. #endif
  148. }
  149. void matrix_print(void)
  150. {
  151. print_matrix_header();
  152. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  153. print_hex8(row); print(": ");
  154. print_matrix_row(row);
  155. print("\n");
  156. }
  157. }
  158. static void init_cols(void)
  159. {
  160. for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
  161. uint8_t pin = col_pins[x];
  162. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  163. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  164. }
  165. }
  166. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  167. {
  168. /* Store last value of row prior to reading */
  169. matrix_row_t last_row_value = current_matrix[current_row];
  170. /* Clear data in matrix row */
  171. current_matrix[current_row] = 0;
  172. /* Select row and wait for row selecton to stabilize */
  173. select_row(current_row);
  174. wait_us(30);
  175. if (mcp23018_status) {
  176. /* if there was an error */
  177. return 0;
  178. } else {
  179. uint8_t data = 0;
  180. mcp23018_status = i2c_read_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
  181. if (!mcp23018_status) {
  182. current_matrix[current_row] |= (~((uint16_t)data) << 8);
  183. }
  184. }
  185. /* For each col... */
  186. for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
  187. /* Select the col pin to read (active low) */
  188. uint8_t pin = col_pins[col_index];
  189. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  190. /* Populate the matrix row with the state of the col pin */
  191. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  192. }
  193. /* Unselect row */
  194. unselect_rows();
  195. return (last_row_value != current_matrix[current_row]);
  196. }
  197. static void select_row(uint8_t row)
  198. {
  199. if (mcp23018_status) {
  200. /* if there was an error do nothing */
  201. } else {
  202. /* set active row low : 0
  203. set active row output : 1
  204. set other rows hi-Z : 1 */
  205. uint8_t port = 0xFF & ~(1<<abs(row-4));
  206. mcp23018_status = i2c_write_register(I2C_ADDR, GPIOB, &port, 1, I2C_TIMEOUT);
  207. }
  208. uint8_t pin = row_pins[row];
  209. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); /* OUT */
  210. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW */
  211. }
  212. static void unselect_rows(void)
  213. {
  214. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  215. uint8_t pin = row_pins[x];
  216. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  217. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  218. }
  219. }