You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.8 KiB

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