matrix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "backlight.h" // TODO fix this dependency
  27. #ifndef DEBOUNCE
  28. # define DEBOUNCE 10
  29. #endif
  30. static uint8_t debouncing = DEBOUNCE;
  31. /* matrix state(1:on, 0:off) */
  32. static matrix_row_t matrix[MATRIX_ROWS];
  33. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  34. static matrix_row_t read_cols(void);
  35. static void init_cols(void);
  36. static void unselect_rows(void);
  37. static void select_row(uint8_t row);
  38. inline
  39. uint8_t matrix_rows(void)
  40. {
  41. return MATRIX_ROWS;
  42. }
  43. inline
  44. uint8_t matrix_cols(void)
  45. {
  46. return MATRIX_COLS;
  47. }
  48. void matrix_init(void)
  49. {
  50. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  51. MCUCR |= (1<<JTD);
  52. MCUCR |= (1<<JTD);
  53. backlight_init_ports();
  54. // Turn status LED on
  55. DDRE |= (1<<6);
  56. PORTE |= (1<<6);
  57. // initialize row and col
  58. unselect_rows();
  59. init_cols();
  60. // initialize matrix state: all keys off
  61. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  62. matrix[i] = 0;
  63. matrix_debouncing[i] = 0;
  64. }
  65. }
  66. uint8_t matrix_scan(void)
  67. {
  68. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  69. select_row(i);
  70. _delay_us(30); // without this wait read unstable value.
  71. matrix_row_t cols = read_cols();
  72. if (matrix_debouncing[i] != cols) {
  73. matrix_debouncing[i] = cols;
  74. if (debouncing) {
  75. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  76. }
  77. debouncing = DEBOUNCE;
  78. }
  79. unselect_rows();
  80. }
  81. if (debouncing) {
  82. if (--debouncing) {
  83. _delay_ms(1);
  84. } else {
  85. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = matrix_debouncing[i];
  87. }
  88. }
  89. }
  90. return 1;
  91. }
  92. bool matrix_is_modified(void)
  93. {
  94. if (debouncing) return false;
  95. return true;
  96. }
  97. inline
  98. bool matrix_is_on(uint8_t row, uint8_t col)
  99. {
  100. return (matrix[row] & ((matrix_row_t)1<col));
  101. }
  102. inline
  103. matrix_row_t matrix_get_row(uint8_t row)
  104. {
  105. return matrix[row];
  106. }
  107. void matrix_print(void)
  108. {
  109. print("\nr/c 0123456789ABCDEF\n");
  110. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  111. phex(row); print(": ");
  112. pbin_reverse16(matrix_get_row(row));
  113. print("\n");
  114. }
  115. }
  116. uint8_t matrix_key_count(void)
  117. {
  118. uint8_t count = 0;
  119. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  120. count += bitpop16(matrix[i]);
  121. }
  122. return count;
  123. }
  124. static void init_cols(void)
  125. {
  126. int B = 0, C = 0, D = 0, E = 0, F = 0;
  127. for(int x = 0; x < MATRIX_COLS; x++) {
  128. int col = COLS[x];
  129. if ((col & 0xF0) == 0x20) {
  130. B |= (1<<(col & 0x0F));
  131. } else if ((col & 0xF0) == 0x30) {
  132. C |= (1<<(col & 0x0F));
  133. } else if ((col & 0xF0) == 0x40) {
  134. D |= (1<<(col & 0x0F));
  135. } else if ((col & 0xF0) == 0x50) {
  136. E |= (1<<(col & 0x0F));
  137. } else if ((col & 0xF0) == 0x60) {
  138. F |= (1<<(col & 0x0F));
  139. }
  140. }
  141. DDRB &= ~(B); PORTB |= (B);
  142. DDRC &= ~(C); PORTC |= (C);
  143. DDRD &= ~(D); PORTD |= (D);
  144. DDRE &= ~(E); PORTE |= (E);
  145. DDRF &= ~(F); PORTF |= (F);
  146. }
  147. static matrix_row_t read_cols(void)
  148. {
  149. matrix_row_t result = 0;
  150. for(int x = 0; x < MATRIX_COLS; x++) {
  151. int col = COLS[x];
  152. if ((col & 0xF0) == 0x20) {
  153. result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
  154. } else if ((col & 0xF0) == 0x30) {
  155. result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
  156. } else if ((col & 0xF0) == 0x40) {
  157. result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
  158. } else if ((col & 0xF0) == 0x50) {
  159. result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
  160. } else if ((col & 0xF0) == 0x60) {
  161. result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
  162. }
  163. }
  164. return result;
  165. }
  166. static void unselect_rows(void)
  167. {
  168. int B = 0, C = 0, D = 0, E = 0, F = 0;
  169. for(int x = 0; x < MATRIX_ROWS; x++) {
  170. int row = ROWS[x];
  171. if ((row & 0xF0) == 0x20) {
  172. B |= (1<<(row & 0x0F));
  173. } else if ((row & 0xF0) == 0x30) {
  174. C |= (1<<(row & 0x0F));
  175. } else if ((row & 0xF0) == 0x40) {
  176. D |= (1<<(row & 0x0F));
  177. } else if ((row & 0xF0) == 0x50) {
  178. E |= (1<<(row & 0x0F));
  179. } else if ((row & 0xF0) == 0x60) {
  180. F |= (1<<(row & 0x0F));
  181. }
  182. }
  183. DDRB &= ~(B); PORTB |= (B);
  184. DDRC &= ~(C); PORTC |= (C);
  185. DDRD &= ~(D); PORTD |= (D);
  186. DDRE &= ~(E); PORTE |= (E);
  187. DDRF &= ~(F); PORTF |= (F);
  188. }
  189. static void select_row(uint8_t row)
  190. {
  191. int row_pin = ROWS[row];
  192. if ((row_pin & 0xF0) == 0x20) {
  193. DDRB |= (1<<(row_pin & 0x0F));
  194. PORTB &= ~(1<<(row_pin & 0x0F));
  195. } else if ((row_pin & 0xF0) == 0x30) {
  196. DDRC |= (1<<(row_pin & 0x0F));
  197. PORTC &= ~(1<<(row_pin & 0x0F));
  198. } else if ((row_pin & 0xF0) == 0x40) {
  199. DDRD |= (1<<(row_pin & 0x0F));
  200. PORTD &= ~(1<<(row_pin & 0x0F));
  201. } else if ((row_pin & 0xF0) == 0x50) {
  202. DDRE |= (1<<(row_pin & 0x0F));
  203. PORTE &= ~(1<<(row_pin & 0x0F));
  204. } else if ((row_pin & 0xF0) == 0x60) {
  205. DDRF |= (1<<(row_pin & 0x0F));
  206. PORTF &= ~(1<<(row_pin & 0x0F));
  207. }
  208. }