matrix.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2012 Jun Wako
  3. Generated by planckkeyboard.com (2014 Jack Humbert)
  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. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include <avr/io.h>
  21. #include <util/delay.h>
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #ifndef DEBOUNCE
  27. # define DEBOUNCE 10
  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. backlight_init_ports();
  53. // Turn status LED on
  54. DDRE |= (1<<6);
  55. PORTE |= (1<<6);
  56. // initialize row and col
  57. unselect_rows();
  58. init_cols();
  59. // initialize matrix state: all keys off
  60. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  61. matrix[i] = 0;
  62. matrix_debouncing[i] = 0;
  63. }
  64. }
  65. uint8_t matrix_scan(void)
  66. {
  67. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  68. select_row(i);
  69. _delay_us(30); // without this wait read unstable value.
  70. matrix_row_t cols = read_cols();
  71. if (matrix_debouncing[i] != cols) {
  72. matrix_debouncing[i] = cols;
  73. if (debouncing) {
  74. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  75. }
  76. debouncing = DEBOUNCE;
  77. }
  78. unselect_rows();
  79. }
  80. if (debouncing) {
  81. if (--debouncing) {
  82. _delay_ms(1);
  83. } else {
  84. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  85. matrix[i] = matrix_debouncing[i];
  86. }
  87. }
  88. }
  89. return 1;
  90. }
  91. bool matrix_is_modified(void)
  92. {
  93. if (debouncing) return false;
  94. return true;
  95. }
  96. inline
  97. bool matrix_is_on(uint8_t row, uint8_t col)
  98. {
  99. return (matrix[row] & ((matrix_row_t)1<col));
  100. }
  101. inline
  102. matrix_row_t matrix_get_row(uint8_t row)
  103. {
  104. return matrix[row];
  105. }
  106. void matrix_print(void)
  107. {
  108. print("\nr/c 0123456789ABCDEF\n");
  109. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  110. phex(row); print(": ");
  111. pbin_reverse16(matrix_get_row(row));
  112. print("\n");
  113. }
  114. }
  115. uint8_t matrix_key_count(void)
  116. {
  117. uint8_t count = 0;
  118. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  119. count += bitpop16(matrix[i]);
  120. }
  121. return count;
  122. }
  123. static void init_cols(void)
  124. {
  125. int B = 0, C = 0, D = 0, E = 0, F = 0;
  126. for(int x = 0; x < MATRIX_COLS; x++) {
  127. int col = COLS[x];
  128. if ((col & 0xF0) == 0x20) {
  129. B |= (1<<(col & 0x0F));
  130. } else if ((col & 0xF0) == 0x30) {
  131. C |= (1<<(col & 0x0F));
  132. } else if ((col & 0xF0) == 0x40) {
  133. D |= (1<<(col & 0x0F));
  134. } else if ((col & 0xF0) == 0x50) {
  135. E |= (1<<(col & 0x0F));
  136. } else if ((col & 0xF0) == 0x60) {
  137. F |= (1<<(col & 0x0F));
  138. }
  139. }
  140. DDRB &= ~(B); PORTB |= (B);
  141. DDRC &= ~(C); PORTC |= (C);
  142. DDRD &= ~(D); PORTD |= (D);
  143. DDRE &= ~(E); PORTE |= (E);
  144. DDRF &= ~(F); PORTF |= (F);
  145. }
  146. static matrix_row_t read_cols(void)
  147. {
  148. matrix_row_t result = 0;
  149. for(int x = 0; x < MATRIX_COLS; x++) {
  150. int col = COLS[x];
  151. if ((col & 0xF0) == 0x20) {
  152. result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
  153. } else if ((col & 0xF0) == 0x30) {
  154. result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
  155. } else if ((col & 0xF0) == 0x40) {
  156. result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
  157. } else if ((col & 0xF0) == 0x50) {
  158. result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
  159. } else if ((col & 0xF0) == 0x60) {
  160. result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
  161. }
  162. }
  163. return result;
  164. }
  165. static void unselect_rows(void)
  166. {
  167. int B = 0, C = 0, D = 0, E = 0, F = 0;
  168. for(int x = 0; x < MATRIX_ROWS; x++) {
  169. int row = ROWS[x];
  170. if ((row & 0xF0) == 0x20) {
  171. B |= (1<<(row & 0x0F));
  172. } else if ((row & 0xF0) == 0x30) {
  173. C |= (1<<(row & 0x0F));
  174. } else if ((row & 0xF0) == 0x40) {
  175. D |= (1<<(row & 0x0F));
  176. } else if ((row & 0xF0) == 0x50) {
  177. E |= (1<<(row & 0x0F));
  178. } else if ((row & 0xF0) == 0x60) {
  179. F |= (1<<(row & 0x0F));
  180. }
  181. }
  182. DDRB &= ~(B); PORTB |= (B);
  183. DDRC &= ~(C); PORTC |= (C);
  184. DDRD &= ~(D); PORTD |= (D);
  185. DDRE &= ~(E); PORTE |= (E);
  186. DDRF &= ~(F); PORTF |= (F);
  187. }
  188. static void select_row(uint8_t row)
  189. {
  190. int row_pin = ROWS[row];
  191. if ((row_pin & 0xF0) == 0x20) {
  192. DDRB |= (1<<(row_pin & 0x0F));
  193. PORTB &= ~(1<<(row_pin & 0x0F));
  194. } else if ((row_pin & 0xF0) == 0x30) {
  195. DDRC |= (1<<(row_pin & 0x0F));
  196. PORTC &= ~(1<<(row_pin & 0x0F));
  197. } else if ((row_pin & 0xF0) == 0x40) {
  198. DDRD |= (1<<(row_pin & 0x0F));
  199. PORTD &= ~(1<<(row_pin & 0x0F));
  200. } else if ((row_pin & 0xF0) == 0x50) {
  201. DDRE |= (1<<(row_pin & 0x0F));
  202. PORTE &= ~(1<<(row_pin & 0x0F));
  203. } else if ((row_pin & 0xF0) == 0x60) {
  204. DDRF |= (1<<(row_pin & 0x0F));
  205. PORTF &= ~(1<<(row_pin & 0x0F));
  206. }
  207. }