matrix.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 DEBOUNCING_DELAY
  23. # define DEBOUNCING_DELAY 5
  24. #endif
  25. static uint8_t debouncing = DEBOUNCING_DELAY;
  26. static matrix_row_t matrix[MATRIX_ROWS];
  27. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  28. static matrix_row_t scan_col(void) {
  29. return (
  30. (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) |
  31. (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) |
  32. (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
  33. (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
  34. (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
  35. (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
  36. (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
  37. (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7))
  38. );
  39. }
  40. static void select_col(uint8_t col) {
  41. switch (col) {
  42. case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
  43. case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
  44. case 2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
  45. case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
  46. case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
  47. case 5: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
  48. case 6: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
  49. case 7: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
  50. case 8: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
  51. case 9: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
  52. case 10: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
  53. case 11: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
  54. case 12: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
  55. case 13: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
  56. case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
  57. case 15: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
  58. case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
  59. case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
  60. }
  61. }
  62. void matrix_init(void) {
  63. /* Row output pins */
  64. DDRD |= 0b01111011;
  65. /* Column input pins */
  66. DDRC &= ~0b10000000;
  67. DDRB &= ~0b01111111;
  68. PORTC |= 0b10000000;
  69. PORTB |= 0b01111111;
  70. for (uint8_t i=0; i < MATRIX_ROWS; i++)
  71. matrix[i] = matrix_debouncing[i] = 0;
  72. matrix_init_quantum();
  73. }
  74. uint8_t matrix_scan(void) {
  75. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  76. select_col(col);
  77. _delay_us(3);
  78. matrix_row_t col_scan = scan_col();
  79. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  80. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  81. bool curr_bit = col_scan & (1<<row);
  82. if (prev_bit != curr_bit) {
  83. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  84. debouncing = DEBOUNCING_DELAY;
  85. }
  86. }
  87. }
  88. if (debouncing) {
  89. if (--debouncing)
  90. _delay_ms(1);
  91. else
  92. for (uint8_t i = 0; i < MATRIX_ROWS; i++)
  93. matrix[i] = matrix_debouncing[i];
  94. }
  95. matrix_scan_quantum();
  96. return 1;
  97. }
  98. inline matrix_row_t matrix_get_row(uint8_t row) {
  99. return matrix[row];
  100. }
  101. void matrix_print(void) {
  102. #ifndef NO_PRINT
  103. print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
  104. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  105. matrix_row_t matrix_row = matrix_get_row(row);
  106. xprintf("%02X: ", row);
  107. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  108. bool curr_bit = matrix_row & (1<<col);
  109. xprintf("%c", curr_bit ? '*' : '.');
  110. }
  111. print("\n");
  112. }
  113. #endif
  114. }
  115. uint8_t matrix_key_count(void) {
  116. uint8_t count = 0;
  117. for (uint8_t row = 0; row < MATRIX_ROWS; row++)
  118. count += bitpop32(matrix[row]);
  119. return count;
  120. }