matrix.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
  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 uint8_t read_rows(void);
  29. static void select_col(uint8_t col);
  30. inline uint8_t matrix_rows(void) {
  31. return MATRIX_ROWS;
  32. }
  33. inline uint8_t matrix_cols(void) {
  34. return MATRIX_COLS;
  35. }
  36. /* Column pin configuration
  37. *
  38. * col: 0 1 2 3 4 5 6 7
  39. * pin: PC7 PD5 PD3 PD1 PC2 PD6 PD4 PD2
  40. *
  41. * Rrr pin configuration
  42. *
  43. * These rrrs uses one 74HC154 4 to 16 bit demultiplexer (low
  44. * active), together with 2 rrrs driven directly from the micro
  45. * controller, to control the 18 rrrs. The rrrs are driven from
  46. * pins B6,5,4,3,2,1,0.
  47. */
  48. void matrix_init(void) {
  49. DDRC &= ~0b10000100; // Row input pins
  50. DDRD &= ~0b01111110;
  51. PORTC |= 0b10000100;
  52. PORTD |= 0b01111110;
  53. DDRB |= 0b01111111; // Column output pins
  54. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  55. matrix[i] = 0;
  56. matrix_debouncing[i] = 0;
  57. }
  58. matrix_init_quantum();
  59. }
  60. uint8_t matrix_scan(void) {
  61. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  62. select_col(col);
  63. _delay_us(3);
  64. uint8_t rows = read_rows();
  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 (prev_bit != curr_bit) {
  69. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  70. debouncing = DEBOUNCING_DELAY;
  71. }
  72. }
  73. }
  74. if (debouncing) {
  75. if (--debouncing) {
  76. _delay_ms(1);
  77. }
  78. else {
  79. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  80. matrix[i] = matrix_debouncing[i];
  81. }
  82. }
  83. }
  84. matrix_scan_quantum();
  85. return 1;
  86. }
  87. bool matrix_is_modified(void) {
  88. if (debouncing)
  89. return false;
  90. else
  91. return true;
  92. }
  93. inline bool matrix_is_on(uint8_t row, uint8_t col) {
  94. return (matrix[row] & ((matrix_row_t)1<<col));
  95. }
  96. inline matrix_row_t matrix_get_row(uint8_t row) {
  97. return matrix[row];
  98. }
  99. void matrix_print(void) {
  100. print("\nr/c 0123456789ABCDEF\n");
  101. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  102. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  103. }
  104. }
  105. uint8_t matrix_key_count(void) {
  106. uint8_t count = 0;
  107. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  108. count += bitpop32(matrix[i]);
  109. }
  110. return count;
  111. }
  112. static uint8_t read_rows(void) {
  113. return
  114. (PINC&(1<<7) ? 0 : (1<<0)) |
  115. (PIND&(1<<5) ? 0 : (1<<1)) |
  116. (PIND&(1<<3) ? 0 : (1<<2)) |
  117. (PIND&(1<<1) ? 0 : (1<<3)) |
  118. (PINC&(1<<2) ? 0 : (1<<4)) |
  119. (PIND&(1<<2) ? 0 : (1<<5)) |
  120. (PIND&(1<<4) ? 0 : (1<<6)) |
  121. (PIND&(1<<6) ? 0 : (1<<7));
  122. }
  123. static void select_col(uint8_t col) {
  124. switch (col) {
  125. case 0: PORTB = (PORTB & ~0b01111111) | 0b01100100; break;
  126. case 1: PORTB = (PORTB & ~0b01111111) | 0b01101100; break;
  127. case 2: PORTB = (PORTB & ~0b01111111) | 0b01100010; break;
  128. case 3: PORTB = (PORTB & ~0b01111111) | 0b01111010; break;
  129. case 4: PORTB = (PORTB & ~0b01111111) | 0b01100110; break;
  130. case 5: PORTB = (PORTB & ~0b01111111) | 0b01110110; break;
  131. case 6: PORTB = (PORTB & ~0b01111111) | 0b01101110; break;
  132. case 7: PORTB = (PORTB & ~0b01111111) | 0b01111110; break;
  133. case 8: PORTB = (PORTB & ~0b01111111) | 0b01000001; break;
  134. case 9: PORTB = (PORTB & ~0b01111111) | 0b00100001; break;
  135. case 10: PORTB = (PORTB & ~0b01111111) | 0b01101010; break;
  136. case 11: PORTB = (PORTB & ~0b01111111) | 0b01110010; break;
  137. case 12: PORTB = (PORTB & ~0b01111111) | 0b01111100; break;
  138. case 13: PORTB = (PORTB & ~0b01111111) | 0b01110100; break;
  139. case 14: PORTB = (PORTB & ~0b01111111) | 0b01111000; break;
  140. case 15: PORTB = (PORTB & ~0b01111111) | 0b01110000; break;
  141. case 16: PORTB = (PORTB & ~0b01111111) | 0b01100000; break;
  142. case 17: PORTB = (PORTB & ~0b01111111) | 0b01101000; break;
  143. }
  144. }