matrix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. Copyright 2016 Ralf Schmitt <ralf@bunkertor.net>
  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. static uint8_t debouncing = DEBOUNCING_DELAY;
  23. /* matrix state(1:on, 0:off) */
  24. static matrix_row_t matrix[MATRIX_ROWS];
  25. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  26. static uint8_t read_rows(uint8_t col);
  27. static void init_ports(void);
  28. static void unselect_cols(void);
  29. static void select_col(uint8_t col);
  30. __attribute__ ((weak))
  31. void matrix_init_kb(void) {
  32. matrix_init_user();
  33. }
  34. __attribute__ ((weak))
  35. void matrix_scan_kb(void) {
  36. matrix_scan_user();
  37. }
  38. __attribute__ ((weak))
  39. void matrix_init_user(void) {
  40. }
  41. __attribute__ ((weak))
  42. void matrix_scan_user(void) {
  43. }
  44. inline
  45. uint8_t matrix_rows(void)
  46. {
  47. return MATRIX_ROWS;
  48. }
  49. inline
  50. uint8_t matrix_cols(void)
  51. {
  52. return MATRIX_COLS;
  53. }
  54. void backlight_init_ports(void)
  55. {
  56. DDRD |= 0b01010000;
  57. PORTD &= 0b10101111;
  58. DDRB |= 0b00001110;
  59. PORTB &= 0b11110001;
  60. DDRE |= 0b01000000;
  61. PORTE &= 0b10111111;
  62. }
  63. void matrix_init(void)
  64. {
  65. backlight_init_ports();
  66. unselect_cols();
  67. init_ports();
  68. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  69. matrix[i] = 0;
  70. matrix_debouncing[i] = 0;
  71. }
  72. }
  73. uint8_t matrix_scan(void)
  74. {
  75. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  76. select_col(col);
  77. _delay_us(3);
  78. uint8_t rows = read_rows(col);
  79. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  80. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  81. bool curr_bit = rows & (1<<row);
  82. if (prev_bit != curr_bit) {
  83. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  84. if (debouncing) {
  85. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  86. }
  87. debouncing = DEBOUNCING_DELAY;
  88. }
  89. }
  90. unselect_cols();
  91. }
  92. if (debouncing) {
  93. if (--debouncing) {
  94. _delay_ms(1);
  95. } else {
  96. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  97. matrix[i] = matrix_debouncing[i];
  98. }
  99. }
  100. }
  101. return 1;
  102. }
  103. bool matrix_is_modified(void)
  104. {
  105. if (debouncing) return false;
  106. return true;
  107. }
  108. inline
  109. bool matrix_is_on(uint8_t row, uint8_t col)
  110. {
  111. return (matrix[row] & ((matrix_row_t)1<<col));
  112. }
  113. inline
  114. matrix_row_t matrix_get_row(uint8_t row)
  115. {
  116. return matrix[row];
  117. }
  118. void matrix_print(void)
  119. {
  120. print("\nr/c 0123456789ABCDEF\n");
  121. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  122. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  123. }
  124. }
  125. uint8_t matrix_key_count(void)
  126. {
  127. uint8_t count = 0;
  128. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  129. count += bitpop32(matrix[i]);
  130. }
  131. return count;
  132. }
  133. static void init_ports(void)
  134. {
  135. // Rows are inputs (inputs are 0)
  136. DDRD &= 0b11010000;
  137. DDRB &= 0b01111111;
  138. DDRE &= 0b11111011;
  139. // Columns are outputs (outputs are high)
  140. DDRF |= 0b00000011;
  141. DDRC |= 0b11000000;
  142. DDRB |= 0b01110001;
  143. DDRD |= 0b10000000;
  144. }
  145. static uint8_t read_rows(uint8_t col)
  146. {
  147. if(col == 15) {
  148. return (PINE&(1<<2) ? 0 : (1<<0)) |
  149. (PIND&(1<<0) ? (1<<1) : 0) |
  150. (PIND&(1<<1) ? (1<<2) : 0) |
  151. (PIND&(1<<2) ? (1<<3) : 0) |
  152. (PIND&(1<<3) ? (1<<4) : 0) |
  153. (PIND&(1<<5) ? (1<<5) : 0);
  154. } else {
  155. return (PINB&(1<<7) ? (1<<0) : 0) |
  156. (PIND&(1<<0) ? (1<<1) : 0) |
  157. (PIND&(1<<1) ? (1<<2) : 0) |
  158. (PIND&(1<<2) ? (1<<3) : 0) |
  159. (PIND&(1<<3) ? (1<<4) : 0) |
  160. (PIND&(1<<5) ? (1<<5) : 0);
  161. }
  162. }
  163. static void unselect_cols(void)
  164. {
  165. PORTF &= 0b11111100;
  166. PORTC &= 0b00111111;
  167. PORTB &= 0b10001110;
  168. PORTD &= 0b01111111;
  169. }
  170. static void select_col(uint8_t col)
  171. {
  172. switch (col) {
  173. case 0:
  174. PORTC |= 0b01000000;
  175. break;
  176. case 1:
  177. PORTC |= 0b01000000;
  178. PORTF |= 0b00000001;
  179. break;
  180. case 2:
  181. PORTC |= 0b01000000;
  182. PORTF |= 0b00000010;
  183. break;
  184. case 3:
  185. PORTC |= 0b01000000;
  186. PORTF |= 0b00000011;
  187. break;
  188. case 4:
  189. PORTC |= 0b11000000;
  190. break;
  191. case 5:
  192. PORTC |= 0b11000000;
  193. PORTF |= 0b00000001;
  194. break;
  195. case 6:
  196. PORTC |= 0b11000000;
  197. PORTF |= 0b00000010;
  198. break;
  199. case 7:
  200. PORTC |= 0b11000000;
  201. PORTF |= 0b00000011;
  202. break;
  203. case 8:
  204. PORTB |= 0b01000000;
  205. break;
  206. case 9:
  207. PORTB |= 0b01000000;
  208. PORTF |= 0b00000001;
  209. break;
  210. case 10:
  211. PORTB |= 0b01000000;
  212. PORTF |= 0b00000010;
  213. break;
  214. case 11:
  215. PORTB |= 0b01000000;
  216. PORTF |= 0b00000011;
  217. break;
  218. case 12:
  219. PORTB |= 0b01000000;
  220. PORTC |= 0b10000000;
  221. break;
  222. case 13:
  223. PORTB |= 0b01000000;
  224. PORTF |= 0b00000001;
  225. PORTC |= 0b10000000;
  226. break;
  227. case 14:
  228. PORTB |= 0b01000000;
  229. PORTF |= 0b00000010;
  230. PORTC |= 0b10000000;
  231. break;
  232. case 15:
  233. PORTB |= 0b01000000;
  234. PORTF |= 0b00000011;
  235. PORTC |= 0b10000000;
  236. break;
  237. case 16:
  238. PORTB |= 0b00100000;
  239. break;
  240. case 17:
  241. PORTB |= 0b00010000;
  242. break;
  243. case 18:
  244. PORTD |= 0b10000000;
  245. break;
  246. case 19:
  247. PORTB |= 0b00000001;
  248. break;
  249. }
  250. }