1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
3 * This is heavily based on phantom/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[MATRIX_ROWS];
28 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
30 static uint8_t read_rows(void);
31 static void init_rows(void);
32 static void unselect_cols(void);
33 static void select_col(uint8_t col);
35 #ifndef SLEEP_LED_ENABLE
36 /* LEDs are on output compare pins OC1B OC1C
37 This activates fast PWM mode on them.
38 Prescaler 256 and 8-bit counter results in
39 16000000/256/256 = 244 Hz blink frequency.
42 /* Output on PWM pins are turned off when the timer
43 reaches the value in the output compare register,
44 and are turned on when it reaches TOP (=256). */
48 TCCR1A |= // Timer control register 1A
49 (1<<WGM10) | // Fast PWM 8-bit
50 (1<<COM1B1)| // Clear OC1B on match, set at TOP
51 (1<<COM1C1); // Clear OC1C on match, set at TOP
52 TCCR1B |= // Timer control register 1B
53 (1<<WGM12) | // Fast PWM 8-bit
54 (1<<CS12); // Prescaler 256
55 OCR1B = LED_BRIGHTNESS; // Output compare register 1B
56 OCR1C = LED_BRIGHTNESS; // Output compare register 1C
57 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
58 DDRB |= (1<<6) | (1<<7);
59 PORTB &= ~((1<<6) | (1<<7));
64 uint8_t matrix_rows(void)
70 uint8_t matrix_cols(void)
75 void matrix_init(void)
77 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
81 // initialize row and col
84 #ifndef SLEEP_LED_ENABLE
88 // initialize matrix state: all keys off
89 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
91 matrix_debouncing[i] = 0;
95 uint8_t matrix_scan(void)
97 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
99 _delay_us(3); // without this wait it won't read stable value.
100 uint8_t rows = read_rows();
101 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
102 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
103 bool curr_bit = rows & (1<<row);
104 if (prev_bit != curr_bit) {
105 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
107 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
109 debouncing = DEBOUNCE;
119 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
120 matrix[i] = matrix_debouncing[i];
128 bool matrix_is_modified(void)
130 if (debouncing) return false;
135 bool matrix_is_on(uint8_t row, uint8_t col)
137 return (matrix[row] & ((matrix_row_t)1<<col));
141 matrix_row_t matrix_get_row(uint8_t row)
146 void matrix_print(void)
148 print("\nr/c 0123456789ABCDEF\n");
149 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
150 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
154 uint8_t matrix_key_count(void)
157 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
158 count += bitpop32(matrix[i]);
163 /* Row pin configuration
165 * pin: B5 B4 B3 B2 B1 B0
167 static void init_rows(void)
169 // Input with pull-up(DDR:0, PORT:1)
174 static uint8_t read_rows(void)
176 return (PINB&(1<<5) ? 0 : (1<<0)) |
177 (PINB&(1<<4) ? 0 : (1<<1)) |
178 (PINB&(1<<3) ? 0 : (1<<2)) |
179 (PINB&(1<<2) ? 0 : (1<<3)) |
180 (PINB&(1<<1) ? 0 : (1<<4)) |
181 (PINB&(1<<0) ? 0 : (1<<5));
184 /* Column pin configuration
185 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
186 * pin: D5 C7 C6 D4 D0 E6 F0 F1 F4 F5 F6 F7 D7 D6 D1 D2 D3
188 static void unselect_cols(void)
190 // Hi-Z(DDR:0, PORT:0) to unselect
191 DDRC |= 0b11000000; // PC: 7 6
193 DDRD |= 0b11111111; // PD: 7 6 5 4 3 2 1 0
195 DDRE |= 0b01000000; // PE: 6
197 DDRF |= 0b11110011; // PF: 7 6 5 4 1 0
201 static void select_col(uint8_t col)
203 // Output low(DDR:1, PORT:0) to select