Keyboard firmwares for Atmel AVR and Cortex-M
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.

преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Copyright 2012 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 uint16_t *matrix;
  31. static uint16_t *matrix_debouncing;
  32. static uint16_t matrix0[MATRIX_ROWS];
  33. static uint16_t matrix1[MATRIX_ROWS];
  34. static bool is_modified;
  35. static uint16_t read_cols(void);
  36. static void init_cols(void);
  37. static void unselect_rows(void);
  38. static void select_row(uint8_t row);
  39. inline
  40. uint8_t matrix_rows(void)
  41. {
  42. return MATRIX_ROWS;
  43. }
  44. inline
  45. uint8_t matrix_cols(void)
  46. {
  47. return MATRIX_COLS;
  48. }
  49. void matrix_init(void)
  50. {
  51. // initialize row and col
  52. unselect_rows();
  53. init_cols();
  54. // initialize matrix state: all keys off
  55. matrix = matrix0;
  56. matrix_debouncing = matrix1;
  57. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  58. matrix[i] = 0;
  59. matrix_debouncing[i] = 0;
  60. }
  61. is_modified = false;
  62. }
  63. uint8_t matrix_scan(void)
  64. {
  65. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  66. //unselect_rows();
  67. select_row(i);
  68. _delay_us(30); // without this wait read unstable value.
  69. uint16_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. is_modified = false;
  77. }
  78. unselect_rows();
  79. }
  80. //unselect_rows();
  81. if (debouncing) {
  82. debouncing--;
  83. _delay_ms(1);
  84. } else {
  85. uint16_t *tmp = matrix;
  86. matrix = matrix_debouncing;
  87. matrix_debouncing = tmp;
  88. is_modified = true;
  89. }
  90. return 1;
  91. }
  92. bool matrix_is_modified(void)
  93. {
  94. return is_modified;
  95. }
  96. inline
  97. bool matrix_has_ghost(void)
  98. {
  99. return false;
  100. }
  101. inline
  102. bool matrix_is_on(uint8_t row, uint8_t col)
  103. {
  104. return (matrix[row] & ((matrix_row_t)1<<col));
  105. }
  106. inline
  107. uint16_t matrix_get_row(uint8_t row)
  108. {
  109. return matrix[row];
  110. }
  111. void matrix_print(void)
  112. {
  113. print("\nr/c 0123456789ABCDEF\n");
  114. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  115. phex(row); print(": ");
  116. pbin_reverse16(matrix_get_row(row));
  117. print("\n");
  118. }
  119. }
  120. uint8_t matrix_key_count(void)
  121. {
  122. uint8_t count = 0;
  123. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  124. count += bitpop16(matrix[i]);
  125. }
  126. return count;
  127. }
  128. /* Column pin configuration
  129. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  130. * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3
  131. */
  132. static void init_cols(void)
  133. {
  134. // Input with pull-up(DDR:0, PORT:1)
  135. DDRF &= ~(1<<0 | 1<<1);
  136. PORTF |= (1<<0 | 1<<1);
  137. DDRE &= ~(1<<6);
  138. PORTE |= (1<<6);
  139. DDRD &= ~(1<<7 | 1<<6 | 1<<4);
  140. PORTD |= (1<<7 | 1<<6 | 1<<4);
  141. DDRC &= ~(1<<7 | 1<<6);
  142. PORTC |= (1<<7 | 1<<6);
  143. DDRB &= ~(1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
  144. PORTB |= (1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
  145. }
  146. static uint16_t read_cols(void)
  147. {
  148. return (PINF&(1<<0) ? 0 : (1<<0)) |
  149. (PINF&(1<<1) ? 0 : (1<<1)) |
  150. (PINE&(1<<6) ? 0 : (1<<2)) |
  151. (PINC&(1<<7) ? 0 : (1<<3)) |
  152. (PINC&(1<<6) ? 0 : (1<<4)) |
  153. (PINB&(1<<6) ? 0 : (1<<5)) |
  154. (PIND&(1<<4) ? 0 : (1<<6)) |
  155. (PINB&(1<<1) ? 0 : (1<<7)) |
  156. (PINB&(1<<0) ? 0 : (1<<8)) |
  157. (PINB&(1<<5) ? 0 : (1<<9)) |
  158. (PINB&(1<<4) ? 0 : (1<<10)) |
  159. (PIND&(1<<7) ? 0 : (1<<11)) |
  160. (PIND&(1<<6) ? 0 : (1<<12)) |
  161. (PINB&(1<<3) ? 0 : (1<<13));
  162. }
  163. /* Row pin configuration
  164. * row: 0 1 2 3 4
  165. * pin: D0 D1 D2 D3 D5
  166. */
  167. static void unselect_rows(void)
  168. {
  169. // Hi-Z(DDR:0, PORT:0) to unselect
  170. DDRD &= ~0b00101111;
  171. PORTD &= ~0b00101111;
  172. }
  173. static void select_row(uint8_t row)
  174. {
  175. // Output low(DDR:1, PORT:0) to select
  176. switch (row) {
  177. case 0:
  178. DDRD |= (1<<0);
  179. PORTD &= ~(1<<0);
  180. break;
  181. case 1:
  182. DDRD |= (1<<1);
  183. PORTD &= ~(1<<1);
  184. break;
  185. case 2:
  186. DDRD |= (1<<2);
  187. PORTD &= ~(1<<2);
  188. break;
  189. case 3:
  190. DDRD |= (1<<3);
  191. PORTD &= ~(1<<3);
  192. break;
  193. case 4:
  194. DDRD |= (1<<5);
  195. PORTD &= ~(1<<5);
  196. break;
  197. }
  198. }