]> git.gir.st - tmk_keyboard.git/blob - keyboard/hid_liber/matrix.c
Fix copyright and license notices.
[tmk_keyboard.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 5
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 NROW 18
34 #define NCOL 8
35 #define _DDRA (uint8_t *const)&DDRA
36 #define _DDRB (uint8_t *const)&DDRB
37 #define _DDRC (uint8_t *const)&DDRC
38 #define _DDRD (uint8_t *const)&DDRD
39 #define _DDRE (uint8_t *const)&DDRE
40 #define _DDRF (uint8_t *const)&DDRF
41
42 #define _PINA (uint8_t *const)&PINA
43 #define _PINB (uint8_t *const)&PINB
44 #define _PINC (uint8_t *const)&PINC
45 #define _PIND (uint8_t *const)&PIND
46 #define _PINE (uint8_t *const)&PINE
47 #define _PINF (uint8_t *const)&PINF
48
49 #define _PORTA (uint8_t *const)&PORTA
50 #define _PORTB (uint8_t *const)&PORTB
51 #define _PORTC (uint8_t *const)&PORTC
52 #define _PORTD (uint8_t *const)&PORTD
53 #define _PORTE (uint8_t *const)&PORTE
54 #define _PORTF (uint8_t *const)&PORTF
55
56 #define _BIT0 0x01
57 #define _BIT1 0x02
58 #define _BIT2 0x04
59 #define _BIT3 0x08
60 #define _BIT4 0x10
61 #define _BIT5 0x20
62 #define _BIT6 0x40
63 #define _BIT7 0x80
64
65 /* Specifies the ports and pin numbers for the rows */
66 static
67 uint8_t *const row_ddr[NROW] = { _DDRB, _DDRB,
68 _DDRC, _DDRC,
69 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD, _DDRD,
70 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF};
71
72 static
73 uint8_t *const row_port[NROW] = { _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[NROW] = { _PINB, _PINB,
80 _PINC, _PINC,
81 _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND, _PIND,
82 _PINF, _PINF, _PINF, _PINF, _PINF, _PINF};
83
84 static
85 const uint8_t row_bit[NROW] = { _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[NCOL] = { 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 < NROW; 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 // initialize row and col
141 setup_io_pins();
142 setup_leds();
143
144 // initialize matrix state: all keys off
145 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
146 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
147 matrix = _matrix0;
148 matrix_debounced = _matrix1;
149 }
150
151 uint8_t matrix_scan(void)
152 {
153 if (!debouncing) {
154 uint8_t *tmp = matrix_debounced;
155 matrix_debounced = matrix;
156 matrix = tmp;
157 }
158
159 for (uint8_t col = 0; col < NCOL; col++) { // 0-7
160 pull_column(col); // output hi on theline
161 _delay_us(1); // without this wait it won't read stable value.
162 for (uint8_t row = 0; row < NROW; row++) { // 0-17
163 bool prev_bit = matrix[row] & (1<<col);
164 bool curr_bit = *row_pin[row] & row_bit[row];
165 if (prev_bit != curr_bit) {
166 matrix[row] ^= (1<<col);
167 if (debouncing) {
168 debug("bounce!: "); debug_hex(debouncing); print("\n");
169 }
170 debouncing = DEBOUNCE;
171 }
172 }
173 release_column(col);
174 }
175
176 if (debouncing) {
177 debouncing--;
178 }
179
180 return 1;
181 }
182
183 bool matrix_is_modified(void)
184 {
185 // NOTE: no longer used
186 return true;
187 }
188
189 inline
190 bool matrix_has_ghost(void)
191 {
192 return false;
193 }
194
195 inline
196 bool matrix_is_on(uint8_t row, uint8_t col)
197 {
198 return (matrix_debounced[row] & (1<<col));
199 }
200
201 inline
202 matrix_row_t matrix_get_row(uint8_t row)
203 {
204 return matrix_debounced[row];
205 }
206
207 void matrix_print(void)
208 {
209 print("\nr/c 01234567\n");
210 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
211 phex(row); print(": ");
212 pbin_reverse(matrix_get_row(row));
213 print("\n");
214 }
215 }
216
217 uint8_t matrix_key_count(void)
218 {
219 uint8_t count = 0;
220 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
221 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
222 if (matrix_is_on(i, j))
223 count++;
224 }
225 }
226 return count;
227 }
Imprint / Impressum