選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

matrix.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright 2014 Ralf Schmitt <[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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #include "backlight.h"
  23. #ifndef DEBOUNCE
  24. # define DEBOUNCE 5
  25. #endif
  26. static uint8_t debouncing = DEBOUNCE;
  27. /* matrix state(1:on, 0:off) */
  28. static matrix_row_t matrix[MATRIX_ROWS];
  29. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  30. static uint16_t read_inputs(void);
  31. static void init_inputs(void);
  32. static void init_outputs(void);
  33. static void reset_inputs(void);
  34. static void reset_outputs(void);
  35. static void select_output(uint8_t col);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. backlight_init_ports();
  49. init_inputs();
  50. init_outputs();
  51. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  52. matrix[i] = 0;
  53. matrix_debouncing[i] = 0;
  54. }
  55. }
  56. uint8_t matrix_scan(void)
  57. {
  58. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  59. reset_inputs();
  60. reset_outputs();
  61. select_output(col);
  62. _delay_us(3);
  63. uint16_t rows = read_inputs();
  64. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  65. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  66. bool curr_bit = rows & (1<<row);
  67. if (prev_bit != curr_bit) {
  68. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  69. if (debouncing) {
  70. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  71. }
  72. debouncing = DEBOUNCE;
  73. }
  74. }
  75. }
  76. if (debouncing) {
  77. if (--debouncing) {
  78. _delay_ms(1);
  79. } else {
  80. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  81. matrix[i] = matrix_debouncing[i];
  82. }
  83. }
  84. }
  85. return 1;
  86. }
  87. bool matrix_is_modified(void)
  88. {
  89. if (debouncing) return false;
  90. return true;
  91. }
  92. inline
  93. bool matrix_is_on(uint8_t row, uint8_t col)
  94. {
  95. return (matrix[row] & ((matrix_row_t)1<<col));
  96. }
  97. inline
  98. matrix_row_t matrix_get_row(uint8_t row)
  99. {
  100. return matrix[row];
  101. }
  102. void matrix_print(void)
  103. {
  104. print("\nr/c 0123456789ABCDEF\n");
  105. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  106. phex(row); print(": ");
  107. pbin_reverse16(matrix_get_row(row));
  108. print("\n");
  109. }
  110. }
  111. uint8_t matrix_key_count(void)
  112. {
  113. uint8_t count = 0;
  114. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  115. count += bitpop16(matrix[i]);
  116. }
  117. return count;
  118. }
  119. static void init_inputs(void)
  120. {
  121. DDRE &= ~0b01000000; // PE6 (Col 0)
  122. DDRB &= ~0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
  123. DDRF &= ~0b00000001; // PF0 (Col 5)
  124. DDRD &= ~0b00100001; // PD0 (Col 6), PD5 (Col 7)
  125. }
  126. static uint16_t read_inputs(void)
  127. {
  128. return (PINE&(1<<6) ? 0 : (1<<0)) | // PE6 (Row 0)
  129. (PINB&(1<<0) ? 0 : (1<<1)) | // PB0 (Row 1)
  130. (PINB&(1<<1) ? 0 : (1<<2)) | // PB1 (Row 2)
  131. (PINB&(1<<2) ? 0 : (1<<3)) | // PB2 (Row 3)
  132. (PINB&(1<<3) ? 0 : (1<<4)) | // PB3 (Row 4)
  133. (PINF&(1<<0) ? 0 : (1<<5)) | // PF0 (Row 5)
  134. (PIND&(1<<0) ? 0 : (1<<6)) | // PD0 (Row 6)
  135. (PIND&(1<<5) ? 0 : (1<<7)); // PD5 (Row 7)
  136. }
  137. static void reset_inputs(void)
  138. {
  139. PORTE |= 0b01000000; // PE6 (Col 0)
  140. PORTB |= 0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
  141. PORTF |= 0b00000001; // PF0 (Col 5)
  142. PORTD |= 0b00100001; // PD0 (Col 6), PD5 (Col 7)
  143. }
  144. static void init_outputs(void)
  145. {
  146. DDRB |= 0b00010000; // PB4 (Row 0)
  147. DDRE |= 0b00000100; // PE2 (Row 1)
  148. DDRF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
  149. DDRC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
  150. DDRD |= 0b10000000; // PD7 (Row 8)
  151. }
  152. static void reset_outputs(void)
  153. {
  154. PORTB |= 0b00010000; // PB4 (Row 0)
  155. PORTE |= 0b00000100; // PE2 (Row 1)
  156. PORTF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
  157. PORTC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
  158. PORTD |= 0b10000000; // PD7 (Row 8)
  159. }
  160. static void select_output(uint8_t col)
  161. {
  162. switch (col) {
  163. case 0:
  164. PORTB &= ~(1<<4);
  165. break;
  166. case 1:
  167. PORTE &= ~(1<<2);
  168. break;
  169. case 2:
  170. PORTF &= ~(1<<4);
  171. break;
  172. case 3:
  173. PORTF &= ~(1<<7);
  174. break;
  175. case 4:
  176. PORTF &= ~(1<<1);
  177. break;
  178. case 5:
  179. PORTF &= ~(1<<6);
  180. break;
  181. case 6:
  182. PORTC &= ~(1<<6);
  183. break;
  184. case 7:
  185. PORTF &= ~(1<<5);
  186. break;
  187. case 8:
  188. PORTD &= ~(1<<7);
  189. break;
  190. case 9:
  191. PORTC &= ~(1<<7);
  192. break;
  193. }
  194. }