]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/frosty_flake/matrix.c
70456ada32542121b66da049782433b23425247f
[qmk_firmware.git] / keyboards / frosty_flake / matrix.c
1 /*
2   Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
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 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26
27 #ifndef CONFIG_SPECIFIC_H
28 #define CONFIG_SPECIFIC_H
29
30 #define CONFIG_LED_IO \
31   DDRB |= (1<<7); \
32   DDRC |= (1<<5) | (1<<6);
33
34 #define USB_LED_CAPS_LOCK_ON    PORTC &= ~(1<<5)
35 #define USB_LED_CAPS_LOCK_OFF   PORTC |=  (1<<5)
36 #define USB_LED_NUM_LOCK_ON     PORTB &= ~(1<<7)
37 #define USB_LED_NUM_LOCK_OFF    PORTB |=  (1<<7)
38 #define USB_LED_SCROLL_LOCK_ON  PORTC &= ~(1<<6)
39 #define USB_LED_SCROLL_LOCK_OFF PORTC |=  (1<<6)
40
41 #define CONFIG_MATRIX_IO   \
42   /* Column output pins */ \
43   DDRD  |=  0b01111011;    \
44   /* Row input pins */     \
45   DDRC  &= ~0b10000000;    \
46   DDRB  &= ~0b01111111;    \
47   PORTC |=  0b10000000;    \
48   PORTB |=  0b01111111;
49
50 #define MATRIX_ROW_SCAN                      \
51   (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) | \
52   (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) | \
53   (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) | \
54   (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) | \
55   (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) | \
56   (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) | \
57   (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) | \
58   (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7))
59
60 #define MATRIX_ROW_SELECT                                     \
61   case  0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break; \
62   case  1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break; \
63   case  2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break; \
64   case  3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break; \
65   case  4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break; \
66   case  5: PORTD = (PORTD & ~0b01111011) | 0b01110001; break; \
67   case  6: PORTD = (PORTD & ~0b01111011) | 0b01100001; break; \
68   case  7: PORTD = (PORTD & ~0b01111011) | 0b01110000; break; \
69   case  8: PORTD = (PORTD & ~0b01111011) | 0b01100000; break; \
70   case  9: PORTD = (PORTD & ~0b01111011) | 0b01101000; break; \
71   case 10: PORTD = (PORTD & ~0b01111011) | 0b00101011; break; \
72   case 11: PORTD = (PORTD & ~0b01111011) | 0b00110011; break; \
73   case 12: PORTD = (PORTD & ~0b01111011) | 0b00100011; break; \
74   case 13: PORTD = (PORTD & ~0b01111011) | 0b01111000; break; \
75   case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break; \
76   case 15: PORTD = (PORTD & ~0b01111011) | 0b01101001; break; \
77   case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break; \
78   case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
79
80 #endif
81
82 #ifndef DEBOUNCING_DELAY
83 #   define DEBOUNCING_DELAY 0
84 #endif
85 static uint8_t debouncing = DEBOUNCING_DELAY;
86
87 static matrix_row_t matrix[MATRIX_ROWS];
88 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
89
90 static matrix_row_t scan_row(void);
91 static void select_row(uint8_t row);
92
93 inline uint8_t matrix_rows(void) {
94   return MATRIX_ROWS;
95 }
96
97 inline uint8_t matrix_cols(void) {
98   return MATRIX_COLS;
99 }
100
101 void matrix_init(void) {
102   CONFIG_MATRIX_IO;
103
104   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
105     matrix[i] = 0;
106     matrix_debouncing[i] = 0;
107   }
108
109   matrix_init_quantum();
110 }
111
112 uint8_t matrix_scan(void) {
113   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
114     select_row(row);
115     _delay_us(3);
116     matrix_row_t row_scan = scan_row();
117     for (uint8_t col = 0; col < MATRIX_COLS; col++) {
118       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
119       bool curr_bit = row_scan & (1<<col);
120       if (prev_bit != curr_bit) {
121         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
122         debouncing = DEBOUNCING_DELAY;
123       }
124     }
125   }
126
127   if (debouncing) {
128     if (--debouncing)
129       _delay_ms(1);
130     else
131       for (uint8_t i = 0; i < MATRIX_ROWS; i++)
132         matrix[i] = matrix_debouncing[i];
133   }
134
135   matrix_scan_quantum();
136   return 1;
137 }
138
139 bool matrix_is_modified(void) {
140   if (debouncing)
141     return false;
142   else
143     return true;
144 }
145
146 inline bool matrix_is_on(uint8_t row, uint8_t col) {
147   return (matrix[row] & ((matrix_row_t)1<<col));
148 }
149
150 inline matrix_row_t matrix_get_row(uint8_t row) {
151   return matrix[row];
152 }
153
154 void matrix_print(void) {
155   print("\nr/c 01234567\n");
156   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
157     matrix_row_t row_scan = matrix_get_row(row);
158     xprintf("%02X: ", row);
159     for (uint8_t col = 0; col < MATRIX_COLS; col++) {
160       bool curr_bit = row_scan & (1<<col);
161       xprintf("%c", curr_bit ? '*' : '.');
162     }
163     print("\n");
164   }
165 }
166
167 uint8_t matrix_key_count(void) {
168   uint8_t count = 0;
169   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
170     count += bitpop32(matrix[i]);
171   }
172   return count;
173 }
174
175 static matrix_row_t scan_row(void) {
176   return MATRIX_ROW_SCAN;
177 }
178
179 static void select_row(uint8_t row) {
180   switch (row) {
181     MATRIX_ROW_SELECT;
182   }
183 }