Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Copyright 2012 Jun Wako <[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 "matrix.h"
  25. #include "rgblight.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 unselect_rows(void);
  36. static void select_row(uint8_t row);
  37. #define LED_ON() do { DDRB |= (1<<0); PORTB |= (1<<0); } while (0)
  38. #define LED_OFF() do { DDRB &= ~(1<<0); PORTB &= ~(1<<0); } while (0)
  39. #define LED_TGL() do { DDRB |= (1<<0); PINB |= (1<<0); } while (0)
  40. void matrix_init(void)
  41. {
  42. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  43. MCUCR |= (1<<JTD);
  44. MCUCR |= (1<<JTD);
  45. backlight_init_ports();
  46. rgblight_init();
  47. // initialize row and col
  48. unselect_rows();
  49. init_cols();
  50. // initialize matrix state: all keys off
  51. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  52. matrix[i] = 0;
  53. matrix_debouncing[i] = 0;
  54. }
  55. //debug
  56. debug_matrix = true;
  57. LED_ON();
  58. _delay_ms(500);
  59. LED_OFF();
  60. }
  61. uint8_t matrix_scan(void)
  62. {
  63. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  64. select_row(i);
  65. _delay_us(30); // without this wait read unstable value.
  66. matrix_row_t cols = read_cols();
  67. if (matrix_debouncing[i] != cols) {
  68. matrix_debouncing[i] = cols;
  69. if (debouncing) {
  70. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  71. }
  72. debouncing = DEBOUNCE;
  73. }
  74. unselect_rows();
  75. }
  76. if (debouncing) {
  77. if (--debouncing) {
  78. _delay_ms(1);
  79. } else {
  80. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  81. matrix[i] = matrix_debouncing[i];
  82. }
  83. }
  84. }
  85. return 1;
  86. }
  87. inline
  88. matrix_row_t matrix_get_row(uint8_t row)
  89. {
  90. return matrix[row];
  91. }
  92. /* Column pin configuration
  93. * col: 0 1 2 3 4 5 6 7
  94. * pin: F4 F5 F6 F7 B1 B3 B2 B6
  95. */
  96. static void init_cols(void)
  97. {
  98. // Input with pull-up(DDR:0, PORT:1)
  99. DDRB &= ~0b01001110;
  100. PORTB |= 0b01001110;
  101. DDRF &= ~0b11110000;
  102. PORTF |= 0b11110000;
  103. }
  104. /* Returns status of switches(1:on, 0:off) */
  105. static matrix_row_t read_cols(void)
  106. {
  107. return (PINF&(1<<4) ? 0 : (1<<0)) |
  108. (PINF&(1<<5) ? 0 : (1<<1)) |
  109. (PINF&(1<<6) ? 0 : (1<<2)) |
  110. (PINF&(1<<7) ? 0 : (1<<3)) |
  111. (PINB&(1<<1) ? 0 : (1<<4)) |
  112. (PINB&(1<<3) ? 0 : (1<<5)) |
  113. (PINB&(1<<2) ? 0 : (1<<6)) |
  114. (PINB&(1<<6) ? 0 : (1<<7)) ;
  115. }
  116. /* Row pin configuration
  117. * row: 0 1 2 3 4 5 6 7
  118. * pin: D3 D2 D1 D0 D4 C6 D7 E6
  119. */
  120. static void unselect_rows(void)
  121. {
  122. // Hi-Z(DDR:0, PORT:0) to unselect
  123. DDRC &= ~0b01000000;
  124. PORTC &= ~0b01000000;
  125. DDRD &= ~0b10011111;
  126. PORTD &= ~0b10011111;
  127. DDRE &= ~0b01000000;
  128. PORTE &= ~0b01000000;
  129. }
  130. static void select_row(uint8_t row)
  131. {
  132. // Output low(DDR:1, PORT:0) to select
  133. switch (row) {
  134. case 0:
  135. DDRD |= (1<<3);
  136. PORTD &= ~(1<<3);
  137. break;
  138. case 1:
  139. DDRD |= (1<<2);
  140. PORTD &= ~(1<<2);
  141. break;
  142. case 2:
  143. DDRD |= (1<<1);
  144. PORTD &= ~(1<<1);
  145. break;
  146. case 3:
  147. DDRD |= (1<<0);
  148. PORTD &= ~(1<<0);
  149. break;
  150. case 4:
  151. DDRD |= (1<<4);
  152. PORTD &= ~(1<<4);
  153. break;
  154. case 5:
  155. DDRC |= (1<<6);
  156. PORTC &= ~(1<<6);
  157. break;
  158. case 6:
  159. DDRD |= (1<<7);
  160. PORTD &= ~(1<<7);
  161. break;
  162. case 7:
  163. DDRE |= (1<<6);
  164. PORTE &= ~(1<<6);
  165. break;
  166. }
  167. }