]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/kinesis/alvicstep/matrix.c
revert accidental rgb_matrix.h changes
[qmk_firmware.git] / keyboards / kinesis / alvicstep / matrix.c
1 /*
2 Copyright 2014 Warren Janssens <warren.janssens@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 "led.h"
31 #include "config.h"
32
33 #ifndef DEBOUNCE
34 #   define DEBOUNCE     5
35 #endif
36 static uint8_t debouncing = DEBOUNCE;
37
38 /* matrix state(1:on, 0:off) */
39 static uint8_t matrix[MATRIX_ROWS];
40 static uint8_t matrix_debouncing[MATRIX_ROWS];
41
42 static matrix_row_t read_row(uint8_t row);
43 static void unselect_rows(void);
44 static void select_rows(uint8_t row);
45
46
47 __attribute__ ((weak))
48 void matrix_init_kb(void) {
49     matrix_init_user();
50 }
51
52 __attribute__ ((weak))
53 void matrix_scan_kb(void) {
54     matrix_scan_user();
55 }
56
57 __attribute__ ((weak))
58 void matrix_init_user(void) {
59 }
60
61 __attribute__ ((weak))
62 void matrix_scan_user(void) {
63 }
64
65 inline
66 uint8_t matrix_rows(void)
67 {
68     return MATRIX_ROWS;
69 }
70
71
72 inline
73 uint8_t matrix_cols(void)
74 {
75     return MATRIX_COLS;
76 }
77
78 void matrix_init(void)
79 {
80     //debug_enable = true;
81     
82     //dprint("matrix_init"); dprintln();
83     // output high (leds)
84     DDRD    = 0xFF;
85     PORTD   = 0xFF;
86     
87     // output low (multiplexers)
88     DDRF    = 0xFF;
89     PORTF   = 0x00;
90     
91     // input with pullup (matrix)
92     DDRB    = 0x00;
93     PORTB   = 0xFF;
94     
95     // input with pullup (program and keypad buttons)
96     DDRC    = 0x00;
97     PORTC   = 0xFF;
98     
99     // initialize row and col
100     unselect_rows();
101
102     // initialize matrix state: all keys off
103     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
104         matrix[i] = 0;
105         matrix_debouncing[i] = 0;
106     }
107         
108 }
109
110 uint8_t matrix_scan(void)
111 {
112
113     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
114         select_rows(i);
115         uint8_t row = read_row(i);
116         if (matrix_debouncing[i] != row) {
117             matrix_debouncing[i] = row;
118             if (debouncing) {
119                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
120             }
121             debouncing = DEBOUNCE;
122         }
123         unselect_rows();
124     }
125
126     if (debouncing) {
127         if (--debouncing) {
128             _delay_ms(1);
129         } else {
130             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
131                 matrix[i] = matrix_debouncing[i];
132             }
133         }
134     }
135     matrix_scan_quantum();
136     return 1;
137 }
138
139 bool matrix_is_modified(void)
140 {
141     if (debouncing) return false;
142     return true;
143 }
144
145 inline
146 bool matrix_is_on(uint8_t row, uint8_t col)
147 {
148     return (matrix[row] & ((matrix_row_t)1<<col));
149 }
150
151 inline
152 matrix_row_t matrix_get_row(uint8_t row)
153 {
154     return matrix[row];
155 }
156
157 void matrix_print(void)
158 {
159     print("\nr/c 01234567\n");
160     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
161         phex(row); print(": ");
162         pbin_reverse(matrix_get_row(row));
163         print("\n");
164     }
165 }
166
167 uint8_t matrix_key_count(void)
168 {
169     uint8_t count = 0;
170     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
171         count += bitpop16(matrix[i]);
172     }
173     return count;
174 }
175
176 static matrix_row_t read_row(uint8_t row)
177 {
178         _delay_us(30);  // without this wait read unstable value.
179
180         //keypad and program buttons
181         if (row == 12)
182         {
183                 return ~(PINC | 0b00111111);
184         }
185         return ~PINB;
186 }
187
188 static void unselect_rows(void)
189 {
190     // set A,B,C,G to 0 (F4 - F7)
191     PORTF &= 0x0F;
192 }
193
194 static void select_rows(uint8_t row)
195 {
196     // set A,B,C,G to row value
197     PORTF |= row << 4;
198 }
199
200
201 /* Row pin configuration
202 PF0             A
203 PF1             B
204 PF2             C
205 PF3             G       0 = U4, 1 = U5
206
207                                 4y0     4y1     4y2     4y3     4y4     4y5     4y6     4y7     5y0     5y1     5y2     5y3     5y4     5y5     5y6     5y7     
208                                 r1      r2       r3 r4  r5      r6      r7      r8      r9      r10     r11     r12     r13     r14     r15     r16     
209 PB0             21      c1      f6      f8      f7      5       4       3       2       1       =+                                                              
210 PB1             22      c2      f3      f5      f4      t       r       e       w       q       TAB                                                             
211 PB2             23      c3      ESC     f2      f1      g       f       d       s       a       CL                                                              
212 PB3             24      c4      f9      f11     f10     b       v       c       x       z       LS      UP              DN              [{      ]}              
213 PB4             25      c5  f12 SL      PS      RT              LT      §±    `~              6       7       8               9       0       -_      
214 PB5             26      c6      PB      PGM     KPD                                                     y       u       i               o       p       \       
215 PB6             27      c7                      LC      DL      BS      RC      EN      SP      h       j       k               l       ;:      '"      
216 PB7             28      c8                                      RA              PU              PD      n       m       ,<              .>      /?      RS      
217  */
218
219