matrix.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
  3. Copyright 2016 Daniel Svensson <dsvensson@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 "wait.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #include "debounce.h"
  25. static matrix_row_t matrix[MATRIX_ROWS];
  26. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  27. static matrix_row_t read_cols(void);
  28. static void select_row(uint8_t col);
  29. inline uint8_t matrix_rows(void)
  30. {
  31. return MATRIX_ROWS;
  32. }
  33. inline uint8_t matrix_cols(void)
  34. {
  35. return MATRIX_COLS;
  36. }
  37. void matrix_init(void)
  38. {
  39. /* Column output pins */
  40. DDRD |= 0b01111011;
  41. /* Row input pins */
  42. DDRC &= ~0b10000000;
  43. DDRB &= ~0b01111111;
  44. PORTC |= 0b10000000;
  45. PORTB |= 0b01111111;
  46. for (uint8_t i=0; i < matrix_rows(); i++) {
  47. matrix[i] = 0;
  48. matrix_debouncing[i] = 0;
  49. }
  50. matrix_init_quantum();
  51. }
  52. uint8_t matrix_scan(void)
  53. {
  54. bool changed = false;
  55. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  56. select_row(col);
  57. wait_us(30);
  58. matrix_row_t rows = read_cols();
  59. for (uint8_t row = 0; row < matrix_rows(); row++) {
  60. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  61. bool curr_bit = rows & (1<<row);
  62. if ((changed |= prev_bit != curr_bit)) {
  63. matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
  64. }
  65. }
  66. }
  67. debounce(matrix_debouncing, matrix, matrix_rows(), changed);
  68. matrix_scan_quantum();
  69. return (uint8_t)changed;
  70. }
  71. inline
  72. matrix_row_t matrix_get_row(uint8_t row)
  73. {
  74. return matrix[row];
  75. }
  76. void matrix_print(void)
  77. {
  78. print("\nr/c 0123456789ABCDEF\n");
  79. for (uint8_t row = 0; row < matrix_rows(); row++) {
  80. phex(row); print(": ");
  81. pbin_reverse16(matrix_get_row(row));
  82. print("\n");
  83. }
  84. }
  85. uint8_t matrix_key_count(void)
  86. {
  87. uint8_t count = 0;
  88. for (uint8_t i = 0; i < matrix_rows(); i++) {
  89. count += bitpop16(matrix_get_row(i));
  90. }
  91. return count;
  92. }
  93. static matrix_row_t read_cols(void)
  94. {
  95. return
  96. (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<0)) |
  97. (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<1)) |
  98. (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
  99. (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
  100. (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
  101. (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
  102. (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
  103. (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7));
  104. }
  105. static void select_row(uint8_t col)
  106. {
  107. switch (col) {
  108. case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
  109. case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
  110. case 2: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
  111. case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
  112. case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
  113. case 5: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
  114. case 6: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
  115. case 7: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
  116. case 8: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
  117. case 9: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
  118. case 10: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
  119. case 11: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
  120. case 12: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
  121. case 13: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
  122. case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
  123. case 15: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
  124. case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
  125. case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
  126. }
  127. }