logo

qmk_firmware

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

matrix.c (4871B)


  1. /*
  2. Copyright 2014 Kai Ryu <kai1103@gmail.com>
  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. #ifdef UART_RGB_ENABLE
  26. #include "uart_rgb.h"
  27. #endif
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. static uint8_t debouncing = DEBOUNCE;
  32. /* matrix state(1:on, 0:off) */
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  35. static matrix_row_t read_cols(void);
  36. static void init_cols(void);
  37. static void init_rows(void);
  38. static void unselect_rows(void);
  39. static void select_row(uint8_t row);
  40. inline
  41. uint8_t matrix_rows(void)
  42. {
  43. return MATRIX_ROWS;
  44. }
  45. inline
  46. uint8_t matrix_cols(void)
  47. {
  48. return MATRIX_COLS;
  49. }
  50. void matrix_init(void)
  51. {
  52. #ifdef UART_RGB_ENABLE
  53. uart_rgb_init();
  54. #endif
  55. // 85 REST
  56. gpio_set_pin_output(D7);
  57. gpio_write_pin_high(D7);
  58. // initialize row and col
  59. init_rows();
  60. init_cols();
  61. // initialize matrix state: all keys off
  62. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  63. matrix[i] = 0;
  64. matrix_debouncing[i] = 0;
  65. }
  66. }
  67. uint8_t matrix_scan(void)
  68. {
  69. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  70. select_row(i);
  71. _delay_us(30); // without this wait read unstable value.
  72. matrix_row_t cols = read_cols();
  73. if (matrix_debouncing[i] != cols) {
  74. matrix_debouncing[i] = cols;
  75. if (debouncing) {
  76. dprintf("bounce!: %02X\n", debouncing);
  77. }
  78. debouncing = DEBOUNCE;
  79. }
  80. unselect_rows();
  81. }
  82. if (debouncing) {
  83. if (--debouncing) {
  84. _delay_ms(1);
  85. } else {
  86. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  87. matrix[i] = matrix_debouncing[i];
  88. }
  89. }
  90. }
  91. return 1;
  92. }
  93. inline
  94. bool matrix_is_on(uint8_t row, uint8_t col)
  95. {
  96. return (matrix[row] & ((matrix_row_t)1<<col));
  97. }
  98. inline
  99. matrix_row_t matrix_get_row(uint8_t row)
  100. {
  101. return matrix[row];
  102. }
  103. void matrix_print(void)
  104. {
  105. print("\nr/c 0123456789ABCDEF\n");
  106. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  107. print_hex8(row); print(": ");
  108. print_bin_reverse16(matrix_get_row(row));
  109. print("\n");
  110. }
  111. }
  112. /* Column pin configuration
  113. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  114. * pin: F7 F6 F5 F4 F1 F0 E6 D7 D6 D5 D1 D0 B7 B6 B0 C7
  115. * */
  116. static void init_cols(void)
  117. {
  118. // Input with pull-up(DDR:0, PORT:1)
  119. DDRF &= 0b00001100;
  120. PORTF |= 0b11110011;
  121. DDRD &= 0b00011100;
  122. PORTD |= 0b11100011;
  123. gpio_set_pin_input_high(B0);
  124. gpio_set_pin_input_high(B6);
  125. gpio_set_pin_input_high(B7);
  126. gpio_set_pin_input_high(E6);
  127. gpio_set_pin_input_high(C7);
  128. }
  129. static matrix_row_t read_cols(void)
  130. {
  131. return (gpio_read_pin(F7) ? 0 : (1<<0)) |
  132. (gpio_read_pin(F6) ? 0 : (1<<1)) |
  133. (gpio_read_pin(F5) ? 0 : (1<<2)) |
  134. (gpio_read_pin(F4) ? 0 : (1<<3)) |
  135. (gpio_read_pin(F1) ? 0 : (1<<4)) |
  136. (gpio_read_pin(F0) ? 0 : (1<<5)) |
  137. (gpio_read_pin(E6) ? 0 : (1<<6)) |
  138. (gpio_read_pin(D7) ? 0 : (1<<7)) |
  139. (gpio_read_pin(D6) ? 0 : (1<<8)) |
  140. (gpio_read_pin(D5) ? 0 : (1<<9)) |
  141. (gpio_read_pin(D1) ? 0 : (1<<10)) |
  142. (gpio_read_pin(D0) ? 0 : (1<<11)) |
  143. (gpio_read_pin(B7) ? 0 : (1<<12)) |
  144. (gpio_read_pin(B6) ? 0 : (1<<13)) |
  145. (gpio_read_pin(B0) ? 0 : (1<<14)) |
  146. (gpio_read_pin(C7) ? 0 : (1<<15));
  147. }
  148. /* Row pin configuration
  149. * row: 0 1 2 3 4 5 x
  150. * pin: B3 0 1 0 1 0 1 1
  151. * B2 0 0 1 1 0 0 1
  152. * B1 0 0 0 0 1 1 1
  153. */
  154. static void init_rows(void)
  155. {
  156. gpio_set_pin_input(B1);
  157. gpio_set_pin_input(B2);
  158. gpio_set_pin_input(B3);
  159. }
  160. static void unselect_rows(void)
  161. {
  162. // Hi-Z(DDR:0, PORT:0) to unselect
  163. gpio_write_pin_high(B1);
  164. gpio_write_pin_high(B2);
  165. gpio_write_pin_high(B3);
  166. }
  167. static void select_row(uint8_t row)
  168. {
  169. // Output low(DDR:1, PORT:0) to select
  170. gpio_write_pin(B3, row & (1<<0));
  171. gpio_write_pin(B2, row & (1<<1));
  172. gpio_write_pin(B1, row & (1<<2));
  173. }