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.1KB

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