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];
33 #define _DDRA (uint8_t *const)&DDRA
34 #define _DDRB (uint8_t *const)&DDRB
35 #define _DDRC (uint8_t *const)&DDRC
36 #define _DDRD (uint8_t *const)&DDRD
37 #define _DDRE (uint8_t *const)&DDRE
38 #define _DDRF (uint8_t *const)&DDRF
40 #define _PINA (uint8_t *const)&PINA
41 #define _PINB (uint8_t *const)&PINB
42 #define _PINC (uint8_t *const)&PINC
43 #define _PIND (uint8_t *const)&PIND
44 #define _PINE (uint8_t *const)&PINE
45 #define _PINF (uint8_t *const)&PINF
47 #define _PORTA (uint8_t *const)&PORTA
48 #define _PORTB (uint8_t *const)&PORTB
49 #define _PORTC (uint8_t *const)&PORTC
50 #define _PORTD (uint8_t *const)&PORTD
51 #define _PORTE (uint8_t *const)&PORTE
52 #define _PORTF (uint8_t *const)&PORTF
63 /* Specifies the ports and pin numbers for the rows */
65 uint8_t *const row_ddr[MATRIX_ROWS] = {
68 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD,
69 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF};
72 uint8_t *const row_port[MATRIX_ROWS] = {
75 _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
76 _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF};
79 uint8_t *const row_pin[MATRIX_ROWS] = {
82 _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND,
83 _PINF, _PINF, _PINF, _PINF, _PINF, _PINF};
86 const uint8_t row_bit[MATRIX_ROWS] = {
89 _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5, _BIT6, _BIT7,
90 _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7};
93 const uint8_t mask = 0x0E;
95 /* Specifies the ports and pin numbers for the columns */
97 const uint8_t col_bit[MATRIX_COLS] = { 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E};
100 inline void pull_column(int col) {
101 PORTB = col_bit[col] | (PORTB & ~mask);
105 inline void release_column(int col) {
108 /* PORTB is set as input with pull-up resistors
109 PORTC,D,E,F are set to high output */
111 void setup_io_pins(void) {
115 for(row = 0; row < MATRIX_ROWS; row++) {
116 *row_ddr[row] &= ~row_bit[row];
117 *row_port[row] &= ~row_bit[row];
122 void setup_leds(void) {
129 uint8_t matrix_rows(void)
135 uint8_t matrix_cols(void)
140 void matrix_init(void)
142 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
146 // initialize row and col
150 // initialize matrix state: all keys off
151 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
152 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
154 matrix_debounced = _matrix1;
157 uint8_t matrix_scan(void)
160 uint8_t *tmp = matrix_debounced;
161 matrix_debounced = matrix;
165 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-7
166 pull_column(col); // output hi on theline
167 _delay_us(5); // without this wait it won't read stable value.
168 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-17
169 bool prev_bit = matrix[row] & (1<<col);
170 bool curr_bit = *row_pin[row] & row_bit[row];
171 if (prev_bit != curr_bit) {
172 matrix[row] ^= (1<<col);
174 debug("bounce!: "); debug_hex(debouncing); print("\n");
176 _delay_ms(1); // improved affect on bouncing
177 debouncing = DEBOUNCE;
190 bool matrix_is_modified(void)
192 // NOTE: no longer used
197 bool matrix_has_ghost(void)
203 bool matrix_is_on(uint8_t row, uint8_t col)
205 return (matrix_debounced[row] & (1<<col));
209 matrix_row_t matrix_get_row(uint8_t row)
211 return matrix_debounced[row];
214 void matrix_print(void)
216 print("\nr/c 01234567\n");
217 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
218 phex(row); print(": ");
219 pbin_reverse(matrix_get_row(row));
224 uint8_t matrix_key_count(void)
227 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
228 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
229 if (matrix_is_on(i, j))