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