Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Copyright 2012 Jun Wako <[email protected]>
  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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "rgblight.h"
  26. #ifndef DEBOUNCE
  27. # define DEBOUNCE 5
  28. #endif
  29. static uint8_t debouncing = DEBOUNCE;
  30. /* matrix state(1:on, 0:off) */
  31. static matrix_row_t matrix[MATRIX_ROWS];
  32. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  33. static matrix_row_t read_cols(void);
  34. static void init_cols(void);
  35. static void unselect_rows(void);
  36. static void select_row(uint8_t row);
  37. inline
  38. uint8_t matrix_rows(void)
  39. {
  40. return MATRIX_ROWS;
  41. }
  42. inline
  43. uint8_t matrix_cols(void)
  44. {
  45. return MATRIX_COLS;
  46. }
  47. void matrix_init(void)
  48. {
  49. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  50. MCUCR |= (1<<JTD);
  51. MCUCR |= (1<<JTD);
  52. rgblight_init();
  53. // initialize row and col
  54. unselect_rows();
  55. init_cols();
  56. // initialize matrix state: all keys off
  57. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  58. matrix[i] = 0;
  59. matrix_debouncing[i] = 0;
  60. }
  61. }
  62. uint8_t matrix_scan(void)
  63. {
  64. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  65. select_row(i);
  66. _delay_us(30); // without this wait read unstable value.
  67. matrix_row_t cols = read_cols();
  68. if (matrix_debouncing[i] != cols) {
  69. matrix_debouncing[i] = cols;
  70. if (debouncing) {
  71. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  72. }
  73. debouncing = DEBOUNCE;
  74. }
  75. unselect_rows();
  76. }
  77. if (debouncing) {
  78. if (--debouncing) {
  79. _delay_ms(1);
  80. } else {
  81. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  82. matrix[i] = matrix_debouncing[i];
  83. }
  84. }
  85. }
  86. return 1;
  87. }
  88. bool matrix_is_modified(void)
  89. {
  90. if (debouncing) return false;
  91. return true;
  92. }
  93. inline
  94. bool matrix_is_on(uint8_t row, uint8_t col)
  95. {
  96. return (matrix[row] & ((matrix_row_t)1<<col));
  97. }
  98. inline
  99. matrix_row_t matrix_get_row(uint8_t row)
  100. {
  101. return matrix[row];
  102. }
  103. void matrix_print(void)
  104. {
  105. print("\nr/c 0123456789ABCDEF\n");
  106. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  107. phex(row); print(": ");
  108. pbin_reverse16(matrix_get_row(row));
  109. print("\n");
  110. }
  111. }
  112. uint8_t matrix_key_count(void)
  113. {
  114. uint8_t count = 0;
  115. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  116. count += bitpop16(matrix[i]);
  117. }
  118. return count;
  119. }
  120. /* Column pin configuration
  121. * col: 0 1 2 3 4 5 6 7 8 9 10 11
  122. * pin: D7 E6 B4 B5 B6 B2 B3 B1 F7 F6 F5 F4
  123. */
  124. static void init_cols(void)
  125. {
  126. // Input with pull-up(DDR:0, PORT:1)
  127. DDRF &= ~(1<<4 | 1<<5 | 1<<6 | 1<<7);
  128. PORTF |= (1<<4 | 1<<5 | 1<<6 | 1<<7);
  129. DDRE &= ~(1<<6);
  130. PORTE |= (1<<6);
  131. DDRD &= ~(1<<7);
  132. PORTD |= (1<<7);
  133. DDRB &= ~(1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6);
  134. PORTB |= (1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6);
  135. }
  136. static matrix_row_t read_cols(void)
  137. {
  138. return (PIND&(1<<7) ? 0 : (1<<0)) |
  139. (PINE&(1<<6) ? 0 : (1<<1)) |
  140. (PINB&(1<<4) ? 0 : (1<<2)) |
  141. (PINB&(1<<5) ? 0 : (1<<3)) |
  142. (PINB&(1<<6) ? 0 : (1<<4)) |
  143. (PINB&(1<<2) ? 0 : (1<<5)) |
  144. (PINB&(1<<3) ? 0 : (1<<6)) |
  145. (PINB&(1<<1) ? 0 : (1<<7)) |
  146. (PINF&(1<<7) ? 0 : (1<<8)) |
  147. (PINF&(1<<6) ? 0 : (1<<9)) |
  148. (PINF&(1<<5) ? 0 : (1<<10)) |
  149. (PINF&(1<<4) ? 0 : (1<<11));
  150. }
  151. /* Row pin configuration
  152. * row: 0 1 2 3
  153. * pin: D1 D0 D4 C6
  154. */
  155. static void unselect_rows(void)
  156. {
  157. // Hi-Z(DDR:0, PORT:0) to unselect
  158. DDRD &= ~0b00010011;
  159. PORTD &= ~0b00010011;
  160. DDRC &= ~0b01000000;
  161. PORTC &= ~0b01000000;
  162. }
  163. static void select_row(uint8_t row)
  164. {
  165. // Output low(DDR:1, PORT:0) to select
  166. switch (row) {
  167. case 0:
  168. DDRD |= (1<<1);
  169. PORTD &= ~(1<<1);
  170. break;
  171. case 1:
  172. DDRD |= (1<<0);
  173. PORTD &= ~(1<<0);
  174. break;
  175. case 2:
  176. DDRD |= (1<<4);
  177. PORTD &= ~(1<<4);
  178. break;
  179. case 3:
  180. DDRC |= (1<<6);
  181. PORTC &= ~(1<<6);
  182. break;
  183. }
  184. }