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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 debouncing = 0;
  36. static uint16_t debouncing_last[MATRIX_COLS];
  37. static matrix_row_t read_cols(void);
  38. static void init_cols(void);
  39. #ifdef PS2_MOUSE_ENABLE
  40. static uint8_t ps2_mouse_detected;
  41. uint8_t ps2_enabled(void)
  42. {
  43. return ps2_mouse_detected;
  44. }
  45. #endif
  46. inline
  47. uint8_t matrix_rows(void)
  48. {
  49. return MATRIX_ROWS;
  50. }
  51. inline
  52. uint8_t matrix_cols(void)
  53. {
  54. return MATRIX_COLS;
  55. }
  56. void matrix_init(void)
  57. {
  58. #ifdef PS2_MOUSE_ENABLE
  59. // ps2 mouse detect
  60. DDRC &= ~(1<<PC2);
  61. PORTC |= (1<<PC2);
  62. if (PINC & (1<<PC2)) {
  63. ps2_mouse_detected = 0;
  64. }
  65. else {
  66. ps2_mouse_detected = 1;
  67. }
  68. PORTC &= ~(1<<PC2);
  69. #endif
  70. keymaps_cache_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 ((cols & (1<<col)) != (matrix & (1<<col))) {
  79. // state changed
  80. if (debouncing & (1<<col)) {
  81. // debouncing
  82. if (timer_elapsed(debouncing_last[col]) > DEBOUNCE) {
  83. // released
  84. matrix &= ~(1<<col);
  85. debouncing &= ~(1<<col);
  86. }
  87. else {
  88. // reset debounce time
  89. debouncing_last[col] = timer_read();
  90. }
  91. }
  92. else {
  93. if (cols & (1<<col)) {
  94. // pressed
  95. matrix |= (1<<col);
  96. }
  97. else {
  98. // start debounce
  99. debouncing_last[col] = timer_read();
  100. debouncing |= (1<<col);
  101. }
  102. }
  103. }
  104. }
  105. /*
  106. if (matrix_debouncing != cols) {
  107. matrix_debouncing = cols;
  108. if (debouncing) {
  109. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  110. }
  111. debouncing = DEBOUNCE;
  112. }
  113. if (debouncing) {
  114. if (--debouncing) {
  115. _delay_ms(1);
  116. } else {
  117. matrix = matrix_debouncing;
  118. }
  119. }
  120. */
  121. return 1;
  122. }
  123. bool matrix_is_modified(void)
  124. {
  125. if (debouncing) return false;
  126. return true;
  127. }
  128. inline
  129. bool matrix_is_on(uint8_t row, uint8_t col)
  130. {
  131. return (matrix & ((matrix_row_t)1<<col));
  132. }
  133. inline
  134. matrix_row_t matrix_get_row(uint8_t row)
  135. {
  136. return matrix;
  137. }
  138. void matrix_print(void)
  139. {
  140. print("\nr/c 0123456789ABCDEF\n");
  141. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  142. phex(row); print(": ");
  143. pbin_reverse16(matrix_get_row(row));
  144. print("\n");
  145. }
  146. }
  147. uint8_t matrix_key_count(void)
  148. {
  149. return bitpop16(matrix);
  150. }
  151. /* Column pin configuration
  152. * col: 0 1 2 3 4
  153. * pin: D1 D2 D5 D6 B3
  154. */
  155. static void init_cols(void)
  156. {
  157. #ifndef EXPERIMENTAL
  158. // Input with pull-up(DDR:0, PORT:1)
  159. DDRD &= ~(1<<PD1 | 1<<PD2 | 1<<PD5 | 1<<PD6);
  160. PORTD |= (1<<PD1 | 1<<PD2 | 1<<PD5 | 1<<PD6);
  161. DDRB &= ~(1<<PB3);
  162. PORTB |= (1<<PB3);
  163. #else
  164. DDRD |= (1<<PD3);
  165. PORTD &= ~(1<<PD3);
  166. DDRE &= ~(1<<PE6);
  167. PORTE |= (1<<PE6);
  168. DDRC &= ~(1<<PC7 | 1<<PC6);
  169. PORTC |= (1<<PC7 | 1<<PC6);
  170. DDRB &= ~(1<<PB7);
  171. PORTB |= (1<<PB7);
  172. DDRD &= ~(1<<PD4);
  173. PORTD |= (1<<PD4);
  174. #endif
  175. }
  176. /* Column pin configuration
  177. * col: 0 1 2 3 4
  178. * pin: D1 D2 D5 D6 B3
  179. */
  180. static matrix_row_t read_cols(void)
  181. {
  182. #ifndef EXPERIMENTAL
  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. #else
  189. return (PINE&(1<<PE6) ? 0 : (1<<0)) |
  190. (PINC&(1<<PC7) ? 0 : (1<<1)) |
  191. (PINC&(1<<PC6) ? 0 : (1<<2)) |
  192. (PINB&(1<<PB7) ? 0 : (1<<3)) |
  193. (PIND&(1<<PD4) ? 0 : (1<<4));
  194. #endif
  195. }