matrix.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // user-defined overridable functions
  30. __attribute__((weak)) void matrix_init_kb(void) {
  31. matrix_init_user();
  32. }
  33. __attribute__((weak)) void matrix_scan_kb(void) {
  34. matrix_scan_user();
  35. }
  36. __attribute__((weak)) void matrix_init_user(void) {}
  37. __attribute__((weak)) void matrix_scan_user(void) {}
  38. // helper functions
  39. inline uint8_t matrix_rows(void) {
  40. return MATRIX_ROWS;
  41. }
  42. inline uint8_t matrix_cols(void) {
  43. return MATRIX_COLS;
  44. }
  45. void matrix_init(void) {
  46. /* Column output pins */
  47. DDRD |= 0b01111011;
  48. /* Row input pins */
  49. DDRC &= ~0b10000000;
  50. DDRB &= ~0b01111111;
  51. PORTC |= 0b10000000;
  52. PORTB |= 0b01111111;
  53. for (uint8_t i = 0; i < matrix_rows(); i++) {
  54. matrix[i] = 0;
  55. matrix_debouncing[i] = 0;
  56. }
  57. matrix_init_kb();
  58. }
  59. uint8_t matrix_scan(void) {
  60. bool changed = false;
  61. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  62. select_row(col);
  63. wait_us(30);
  64. matrix_row_t rows = read_cols();
  65. for (uint8_t row = 0; row < matrix_rows(); row++) {
  66. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1 << col);
  67. bool curr_bit = rows & (1 << row);
  68. if ((changed |= prev_bit != curr_bit)) {
  69. matrix_debouncing[row] ^= (matrix_row_t)1 << col;
  70. }
  71. }
  72. }
  73. debounce(matrix_debouncing, matrix, changed);
  74. matrix_scan_kb();
  75. return (uint8_t)changed;
  76. }
  77. inline matrix_row_t matrix_get_row(uint8_t row) {
  78. return matrix[row];
  79. }
  80. void matrix_print(void) {
  81. print("\nr/c 0123456789ABCDEF\n");
  82. for (uint8_t row = 0; row < matrix_rows(); row++) {
  83. print_hex8(row);
  84. print(": ");
  85. print_bin_reverse16(matrix_get_row(row));
  86. print("\n");
  87. }
  88. }
  89. static matrix_row_t read_cols(void) {
  90. return (PINB & (1 << 5) ? 0 : ((matrix_row_t)1 << 0)) | (PINC & (1 << 7) ? 0 : ((matrix_row_t)1 << 1)) | (PINB & (1 << 4) ? 0 : ((matrix_row_t)1 << 2)) | (PINB & (1 << 6) ? 0 : ((matrix_row_t)1 << 3)) | (PINB & (1 << 1) ? 0 : ((matrix_row_t)1 << 4)) | (PINB & (1 << 2) ? 0 : ((matrix_row_t)1 << 5)) | (PINB & (1 << 3) ? 0 : ((matrix_row_t)1 << 6)) | (PINB & (1 << 0) ? 0 : ((matrix_row_t)1 << 7));
  91. }
  92. static void select_row(uint8_t col) {
  93. switch (col) {
  94. case 0:
  95. PORTD = (PORTD & ~0b01111011) | 0b00011011;
  96. break;
  97. case 1:
  98. PORTD = (PORTD & ~0b01111011) | 0b01000011;
  99. break;
  100. case 2:
  101. PORTD = (PORTD & ~0b01111011) | 0b01100000;
  102. break;
  103. case 3:
  104. PORTD = (PORTD & ~0b01111011) | 0b01111001;
  105. break;
  106. case 4:
  107. PORTD = (PORTD & ~0b01111011) | 0b01100010;
  108. break;
  109. case 5:
  110. PORTD = (PORTD & ~0b01111011) | 0b01101010;
  111. break;
  112. case 6:
  113. PORTD = (PORTD & ~0b01111011) | 0b01110001;
  114. break;
  115. case 7:
  116. PORTD = (PORTD & ~0b01111011) | 0b01101001;
  117. break;
  118. case 8:
  119. PORTD = (PORTD & ~0b01111011) | 0b01100001;
  120. break;
  121. case 9:
  122. PORTD = (PORTD & ~0b01111011) | 0b01111000;
  123. break;
  124. case 10:
  125. PORTD = (PORTD & ~0b01111011) | 0b00100011;
  126. break;
  127. case 11:
  128. PORTD = (PORTD & ~0b01111011) | 0b00101011;
  129. break;
  130. case 12:
  131. PORTD = (PORTD & ~0b01111011) | 0b00110011;
  132. break;
  133. case 13:
  134. PORTD = (PORTD & ~0b01111011) | 0b01110000;
  135. break;
  136. case 14:
  137. PORTD = (PORTD & ~0b01111011) | 0b00010011;
  138. break;
  139. case 15:
  140. PORTD = (PORTD & ~0b01111011) | 0b01101000;
  141. break;
  142. case 16:
  143. PORTD = (PORTD & ~0b01111011) | 0b00001011;
  144. break;
  145. case 17:
  146. PORTD = (PORTD & ~0b01111011) | 0b00111011;
  147. break;
  148. }
  149. }