Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
10 лет назад
9 лет назад
9 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright 2014 Kai Ryu <[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 "timer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #ifdef PS2_MOUSE_ENABLE
  27. #include "ps2.h"
  28. #endif
  29. #include "vibration.h"
  30. #include "buzzer.h"
  31. #include "keymap_common.h"
  32. #ifndef DEBOUNCE
  33. # define DEBOUNCE 5
  34. #endif
  35. /* matrix state(1:on, 0:off) */
  36. static matrix_row_t matrix = 0;
  37. static matrix_row_t matrix_debouncing = 0;
  38. static matrix_row_t debouncing = 0;
  39. static uint16_t debouncing_last[MATRIX_COLS];
  40. static matrix_row_t read_cols(void);
  41. static void init_cols(void);
  42. #ifdef PS2_MOUSE_ENABLE
  43. uint8_t ps2_mouse_enabled;
  44. #endif
  45. inline
  46. uint8_t matrix_rows(void)
  47. {
  48. return MATRIX_ROWS;
  49. }
  50. inline
  51. uint8_t matrix_cols(void)
  52. {
  53. return MATRIX_COLS;
  54. }
  55. void matrix_init(void)
  56. {
  57. #ifdef PS2_MOUSE_ENABLE
  58. // ps2 mouse detect
  59. DDRD &= ~(1<<PD3);
  60. PORTD |= (1<<PD3);
  61. _delay_us(30);
  62. if (PIND & (1<<PD3)) {
  63. ps2_mouse_enabled = 0;
  64. }
  65. else {
  66. ps2_mouse_enabled = 1;
  67. }
  68. PORTD &= ~(1<<PD3);
  69. #endif
  70. keymaps_cache_init();
  71. vibration_init();
  72. buzzer_init();
  73. // initialize cols
  74. init_cols();
  75. }
  76. uint8_t matrix_scan(void)
  77. {
  78. matrix_row_t cols = read_cols();
  79. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  80. if (debouncing & (1<<col)) {
  81. if (timer_elapsed(debouncing_last[col]) > DEBOUNCE) {
  82. debouncing &= ~(1<<col);
  83. if (!(cols & (1<<col))) {
  84. matrix &= ~(1<<col);
  85. }
  86. }
  87. }
  88. if ((cols & (1<<col)) != (matrix_debouncing & (1<<col))) {
  89. if (!(debouncing & (1<<col)) && (cols & (1<<col))) {
  90. matrix |= (1<<col);
  91. }
  92. else {
  93. debouncing_last[col] = timer_read();
  94. debouncing |= (1<<col);
  95. }
  96. }
  97. }
  98. matrix_debouncing = cols;
  99. /*
  100. if (matrix_debouncing != cols) {
  101. matrix_debouncing = cols;
  102. if (debouncing) {
  103. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  104. }
  105. debouncing = DEBOUNCE;
  106. }
  107. if (debouncing) {
  108. if (--debouncing) {
  109. _delay_ms(1);
  110. } else {
  111. matrix = matrix_debouncing;
  112. }
  113. }
  114. */
  115. vibration_task();
  116. return 1;
  117. }
  118. bool matrix_is_modified(void)
  119. {
  120. if (debouncing) return false;
  121. return true;
  122. }
  123. inline
  124. bool matrix_is_on(uint8_t row, uint8_t col)
  125. {
  126. return (matrix & ((matrix_row_t)1<<col));
  127. }
  128. inline
  129. matrix_row_t matrix_get_row(uint8_t row)
  130. {
  131. return matrix;
  132. }
  133. void matrix_print(void)
  134. {
  135. print("\nr/c 0123456789ABCDEF\n");
  136. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  137. phex(row); print(": ");
  138. pbin_reverse16(matrix_get_row(row));
  139. print("\n");
  140. }
  141. }
  142. uint8_t matrix_key_count(void)
  143. {
  144. return bitpop16(matrix);
  145. }
  146. /* Column pin configuration
  147. * col: 0 1 2 3 4
  148. * pin: B0 B1 B2 B3 B4
  149. */
  150. static void init_cols(void)
  151. {
  152. #ifndef EXPERIMENTAL
  153. // Input with pull-up(DDR:0, PORT:1)
  154. /*
  155. DDRD &= ~(1<<PD1 | 1<<PD2 | 1<<PD5 | 1<<PD6);
  156. PORTD |= (1<<PD1 | 1<<PD2 | 1<<PD5 | 1<<PD6);
  157. DDRB &= ~(1<<PB3);
  158. PORTB |= (1<<PB3);
  159. */
  160. DDRB &= ~(1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3 | 1<<PB4);
  161. PORTB |= (1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3 | 1<<PB4);
  162. DDRB |= (1<<PB5);
  163. PORTB |= (1<<PB5);
  164. #else
  165. DDRD |= (1<<PD3);
  166. PORTD &= ~(1<<PD3);
  167. DDRE &= ~(1<<PE6);
  168. PORTE |= (1<<PE6);
  169. DDRC &= ~(1<<PC7 | 1<<PC6);
  170. PORTC |= (1<<PC7 | 1<<PC6);
  171. DDRB &= ~(1<<PB7);
  172. PORTB |= (1<<PB7);
  173. DDRD &= ~(1<<PD4);
  174. PORTD |= (1<<PD4);
  175. #endif
  176. }
  177. /* Column pin configuration
  178. * col: 0 1 2 3 4
  179. * pin: B0 B1 B2 B3 B4
  180. */
  181. static matrix_row_t read_cols(void)
  182. {
  183. #ifndef EXPERIMENTAL
  184. /*
  185. return (PIND&(1<<PD1) ? 0 : (1<<0)) |
  186. (PIND&(1<<PD2) ? 0 : (1<<1)) |
  187. (PIND&(1<<PD5) ? 0 : (1<<2)) |
  188. (PIND&(1<<PD6) ? 0 : (1<<3)) |
  189. (PINB&(1<<PB3) ? 0 : (1<<4));
  190. */
  191. /*
  192. return (PINB&(1<<PB0) ? 0 : (1<<0)) |
  193. (PINB&(1<<PB1) ? 0 : (1<<1)) |
  194. (PINB&(1<<PB3) ? 0 : (1<<2)) |
  195. (PINB&(1<<PB2) ? 0 : (1<<3)) |
  196. (PINB&(1<<PB4) ? 0 : (1<<4));
  197. */
  198. return (~PINB) & 0b00011111;
  199. #else
  200. return (PINE&(1<<PE6) ? 0 : (1<<0)) |
  201. (PINC&(1<<PC7) ? 0 : (1<<1)) |
  202. (PINC&(1<<PC6) ? 0 : (1<<2)) |
  203. (PINB&(1<<PB7) ? 0 : (1<<3)) |
  204. (PIND&(1<<PD4) ? 0 : (1<<4));
  205. #endif
  206. }