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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #ifdef PS2_MOUSE_ENABLE
  25. #include "ps2.h"
  26. #endif
  27. #include "matrix.h"
  28. #include "rgb.h"
  29. #include "softpwm_led.h"
  30. #ifndef DEBOUNCE
  31. # define DEBOUNCE 5
  32. #endif
  33. static uint8_t debouncing = DEBOUNCE;
  34. /* matrix state(1:on, 0:off) */
  35. static matrix_row_t matrix[MATRIX_ROWS];
  36. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  37. static matrix_row_t read_cols(void);
  38. static void init_cols(void);
  39. static void unselect_rows(void);
  40. static void select_row(uint8_t row);
  41. #ifdef PS2_MOUSE_ENABLE
  42. uint8_t ps2_mouse_enabled = 0;
  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. // disable JTAG
  57. MCUCR = (1<<JTD);
  58. MCUCR = (1<<JTD);
  59. // initialize RGB LED
  60. rgb_init();
  61. // initialize row and col
  62. unselect_rows();
  63. init_cols();
  64. #ifdef PS2_MOUSE_ENABLE
  65. // ps2 mouse detecting
  66. DDRC &= ~(1<<PC7);
  67. PORTC |= (1<<PC7);
  68. _delay_us(30); // without this wait read unstable value.
  69. if ((PINC & (1<<PC7)) == 0) {
  70. ps2_mouse_enabled = 1;
  71. #ifdef SOFTPWM_LED_ENABLE
  72. softpwm_led_disable();
  73. #endif
  74. }
  75. else {
  76. ps2_mouse_enabled = 0;
  77. #ifdef SOFTPWM_LED_ENABLE
  78. softpwm_led_enable();
  79. #endif
  80. }
  81. PORTC &= ~(1<<PC7);
  82. #endif
  83. // initialize matrix state: all keys off
  84. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  85. matrix[i] = 0;
  86. matrix_debouncing[i] = 0;
  87. }
  88. }
  89. uint8_t matrix_scan(void)
  90. {
  91. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  92. select_row(i);
  93. _delay_us(30); // without this wait read unstable value.
  94. matrix_row_t cols = read_cols();
  95. if (matrix_debouncing[i] != cols) {
  96. matrix_debouncing[i] = cols;
  97. if (debouncing) {
  98. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  99. }
  100. debouncing = DEBOUNCE;
  101. }
  102. unselect_rows();
  103. }
  104. if (debouncing) {
  105. if (--debouncing) {
  106. _delay_ms(1);
  107. } else {
  108. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  109. matrix[i] = matrix_debouncing[i];
  110. }
  111. }
  112. }
  113. return 1;
  114. }
  115. bool matrix_is_modified(void)
  116. {
  117. if (debouncing) return false;
  118. return true;
  119. }
  120. inline
  121. bool matrix_is_on(uint8_t row, uint8_t col)
  122. {
  123. return (matrix[row] & ((matrix_row_t)1<<col));
  124. }
  125. inline
  126. matrix_row_t matrix_get_row(uint8_t row)
  127. {
  128. return matrix[row];
  129. }
  130. void matrix_print(void)
  131. {
  132. print("\nr/c 0123456789ABCDEF\n");
  133. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  134. phex(row); print(": ");
  135. pbin_reverse16(matrix_get_row(row));
  136. print("\n");
  137. }
  138. }
  139. uint8_t matrix_key_count(void)
  140. {
  141. uint8_t count = 0;
  142. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  143. count += bitpop16(matrix[i]);
  144. }
  145. return count;
  146. }
  147. /* Column pin configuration
  148. * col: 0 1 2 3 4 5 6 7
  149. * pin: D0 D1 D2 D3 D4 D5 D6 D7
  150. */
  151. static void init_cols(void)
  152. {
  153. // Input with pull-up(DDR:0, PORT:1)
  154. DDRD &= ~0b11111111;
  155. PORTD |= 0b11111111;
  156. }
  157. static matrix_row_t read_cols(void)
  158. {
  159. return ~PIND;
  160. }
  161. /* Row pin configuration
  162. * row: 0 1 2 3 4 5 6 7
  163. * pin: B0 B1 B2 B3 B4 B5 B6 B7
  164. */
  165. static void unselect_rows(void)
  166. {
  167. DDRB |= 0b11111111;
  168. PORTB |= 0b11111111;
  169. }
  170. static void select_row(uint8_t row)
  171. {
  172. DDRB |= (1 << row);
  173. PORTB &= ~(1 << row);
  174. }