]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hid_liber/matrix.c
Fix debouncing and add legacy keymap support
[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;
28 static matrix_row_t *matrix_debounced;
29 static matrix_row_t _matrix0[MATRIX_ROWS];
30 static matrix_row_t _matrix1[MATRIX_ROWS];
31
32
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
39
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
46
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
53
54 #define _BIT0 0x01
55 #define _BIT1 0x02
56 #define _BIT2 0x04
57 #define _BIT3 0x08
58 #define _BIT4 0x10
59 #define _BIT5 0x20
60 #define _BIT6 0x40
61 #define _BIT7 0x80
62
63 /* Specifies the ports and pin numbers for the rows */
64 static
65 uint8_t *const row_ddr[MATRIX_ROWS] = {
66                                            _DDRB,                  _DDRB,
67                                                            _DDRC,  _DDRC,
68            _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,  _DDRD,
69            _DDRF,  _DDRF,                  _DDRF,  _DDRF,  _DDRF,  _DDRF};
70
71 static
72 uint8_t *const row_port[MATRIX_ROWS] = {
73                                           _PORTB,                 _PORTB,
74                                                           _PORTC, _PORTC,
75           _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD, _PORTD,
76           _PORTF, _PORTF,                 _PORTF, _PORTF, _PORTF, _PORTF};
77
78 static
79 uint8_t *const row_pin[MATRIX_ROWS] = {
80                                            _PINB,                  _PINB,
81                                                            _PINC,  _PINC,
82            _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,  _PIND,
83            _PINF,  _PINF,                  _PINF,  _PINF,  _PINF,  _PINF};
84
85 static
86 const uint8_t row_bit[MATRIX_ROWS] = {
87                                            _BIT4,                  _BIT7,
88                                                            _BIT6,  _BIT7,
89            _BIT0,  _BIT1,  _BIT2,  _BIT3,  _BIT4,  _BIT5,  _BIT6,  _BIT7,
90            _BIT0,  _BIT1,                  _BIT4,  _BIT5,  _BIT6,  _BIT7};
91
92 static
93 const uint8_t mask = 0x0E;
94
95 /* Specifies the ports and pin numbers for the columns */
96 static
97 const uint8_t   col_bit[MATRIX_COLS] = {  0x00,   0x02,   0x04,   0x06,   0x08,   0x0A,   0x0C,   0x0E};
98
99 static
100 inline void pull_column(int col) {
101   PORTB = col_bit[col] | (PORTB & ~mask);
102 }
103
104 static
105 inline void release_column(int col) {
106 }
107
108 /* PORTB is set as input with pull-up resistors
109    PORTC,D,E,F are set to high output */
110 static
111 void setup_io_pins(void) {
112   uint8_t row;
113   DDRB  |=  0x0E;
114   PORTB &= ~0x0E;
115   for(row = 0; row < MATRIX_ROWS; row++) {
116     *row_ddr[row]  &= ~row_bit[row];
117     *row_port[row] &= ~row_bit[row];
118   }
119 }
120
121 static
122 void setup_leds(void) {
123   DDRB  |=  0x60;
124   PORTB |=  0x60;
125 }
126
127
128 inline
129 uint8_t matrix_rows(void)
130 {
131     return MATRIX_ROWS;
132 }
133
134 inline
135 uint8_t matrix_cols(void)
136 {
137     return MATRIX_COLS;
138 }
139
140 void matrix_init(void)
141 {
142     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
143     MCUCR |= (1<<JTD);
144     MCUCR |= (1<<JTD);
145
146     // initialize row and col
147     setup_io_pins();
148     setup_leds();
149
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;
153     matrix = _matrix0;
154     matrix_debounced = _matrix1;
155 }
156
157 uint8_t matrix_scan(void)
158 {
159     if (!debouncing) {
160         uint8_t *tmp = matrix_debounced;
161         matrix_debounced = matrix;
162         matrix = tmp;
163     }
164
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);
173                 if (debouncing) {
174                     debug("bounce!: "); debug_hex(debouncing); print("\n");
175                 }
176                 debouncing = DEBOUNCE;
177             }
178         }
179         release_column(col);
180     }
181
182     if (debouncing) {
183         _delay_ms(1);
184         debouncing--;
185     }
186
187     return 1;
188 }
189
190 bool matrix_is_modified(void)
191 {
192     // NOTE: no longer used
193     return true;
194 }
195
196 inline
197 bool matrix_has_ghost(void)
198 {
199     return false;
200 }
201
202 inline
203 bool matrix_is_on(uint8_t row, uint8_t col)
204 {
205     return (matrix_debounced[row] & (1<<col));
206 }
207
208 inline
209 matrix_row_t matrix_get_row(uint8_t row)
210 {
211     return matrix_debounced[row];
212 }
213
214 void matrix_print(void)
215 {
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));
220         print("\n");
221     }
222 }
223
224 uint8_t matrix_key_count(void)
225 {
226     uint8_t count = 0;
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))
230                 count++;
231         }
232     }
233     return count;
234 }