logo

qmk_firmware

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

matrix.c (4633B)


  1. /*
  2. Copyright 2018 Carlos Filoteo
  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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #ifndef DEBOUNCE
  26. # define DEBOUNCE 5
  27. #endif
  28. static uint8_t debouncing = DEBOUNCE;
  29. /* matrix state(1:on, 0:off) */
  30. static matrix_row_t matrix[MATRIX_ROWS];
  31. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  32. static matrix_row_t read_cols(void);
  33. static void init_cols(void);
  34. static void unselect_rows(void);
  35. static void select_row(uint8_t row);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. // initialize row and col
  49. unselect_rows();
  50. init_cols();
  51. // initialize matrix state: all keys off
  52. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  53. matrix[i] = 0;
  54. matrix_debouncing[i] = 0;
  55. }
  56. }
  57. uint8_t matrix_scan(void)
  58. {
  59. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  60. select_row(i);
  61. _delay_us(30); // without this wait read unstable value.
  62. matrix_row_t cols = read_cols();
  63. if (matrix_debouncing[i] != cols) {
  64. matrix_debouncing[i] = cols;
  65. if (debouncing) {
  66. dprintf("bounce!: %02X\n", debouncing);
  67. }
  68. debouncing = DEBOUNCE;
  69. }
  70. unselect_rows();
  71. }
  72. if (debouncing) {
  73. if (--debouncing) {
  74. _delay_ms(1);
  75. } else {
  76. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  77. matrix[i] = matrix_debouncing[i];
  78. }
  79. }
  80. }
  81. return 1;
  82. }
  83. inline
  84. bool matrix_is_on(uint8_t row, uint8_t col)
  85. {
  86. return (matrix[row] & ((matrix_row_t)1<<col));
  87. }
  88. inline
  89. matrix_row_t matrix_get_row(uint8_t row)
  90. {
  91. return matrix[row];
  92. }
  93. void matrix_print(void)
  94. {
  95. print("\nr/c 0123456789ABCDEF\n");
  96. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  97. print_hex8(row); print(": ");
  98. print_bin_reverse16(matrix_get_row(row));
  99. print("\n");
  100. }
  101. }
  102. /* Column pin configuration
  103. * col: 0 1 2 3 4 5 6 7 8 9 10 11
  104. * pin: D7 E6 B4 B5 B6 B2 B3 B1 F7 F6 F5 F4
  105. */
  106. static void init_cols(void)
  107. {
  108. // Input with pull-up(DDR:0, PORT:1)
  109. gpio_set_pin_input_high(F4);
  110. gpio_set_pin_input_high(F5);
  111. gpio_set_pin_input_high(F6);
  112. gpio_set_pin_input_high(F7);
  113. gpio_set_pin_input_high(E6);
  114. gpio_set_pin_input_high(D7);
  115. gpio_set_pin_input_high(B1);
  116. gpio_set_pin_input_high(B2);
  117. gpio_set_pin_input_high(B3);
  118. gpio_set_pin_input_high(B4);
  119. gpio_set_pin_input_high(B5);
  120. gpio_set_pin_input_high(B6);
  121. }
  122. static matrix_row_t read_cols(void)
  123. {
  124. return (PIND&(1<<7) ? 0 : (1<<0)) |
  125. (PINE&(1<<6) ? 0 : (1<<1)) |
  126. (PINB&(1<<4) ? 0 : (1<<2)) |
  127. (PINB&(1<<5) ? 0 : (1<<3)) |
  128. (PINB&(1<<6) ? 0 : (1<<4)) |
  129. (PINB&(1<<2) ? 0 : (1<<5)) |
  130. (PINB&(1<<3) ? 0 : (1<<6)) |
  131. (PINB&(1<<1) ? 0 : (1<<7)) |
  132. (PINF&(1<<7) ? 0 : (1<<8)) |
  133. (PINF&(1<<6) ? 0 : (1<<9)) |
  134. (PINF&(1<<5) ? 0 : (1<<10)) |
  135. (PINF&(1<<4) ? 0 : (1<<11));
  136. }
  137. /* Row pin configuration
  138. * row: 0 1 2 3
  139. * pin: D1 D0 D4 C6
  140. */
  141. static void unselect_rows(void)
  142. {
  143. // Hi-Z(DDR:0, PORT:0) to unselect
  144. gpio_set_pin_input(C6);
  145. gpio_set_pin_input(D0);
  146. gpio_set_pin_input(D1);
  147. gpio_set_pin_input(D4);
  148. }
  149. static void select_row(uint8_t row)
  150. {
  151. // Output low(DDR:1, PORT:0) to select
  152. switch (row) {
  153. case 0:
  154. gpio_set_pin_output(D1);
  155. gpio_write_pin_low(D1);
  156. break;
  157. case 1:
  158. gpio_set_pin_output(D0);
  159. gpio_write_pin_low(D0);
  160. break;
  161. case 2:
  162. gpio_set_pin_output(D4);
  163. gpio_write_pin_low(D4);
  164. break;
  165. case 3:
  166. gpio_set_pin_output(C6);
  167. gpio_write_pin_low(C6);
  168. break;
  169. }
  170. }