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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 REDSCARFII_PLUS
  25. #include "rgb.h"
  26. #endif
  27. #include "matrix.h"
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. static uint8_t debouncing = DEBOUNCE;
  32. /* matrix state(1:on, 0:off) */
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  35. static matrix_row_t read_cols(void);
  36. static void init_cols(void);
  37. static void init_rows(void);
  38. static void unselect_rows(void);
  39. static void select_row(uint8_t row);
  40. inline
  41. uint8_t matrix_rows(void)
  42. {
  43. return MATRIX_ROWS;
  44. }
  45. inline
  46. uint8_t matrix_cols(void)
  47. {
  48. return MATRIX_COLS;
  49. }
  50. void matrix_init(void)
  51. {
  52. // disable JTAG
  53. MCUCR = (1<<JTD);
  54. MCUCR = (1<<JTD);
  55. #ifdef REDSCARFII_PLUS
  56. // initialize rgb led
  57. rgb_init();
  58. #endif
  59. // initialize row and col
  60. init_rows();
  61. init_cols();
  62. // initialize matrix state: all keys off
  63. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  64. matrix[i] = 0;
  65. matrix_debouncing[i] = 0;
  66. }
  67. }
  68. uint8_t matrix_scan(void)
  69. {
  70. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  71. select_row(i);
  72. _delay_us(30); // without this wait read unstable value.
  73. matrix_row_t cols = read_cols();
  74. if (matrix_debouncing[i] != cols) {
  75. matrix_debouncing[i] = cols;
  76. if (debouncing) {
  77. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  78. }
  79. debouncing = DEBOUNCE;
  80. }
  81. unselect_rows();
  82. }
  83. if (debouncing) {
  84. if (--debouncing) {
  85. _delay_ms(1);
  86. } else {
  87. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  88. matrix[i] = matrix_debouncing[i];
  89. }
  90. }
  91. }
  92. return 1;
  93. }
  94. bool matrix_is_modified(void)
  95. {
  96. if (debouncing) return false;
  97. return true;
  98. }
  99. inline
  100. bool matrix_is_on(uint8_t row, uint8_t col)
  101. {
  102. return (matrix[row] & ((matrix_row_t)1<<col));
  103. }
  104. inline
  105. matrix_row_t matrix_get_row(uint8_t row)
  106. {
  107. return matrix[row];
  108. }
  109. void matrix_print(void)
  110. {
  111. print("\nr/c 0123456789ABCDEF\n");
  112. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  113. phex(row); print(": ");
  114. pbin_reverse16(matrix_get_row(row));
  115. print("\n");
  116. }
  117. }
  118. uint8_t matrix_key_count(void)
  119. {
  120. uint8_t count = 0;
  121. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  122. count += bitpop16(matrix[i]);
  123. }
  124. return count;
  125. }
  126. /* Column pin configuration
  127. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  128. * pin: F4 F1 F0 B3 D0 D1 D4 D5 D6 D7 F7 F6 D2 D3 B6 B5 B4 F5
  129. */
  130. static void init_cols(void)
  131. {
  132. // Input with pull-up(DDR:0, PORT:1)
  133. DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4 | 1<<PF1 | 1<<PF0);
  134. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4 | 1<<PF1 | 1<<PF0);
  135. DDRD &= ~(1<<PD7 | 1<<PD6 | 1<<PD5 | 1<<PD4 | 1<<PD3 | 1<<PD2 | 1<<PD1 | 1<<PD0);
  136. PORTD |= (1<<PD7 | 1<<PD6 | 1<<PD5 | 1<<PD4 | 1<<PD3 | 1<<PD2 | 1<<PD1 | 1<<PD0);
  137. DDRB &= ~(1<<PB6 | 1<<PB5 | 1<<PB4 | 1<<PB3);
  138. PORTB |= (1<<PB6 | 1<<PB5 | 1<<PB4 | 1<<PB3);
  139. }
  140. static matrix_row_t read_cols(void)
  141. {
  142. return (PINF&(1<<PF4) ? 0 : (1<<0)) |
  143. (PINF&(1<<PF1) ? 0 : (1<<1)) |
  144. (PINF&(1<<PF0) ? 0 : (1<<2)) |
  145. (PINB&(1<<PB3) ? 0 : (1<<3)) |
  146. (PIND&(1<<PD0) ? 0 : (1<<4)) |
  147. (PIND&(1<<PD1) ? 0 : (1<<5)) |
  148. (PIND&(1<<PD4) ? 0 : (1<<6)) |
  149. (PIND&(1<<PD5) ? 0 : (1<<7)) |
  150. (PIND&(1<<PD6) ? 0 : (1<<8)) |
  151. (PIND&(1<<PD7) ? 0 : (1<<9)) |
  152. (PINF&(1<<PF7) ? 0 : (1<<10)) |
  153. (PINF&(1<<PF6) ? 0 : (1<<11)) |
  154. (PIND&(1<<PD2) ? 0 : (1<<12)) |
  155. (PIND&(1<<PD3) ? 0 : (1<<13)) |
  156. (PINB&(1<<PB6) ? 0 : (1<<14)) |
  157. (PINB&(1<<PB5) ? 0 : (1UL<<15)) |
  158. (PINB&(1<<PB4) ? 0 : (1UL<<16)) |
  159. (PINF&(1<<PF5) ? 0 : (1UL<<17));
  160. }
  161. /* Row pin configuration
  162. * row: 0 1 2 3 4 5 x
  163. * pin: B2 0 1 0 1 0 1 1
  164. * B1 0 0 1 1 0 0 1
  165. * B0 0 0 0 0 1 1 1
  166. */
  167. static void init_rows(void)
  168. {
  169. DDRB |= (1<<PB2 | 1<<PB1 | 1<<PB0);
  170. }
  171. static void unselect_rows(void)
  172. {
  173. //PORTB |= (1<<PB2 | 1<<PB1 | 1<<PB0);
  174. PORTB |= (1<<PB2);
  175. PORTB |= (1<<PB1);
  176. PORTB |= (1<<PB0);
  177. }
  178. static void select_row(uint8_t row)
  179. {
  180. (row & (1<<0)) ? (PORTB |= (1<<PB2)) : (PORTB &= ~(1<<PB2));
  181. (row & (1<<1)) ? (PORTB |= (1<<PB1)) : (PORTB &= ~(1<<PB1));
  182. (row & (1<<2)) ? (PORTB |= (1<<PB0)) : (PORTB &= ~(1<<PB0));
  183. }