]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/meira/matrix.c
[Keyboard] Fix v60_type_r compile failures (#7250)
[qmk_firmware.git] / keyboards / meira / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3 Copyright 2017 Cole Markham <cole@ccmcomputing.net>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20  * scan matrix
21  */
22 #include <stdint.h>
23 #include <stdbool.h>
24 #if defined(__AVR__)
25 #include <avr/io.h>
26 #endif
27 #include "meira.h"
28 #include "wait.h"
29 #include "print.h"
30 #include "debug.h"
31 #include "util.h"
32 #include "matrix.h"
33 #include "config.h"
34 #include "timer.h"
35
36 #ifndef DEBOUNCE
37 #   define DEBOUNCE 5
38 #endif
39
40 #if (DEBOUNCE > 0)
41     static uint16_t debouncing_time;
42     static bool debouncing = false;
43 #endif
44
45 #if (MATRIX_COLS <= 8)
46 #    define print_matrix_header()  print("\nr/c 01234567\n")
47 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
48 #    define matrix_bitpop(i)       bitpop(matrix[i])
49 #    define ROW_SHIFTER ((uint8_t)1)
50 #elif (MATRIX_COLS <= 16)
51 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
52 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
53 #    define matrix_bitpop(i)       bitpop16(matrix[i])
54 #    define ROW_SHIFTER ((uint16_t)1)
55 #elif (MATRIX_COLS <= 32)
56 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
57 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
58 #    define matrix_bitpop(i)       bitpop32(matrix[i])
59 #    define ROW_SHIFTER  ((uint32_t)1)
60 #endif
61
62 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
63
64 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
65 static const uint8_t col_pins[4] = MATRIX_COL_PINS;
66 //static const uint8_t lrow_pins[MATRIX_ROWS] = LED_ROW_PINS;
67 //static const uint8_t lcol_pins[4] = LED_COL_PINS;
68
69 /* matrix state(1:on, 0:off) */
70 static matrix_row_t matrix[MATRIX_ROWS];
71 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
72 static void init_rows(void);
73 //static void init_lcols(void);
74 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
75 static void unselect_cols(void);
76 static void select_col(uint8_t col);
77
78
79 __attribute__ ((weak))
80 void matrix_init_kb(void) {
81     matrix_init_user();
82 }
83
84 __attribute__ ((weak))
85 void matrix_scan_kb(void) {
86     matrix_scan_user();
87 }
88
89 __attribute__ ((weak))
90 void matrix_init_user(void) {
91 }
92
93 __attribute__ ((weak))
94 void matrix_scan_user(void) {
95 }
96
97 inline
98 uint8_t matrix_rows(void)
99 {
100     return MATRIX_ROWS;
101 }
102
103 inline
104 uint8_t matrix_cols(void)
105 {
106     return MATRIX_COLS;
107 }
108
109 void matrix_init(void)
110 {
111     debug_enable = true;
112     debug_matrix = true;
113     debug_mouse = true;
114     // initialize row and col
115     unselect_cols();
116     init_rows();
117 //    init_lcols();
118
119     // initialize matrix state: all keys off
120     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
121         matrix[i] = 0;
122         matrix_debouncing[i] = 0;
123     }
124
125     matrix_init_quantum();
126
127 }
128
129 uint8_t _matrix_scan(void)
130 {
131     // Set col, read rows
132     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
133 #       if (DEBOUNCE > 0)
134             bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
135             if (matrix_changed) {
136                 debouncing = true;
137                 debouncing_time = timer_read();
138             }
139 #       else
140              read_rows_on_col(matrix, current_col);
141 #       endif
142
143     }
144
145 #   if (DEBOUNCE > 0)
146         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
147             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
148                 matrix[i] = matrix_debouncing[i];
149             }
150             debouncing = false;
151         }
152 #   endif
153
154         return 1;
155 }
156
157 uint8_t matrix_scan(void)
158 {
159         uint8_t ret = _matrix_scan();
160         matrix_scan_quantum();
161         return ret;
162 }
163
164 bool matrix_is_modified(void)
165 {
166     if (debouncing) return false;
167     return true;
168 }
169
170 inline
171 bool matrix_is_on(uint8_t row, uint8_t col)
172 {
173     return (matrix[row] & ((matrix_row_t)1<<col));
174 }
175
176 inline
177 matrix_row_t matrix_get_row(uint8_t row)
178 {
179     return matrix[row];
180 }
181
182 void matrix_print(void)
183 {
184     print("\nr/c 0123456789ABCDEF\n");
185     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
186         phex(row); print(": ");
187         pbin_reverse16(matrix_get_row(row));
188         print("\n");
189     }
190 }
191
192 uint8_t matrix_key_count(void)
193 {
194     uint8_t count = 0;
195     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
196         count += bitpop16(matrix[i]);
197     }
198     return count;
199 }
200
201
202 static void init_rows(void)
203 {
204     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
205         uint8_t pin = row_pins[x];
206         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
207         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
208     }
209 }
210
211
212 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
213 {
214     bool matrix_changed = false;
215
216     // Select col and wait for col selection to stabilize
217     select_col(current_col);
218     wait_us(30);
219
220     // For each row...
221     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
222     {
223
224         // Store last value of row prior to reading
225         matrix_row_t last_row_value = current_matrix[row_index];
226
227         // Check row pin state
228         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
229         {
230             // Pin LO, set col bit
231             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
232         }
233         else
234         {
235             // Pin HI, clear col bit
236             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
237         }
238
239         // Determine if the matrix changed state
240         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
241         {
242             matrix_changed = true;
243         }
244     }
245
246     // Unselect col
247     unselect_cols();
248
249     return matrix_changed;
250 }
251
252 static void select_col(uint8_t col)
253 {
254 #ifdef FLIPPED_BOARD
255         col = MATRIX_COLS - col - 1;
256 #endif
257     for(uint8_t x = 0; x < 4; x++) {
258                 uint8_t pin = col_pins[x];
259         _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
260                 if (((col >> x) & 0x1) == 1){
261                         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HIGH
262                 } else {
263                         _SFR_IO8((pin >> 4) + 2) &=  ~_BV(pin & 0xF); // LOW
264                 }
265         }
266 }
267
268 static void unselect_cols(void)
269 {
270         // FIXME This really needs to use the global enable on the decoder, because currently this sets the value to col1
271     for(uint8_t x = 0; x < 4; x++) {
272         uint8_t pin = col_pins[x];
273         _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
274         _SFR_IO8((pin >> 4) + 2) &=  ~_BV(pin & 0xF); // LOW
275     }
276 }