logo

qmk_firmware

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

matrix.c (4572B)


  1. /*
  2. Copyright 2017 Gabriel Young <gabeplaysdrums@live.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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #ifndef DEBOUNCE
  23. # define DEBOUNCE 5
  24. #endif
  25. static uint8_t debouncing = DEBOUNCE;
  26. static matrix_row_t matrix[MATRIX_ROWS];
  27. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  28. __attribute__ ((weak))
  29. void matrix_init_kb(void) {
  30. matrix_init_user();
  31. }
  32. __attribute__ ((weak))
  33. void matrix_scan_kb(void) {
  34. matrix_scan_user();
  35. }
  36. __attribute__ ((weak))
  37. void matrix_init_user(void) {
  38. }
  39. __attribute__ ((weak))
  40. void matrix_scan_user(void) {
  41. }
  42. static matrix_row_t scan_col(void) {
  43. return (
  44. (PINC&(1<<2) ? 0 : ((matrix_row_t)1<<0)) | \
  45. (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<1)) | \
  46. (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) | \
  47. (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<3)) | \
  48. (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) | \
  49. (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<5)) | \
  50. (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<6)) | \
  51. (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<7))
  52. );
  53. }
  54. static void select_col(uint8_t col) {
  55. switch (col) {
  56. case 0: PORTD = (PORTD & ~0b01110111) | 0b01110110; break; \
  57. case 1: PORTD = (PORTD & ~0b01110111) | 0b01100001; break; \
  58. case 2: PORTD = (PORTD & ~0b01110111) | 0b01100101; break; \
  59. case 3: PORTD = (PORTD & ~0b01110111) | 0b00000011; break; \
  60. case 4: PORTD = (PORTD & ~0b01110111) | 0b00000111; break; \
  61. case 5: PORTD = (PORTD & ~0b01110111) | 0b00010011; break; \
  62. case 6: PORTD = (PORTD & ~0b01110111) | 0b00010111; break; \
  63. case 7: PORTD = (PORTD & ~0b01110111) | 0b00100011; break; \
  64. case 8: PORTD = (PORTD & ~0b01110111) | 0b00100111; break; \
  65. case 9: PORTD = (PORTD & ~0b01110111) | 0b00110011; break; \
  66. case 10: PORTD = (PORTD & ~0b01110111) | 0b01110010; break; \
  67. case 11: PORTD = (PORTD & ~0b01110111) | 0b01100110; break; \
  68. case 12: PORTD = (PORTD & ~0b01110111) | 0b01110000; break; \
  69. case 13: PORTD = (PORTD & ~0b01110111) | 0b01100100; break; \
  70. case 14: PORTD = (PORTD & ~0b01110111) | 0b01100000; break; \
  71. case 15: PORTD = (PORTD & ~0b01110111) | 0b01000111; break; \
  72. case 16: PORTD = (PORTD & ~0b01110111) | 0b01000011; break; \
  73. case 17: PORTD = (PORTD & ~0b01110111) | 0b00110111; break;
  74. }
  75. }
  76. void matrix_init(void) {
  77. /* Column output pins */ \
  78. DDRD |= 0b01110111; \
  79. /* Row input pins */ \
  80. DDRC &= ~0b10000100; \
  81. DDRB &= ~0b01111110; \
  82. PORTC |= 0b10000100; \
  83. PORTB |= 0b01111110;
  84. for (uint8_t i=0; i < MATRIX_ROWS; i++)
  85. matrix[i] = matrix_debouncing[i] = 0;
  86. matrix_init_kb();
  87. }
  88. uint8_t matrix_scan(void) {
  89. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  90. select_col(col);
  91. _delay_us(3);
  92. matrix_row_t col_scan = scan_col();
  93. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  94. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  95. bool curr_bit = col_scan & (1<<row);
  96. if (prev_bit != curr_bit) {
  97. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  98. debouncing = DEBOUNCE;
  99. }
  100. }
  101. }
  102. if (debouncing) {
  103. if (--debouncing)
  104. _delay_ms(1);
  105. else
  106. for (uint8_t i = 0; i < MATRIX_ROWS; i++)
  107. matrix[i] = matrix_debouncing[i];
  108. }
  109. matrix_scan_kb();
  110. return 1;
  111. }
  112. inline matrix_row_t matrix_get_row(uint8_t row) {
  113. return matrix[row];
  114. }
  115. void matrix_print(void) {
  116. #ifndef NO_PRINT
  117. print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
  118. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  119. matrix_row_t matrix_row = matrix_get_row(row);
  120. xprintf("%02X: ", row);
  121. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  122. bool curr_bit = matrix_row & (1<<col);
  123. xprintf("%c", curr_bit ? '*' : '.');
  124. }
  125. print("\n");
  126. }
  127. #endif
  128. }