]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/meira/matrix.c
Updated copyright headers and peer review fixes
[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 DEBOUNCING_DELAY
37 #   define DEBOUNCING_DELAY 5
38 #endif
39
40 #if (DEBOUNCING_DELAY > 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 __attribute__ ((weak))
79 void matrix_init_quantum(void) {
80     matrix_init_kb();
81 }
82
83 __attribute__ ((weak))
84 void matrix_scan_quantum(void) {
85     matrix_scan_kb();
86 }
87
88 __attribute__ ((weak))
89 void matrix_init_kb(void) {
90     matrix_init_user();
91 }
92
93 __attribute__ ((weak))
94 void matrix_scan_kb(void) {
95     matrix_scan_user();
96 }
97
98 __attribute__ ((weak))
99 void matrix_init_user(void) {
100 }
101
102 __attribute__ ((weak))
103 void matrix_scan_user(void) {
104 }
105
106 inline
107 uint8_t matrix_rows(void)
108 {
109     return MATRIX_ROWS;
110 }
111
112 inline
113 uint8_t matrix_cols(void)
114 {
115     return MATRIX_COLS;
116 }
117
118 void matrix_init(void)
119 {
120     debug_enable = true;
121     debug_matrix = true;
122     debug_mouse = true;
123     // initialize row and col
124     unselect_cols();
125     init_rows();
126 //    init_lcols();
127
128 //    TX_RX_LED_INIT;
129
130     // initialize matrix state: all keys off
131     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
132         matrix[i] = 0;
133         matrix_debouncing[i] = 0;
134     }
135
136     matrix_init_quantum();
137
138 }
139
140 uint8_t _matrix_scan(void)
141 {
142     // Set col, read rows
143     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
144 #       if (DEBOUNCING_DELAY > 0)
145             bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
146             if (matrix_changed) {
147                 debouncing = true;
148                 debouncing_time = timer_read();
149             }
150 #       else
151              read_rows_on_col(matrix, current_col);
152 #       endif
153
154     }
155
156 #   if (DEBOUNCING_DELAY > 0)
157         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
158             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
159                 matrix[i] = matrix_debouncing[i];
160             }
161             debouncing = false;
162         }
163 #   endif
164
165         return 1;
166 }
167
168 uint8_t matrix_scan(void)
169 {
170         uint8_t ret = _matrix_scan();
171         matrix_scan_quantum();
172 //      // HACK backlighting
173 //      for (uint8_t t = 0; t < meira_get_backlight_level(); t++) {
174 //              for (uint8_t x = 0; x < 13; x++) {
175 //                      for (uint8_t y = 0; y < 4; y++) {
176 //                              uint8_t pin = lcol_pins[y];
177 //                              if ((x >> y) & 1) {
178 //                                      _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
179 //                              } else {
180 //                                      _SFR_IO8((pin >> 4) + 2) &=  ~_BV(pin & 0xF); // LO
181 //                              }
182 //                      }
183 //              }
184 //      }
185         return ret;
186 }
187
188 bool matrix_is_modified(void)
189 {
190     if (debouncing) return false;
191     return true;
192 }
193
194 inline
195 bool matrix_is_on(uint8_t row, uint8_t col)
196 {
197     return (matrix[row] & ((matrix_row_t)1<<col));
198 }
199
200 inline
201 matrix_row_t matrix_get_row(uint8_t row)
202 {
203     return matrix[row];
204 }
205
206 void matrix_print(void)
207 {
208     print("\nr/c 0123456789ABCDEF\n");
209     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
210         phex(row); print(": ");
211         pbin_reverse16(matrix_get_row(row));
212         print("\n");
213     }
214 }
215
216 uint8_t matrix_key_count(void)
217 {
218     uint8_t count = 0;
219     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
220         count += bitpop16(matrix[i]);
221     }
222     return count;
223 }
224
225
226 static void init_rows(void)
227 {
228     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
229         uint8_t pin = row_pins[x];
230         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
231         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
232 //        // HACK backlighting
233 //        uint8_t lpin = lrow_pins[x];
234 //        _SFR_IO8((lpin >> 4) + 1) |= _BV(lpin & 0xF); // OUT
235 //        _SFR_IO8((lpin >> 4) + 2) |=  _BV(lpin & 0xF); // HI
236     }
237 }
238
239 //static void init_lcols(void)
240 //{
241 //      for (uint8_t x = 0; x < 4; x++) {
242 //              uint8_t pin = lcol_pins[x];
243 //              _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
244 //              _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HIGH
245 //      }
246 //}
247
248 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
249 {
250     bool matrix_changed = false;
251
252     // Select col and wait for col selection to stabilize
253     select_col(current_col);
254     wait_us(30);
255
256     // For each row...
257     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
258     {
259
260         // Store last value of row prior to reading
261         matrix_row_t last_row_value = current_matrix[row_index];
262
263         // Check row pin state
264         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
265         {
266             // Pin LO, set col bit
267             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
268         }
269         else
270         {
271             // Pin HI, clear col bit
272             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
273         }
274
275         // Determine if the matrix changed state
276         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
277         {
278             matrix_changed = true;
279         }
280     }
281
282     // Unselect col
283     unselect_cols();
284
285     return matrix_changed;
286 }
287
288 static void select_col(uint8_t col)
289 {
290 #ifdef FLIPPED_BOARD
291         col = MATRIX_COLS - col - 1;
292 #endif
293     for(uint8_t x = 0; x < 4; x++) {
294                 uint8_t pin = col_pins[x];
295         _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
296                 if (((col >> x) & 0x1) == 1){
297                         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HIGH
298                 } else {
299                         _SFR_IO8((pin >> 4) + 2) &=  ~_BV(pin & 0xF); // LOW
300                 }
301         }
302 }
303
304 static void unselect_cols(void)
305 {
306         // FIXME This really needs to use the global enable on the decoder, because currently this sets the value to col1
307     for(uint8_t x = 0; x < 4; x++) {
308         uint8_t pin = col_pins[x];
309         _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
310         _SFR_IO8((pin >> 4) + 2) &=  ~_BV(pin & 0xF); // LOW
311     }
312 }
313
314