matrix.c 4.5 KB

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