]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboard/planck/matrix_center.c
midi
[qmk_firmware.git] / keyboard / planck / matrix_center.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "action_layer.h"
26 // #include "print.h"
27 #include "debug.h"
28 #include "util.h"
29 #include "matrix.h"
30 #include "analog.h"
31
32
33 #ifndef DEBOUNCE
34 #   define DEBOUNCE     10
35 #endif
36 static uint8_t debouncing = DEBOUNCE;
37
38 /* matrix state(1:on, 0:off) */
39 static matrix_row_t matrix[MATRIX_ROWS];
40 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
41
42 static matrix_row_t read_cols(void);
43 static void init_cols(void);
44 static void init_encoder(void);
45 static void init_pot(void);
46 static void unselect_rows(void);
47 static void select_row(uint8_t row);
48 int16_t analogRead(uint8_t pin);
49 uint8_t state;
50 int32_t position;
51 int16_t value;
52
53 inline
54 uint8_t matrix_rows(void)
55 {
56     return MATRIX_ROWS;
57 }
58
59 inline
60 uint8_t matrix_cols(void)
61 {
62     return MATRIX_COLS;
63 }
64
65 void matrix_init(void)
66 {
67     // initialize row and col
68     unselect_rows();
69     init_cols();
70     init_encoder();
71     init_pot();
72
73     // initialize matrix state: all keys off
74     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
75         matrix[i] = 0;
76         matrix_debouncing[i] = 0;
77     }
78 }
79
80 static void init_encoder(void)
81 {
82     DDRC &= ~(1<<6 | 1<<7);
83     PORTC |=  (1<<6 | 1<<7);
84
85     uint8_t s = 0;
86     _delay_ms(1);
87     if (PINC&(1<<6)) s |= 1;
88     if (PINC&(1<<7)) s |= 2;
89     state = s;
90     position = 0;
91 }
92
93 void read_encoder(void)
94 {
95     uint8_t s = state & 3;
96     if (PINC&(1<<6)) s |= 4;
97     if (PINC&(1<<7)) s |= 8;
98     state = (s >> 2);
99     switch (s) {
100         case 1: case 7: case 8: case 14:
101             position++; 
102             break;
103         case 2: case 4: case 11: case 13:
104             position--; 
105             break;
106         case 3: case 12:
107             position += 2; 
108             break;
109         case 6: case 9:
110             position -= 2; 
111             break;
112     }
113 }
114
115 #define HEX(n) (((n) < 10) ? ((n) + '0') : ((n) + 'A' - 10))
116
117 static void init_pot(void)
118 {
119     // DDRD &= ~(1<<4);
120     // PORTD |=  (1<<4);
121     // DIDR2 = (1<<0);
122 }
123
124 uint8_t matrix_scan(void)
125 {
126     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
127         select_row(i);
128         _delay_us(30);  // without this wait read unstable value.
129         matrix_row_t cols = read_cols();
130         if (matrix_debouncing[i] != cols) {
131             matrix_debouncing[i] = cols;
132             if (debouncing) {
133                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
134             }
135             debouncing = DEBOUNCE;
136         }
137         unselect_rows();
138     }
139
140     if (debouncing) {
141         if (--debouncing) {
142             _delay_ms(1);
143         } else {
144             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
145                 matrix[i] = matrix_debouncing[i];
146             }
147         }
148     }
149
150     read_encoder();
151     if (position >= 2) {
152         register_code(KC_AUDIO_VOL_UP);
153         unregister_code(KC_AUDIO_VOL_UP);
154         position = 0;
155     } else if (position <= -2) {
156         register_code(KC_AUDIO_VOL_DOWN);
157         unregister_code(KC_AUDIO_VOL_DOWN);
158         position = 0;
159     }
160
161     // uint16_t val = analogRead(11);
162
163     // debug("analogRead: "); debug_hex(val); debug("\n");
164
165     return 1;
166 }
167
168 bool matrix_is_modified(void)
169 {
170     if (debouncing) return false;
171     return true;
172 }
173
174 inline
175 bool matrix_is_on(uint8_t row, uint8_t col)
176 {
177     return (matrix[row] & ((matrix_row_t)1<<col));
178 }
179
180 inline
181 matrix_row_t matrix_get_row(uint8_t row)
182 {
183     return matrix[row];
184 }
185
186 void matrix_print(void)
187 {
188     print("\nr/c 0123456789ABCDEF\n");
189     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
190         phex(row); print(": ");
191         pbin_reverse16(matrix_get_row(row));
192         print("\n");
193     }
194 }
195
196 uint8_t matrix_key_count(void)
197 {
198     uint8_t count = 0;
199     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
200         count += bitpop16(matrix[i]);
201     }
202     return count;
203 }
204
205 /* Column pin configuration
206  * col: 0  1  2  3  4  5  6  7  8  9  10 11
207  * pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
208  */
209
210 static void init_cols(void)
211 {
212     DDRF  &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
213     PORTF |=  (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
214     DDRD  &= ~(1<<0);
215     PORTD |=  (1<<0);
216     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
217     PORTB |=  (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
218 }
219
220 static matrix_row_t read_cols(void)
221 {
222     return (PINB&(1<<0) ? 0 : (1<< 0)) |
223            (PINB&(1<<1) ? 0 : (1<< 1)) |
224            (PINB&(1<<2) ? 0 : (1<< 2)) |
225            (PINB&(1<<3) ? 0 : (1<< 3)) |
226            (PINB&(1<<7) ? 0 : (1<< 4)) |
227            (PIND&(1<<0) ? 0 : (1<< 5)) |
228            (PINF&(1<<7) ? 0 : (1<< 6)) |
229            (PINF&(1<<6) ? 0 : (1<< 7)) |
230            (PINF&(1<<5) ? 0 : (1<< 8)) |
231            (PINF&(1<<4) ? 0 : (1<< 9)) |
232            (PINF&(1<<1) ? 0 : (1<<10)) |
233            (PINF&(1<<0) ? 0 : (1<<11));
234 }
235
236 /* Row pin configuration
237  * row: 0  1  2  3
238  * pin: B0 B1 B2 B3
239  */
240 static void unselect_rows(void)
241 {
242     // Hi-Z(DDR:0, PORT:0) to unselect
243     DDRB  &= ~0b01110000;
244     PORTB &= ~0b01110000;
245     DDRD  &= ~0b10000000;
246     PORTD &= ~0b10000000;
247 }
248
249 static void select_row(uint8_t row)
250 {
251     switch (row) {
252         case 0:
253             DDRB  |= (1<<6);
254             PORTB &= ~(1<<6);
255             break;
256         case 1:
257             DDRB  |= (1<<5);
258             PORTB &= ~(1<<5);
259             break;
260         case 2:
261             DDRB  |= (1<<4);
262             PORTB &= ~(1<<4);
263             break;
264         case 3:
265             DDRD  |= (1<<7);
266             PORTD &= ~(1<<7);
267             break;
268     }
269 }