]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/sx60/matrix.c
Remove more commented out MCUs
[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
117     /* To use PORTF disable JTAG with writing JTD bit twice within four cycles. */
118     #if  (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
119         MCUCR |= _BV(JTD);
120         MCUCR |= _BV(JTD);
121     #endif
122
123     mcp23018_status = true;
124
125     /* initialize row and col */
126     unselect_rows();
127     init_cols();
128
129     /* initialize matrix state: all keys off */
130     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
131         matrix[i] = 0;
132         matrix_debouncing[i] = 0;
133     }
134
135     matrix_init_quantum();
136 }
137
138 uint8_t matrix_scan(void)
139 {
140     if (mcp23018_status) {
141         /* if there was an error */
142         if (++mcp23018_reset_loop == 0) {
143             /* since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
144                this will be approx bit more frequent than once per second */
145             print("trying to reset mcp23018\n");
146             mcp23018_status = init_mcp23018();
147             if (mcp23018_status) {
148                 print("left side not responding\n");
149             } else {
150                 print("left side attached\n");
151             }
152         }
153     }
154
155     /* Set row, read cols */
156     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
157 #       if (DEBOUNCE > 0)
158             bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
159
160             if (matrix_changed) {
161                 debouncing = true;
162                 debouncing_time = timer_read();
163             }
164 #       else
165             read_cols_on_row(matrix, current_row);
166 #       endif
167     }
168
169 #   if (DEBOUNCE > 0)
170         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
171             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
172                 matrix[i] = matrix_debouncing[i];
173             }
174             debouncing = false;
175         }
176 #   endif
177
178     matrix_scan_quantum();
179     return 1;
180 }
181
182 bool matrix_is_modified(void)
183 {
184 #if (DEBOUNCE > 0)
185     if (debouncing) return false;
186 #endif
187     return true;
188 }
189
190 inline
191 bool matrix_is_on(uint8_t row, uint8_t col)
192 {
193     return (matrix[row] & ((matrix_row_t)1<<col));
194 }
195
196 inline
197 matrix_row_t matrix_get_row(uint8_t row)
198 {
199     /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
200        switch blocker installed and the switch is always pressed. */
201 #ifdef MATRIX_MASKED
202     return matrix[row] & matrix_mask[row];
203 #else
204     return matrix[row];
205 #endif
206 }
207
208 void matrix_print(void)
209 {
210     print_matrix_header();
211
212     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
213         phex(row); print(": ");
214         print_matrix_row(row);
215         print("\n");
216     }
217 }
218
219 uint8_t matrix_key_count(void)
220 {
221     uint8_t count = 0;
222     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
223         count += matrix_bitpop(i);
224     }
225     return count;
226 }
227
228 static void init_cols(void)
229 {
230     for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
231         uint8_t pin = col_pins[x];
232         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
233         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); /* HI */
234     }
235 }
236
237 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
238 {
239     /* Store last value of row prior to reading */
240     matrix_row_t last_row_value = current_matrix[current_row];
241
242     /* Clear data in matrix row */
243     current_matrix[current_row] = 0;
244
245     /* Select row and wait for row selecton to stabilize */
246     select_row(current_row);
247     wait_us(30);
248
249     if (mcp23018_status) {
250         /* if there was an error */
251         return 0;
252     } else {
253         uint16_t data = 0;
254         mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
255         mcp23018_status = i2c_write(GPIOA);             if (mcp23018_status) goto out;
256         mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
257         data = i2c_readNak();
258         data = ~data;
259     out:
260         i2c_stop();
261         current_matrix[current_row] |= (data << 8);
262     }
263
264     /* For each col... */
265     for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
266         /* Select the col pin to read (active low) */
267         uint8_t pin = col_pins[col_index];
268         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
269
270         /* Populate the matrix row with the state of the col pin */
271         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
272     }
273
274     /* Unselect row */
275     unselect_rows();
276
277     return (last_row_value != current_matrix[current_row]);
278 }
279
280 static void select_row(uint8_t row)
281 {
282     if (mcp23018_status) {
283         /* if there was an error do nothing */
284     } else {
285         /* set active row low  : 0
286            set active row output : 1
287            set other rows hi-Z : 1 */
288         mcp23018_status = i2c_start(I2C_ADDR_WRITE);   if (mcp23018_status) goto out;
289         mcp23018_status = i2c_write(GPIOB);            if (mcp23018_status) goto out;
290         mcp23018_status = i2c_write(0xFF & ~(1<<abs(row-4))); if (mcp23018_status) goto out;
291     out:
292         i2c_stop();
293     }
294
295     uint8_t pin = row_pins[row];
296     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); /*  OUT  */
297     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW  */
298 }
299
300 static void unselect_rows(void)
301 {
302     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
303         uint8_t pin = row_pins[x];
304         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
305         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); /* HI */
306     }
307 }