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[MATRIX_ROWS];
28 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
31 #define _DDRA (uint8_t *const)&DDRA
32 #define _DDRB (uint8_t *const)&DDRB
33 #define _DDRC (uint8_t *const)&DDRC
34 #define _DDRD (uint8_t *const)&DDRD
35 #define _DDRE (uint8_t *const)&DDRE
36 #define _DDRF (uint8_t *const)&DDRF
38 #define _PINA (uint8_t *const)&PINA
39 #define _PINB (uint8_t *const)&PINB
40 #define _PINC (uint8_t *const)&PINC
41 #define _PIND (uint8_t *const)&PIND
42 #define _PINE (uint8_t *const)&PINE
43 #define _PINF (uint8_t *const)&PINF
45 #define _PORTA (uint8_t *const)&PORTA
46 #define _PORTB (uint8_t *const)&PORTB
47 #define _PORTC (uint8_t *const)&PORTC
48 #define _PORTD (uint8_t *const)&PORTD
49 #define _PORTE (uint8_t *const)&PORTE
50 #define _PORTF (uint8_t *const)&PORTF
61 /* Specifies the ports and pin numbers for the rows */
63 uint8_t *const row_ddr[MATRIX_ROWS] = {
66 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD,
67 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF};
70 uint8_t *const row_port[MATRIX_ROWS] = {
73 _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
74 _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF};
77 uint8_t *const row_pin[MATRIX_ROWS] = {
80 _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND,
81 _PINF, _PINF, _PINF, _PINF, _PINF, _PINF};
84 const uint8_t row_bit[MATRIX_ROWS] = {
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[MATRIX_COLS] = { 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 < MATRIX_ROWS; 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 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
144 // initialize row and col
148 // initialize matrix state: all keys off
149 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
151 matrix_debouncing[i] = 0;
155 uint8_t matrix_scan(void)
157 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-7
158 pull_column(col); // output hi on theline
159 _delay_us(5); // without this wait it won't read stable value.
160 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-17
161 bool prev_bit = matrix_debouncing[row] & (1<<col);
162 bool curr_bit = *row_pin[row] & row_bit[row];
163 if (prev_bit != curr_bit) {
164 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
166 dprintf("bounce!: %02X\n", debouncing);
168 debouncing = DEBOUNCE;
178 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
179 matrix[i] = matrix_debouncing[i];
187 bool matrix_is_modified(void)
189 // NOTE: no longer used
194 bool matrix_has_ghost(void)
200 bool matrix_is_on(uint8_t row, uint8_t col)
202 return (matrix[row] & ((matrix_row_t)1<<col));
206 matrix_row_t matrix_get_row(uint8_t row)
211 void matrix_print(void)
213 print("\nr/c 01234567\n");
214 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
215 phex(row); print(": ");
216 pbin_reverse(matrix_get_row(row));
221 uint8_t matrix_key_count(void)
224 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
225 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
226 if (matrix_is_on(i, j))