]>
git.gir.st - tmk_keyboard.git/blob - macway/matrix.c
7 #include <util/delay.h>
10 #include "matrix_skel.h"
13 // matrix state buffer (key on: 1/key off: 0)
14 static uint8_t *matrix
;
15 static uint8_t *matrix_prev
;
16 static uint8_t _matrix0
[MATRIX_ROWS
];
17 static uint8_t _matrix1
[MATRIX_ROWS
];
19 static bool matrix_has_ghost_in_row(uint8_t row
);
20 static uint8_t read_col(void);
21 static void unselect_rows(void);
22 static void select_row(uint8_t row
);
37 // this must be called once before matrix_scan.
38 void matrix_init(void)
40 // initialize row and col
42 // Input with pull-up(DDR:0, PORT:1)
46 // initialize matrix state: all keys off
47 for (int i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
48 for (int i
=0; i
< MATRIX_ROWS
; i
++) _matrix1
[i
] = 0x00;
50 matrix_prev
= _matrix1
;
61 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
64 _delay_us(30); // without this wait read unstable value.
65 matrix
[i
] = ~read_col();
71 bool matrix_is_modified(void)
73 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
74 if (matrix
[i
] != matrix_prev
[i
])
80 bool matrix_has_ghost(void)
82 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
83 if (matrix_has_ghost_in_row(i
))
90 bool matrix_is_on(int row
, int col
)
92 return (matrix
[row
] & (1<<col
));
96 uint16_t matrix_get_row(int row
)
101 void matrix_print(void)
103 print("\nr/c 01234567\n");
104 for (int row
= 0; row
< matrix_rows(); row
++) {
105 phex(row
); print(": ");
106 pbin_reverse(matrix_get_row(row
));
107 if (matrix_has_ghost_in_row(row
)) {
114 int matrix_key_count(void)
117 for (int i
= 0; i
< MATRIX_ROWS
; i
++) {
118 count
+= bitpop(matrix
[i
]);
123 static bool matrix_has_ghost_in_row(uint8_t row
)
125 // no ghost exists in case less than 2 keys on
126 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
129 // ghost exists in case same state as other row
130 for (int i
=0; i
< MATRIX_ROWS
; i
++) {
131 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
137 static uint8_t read_col(void)
142 static void unselect_rows(void)
144 // Hi-Z(DDR:0, PORT:0) to unselect
145 DDRC
&= ~0b11000000; // PC: 7,6
146 PORTC
&= ~0b11000000;
147 DDRD
&= ~0b11000111; // PD: 7,6,2,1,0
148 PORTD
&= ~0b11000111;
149 DDRF
&= ~0b11000000; // PF: 7,6
150 PORTF
&= ~0b11000000;
153 static void select_row(uint8_t row
)
155 // Output low(DDR:1, PORT:0) to select
156 // row: 0 1 2 3 4 5 6 7 8
157 // pin: PD0, PC7, PD7, PF6, PD6, PD1, PD2, PC6, PF7