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.

204 lines
4.3 KiB

  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. static uint8_t debouncing = DEBOUNCING_DELAY;
  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. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  55. select_row(col);
  56. wait_us(30);
  57. matrix_row_t rows = read_cols();
  58. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  59. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  60. bool curr_bit = rows & (1<<row);
  61. if (prev_bit != curr_bit) {
  62. matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
  63. debouncing = DEBOUNCING_DELAY;
  64. }
  65. }
  66. }
  67. if (debouncing) {
  68. if (--debouncing) {
  69. wait_ms(1);
  70. } else {
  71. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  72. matrix[i] = matrix_debouncing[i];
  73. }
  74. }
  75. }
  76. matrix_scan_quantum();
  77. return 1;
  78. }
  79. bool matrix_is_modified(void)
  80. {
  81. if (debouncing)
  82. return false;
  83. return true;
  84. }
  85. inline
  86. bool matrix_is_on(uint8_t row, uint8_t col)
  87. {
  88. return matrix[row] & 1 << col;
  89. }
  90. inline
  91. matrix_row_t matrix_get_row(uint8_t row)
  92. {
  93. return matrix[row];
  94. }
  95. void matrix_print(void)
  96. {
  97. print("\nr/c 0123456789ABCDEF\n");
  98. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  99. phex(row); print(": ");
  100. pbin_reverse16(matrix_get_row(row));
  101. print("\n");
  102. }
  103. }
  104. uint8_t matrix_key_count(void)
  105. {
  106. uint8_t count = 0;
  107. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  108. count += bitpop16(matrix[i]);
  109. }
  110. return count;
  111. }
  112. static matrix_row_t read_cols(void)
  113. {
  114. return
  115. (PINB & (1 << 5) ? 0 : 1 << 0) |
  116. (PINC & (1 << 7) ? 0 : 1 << 1) |
  117. (PINB & (1 << 4) ? 0 : 1 << 2) |
  118. (PINB & (1 << 6) ? 0 : 1 << 3) |
  119. (PINB & (1 << 1) ? 0 : 1 << 4) |
  120. (PINB & (1 << 0) ? 0 : 1 << 5) |
  121. (PINB & (1 << 3) ? 0 : 1 << 6) |
  122. (PINB & (1 << 2) ? 0 : 1 << 7);
  123. }
  124. static void select_row(uint8_t col)
  125. {
  126. switch (col) {
  127. case 0:
  128. PORTD = (PORTD & ~0b01111011) | 0b00110011;
  129. break;
  130. case 1:
  131. PORTD = (PORTD & ~0b01111011) | 0b01110000;
  132. break;
  133. case 2:
  134. PORTD = (PORTD & ~0b01111011) | 0b00010011;
  135. break;
  136. case 3:
  137. PORTD = (PORTD & ~0b01111011) | 0b01101000;
  138. break;
  139. case 4:
  140. PORTD = (PORTD & ~0b01111011) | 0b00001011;
  141. break;
  142. case 5:
  143. PORTD = (PORTD & ~0b01111011) | 0b00111011;
  144. break;
  145. case 6:
  146. PORTD = (PORTD & ~0b01111011) | 0b01111000;
  147. break;
  148. case 7:
  149. PORTD = (PORTD & ~0b01111011) | 0b01100001;
  150. break;
  151. case 8:
  152. PORTD = (PORTD & ~0b01111011) | 0b01101001;
  153. break;
  154. case 9:
  155. PORTD = (PORTD & ~0b01111011) | 0b01110001;
  156. break;
  157. case 10:
  158. PORTD = (PORTD & ~0b01111011) | 0b01101010;
  159. break;
  160. case 11:
  161. PORTD = (PORTD & ~0b01111011) | 0b01100010;
  162. break;
  163. case 12:
  164. PORTD = (PORTD & ~0b01111011) | 0b01111001;
  165. break;
  166. case 13:
  167. PORTD = (PORTD & ~0b01111011) | 0b01100000;
  168. break;
  169. case 14:
  170. PORTD = (PORTD & ~0b01111011) | 0b01000011;
  171. break;
  172. case 15:
  173. PORTD = (PORTD & ~0b01111011) | 0b00011011;
  174. break;
  175. case 16:
  176. PORTD = (PORTD & ~0b01111011) | 0b00100011;
  177. break;
  178. case 17:
  179. PORTD = (PORTD & ~0b01111011) | 0b00101011;
  180. break;
  181. }
  182. }