]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/sx60/matrix.c
[Keymap] Tominabox1 userspace creation (#7014)
[qmk_firmware.git] / keyboards / sx60 / matrix.c
1 /*
2 Copyright 2012-2017 Jun Wako, Jack Humbert
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 #include <stdint.h>
18 #include <stdbool.h>
19 #if defined(__AVR__)
20 #include <avr/io.h>
21 #endif
22 #include "wait.h"
23 #include "print.h"
24 #include "debug.h"
25 #include "util.h"
26 #include "matrix.h"
27 #include "timer.h"
28 #include "sx60.h"
29
30
31 /* Set 0 if debouncing isn't needed */
32
33 #ifndef DEBOUNCE
34 #   define DEBOUNCE 5
35 #endif
36
37 #if (DEBOUNCE > 0)
38     static uint16_t debouncing_time;
39     static bool debouncing = false;
40 #endif
41
42 #if (MATRIX_COLS <= 8)
43 #    define print_matrix_header()  print("\nr/c 01234567\n")
44 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
45 #    define matrix_bitpop(i)       bitpop(matrix[i])
46 #    define ROW_SHIFTER ((uint8_t)1)
47 #elif (MATRIX_COLS <= 16)
48 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
49 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
50 #    define matrix_bitpop(i)       bitpop16(matrix[i])
51 #    define ROW_SHIFTER ((uint16_t)1)
52 #elif (MATRIX_COLS <= 32)
53 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
54 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
55 #    define matrix_bitpop(i)       bitpop32(matrix[i])
56 #    define ROW_SHIFTER  ((uint32_t)1)
57 #endif
58
59 #ifdef MATRIX_MASKED
60     extern const matrix_row_t matrix_mask[];
61 #endif
62
63 static const uint8_t col_pins[ATMEGA_COLS] = MATRIX_COL_PINS;
64 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
65
66 /* matrix state(1:on, 0:off) */
67 static matrix_row_t matrix[MATRIX_ROWS];
68 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
69 static uint8_t mcp23018_reset_loop;
70
71
72 static void init_cols(void);
73 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
74 static void unselect_rows(void);
75 static void select_row(uint8_t row);
76
77 __attribute__ ((weak))
78 void matrix_init_quantum(void) {
79     matrix_init_kb();
80 }
81
82 __attribute__ ((weak))
83 void matrix_scan_quantum(void) {
84     matrix_scan_kb();
85 }
86
87 __attribute__ ((weak))
88 void matrix_init_kb(void) {
89     matrix_init_user();
90 }
91
92 __attribute__ ((weak))
93 void matrix_scan_kb(void) {
94     matrix_scan_user();
95 }
96
97 __attribute__ ((weak))
98 void matrix_init_user(void) {
99 }
100
101 __attribute__ ((weak))
102 void matrix_scan_user(void) {
103 }
104
105 inline
106 uint8_t matrix_rows(void) {
107     return MATRIX_ROWS;
108 }
109
110 inline
111 uint8_t matrix_cols(void) {
112     return MATRIX_COLS;
113 }
114
115 void matrix_init(void) {
116     mcp23018_status = true;
117
118     /* initialize row and col */
119     unselect_rows();
120     init_cols();
121
122     /* initialize matrix state: all keys off */
123     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
124         matrix[i] = 0;
125         matrix_debouncing[i] = 0;
126     }
127
128     matrix_init_quantum();
129 }
130
131 uint8_t matrix_scan(void)
132 {
133     if (mcp23018_status) {
134         /* if there was an error */
135         if (++mcp23018_reset_loop == 0) {
136             /* since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
137                this will be approx bit more frequent than once per second */
138             print("trying to reset mcp23018\n");
139             mcp23018_status = init_mcp23018();
140             if (mcp23018_status) {
141                 print("left side not responding\n");
142             } else {
143                 print("left side attached\n");
144             }
145         }
146     }
147
148     /* Set row, read cols */
149     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
150 #       if (DEBOUNCE > 0)
151             bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
152
153             if (matrix_changed) {
154                 debouncing = true;
155                 debouncing_time = timer_read();
156             }
157 #       else
158             read_cols_on_row(matrix, current_row);
159 #       endif
160     }
161
162 #   if (DEBOUNCE > 0)
163         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
164             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
165                 matrix[i] = matrix_debouncing[i];
166             }
167             debouncing = false;
168         }
169 #   endif
170
171     matrix_scan_quantum();
172     return 1;
173 }
174
175 bool matrix_is_modified(void)
176 {
177 #if (DEBOUNCE > 0)
178     if (debouncing) return false;
179 #endif
180     return true;
181 }
182
183 inline
184 bool matrix_is_on(uint8_t row, uint8_t col)
185 {
186     return (matrix[row] & ((matrix_row_t)1<<col));
187 }
188
189 inline
190 matrix_row_t matrix_get_row(uint8_t row)
191 {
192     /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
193        switch blocker installed and the switch is always pressed. */
194 #ifdef MATRIX_MASKED
195     return matrix[row] & matrix_mask[row];
196 #else
197     return matrix[row];
198 #endif
199 }
200
201 void matrix_print(void)
202 {
203     print_matrix_header();
204
205     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
206         phex(row); print(": ");
207         print_matrix_row(row);
208         print("\n");
209     }
210 }
211
212 uint8_t matrix_key_count(void)
213 {
214     uint8_t count = 0;
215     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
216         count += matrix_bitpop(i);
217     }
218     return count;
219 }
220
221 static void init_cols(void)
222 {
223     for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
224         uint8_t pin = col_pins[x];
225         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
226         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); /* HI */
227     }
228 }
229
230 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
231 {
232     /* Store last value of row prior to reading */
233     matrix_row_t last_row_value = current_matrix[current_row];
234
235     /* Clear data in matrix row */
236     current_matrix[current_row] = 0;
237
238     /* Select row and wait for row selecton to stabilize */
239     select_row(current_row);
240     wait_us(30);
241
242     if (mcp23018_status) {
243         /* if there was an error */
244         return 0;
245     } else {
246         uint16_t data = 0;
247         mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
248         mcp23018_status = i2c_write(GPIOA);             if (mcp23018_status) goto out;
249         mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
250         data = i2c_readNak();
251         data = ~data;
252     out:
253         i2c_stop();
254         current_matrix[current_row] |= (data << 8);
255     }
256
257     /* For each col... */
258     for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
259         /* Select the col pin to read (active low) */
260         uint8_t pin = col_pins[col_index];
261         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
262
263         /* Populate the matrix row with the state of the col pin */
264         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
265     }
266
267     /* Unselect row */
268     unselect_rows();
269
270     return (last_row_value != current_matrix[current_row]);
271 }
272
273 static void select_row(uint8_t row)
274 {
275     if (mcp23018_status) {
276         /* if there was an error do nothing */
277     } else {
278         /* set active row low  : 0
279            set active row output : 1
280            set other rows hi-Z : 1 */
281         mcp23018_status = i2c_start(I2C_ADDR_WRITE);   if (mcp23018_status) goto out;
282         mcp23018_status = i2c_write(GPIOB);            if (mcp23018_status) goto out;
283         mcp23018_status = i2c_write(0xFF & ~(1<<abs(row-4))); if (mcp23018_status) goto out;
284     out:
285         i2c_stop();
286     }
287
288     uint8_t pin = row_pins[row];
289     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); /*  OUT  */
290     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW  */
291 }
292
293 static void unselect_rows(void)
294 {
295     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
296         uint8_t pin = row_pins[x];
297         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
298         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); /* HI */
299     }
300 }