matrix.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // TODO fix this dependency
  54. backlight_init_ports();
  55. // initialize row and col
  56. unselect_rows();
  57. init_cols();
  58. // initialize matrix state: all keys off
  59. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  60. matrix[i] = 0;
  61. matrix_debouncing[i] = 0;
  62. }
  63. }
  64. uint8_t matrix_scan(void)
  65. {
  66. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  67. select_row(i);
  68. _delay_us(30); // without this wait read unstable value.
  69. matrix_row_t cols = read_cols();
  70. if (matrix_debouncing[i] != cols) {
  71. matrix_debouncing[i] = cols;
  72. if (debouncing) {
  73. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  74. }
  75. debouncing = DEBOUNCE;
  76. }
  77. unselect_rows();
  78. }
  79. if (debouncing) {
  80. if (--debouncing) {
  81. _delay_ms(1);
  82. } else {
  83. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  84. matrix[i] = matrix_debouncing[i];
  85. }
  86. }
  87. }
  88. return 1;
  89. }
  90. bool matrix_is_modified(void)
  91. {
  92. if (debouncing) return false;
  93. return true;
  94. }
  95. inline
  96. bool matrix_is_on(uint8_t row, uint8_t col)
  97. {
  98. return (matrix[row] & ((matrix_row_t)1<col));
  99. }
  100. inline
  101. matrix_row_t matrix_get_row(uint8_t row)
  102. {
  103. return matrix[row];
  104. }
  105. void matrix_print(void)
  106. {
  107. print("\nr/c 0123456789ABCDEF\n");
  108. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  109. phex(row); print(": ");
  110. pbin_reverse16(matrix_get_row(row));
  111. print("\n");
  112. }
  113. }
  114. uint8_t matrix_key_count(void)
  115. {
  116. uint8_t count = 0;
  117. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  118. count += bitpop16(matrix[i]);
  119. }
  120. return count;
  121. }
  122. //
  123. // Planck PCB Rev 1 Pin Assignments
  124. //
  125. // Column: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  126. // Pin: F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7
  127. //
  128. static void init_cols(void)
  129. {
  130. DDRB &= ~(1<<4 | 1<<0);
  131. PORTB |= (1<<4 | 1<<0);
  132. DDRC &= ~(1<<7);
  133. PORTC |= (1<<7);
  134. DDRD &= ~(1<<7 | 1<<6 | 1<<4);
  135. PORTD |= (1<<7 | 1<<6 | 1<<4);
  136. DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  137. PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  138. }
  139. static matrix_row_t read_cols(void)
  140. {
  141. return (PINF&(1<<1) ? 0 : (1<<0)) |
  142. (PINF&(1<<0) ? 0 : (1<<1)) |
  143. (PINB&(1<<0) ? 0 : (1<<2)) |
  144. (PINC&(1<<7) ? 0 : (1<<3)) |
  145. (PINF&(1<<4) ? 0 : (1<<4)) |
  146. (PINF&(1<<5) ? 0 : (1<<5)) |
  147. (PINF&(1<<6) ? 0 : (1<<6)) |
  148. (PINF&(1<<7) ? 0 : (1<<7)) |
  149. (PIND&(1<<4) ? 0 : (1<<8)) |
  150. (PIND&(1<<6) ? 0 : (1<<9)) |
  151. (PINB&(1<<4) ? 0 : (1<<10)) |
  152. (PIND&(1<<7) ? 0 : (1<<11));
  153. }
  154. static void unselect_rows(void)
  155. {
  156. DDRB &= ~(1<<5 | 1<<6);
  157. PORTB |= (1<<5 | 1<<6);
  158. DDRD &= ~(1<<0 | 1<<5);
  159. PORTD |= (1<<0 | 1<<5);
  160. }
  161. //
  162. // Planck PCB Rev 1 Pin Assignments
  163. //
  164. // Row: 0, 1, 2, 3
  165. // Pin: D0, D5, B5, B6
  166. //
  167. static void select_row(uint8_t row)
  168. {
  169. switch (row) {
  170. case 0:
  171. DDRD |= (1<<0);
  172. PORTD &= ~(1<<0);
  173. break;
  174. case 1:
  175. DDRD |= (1<<5);
  176. PORTD &= ~(1<<5);
  177. break;
  178. case 2:
  179. DDRB |= (1<<5);
  180. PORTB &= ~(1<<5);
  181. break;
  182. case 3:
  183. DDRB |= (1<<6);
  184. PORTB &= ~(1<<6);
  185. break;
  186. }
  187. }