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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright 2014 Ralf Schmitt <[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. #include "backlight.h"
  23. #ifndef DEBOUNCE
  24. # define DEBOUNCE 5
  25. #endif
  26. static uint8_t debouncing = DEBOUNCE;
  27. /* matrix state(1:on, 0:off) */
  28. static matrix_row_t matrix[MATRIX_ROWS];
  29. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  30. static uint16_t read_inputs(void);
  31. static void init_inputs(void);
  32. static void init_outputs(void);
  33. static void reset_inputs(void);
  34. static void reset_outputs(void);
  35. static void select_output(uint8_t col);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. backlight_init_ports();
  49. init_inputs();
  50. init_outputs();
  51. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  52. matrix[i] = 0;
  53. matrix_debouncing[i] = 0;
  54. }
  55. }
  56. uint8_t matrix_scan(void)
  57. {
  58. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  59. reset_inputs();
  60. reset_outputs();
  61. select_output(col);
  62. _delay_us(3);
  63. uint16_t rows = read_inputs();
  64. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  65. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  66. bool curr_bit = rows & (1<<row);
  67. if (prev_bit != curr_bit) {
  68. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  69. if (debouncing) {
  70. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  71. }
  72. debouncing = DEBOUNCE;
  73. }
  74. }
  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. bool matrix_is_modified(void)
  88. {
  89. if (debouncing) return false;
  90. return true;
  91. }
  92. inline
  93. bool matrix_is_on(uint8_t row, uint8_t col)
  94. {
  95. return (matrix[row] & ((matrix_row_t)1<<col));
  96. }
  97. inline
  98. matrix_row_t matrix_get_row(uint8_t row)
  99. {
  100. return matrix[row];
  101. }
  102. void matrix_print(void)
  103. {
  104. print("\nr/c 0123456789ABCDEF\n");
  105. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  106. phex(row); print(": ");
  107. pbin_reverse16(matrix_get_row(row));
  108. print("\n");
  109. }
  110. }
  111. uint8_t matrix_key_count(void)
  112. {
  113. uint8_t count = 0;
  114. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  115. count += bitpop16(matrix[i]);
  116. }
  117. return count;
  118. }
  119. static void init_inputs(void)
  120. {
  121. DDRE &= ~0b01000000; // PE6 (Col 0)
  122. DDRB &= ~0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
  123. DDRF &= ~0b00000001; // PF0 (Col 5)
  124. DDRD &= ~0b00100011; // PD0 (Col 6), PD1 (Col 8 TKL), PD5 (Col 7)
  125. }
  126. static uint16_t read_inputs(void)
  127. {
  128. return (PINE&(1<<6) ? 0 : (1<<0)) | // PE6 (Col 0)
  129. (PINB&(1<<0) ? 0 : (1<<1)) | // PB0 (Col 1)
  130. (PINB&(1<<1) ? 0 : (1<<2)) | // PB1 (Col 2)
  131. (PINB&(1<<2) ? 0 : (1<<3)) | // PB2 (Col 3)
  132. (PINB&(1<<3) ? 0 : (1<<4)) | // PB3 (Col 4)
  133. (PINF&(1<<0) ? 0 : (1<<5)) | // PF0 (Col 5)
  134. (PIND&(1<<0) ? 0 : (1<<6)) | // PD0 (Col 6)
  135. (PIND&(1<<5) ? 0 : (1<<7)) | // PD5 (Col 7)
  136. (PIND&(1<<1) ? 0 : (1<<8)); // PD1 (Col 8 TKL)
  137. }
  138. static void reset_inputs(void)
  139. {
  140. PORTE |= 0b01000000; // PE6 (Col 0)
  141. PORTB |= 0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
  142. PORTF |= 0b00000001; // PF0 (Col 5)
  143. PORTD |= 0b00100011; // PD0 (Col 6), PD1 (Col 8 TKL), PD5 (Col 7)
  144. }
  145. static void init_outputs(void)
  146. {
  147. DDRB |= 0b00010000; // PB4 (Row 0)
  148. DDRE |= 0b00000100; // PE2 (Row 1)
  149. DDRF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
  150. DDRC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
  151. DDRD |= 0b10000000; // PD7 (Row 8)
  152. }
  153. static void reset_outputs(void)
  154. {
  155. PORTB |= 0b00010000; // PB4 (Row 0)
  156. PORTE |= 0b00000100; // PE2 (Row 1)
  157. PORTF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
  158. PORTC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
  159. PORTD |= 0b10000000; // PD7 (Row 8)
  160. }
  161. static void select_output(uint8_t col)
  162. {
  163. switch (col) {
  164. case 0:
  165. PORTB &= ~(1<<4);
  166. break;
  167. case 1:
  168. PORTE &= ~(1<<2);
  169. break;
  170. case 2:
  171. PORTF &= ~(1<<4);
  172. break;
  173. case 3:
  174. PORTF &= ~(1<<7);
  175. break;
  176. case 4:
  177. PORTF &= ~(1<<1);
  178. break;
  179. case 5:
  180. PORTF &= ~(1<<6);
  181. break;
  182. case 6:
  183. PORTC &= ~(1<<6);
  184. break;
  185. case 7:
  186. PORTF &= ~(1<<5);
  187. break;
  188. case 8:
  189. PORTD &= ~(1<<7);
  190. break;
  191. case 9:
  192. PORTC &= ~(1<<7);
  193. break;
  194. }
  195. }