Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
pirms 13 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright 2011 Jun Wako <[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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #ifndef DEBOUNCE
  26. # define DEBOUNCE 5
  27. #endif
  28. static uint8_t debouncing = DEBOUNCE;
  29. /* matrix state(1:on, 0:off) */
  30. static matrix_row_t matrix[MATRIX_ROWS];
  31. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  32. #ifdef MATRIX_HAS_GHOST
  33. static bool matrix_has_ghost_in_row(uint8_t row);
  34. #endif
  35. static matrix_row_t read_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. // initialize row and col
  51. unselect_rows();
  52. // Input with pull-up(DDR:0, PORT:1)
  53. DDRB = 0x00;
  54. PORTB = 0xFF;
  55. // initialize matrix state: all keys off
  56. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  57. matrix[i] = 0;
  58. matrix_debouncing[i] = 0;
  59. }
  60. }
  61. uint8_t matrix_scan(void)
  62. {
  63. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  64. select_row(i);
  65. _delay_us(30); // without this wait read unstable value.
  66. matrix_row_t cols = read_cols();
  67. if (matrix_debouncing[i] != cols) {
  68. matrix_debouncing[i] = cols;
  69. if (debouncing) {
  70. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  71. }
  72. debouncing = DEBOUNCE;
  73. }
  74. unselect_rows();
  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 01234567\n");
  105. for (uint8_t row = 0; row < matrix_rows(); row++) {
  106. phex(row); print(": ");
  107. pbin_reverse(matrix_get_row(row));
  108. #ifdef MATRIX_HAS_GHOST
  109. if (matrix_has_ghost_in_row(row)) {
  110. print(" <ghost");
  111. }
  112. #endif
  113. print("\n");
  114. }
  115. }
  116. #ifdef MATRIX_HAS_GHOST
  117. inline
  118. static bool matrix_has_ghost_in_row(uint8_t row)
  119. {
  120. // no ghost exists in case less than 2 keys on
  121. if (((matrix[row] - 1) & matrix[row]) == 0)
  122. return false;
  123. // ghost exists in case same state as other row
  124. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  125. if (i != row && (matrix[i] & matrix[row]))
  126. return true;
  127. }
  128. return false;
  129. }
  130. #endif
  131. inline
  132. static matrix_row_t read_cols(void)
  133. {
  134. return ~PINB;
  135. }
  136. inline
  137. static void unselect_rows(void)
  138. {
  139. // Hi-Z(DDR:0, PORT:0) to unselect
  140. DDRC &= ~0b01000000; // PC: 6
  141. PORTC &= ~0b01000000;
  142. DDRD &= ~0b11100111; // PD: 7,6,5,2,1,0
  143. PORTD &= ~0b11100111;
  144. DDRF &= ~0b11000000; // PF: 7,6
  145. PORTF &= ~0b11000000;
  146. }
  147. inline
  148. static void select_row(uint8_t row)
  149. {
  150. // Output low(DDR:1, PORT:0) to select
  151. // row: 0 1 2 3 4 5 6 7 8
  152. // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7
  153. switch (row) {
  154. case 0:
  155. DDRD |= (1<<0);
  156. PORTD &= ~(1<<0);
  157. break;
  158. case 1:
  159. DDRD |= (1<<5);
  160. PORTD &= ~(1<<5);
  161. break;
  162. case 2:
  163. DDRD |= (1<<7);
  164. PORTD &= ~(1<<7);
  165. break;
  166. case 3:
  167. DDRF |= (1<<6);
  168. PORTF &= ~(1<<6);
  169. break;
  170. case 4:
  171. DDRD |= (1<<6);
  172. PORTD &= ~(1<<6);
  173. break;
  174. case 5:
  175. DDRD |= (1<<1);
  176. PORTD &= ~(1<<1);
  177. break;
  178. case 6:
  179. DDRD |= (1<<2);
  180. PORTD &= ~(1<<2);
  181. break;
  182. case 7:
  183. DDRC |= (1<<6);
  184. PORTC &= ~(1<<6);
  185. break;
  186. case 8:
  187. DDRF |= (1<<7);
  188. PORTF &= ~(1<<7);
  189. break;
  190. }
  191. }