]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hid_liber/matrix.c
Merge branch 'rhaberkorn-serial-mouse'
[tmk_firmware.git] / keyboard / hid_liber / matrix.c
1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
2  *
3  * This is heavily based on hid_liber/board.{c|h}.
4  * https://github.com/BathroomEpiphanies/AVR-Keyboard
5  *
6  * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7  * http://bathroomepiphanies.com
8  *
9  * As for liscensing consult with the original files or its author.
10  */
11 #include <stdint.h>
12 #include <stdbool.h>
13 #include <avr/io.h>
14 #include <util/delay.h>
15 #include "print.h"
16 #include "debug.h"
17 #include "util.h"
18 #include "matrix.h"
19
20
21 #ifndef DEBOUNCE
22 #   define DEBOUNCE 0
23 #endif
24 static uint8_t debouncing = DEBOUNCE;
25
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];
29
30
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
37
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
44
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
51
52 #define _BIT0 0x01
53 #define _BIT1 0x02
54 #define _BIT2 0x04
55 #define _BIT3 0x08
56 #define _BIT4 0x10
57 #define _BIT5 0x20
58 #define _BIT6 0x40
59 #define _BIT7 0x80
60
61 /* Specifies the ports and pin numbers for the rows */
62 static
63 uint8_t *const row_ddr[MATRIX_ROWS] = {
64                                            _DDRB,                  _DDRB,
65                                                            _DDRC,  _DDRC,
66            _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,
67            _DDRF,  _DDRF,                  _DDRF,  _DDRF,  _DDRF,  _DDRF};
68
69 static
70 uint8_t *const row_port[MATRIX_ROWS] = {
71                                           _PORTB,                 _PORTB,
72                                                           _PORTC, _PORTC,
73           _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
74           _PORTF, _PORTF,                 _PORTF, _PORTF, _PORTF, _PORTF};
75
76 static
77 uint8_t *const row_pin[MATRIX_ROWS] = {
78                                            _PINB,                  _PINB,
79                                                            _PINC,  _PINC,
80            _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,
81            _PINF,  _PINF,                  _PINF,  _PINF,  _PINF,  _PINF};
82
83 static
84 const uint8_t row_bit[MATRIX_ROWS] = {
85                                            _BIT4,                  _BIT7,
86                                                            _BIT6,  _BIT7,
87            _BIT0,  _BIT1,  _BIT2,  _BIT3,  _BIT4,  _BIT5,  _BIT6,  _BIT7,
88            _BIT0,  _BIT1,                  _BIT4,  _BIT5,  _BIT6,  _BIT7};
89
90 static
91 const uint8_t mask = 0x0E;
92
93 /* Specifies the ports and pin numbers for the columns */
94 static
95 const uint8_t   col_bit[MATRIX_COLS] = {  0x00,   0x02,   0x04,   0x06,   0x08,   0x0A,   0x0C,   0x0E};
96
97 static
98 inline void pull_column(int col) {
99   PORTB = col_bit[col] | (PORTB & ~mask);
100 }
101
102 static
103 inline void release_column(int col) {
104 }
105
106 /* PORTB is set as input with pull-up resistors
107    PORTC,D,E,F are set to high output */
108 static
109 void setup_io_pins(void) {
110   uint8_t row;
111   DDRB  |=  0x0E;
112   PORTB &= ~0x0E;
113   for(row = 0; row < MATRIX_ROWS; row++) {
114     *row_ddr[row]  &= ~row_bit[row];
115     *row_port[row] &= ~row_bit[row];
116   }
117 }
118
119 static
120 void setup_leds(void) {
121   DDRB  |=  0x60;
122   PORTB |=  0x60;
123 }
124
125
126 inline
127 uint8_t matrix_rows(void)
128 {
129     return MATRIX_ROWS;
130 }
131
132 inline
133 uint8_t matrix_cols(void)
134 {
135     return MATRIX_COLS;
136 }
137
138 void matrix_init(void)
139 {
140     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
141     MCUCR |= (1<<JTD);
142     MCUCR |= (1<<JTD);
143
144     // initialize row and col
145     setup_io_pins();
146     setup_leds();
147
148     // initialize matrix state: all keys off
149     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
150         matrix[i] = 0;
151         matrix_debouncing[i] = 0;
152     }
153 }
154
155 uint8_t matrix_scan(void)
156 {
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);
165                 if (debouncing) {
166                     dprintf("bounce!: %02X\n", debouncing);
167                 }
168                 debouncing = DEBOUNCE;
169             }
170         }
171         release_column(col);
172     }
173
174     if (debouncing) {
175         if (--debouncing) {
176             _delay_ms(1);
177         } else {
178             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
179                 matrix[i] = matrix_debouncing[i];
180             }
181         }
182     }
183
184     return 1;
185 }
186
187 bool matrix_is_modified(void)
188 {
189     // NOTE: no longer used
190     return true;
191 }
192
193 inline
194 bool matrix_has_ghost(void)
195 {
196     return false;
197 }
198
199 inline
200 bool matrix_is_on(uint8_t row, uint8_t col)
201 {
202     return (matrix[row] & ((matrix_row_t)1<<col));
203 }
204
205 inline
206 matrix_row_t matrix_get_row(uint8_t row)
207 {
208     return matrix[row];
209 }
210
211 void matrix_print(void)
212 {
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));
217         print("\n");
218     }
219 }
220
221 uint8_t matrix_key_count(void)
222 {
223     uint8_t count = 0;
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))
227                 count++;
228         }
229     }
230     return count;
231 }