36c95815f66ff077baa25d5634acd83d42674116
[tmk_keyboard.git] / keyboard / infinity / matrix.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include "gpio_api.h"
4 #include "timer.h"
5 #include "wait.h"
6 #include "matrix.h"
7
8
9 #ifndef DEBOUNCE
10 #define DEBOUNCE 5
11 #endif
12
13 /*
14 * Infinity Pinusage:
15 * Column pins are input with internal pull-down. Row pins are output and strobe with high.
16 * Key is high or 1 when it turns on.
17 *
18 * col: { PTD1, PTD2, PTD3, PTD4, PTD5, PTD6, PTD7 }
19 * row: { PTB0, PTB1, PTB2, PTB3, PTB16, PTB17, PTC4, PTC5, PTD0 }
20 */
21 static gpio_t col[MATRIX_COLS];
22 static gpio_t row[MATRIX_ROWS];
23
24 /* matrix state(1:on, 0:off) */
25 static matrix_row_t matrix[MATRIX_ROWS];
26 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
27 static bool debouncing = false;
28 static uint16_t debouncing_time = 0;
29
30
31 void matrix_init(void)
32 {
33 /* Column(sense) */
34 gpio_init_in_ex(&col[0], PTD1, PullDown);
35 gpio_init_in_ex(&col[1], PTD2, PullDown);
36 gpio_init_in_ex(&col[2], PTD3, PullDown);
37 gpio_init_in_ex(&col[3], PTD4, PullDown);
38 gpio_init_in_ex(&col[4], PTD5, PullDown);
39 gpio_init_in_ex(&col[5], PTD6, PullDown);
40 gpio_init_in_ex(&col[6], PTD7, PullDown);
41
42 #ifdef INFINITY_LED
43 /* Row(strobe) */
44 gpio_init_out_ex(&row[0], PTB0, 0);
45 gpio_init_out_ex(&row[1], PTB1, 0);
46 gpio_init_out_ex(&row[2], PTB2, 0);
47 gpio_init_out_ex(&row[3], PTB3, 0);
48 gpio_init_out_ex(&row[4], PTB16, 0);
49 gpio_init_out_ex(&row[5], PTB17, 0);
50 gpio_init_out_ex(&row[6], PTC4, 0);
51 gpio_init_out_ex(&row[7], PTC5, 0);
52 gpio_init_out_ex(&row[8], PTD0, 0);
53 #else
54 gpio_init_out_ex(&row[0], PTC0, 0);
55 gpio_init_out_ex(&row[1], PTC1, 0);
56 gpio_init_out_ex(&row[2], PTC2, 0);
57 gpio_init_out_ex(&row[3], PTC3, 0);
58 gpio_init_out_ex(&row[4], PTC4, 0);
59 gpio_init_out_ex(&row[5], PTC5, 0);
60 gpio_init_out_ex(&row[6], PTC6, 0);
61 gpio_init_out_ex(&row[7], PTC7, 0);
62 gpio_init_out_ex(&row[8], PTD0, 0);
63
64 #endif
65 }
66
67 uint8_t matrix_scan(void)
68 {
69 for (int i = 0; i < MATRIX_ROWS; i++) {
70 matrix_row_t r = 0;
71
72 gpio_write(&row[i], 1);
73 wait_us(1); // need wait to settle pin state
74 for (int j = 0; j < MATRIX_COLS; j++) {
75 if (gpio_read(&col[j])) {
76 r |= (1<<j);
77 }
78 }
79 gpio_write(&row[i], 0);
80
81 if (matrix_debouncing[i] != r) {
82 matrix_debouncing[i] = r;
83 debouncing = true;
84 debouncing_time = timer_read();
85 }
86 }
87
88 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
89 for (int i = 0; i < MATRIX_ROWS; i++) {
90 matrix[i] = matrix_debouncing[i];
91 }
92 debouncing = false;
93 }
94 /*
95 if (debouncing) {
96 if (--debouncing) {
97 return 0;
98 } else {
99 for (int i = 0; i < MATRIX_ROWS; i++) {
100 matrix[i] = matrix_debouncing[i];
101 }
102 }
103 }
104 */
105 return 1;
106 }
107
108 bool matrix_is_on(uint8_t row, uint8_t col)
109 {
110 return (matrix[row] & (1<<col));
111 }
112
113 matrix_row_t matrix_get_row(uint8_t row)
114 {
115 return matrix[row];
116 }
117
118 void matrix_print(void)
119 {
120 }
Imprint / Impressum