]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/sol/rev1/matrix.c
cleanup
[qmk_firmware.git] / keyboards / sol / rev1 / matrix.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 <avr/wdt.h>
25 #include <avr/interrupt.h>
26 #include <util/delay.h>
27 #include "print.h"
28 #include "debug.h"
29 #include "util.h"
30 #include "matrix.h"
31 #include "split_util.h"
32 #include <drivers/avr/pro_micro.h>
33
34 #include "serial.h"
35
36 #ifndef DEBOUNCE
37 #  define DEBOUNCE      5
38 #endif
39
40 #ifdef ENCODER_ENABLE_CUSTOM
41   #include "common/knob_v2.h"
42 #endif
43
44 #define ERROR_DISCONNECT_COUNT 5
45
46 static uint8_t debouncing = DEBOUNCE;
47 static const int ROWS_PER_HAND = MATRIX_ROWS/2;
48 static uint8_t error_count = 0;
49 uint8_t is_master = 0 ;
50
51 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
52 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
53
54 /* matrix state(1:on, 0:off) */
55 static matrix_row_t matrix[MATRIX_ROWS];
56 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
57
58 static matrix_row_t read_cols(void);
59 static void init_cols(void);
60 static void unselect_rows(void);
61 static void select_row(uint8_t row);
62 static uint8_t matrix_master_scan(void);
63
64
65 __attribute__ ((weak))
66 void matrix_init_kb(void) {
67     matrix_init_user();
68 }
69
70 __attribute__ ((weak))
71 void matrix_scan_kb(void) {
72     matrix_scan_user();
73 }
74
75 __attribute__ ((weak))
76 void matrix_init_user(void) {
77 }
78
79 __attribute__ ((weak))
80 void matrix_scan_user(void) {
81 }
82
83 inline
84 uint8_t matrix_rows(void)
85 {
86     return MATRIX_ROWS;
87 }
88
89 inline
90 uint8_t matrix_cols(void)
91 {
92     return MATRIX_COLS;
93 }
94
95 void matrix_init(void)
96 {
97     debug_enable = true;
98     debug_matrix = true;
99     debug_mouse = true;
100     // initialize row and col
101     unselect_rows();
102     init_cols();
103
104     TX_RX_LED_INIT;
105
106     // initialize matrix state: all keys off
107     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
108         matrix[i] = 0;
109         matrix_debouncing[i] = 0;
110     }
111
112     is_master = has_usb();
113
114     #ifdef ENCODER_ENABLE_CUSTOM
115         knob_init();  //FOR ENCODER
116     #endif
117     matrix_init_quantum();
118 }
119
120 uint8_t _matrix_scan(void)
121 {
122     // Right hand is stored after the left in the matirx so, we need to offset it
123     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
124
125     for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
126         select_row(i);
127         _delay_us(30);  // without this wait read unstable value.
128         matrix_row_t cols = read_cols();
129         if (matrix_debouncing[i+offset] != cols) {
130             matrix_debouncing[i+offset] = cols;
131             debouncing = DEBOUNCE;
132         }
133         unselect_rows();
134     }
135
136     if (debouncing) {
137         if (--debouncing) {
138             _delay_ms(1);
139         } else {
140             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
141                 matrix[i+offset] = matrix_debouncing[i+offset];
142             }
143         }
144     }
145
146     #ifdef ENCODER_ENABLE_CUSTOM
147       knob_report_t knob_report = knob_report_read();
148
149       knob_report_reset();
150
151       matrix[5 + offset] &= 0b11111100;
152       if (knob_report.phase) { // I check for phase to avoid handling the rotation twice (on 90 and 270 degrees).
153         if (knob_report.dir > 0) {
154           matrix[5 + offset] |= 0b00000001;
155         } else if (knob_report.dir < 0) {
156           matrix[5 + offset] |= 0b00000010;
157         }
158       }
159     #endif
160
161     return 1;
162 }
163
164 int serial_transaction(void) {
165     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
166     int ret=serial_update_buffers();
167     if (ret ) {
168         return 1;
169     }
170
171     for (int i = 0; i < ROWS_PER_HAND; ++i) {
172         matrix[slaveOffset+i] = serial_slave_buffer[i];
173     }
174     return 0;
175 }
176
177 uint8_t matrix_scan(void)
178 {
179     if (is_master) {
180         matrix_master_scan();
181     }else{
182         matrix_slave_scan();
183
184         int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
185
186         for (int i = 0; i < ROWS_PER_HAND; ++i) {
187             matrix[offset+i] = serial_master_buffer[i];
188         }
189
190         matrix_scan_quantum();
191     }
192     return 1;
193 }
194
195
196 uint8_t matrix_master_scan(void) {
197
198     int ret = _matrix_scan();
199
200     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
201
202     for (int i = 0; i < ROWS_PER_HAND; ++i) {
203         serial_master_buffer[i] = matrix[offset+i];
204     }
205
206     if( serial_transaction() ) {
207         // turn on the indicator led when halves are disconnected
208         TXLED1;
209
210         error_count++;
211
212         if (error_count > ERROR_DISCONNECT_COUNT) {
213             // reset other half if disconnected
214             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
215             for (int i = 0; i < ROWS_PER_HAND; ++i) {
216                 matrix[slaveOffset+i] = 0;
217             }
218         }
219     } else {
220         // turn off the indicator led on no error
221         TXLED0;
222         error_count = 0;
223     }
224     matrix_scan_quantum();
225     return ret;
226 }
227
228 void matrix_slave_scan(void) {
229     _matrix_scan();
230
231     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
232
233     for (int i = 0; i < ROWS_PER_HAND; ++i) {
234         serial_slave_buffer[i] = matrix[offset+i];
235     }
236 }
237
238 bool matrix_is_modified(void)
239 {
240     if (debouncing) return false;
241     return true;
242 }
243
244 inline
245 bool matrix_is_on(uint8_t row, uint8_t col)
246 {
247     return (matrix[row] & ((matrix_row_t)1<<col));
248 }
249
250 inline
251 matrix_row_t matrix_get_row(uint8_t row)
252 {
253     return matrix[row];
254 }
255
256 void matrix_print(void)
257 {
258     print("\nr/c 0123456789ABCDEF\n");
259     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
260         phex(row); print(": ");
261         pbin_reverse16(matrix_get_row(row));
262         print("\n");
263     }
264 }
265
266 uint8_t matrix_key_count(void)
267 {
268     uint8_t count = 0;
269     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
270         count += bitpop16(matrix[i]);
271     }
272     return count;
273 }
274
275 static void  init_cols(void)
276 {
277     for(int x = 0; x < MATRIX_COLS; x++) {
278         _SFR_IO8((col_pins[x] >> 4) + 1) &=  ~_BV(col_pins[x] & 0xF);
279         _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
280     }
281 }
282
283 static matrix_row_t read_cols(void)
284 {
285     matrix_row_t result = 0;
286     for(int x = 0; x < MATRIX_COLS; x++) {
287         result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
288     }
289     return result;
290 }
291
292 static void unselect_rows(void)
293 {
294     for(int x = 0; x < ROWS_PER_HAND; x++) {
295         _SFR_IO8((row_pins[x] >> 4) + 1) &=  ~_BV(row_pins[x] & 0xF);
296         _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
297     }
298 }
299
300 static void select_row(uint8_t row)
301 {
302     _SFR_IO8((row_pins[row] >> 4) + 1) |=  _BV(row_pins[row] & 0xF);
303     _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
304 }