logo

qmk_firmware

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

matrix.c (4593B)


  1. /*
  2. Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
  3. Copyright 2016 Daniel Svensson <dsvensson@gmail.com>
  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. #include <avr/io.h>
  18. #include <util/delay.h>
  19. #include "wait.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #ifndef DEBOUNCE
  25. # define DEBOUNCE 5
  26. #endif
  27. static uint8_t debouncing = DEBOUNCE;
  28. static matrix_row_t matrix[MATRIX_ROWS];
  29. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  30. static matrix_row_t read_cols(void);
  31. static void select_row(uint8_t col);
  32. __attribute__ ((weak))
  33. void matrix_init_kb(void) {
  34. matrix_init_user();
  35. }
  36. __attribute__ ((weak))
  37. void matrix_scan_kb(void) {
  38. matrix_scan_user();
  39. }
  40. __attribute__ ((weak))
  41. void matrix_init_user(void) {
  42. }
  43. __attribute__ ((weak))
  44. void matrix_scan_user(void) {
  45. }
  46. inline uint8_t matrix_rows(void)
  47. {
  48. return MATRIX_ROWS;
  49. }
  50. inline uint8_t matrix_cols(void)
  51. {
  52. return MATRIX_COLS;
  53. }
  54. void matrix_init(void)
  55. {
  56. /* Column output pins */
  57. DDRD |= 0b01111011;
  58. /* Row input pins */
  59. DDRC &= (unsigned char) ~0b00000000;
  60. DDRB &= (unsigned char) ~0b11111111;
  61. PORTC |= 0b00000000;
  62. PORTB |= 0b11111111;
  63. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  64. matrix[i] = 0;
  65. matrix_debouncing[i] = 0;
  66. }
  67. matrix_init_kb();
  68. }
  69. uint8_t matrix_scan(void)
  70. {
  71. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  72. select_row(col);
  73. wait_us(30);
  74. matrix_row_t rows = read_cols();
  75. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  76. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  77. bool curr_bit = rows & (1<<row);
  78. if (prev_bit != curr_bit) {
  79. matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
  80. debouncing = DEBOUNCE;
  81. }
  82. }
  83. }
  84. if (debouncing) {
  85. if (--debouncing) {
  86. wait_ms(1);
  87. } else {
  88. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  89. matrix[i] = matrix_debouncing[i];
  90. }
  91. }
  92. }
  93. matrix_scan_kb();
  94. return 1;
  95. }
  96. inline
  97. bool matrix_is_on(uint8_t row, uint8_t col)
  98. {
  99. return matrix[row] & 1 << col;
  100. }
  101. inline
  102. matrix_row_t matrix_get_row(uint8_t row)
  103. {
  104. return matrix[row];
  105. }
  106. void matrix_print(void)
  107. {
  108. print("\nr/c 0123456789ABCDEF\n");
  109. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  110. print_hex8(row); print(": ");
  111. print_bin_reverse16(matrix_get_row(row));
  112. print("\n");
  113. }
  114. }
  115. static matrix_row_t read_cols(void)
  116. {
  117. return
  118. (PINB & (1 << 5) ? 0 : (matrix_row_t) 1 << 0) |
  119. (PINB & (1 << 7) ? 0 : (matrix_row_t) 1 << 1) |
  120. (PINB & (1 << 4) ? 0 : (matrix_row_t) 1 << 2) |
  121. (PINB & (1 << 6) ? 0 : (matrix_row_t) 1 << 3) |
  122. (PINB & (1 << 1) ? 0 : (matrix_row_t) 1 << 4) |
  123. (PINB & (1 << 2) ? 0 : (matrix_row_t) 1 << 5) |
  124. (PINB & (1 << 3) ? 0 : (matrix_row_t) 1 << 6) |
  125. (PINB & (1 << 0) ? 0 : (matrix_row_t) 1 << 7);
  126. }
  127. static void select_row(uint8_t col)
  128. {
  129. switch (col) {
  130. case 0:
  131. PORTD = (PORTD & ~0b01111011) | 0b00010011;
  132. break;
  133. case 1:
  134. PORTD = (PORTD & ~0b01111011) | 0b01000011;
  135. break;
  136. case 2:
  137. PORTD = (PORTD & ~0b01111011) | 0b01100000;
  138. break;
  139. case 3:
  140. PORTD = (PORTD & ~0b01111011) | 0b01111001;
  141. break;
  142. case 4:
  143. PORTD = (PORTD & ~0b01111011) | 0b01100010;
  144. break;
  145. case 5:
  146. PORTD = (PORTD & ~0b01111011) | 0b01101010;
  147. break;
  148. case 6:
  149. PORTD = (PORTD & ~0b01111011) | 0b01110001;
  150. break;
  151. case 7:
  152. PORTD = (PORTD & ~0b01111011) | 0b01101001;
  153. break;
  154. case 8:
  155. PORTD = (PORTD & ~0b01111011) | 0b01100001;
  156. break;
  157. case 9:
  158. PORTD = (PORTD & ~0b01111011) | 0b01111000;
  159. break;
  160. case 10:
  161. PORTD = (PORTD & ~0b01111011) | 0b00011011;
  162. break;
  163. case 11:
  164. PORTD = (PORTD & ~0b01111011) | 0b00100011;
  165. break;
  166. case 12:
  167. PORTD = (PORTD & ~0b01111011) | 0b00101011;
  168. break;
  169. case 13:
  170. PORTD = (PORTD & ~0b01111011) | 0b01110000;
  171. break;
  172. case 14:
  173. PORTD = (PORTD & ~0b01111011) | 0b00001011;
  174. break;
  175. case 15:
  176. PORTD = (PORTD & ~0b01111011) | 0b01101000;
  177. break;
  178. case 16:
  179. PORTD = (PORTD & ~0b01111011) | 0b00000011;
  180. break;
  181. case 17:
  182. PORTD = (PORTD & ~0b01111011) | 0b00111011;
  183. break;
  184. }
  185. }