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.

222 lines
4.5 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 = DEBOUNCE;
  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. __attribute__ ((weak))
  30. void matrix_init_kb(void) {
  31. matrix_init_user();
  32. }
  33. __attribute__ ((weak))
  34. void matrix_scan_kb(void) {
  35. matrix_scan_user();
  36. }
  37. __attribute__ ((weak))
  38. void matrix_init_user(void) {
  39. }
  40. __attribute__ ((weak))
  41. void matrix_scan_user(void) {
  42. }
  43. inline uint8_t matrix_rows(void)
  44. {
  45. return MATRIX_ROWS;
  46. }
  47. inline uint8_t matrix_cols(void)
  48. {
  49. return MATRIX_COLS;
  50. }
  51. void matrix_init(void)
  52. {
  53. /* Column output pins */
  54. DDRD |= 0b01111011;
  55. /* Row input pins */
  56. DDRC &= ~0b10000000;
  57. DDRB &= ~0b01111111;
  58. PORTC |= 0b10000000;
  59. PORTB |= 0b01111111;
  60. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  61. matrix[i] = 0;
  62. matrix_debouncing[i] = 0;
  63. }
  64. matrix_init_quantum();
  65. }
  66. uint8_t matrix_scan(void)
  67. {
  68. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  69. select_row(col);
  70. wait_us(30);
  71. matrix_row_t rows = read_cols();
  72. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  73. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  74. bool curr_bit = rows & (1<<row);
  75. if (prev_bit != curr_bit) {
  76. matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
  77. debouncing = DEBOUNCE;
  78. }
  79. }
  80. }
  81. if (debouncing) {
  82. if (--debouncing) {
  83. wait_ms(1);
  84. } else {
  85. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = matrix_debouncing[i];
  87. }
  88. }
  89. }
  90. matrix_scan_quantum();
  91. return 1;
  92. }
  93. bool matrix_is_modified(void)
  94. {
  95. if (debouncing)
  96. return false;
  97. return true;
  98. }
  99. inline
  100. bool matrix_is_on(uint8_t row, uint8_t col)
  101. {
  102. return matrix[row] & 1 << col;
  103. }
  104. inline
  105. matrix_row_t matrix_get_row(uint8_t row)
  106. {
  107. return matrix[row];
  108. }
  109. void matrix_print(void)
  110. {
  111. print("\nr/c 0123456789ABCDEF\n");
  112. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  113. print_hex8(row); print(": ");
  114. print_bin_reverse16(matrix_get_row(row));
  115. print("\n");
  116. }
  117. }
  118. uint8_t matrix_key_count(void)
  119. {
  120. uint8_t count = 0;
  121. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  122. count += bitpop16(matrix[i]);
  123. }
  124. return count;
  125. }
  126. static matrix_row_t read_cols(void)
  127. {
  128. return
  129. (PINB & (1 << 5) ? 0 : 1 << 0) |
  130. (PINC & (1 << 7) ? 0 : 1 << 1) |
  131. (PINB & (1 << 4) ? 0 : 1 << 2) |
  132. (PINB & (1 << 6) ? 0 : 1 << 3) |
  133. (PINB & (1 << 1) ? 0 : 1 << 4) |
  134. (PINB & (1 << 0) ? 0 : 1 << 5) |
  135. (PINB & (1 << 3) ? 0 : 1 << 6) |
  136. (PINB & (1 << 2) ? 0 : 1 << 7);
  137. }
  138. static void select_row(uint8_t col)
  139. {
  140. switch (col) {
  141. case 0:
  142. PORTD = (PORTD & ~0b01111011) | 0b00110011;
  143. break;
  144. case 1:
  145. PORTD = (PORTD & ~0b01111011) | 0b01110000;
  146. break;
  147. case 2:
  148. PORTD = (PORTD & ~0b01111011) | 0b00010011;
  149. break;
  150. case 3:
  151. PORTD = (PORTD & ~0b01111011) | 0b01101000;
  152. break;
  153. case 4:
  154. PORTD = (PORTD & ~0b01111011) | 0b00001011;
  155. break;
  156. case 5:
  157. PORTD = (PORTD & ~0b01111011) | 0b00111011;
  158. break;
  159. case 6:
  160. PORTD = (PORTD & ~0b01111011) | 0b01111000;
  161. break;
  162. case 7:
  163. PORTD = (PORTD & ~0b01111011) | 0b01100001;
  164. break;
  165. case 8:
  166. PORTD = (PORTD & ~0b01111011) | 0b01101001;
  167. break;
  168. case 9:
  169. PORTD = (PORTD & ~0b01111011) | 0b01110001;
  170. break;
  171. case 10:
  172. PORTD = (PORTD & ~0b01111011) | 0b01101010;
  173. break;
  174. case 11:
  175. PORTD = (PORTD & ~0b01111011) | 0b01100010;
  176. break;
  177. case 12:
  178. PORTD = (PORTD & ~0b01111011) | 0b01111001;
  179. break;
  180. case 13:
  181. PORTD = (PORTD & ~0b01111011) | 0b01100000;
  182. break;
  183. case 14:
  184. PORTD = (PORTD & ~0b01111011) | 0b01000011;
  185. break;
  186. case 15:
  187. PORTD = (PORTD & ~0b01111011) | 0b00011011;
  188. break;
  189. case 16:
  190. PORTD = (PORTD & ~0b01111011) | 0b00100011;
  191. break;
  192. case 17:
  193. PORTD = (PORTD & ~0b01111011) | 0b00101011;
  194. break;
  195. }
  196. }