Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

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