matrix.c 4.5 KB

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