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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright 2011 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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #ifndef DEBOUNCE
  23. # define DEBOUNCE 5
  24. #endif
  25. static uint8_t debouncing = DEBOUNCE;
  26. // bit array of key state(1:on, 0:off)
  27. static matrix_row_t *matrix;
  28. static matrix_row_t *matrix_debounced;
  29. static matrix_row_t _matrix0[MATRIX_ROWS];
  30. static matrix_row_t _matrix1[MATRIX_ROWS];
  31. #define NROW 18
  32. #define NCOL 8
  33. #define _DDRA (uint8_t *const)&DDRA
  34. #define _DDRB (uint8_t *const)&DDRB
  35. #define _DDRC (uint8_t *const)&DDRC
  36. #define _DDRD (uint8_t *const)&DDRD
  37. #define _DDRE (uint8_t *const)&DDRE
  38. #define _DDRF (uint8_t *const)&DDRF
  39. #define _PINA (uint8_t *const)&PINA
  40. #define _PINB (uint8_t *const)&PINB
  41. #define _PINC (uint8_t *const)&PINC
  42. #define _PIND (uint8_t *const)&PIND
  43. #define _PINE (uint8_t *const)&PINE
  44. #define _PINF (uint8_t *const)&PINF
  45. #define _PORTA (uint8_t *const)&PORTA
  46. #define _PORTB (uint8_t *const)&PORTB
  47. #define _PORTC (uint8_t *const)&PORTC
  48. #define _PORTD (uint8_t *const)&PORTD
  49. #define _PORTE (uint8_t *const)&PORTE
  50. #define _PORTF (uint8_t *const)&PORTF
  51. #define _BIT0 0x01
  52. #define _BIT1 0x02
  53. #define _BIT2 0x04
  54. #define _BIT3 0x08
  55. #define _BIT4 0x10
  56. #define _BIT5 0x20
  57. #define _BIT6 0x40
  58. #define _BIT7 0x80
  59. /* Specifies the ports and pin numbers for the rows */
  60. static
  61. uint8_t *const row_ddr[NROW] = { _DDRB, _DDRB,
  62. _DDRC, _DDRC,
  63. _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD,
  64. _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF};
  65. static
  66. uint8_t *const row_port[NROW] = { _PORTB, _PORTB,
  67. _PORTC, _PORTC,
  68. _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
  69. _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF};
  70. static
  71. uint8_t *const row_pin[NROW] = { _PINB, _PINB,
  72. _PINC, _PINC,
  73. _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND,
  74. _PINF, _PINF, _PINF, _PINF, _PINF, _PINF};
  75. static
  76. const uint8_t row_bit[NROW] = { _BIT4, _BIT7,
  77. _BIT6, _BIT7,
  78. _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5, _BIT6, _BIT7,
  79. _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7};
  80. static
  81. const uint8_t mask = 0x0E;
  82. /* Specifies the ports and pin numbers for the columns */
  83. static
  84. const uint8_t col_bit[NCOL] = { 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E};
  85. static
  86. inline void pull_column(int col) {
  87. PORTB = col_bit[col] | (PORTB & ~mask);
  88. }
  89. static
  90. inline void release_column(int col) {
  91. }
  92. /* PORTB is set as input with pull-up resistors
  93. PORTC,D,E,F are set to high output */
  94. static
  95. void setup_io_pins(void) {
  96. uint8_t row;
  97. DDRB |= 0x0E;
  98. PORTB &= ~0x0E;
  99. for(row = 0; row < NROW; row++) {
  100. *row_ddr[row] &= ~row_bit[row];
  101. *row_port[row] &= ~row_bit[row];
  102. }
  103. }
  104. static
  105. void setup_leds(void) {
  106. DDRB |= 0x60;
  107. PORTB |= 0x60;
  108. }
  109. inline
  110. uint8_t matrix_rows(void)
  111. {
  112. return MATRIX_ROWS;
  113. }
  114. inline
  115. uint8_t matrix_cols(void)
  116. {
  117. return MATRIX_COLS;
  118. }
  119. void matrix_init(void)
  120. {
  121. // initialize row and col
  122. setup_io_pins();
  123. setup_leds();
  124. // initialize matrix state: all keys off
  125. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  126. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  127. matrix = _matrix0;
  128. matrix_debounced = _matrix1;
  129. }
  130. uint8_t matrix_scan(void)
  131. {
  132. if (!debouncing) {
  133. uint8_t *tmp = matrix_debounced;
  134. matrix_debounced = matrix;
  135. matrix = tmp;
  136. }
  137. for (uint8_t col = 0; col < NCOL; col++) { // 0-7
  138. pull_column(col); // output hi on theline
  139. _delay_us(1); // without this wait it won't read stable value.
  140. for (uint8_t row = 0; row < NROW; row++) { // 0-17
  141. bool prev_bit = matrix[row] & (1<<col);
  142. bool curr_bit = *row_pin[row] & row_bit[row];
  143. if (prev_bit != curr_bit) {
  144. matrix[row] ^= (1<<col);
  145. if (debouncing) {
  146. debug("bounce!: "); debug_hex(debouncing); print("\n");
  147. }
  148. debouncing = DEBOUNCE;
  149. }
  150. }
  151. release_column(col);
  152. }
  153. if (debouncing) {
  154. debouncing--;
  155. }
  156. return 1;
  157. }
  158. bool matrix_is_modified(void)
  159. {
  160. // NOTE: no longer used
  161. return true;
  162. }
  163. inline
  164. bool matrix_has_ghost(void)
  165. {
  166. return false;
  167. }
  168. inline
  169. bool matrix_is_on(uint8_t row, uint8_t col)
  170. {
  171. return (matrix_debounced[row] & (1<<col));
  172. }
  173. inline
  174. matrix_row_t matrix_get_row(uint8_t row)
  175. {
  176. return matrix_debounced[row];
  177. }
  178. void matrix_print(void)
  179. {
  180. print("\nr/c 01234567\n");
  181. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  182. phex(row); print(": ");
  183. pbin_reverse(matrix_get_row(row));
  184. print("\n");
  185. }
  186. }
  187. uint8_t matrix_key_count(void)
  188. {
  189. uint8_t count = 0;
  190. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  191. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  192. if (matrix_is_on(i, j))
  193. count++;
  194. }
  195. }
  196. return count;
  197. }