logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (6222B)


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