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.

296 lines
7.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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. #if DIODE_DIRECTION == ROW2COL
  34. static matrix_row_t matrix_reversed[MATRIX_COLS];
  35. static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
  36. #endif
  37. static matrix_row_t read_cols(void);
  38. static void init_cols(void);
  39. static void unselect_rows(void);
  40. static void select_row(uint8_t row);
  41. inline
  42. uint8_t matrix_rows(void)
  43. {
  44. return MATRIX_ROWS;
  45. }
  46. inline
  47. uint8_t matrix_cols(void)
  48. {
  49. return MATRIX_COLS;
  50. }
  51. void matrix_init(void)
  52. {
  53. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  54. MCUCR |= (1<<JTD);
  55. MCUCR |= (1<<JTD);
  56. backlight_init_ports();
  57. // Turn status LED on
  58. DDRE |= (1<<6);
  59. PORTE |= (1<<6);
  60. // initialize row and col
  61. unselect_rows();
  62. init_cols();
  63. // initialize matrix state: all keys off
  64. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  65. matrix[i] = 0;
  66. matrix_debouncing[i] = 0;
  67. }
  68. }
  69. uint8_t matrix_scan(void)
  70. {
  71. #if DIODE_DIRECTION == COL2ROW
  72. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  73. select_row(i);
  74. _delay_us(30); // without this wait read unstable value.
  75. matrix_row_t cols = read_cols();
  76. if (matrix_debouncing[i] != cols) {
  77. matrix_debouncing[i] = cols;
  78. if (debouncing) {
  79. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  80. }
  81. debouncing = DEBOUNCE;
  82. }
  83. unselect_rows();
  84. }
  85. if (debouncing) {
  86. if (--debouncing) {
  87. _delay_ms(1);
  88. } else {
  89. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  90. matrix[i] = matrix_debouncing[i];
  91. }
  92. }
  93. }
  94. #else
  95. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  96. select_row(i);
  97. _delay_us(30); // without this wait read unstable value.
  98. matrix_row_t rows = read_cols();
  99. if (matrix_reversed_debouncing[i] != rows) {
  100. matrix_reversed_debouncing[i] = rows;
  101. if (debouncing) {
  102. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  103. }
  104. debouncing = DEBOUNCE;
  105. }
  106. unselect_rows();
  107. }
  108. if (debouncing) {
  109. if (--debouncing) {
  110. _delay_ms(1);
  111. } else {
  112. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  113. matrix_reversed[i] = matrix_reversed_debouncing[i];
  114. }
  115. }
  116. }
  117. for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
  118. matrix_row_t row = 0;
  119. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  120. row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
  121. }
  122. matrix[y] = row;
  123. }
  124. #endif
  125. return 1;
  126. }
  127. bool matrix_is_modified(void)
  128. {
  129. if (debouncing) return false;
  130. return true;
  131. }
  132. inline
  133. bool matrix_is_on(uint8_t row, uint8_t col)
  134. {
  135. return (matrix[row] & ((matrix_row_t)1<col));
  136. }
  137. inline
  138. matrix_row_t matrix_get_row(uint8_t row)
  139. {
  140. return matrix[row];
  141. }
  142. void matrix_print(void)
  143. {
  144. print("\nr/c 0123456789ABCDEF\n");
  145. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  146. phex(row); print(": ");
  147. pbin_reverse16(matrix_get_row(row));
  148. print("\n");
  149. }
  150. }
  151. uint8_t matrix_key_count(void)
  152. {
  153. uint8_t count = 0;
  154. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  155. count += bitpop16(matrix[i]);
  156. }
  157. return count;
  158. }
  159. static void init_cols(void)
  160. {
  161. int B = 0, C = 0, D = 0, E = 0, F = 0;
  162. #if DIODE_DIRECTION == COL2ROW
  163. for(int x = 0; x < MATRIX_COLS; x++) {
  164. int col = COLS[x];
  165. #else
  166. for(int x = 0; x < MATRIX_ROWS; x++) {
  167. int col = ROWS[x];
  168. #endif
  169. if ((col & 0xF0) == 0x20) {
  170. B |= (1<<(col & 0x0F));
  171. } else if ((col & 0xF0) == 0x30) {
  172. C |= (1<<(col & 0x0F));
  173. } else if ((col & 0xF0) == 0x40) {
  174. D |= (1<<(col & 0x0F));
  175. } else if ((col & 0xF0) == 0x50) {
  176. E |= (1<<(col & 0x0F));
  177. } else if ((col & 0xF0) == 0x60) {
  178. F |= (1<<(col & 0x0F));
  179. }
  180. }
  181. DDRB &= ~(B); PORTB |= (B);
  182. DDRC &= ~(C); PORTC |= (C);
  183. DDRD &= ~(D); PORTD |= (D);
  184. DDRE &= ~(E); PORTE |= (E);
  185. DDRF &= ~(F); PORTF |= (F);
  186. }
  187. static matrix_row_t read_cols(void)
  188. {
  189. matrix_row_t result = 0;
  190. #if DIODE_DIRECTION == COL2ROW
  191. for(int x = 0; x < MATRIX_COLS; x++) {
  192. int col = COLS[x];
  193. #else
  194. for(int x = 0; x < MATRIX_ROWS; x++) {
  195. int col = ROWS[x];
  196. #endif
  197. if ((col & 0xF0) == 0x20) {
  198. result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
  199. } else if ((col & 0xF0) == 0x30) {
  200. result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
  201. } else if ((col & 0xF0) == 0x40) {
  202. result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
  203. } else if ((col & 0xF0) == 0x50) {
  204. result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
  205. } else if ((col & 0xF0) == 0x60) {
  206. result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
  207. }
  208. }
  209. return result;
  210. }
  211. static void unselect_rows(void)
  212. {
  213. int B = 0, C = 0, D = 0, E = 0, F = 0;
  214. #if DIODE_DIRECTION == COL2ROW
  215. for(int x = 0; x < MATRIX_ROWS; x++) {
  216. int row = ROWS[x];
  217. #else
  218. for(int x = 0; x < MATRIX_COLS; x++) {
  219. int row = COLS[x];
  220. #endif
  221. if ((row & 0xF0) == 0x20) {
  222. B |= (1<<(row & 0x0F));
  223. } else if ((row & 0xF0) == 0x30) {
  224. C |= (1<<(row & 0x0F));
  225. } else if ((row & 0xF0) == 0x40) {
  226. D |= (1<<(row & 0x0F));
  227. } else if ((row & 0xF0) == 0x50) {
  228. E |= (1<<(row & 0x0F));
  229. } else if ((row & 0xF0) == 0x60) {
  230. F |= (1<<(row & 0x0F));
  231. }
  232. }
  233. DDRB &= ~(B); PORTB |= (B);
  234. DDRC &= ~(C); PORTC |= (C);
  235. DDRD &= ~(D); PORTD |= (D);
  236. DDRE &= ~(E); PORTE |= (E);
  237. DDRF &= ~(F); PORTF |= (F);
  238. }
  239. static void select_row(uint8_t row)
  240. {
  241. #if DIODE_DIRECTION == COL2ROW
  242. int row_pin = ROWS[row];
  243. #else
  244. int row_pin = COLS[row];
  245. #endif
  246. if ((row_pin & 0xF0) == 0x20) {
  247. DDRB |= (1<<(row_pin & 0x0F));
  248. PORTB &= ~(1<<(row_pin & 0x0F));
  249. } else if ((row_pin & 0xF0) == 0x30) {
  250. DDRC |= (1<<(row_pin & 0x0F));
  251. PORTC &= ~(1<<(row_pin & 0x0F));
  252. } else if ((row_pin & 0xF0) == 0x40) {
  253. DDRD |= (1<<(row_pin & 0x0F));
  254. PORTD &= ~(1<<(row_pin & 0x0F));
  255. } else if ((row_pin & 0xF0) == 0x50) {
  256. DDRE |= (1<<(row_pin & 0x0F));
  257. PORTE &= ~(1<<(row_pin & 0x0F));
  258. } else if ((row_pin & 0xF0) == 0x60) {
  259. DDRF |= (1<<(row_pin & 0x0F));
  260. PORTF &= ~(1<<(row_pin & 0x0F));
  261. }
  262. }