]>
git.gir.st - tmk_keyboard.git/blob - keyboard/hid_liber/matrix.c
1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
3 * This is heavily based on hid_liber/board.{c|h}.
4 * https://github.com/BathroomEpiphanies/AVR-Keyboard
6 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7 * http://bathroomepiphanies.com
9 * As for liscensing consult with the original files or its author.
14 #include <util/delay.h>
24 static uint8_t debouncing
= DEBOUNCE
;
26 // bit array of key state(1:on, 0:off)
27 static matrix_row_t
*matrix
;
28 static matrix_row_t
*matrix_debounced
;
29 static matrix_row_t _matrix0
[MATRIX_ROWS
];
30 static matrix_row_t _matrix1
[MATRIX_ROWS
];
35 #define _DDRA (uint8_t *const)&DDRA
36 #define _DDRB (uint8_t *const)&DDRB
37 #define _DDRC (uint8_t *const)&DDRC
38 #define _DDRD (uint8_t *const)&DDRD
39 #define _DDRE (uint8_t *const)&DDRE
40 #define _DDRF (uint8_t *const)&DDRF
42 #define _PINA (uint8_t *const)&PINA
43 #define _PINB (uint8_t *const)&PINB
44 #define _PINC (uint8_t *const)&PINC
45 #define _PIND (uint8_t *const)&PIND
46 #define _PINE (uint8_t *const)&PINE
47 #define _PINF (uint8_t *const)&PINF
49 #define _PORTA (uint8_t *const)&PORTA
50 #define _PORTB (uint8_t *const)&PORTB
51 #define _PORTC (uint8_t *const)&PORTC
52 #define _PORTD (uint8_t *const)&PORTD
53 #define _PORTE (uint8_t *const)&PORTE
54 #define _PORTF (uint8_t *const)&PORTF
65 /* Specifies the ports and pin numbers for the rows */
67 uint8_t *const row_ddr
[NROW
] = { _DDRB
, _DDRB
,
69 _DDRD
, _DDRD
, _DDRD
, _DDRD
, _DDRD
, _DDRD
, _DDRD
, _DDRD
,
70 _DDRF
, _DDRF
, _DDRF
, _DDRF
, _DDRF
, _DDRF
};
73 uint8_t *const row_port
[NROW
] = { _PORTB
, _PORTB
,
75 _PORTD
, _PORTD
, _PORTD
, _PORTD
, _PORTD
, _PORTD
, _PORTD
, _PORTD
,
76 _PORTF
, _PORTF
, _PORTF
, _PORTF
, _PORTF
, _PORTF
};
79 uint8_t *const row_pin
[NROW
] = { _PINB
, _PINB
,
81 _PIND
, _PIND
, _PIND
, _PIND
, _PIND
, _PIND
, _PIND
, _PIND
,
82 _PINF
, _PINF
, _PINF
, _PINF
, _PINF
, _PINF
};
85 const uint8_t row_bit
[NROW
] = { _BIT4
, _BIT7
,
87 _BIT0
, _BIT1
, _BIT2
, _BIT3
, _BIT4
, _BIT5
, _BIT6
, _BIT7
,
88 _BIT0
, _BIT1
, _BIT4
, _BIT5
, _BIT6
, _BIT7
};
91 const uint8_t mask
= 0x0E;
93 /* Specifies the ports and pin numbers for the columns */
95 const uint8_t col_bit
[NCOL
] = { 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E};
98 inline void pull_column(int col
) {
99 PORTB
= col_bit
[col
] | (PORTB
& ~mask
);
103 inline void release_column(int col
) {
106 /* PORTB is set as input with pull-up resistors
107 PORTC,D,E,F are set to high output */
109 void setup_io_pins(void) {
113 for(row
= 0; row
< NROW
; row
++) {
114 *row_ddr
[row
] &= ~row_bit
[row
];
115 *row_port
[row
] &= ~row_bit
[row
];
120 void setup_leds(void) {
127 uint8_t matrix_rows(void)
133 uint8_t matrix_cols(void)
138 void matrix_init(void)
140 // initialize row and col
144 // initialize matrix state: all keys off
145 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix0
[i
] = 0x00;
146 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) _matrix1
[i
] = 0x00;
148 matrix_debounced
= _matrix1
;
151 uint8_t matrix_scan(void)
154 uint8_t *tmp
= matrix_debounced
;
155 matrix_debounced
= matrix
;
159 for (uint8_t col
= 0; col
< NCOL
; col
++) { // 0-7
160 pull_column(col
); // output hi on theline
161 _delay_us(1); // without this wait it won't read stable value.
162 for (uint8_t row
= 0; row
< NROW
; row
++) { // 0-17
163 bool prev_bit
= matrix
[row
] & (1<<col
);
164 bool curr_bit
= *row_pin
[row
] & row_bit
[row
];
165 if (prev_bit
!= curr_bit
) {
166 matrix
[row
] ^= (1<<col
);
168 debug("bounce!: "); debug_hex(debouncing
); print("\n");
170 debouncing
= DEBOUNCE
;
183 bool matrix_is_modified(void)
185 // NOTE: no longer used
190 bool matrix_has_ghost(void)
196 bool matrix_is_on(uint8_t row
, uint8_t col
)
198 return (matrix_debounced
[row
] & (1<<col
));
202 matrix_row_t
matrix_get_row(uint8_t row
)
204 return matrix_debounced
[row
];
207 void matrix_print(void)
209 print("\nr/c 01234567\n");
210 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
211 phex(row
); print(": ");
212 pbin_reverse(matrix_get_row(row
));
217 uint8_t matrix_key_count(void)
220 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
221 for (uint8_t j
= 0; j
< MATRIX_COLS
; j
++) {
222 if (matrix_is_on(i
, j
))