#include "util.h"
#include "matrix.h"
-#define CONFIG_LED_IO \
- DDRB |= (1<<7); \
- DDRC |= (1<<5) | (1<<6);
-
-#define USB_LED_CAPS_LOCK_ON PORTC &= ~(1<<5)
-#define USB_LED_CAPS_LOCK_OFF PORTC |= (1<<5)
-#define USB_LED_NUM_LOCK_ON PORTB &= ~(1<<7)
-#define USB_LED_NUM_LOCK_OFF PORTB |= (1<<7)
-#define USB_LED_SCROLL_LOCK_ON PORTC &= ~(1<<6)
-#define USB_LED_SCROLL_LOCK_OFF PORTC |= (1<<6)
-
#ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5
#endif
static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];
-static matrix_row_t scan_col(void);
-static void select_col(uint8_t row);
-
-void matrix_init(void) {
- /* Row output pins */
- DDRD |= 0b01111011;
- /* Column input pins */
- DDRC &= ~0b10000000;
- DDRB &= ~0b01111111;
- PORTC |= 0b10000000;
- PORTB |= 0b01111111;
-
- for (uint8_t i=0; i < MATRIX_ROWS; i++)
- matrix[i] = matrix_debouncing[i] = 0;
-
- matrix_init_quantum();
-}
-
-uint8_t matrix_scan(void) {
- for (uint8_t col = 0; col < MATRIX_COLS; col++) {
- select_col(col);
- _delay_us(3);
- matrix_row_t col_scan = scan_col();
- for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
- bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
- bool curr_bit = col_scan & (1<<row);
- if (prev_bit != curr_bit) {
- matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
- debouncing = DEBOUNCING_DELAY;
- }
- }
- }
-
- if (debouncing) {
- if (--debouncing)
- _delay_ms(1);
- else
- for (uint8_t i = 0; i < MATRIX_ROWS; i++)
- matrix[i] = matrix_debouncing[i];
- }
-
- matrix_scan_quantum();
- return 1;
-}
-
-inline matrix_row_t matrix_get_row(uint8_t row) {
- return matrix[row];
-}
-
-void matrix_print(void) {
- print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
- for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
- matrix_row_t matrix_row = matrix_get_row(row);
- xprintf("%02X: ", row);
- for (uint8_t col = 0; col < MATRIX_COLS; col++) {
- bool curr_bit = matrix_row & (1<<col);
- xprintf("%c", curr_bit ? '*' : '.');
- }
- print("\n");
- }
-}
-
-uint8_t matrix_key_count(void) {
- uint8_t count = 0;
- for (uint8_t row = 0; row < MATRIX_ROWS; row++)
- count += bitpop32(matrix[row]);
- return count;
-}
-
static matrix_row_t scan_col(void) {
return (
(PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) |
case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
}
-}
\ No newline at end of file
+}
+
+void matrix_init(void) {
+ /* Row output pins */
+ DDRD |= 0b01111011;
+ /* Column input pins */
+ DDRC &= ~0b10000000;
+ DDRB &= ~0b01111111;
+ PORTC |= 0b10000000;
+ PORTB |= 0b01111111;
+
+ for (uint8_t i=0; i < MATRIX_ROWS; i++)
+ matrix[i] = matrix_debouncing[i] = 0;
+
+ matrix_init_quantum();
+}
+
+uint8_t matrix_scan(void) {
+ for (uint8_t col = 0; col < MATRIX_COLS; col++) {
+ select_col(col);
+ _delay_us(3);
+ matrix_row_t col_scan = scan_col();
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
+ bool curr_bit = col_scan & (1<<row);
+ if (prev_bit != curr_bit) {
+ matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
+ debouncing = DEBOUNCING_DELAY;
+ }
+ }
+ }
+
+ if (debouncing) {
+ if (--debouncing)
+ _delay_ms(1);
+ else
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++)
+ matrix[i] = matrix_debouncing[i];
+ }
+
+ matrix_scan_quantum();
+ return 1;
+}
+
+inline matrix_row_t matrix_get_row(uint8_t row) {
+ return matrix[row];
+}
+
+void matrix_print(void) {
+#ifndef NO_PRINT
+ print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ matrix_row_t matrix_row = matrix_get_row(row);
+ xprintf("%02X: ", row);
+ for (uint8_t col = 0; col < MATRIX_COLS; col++) {
+ bool curr_bit = matrix_row & (1<<col);
+ xprintf("%c", curr_bit ? '*' : '.');
+ }
+ print("\n");
+ }
+#endif
+}
+
+uint8_t matrix_key_count(void) {
+ uint8_t count = 0;
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++)
+ count += bitpop32(matrix[row]);
+ return count;
+}