]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/kitten_paw/matrix.c
Do some cleanup for the API
[qmk_firmware.git] / keyboards / kitten_paw / 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 DEBOUNCING_DELAY
28 #   define DEBOUNCING_DELAY 5
29 #endif
30 static uint8_t debouncing = DEBOUNCING_DELAY;
31
32 static matrix_row_t matrix[MATRIX_ROWS];
33 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
34
35 static uint8_t read_rows(void);
36 static void select_col(uint8_t col);
37
38 inline uint8_t matrix_rows(void) {
39   return MATRIX_ROWS;
40 }
41
42 inline uint8_t matrix_cols(void) {
43   return MATRIX_COLS;
44 }
45
46 /* Column pin configuration
47  *
48  * col: 0    1    2    3    4    5    6    7
49  * pin: PC7  PD5  PD3  PD1  PC2  PD6  PD4  PD2
50  *
51  * Rrr pin configuration 
52  *
53  * These rrrs uses one 74HC154 4 to 16 bit demultiplexer (low
54  * active), together with 2 rrrs driven directly from the micro
55  * controller, to control the 18 rrrs. The rrrs are driven from
56  * pins B6,5,4,3,2,1,0.
57  */
58 void matrix_init(void) {
59   DDRC  &= ~0b10000100;  // Row input pins
60   DDRD  &= ~0b01111110;
61   PORTC |=  0b10000100;
62   PORTD |=  0b01111110;
63
64   DDRB  |=  0b01111111;  // Column output pins
65
66   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
67     matrix[i] = 0;
68     matrix_debouncing[i] = 0;
69   }
70   matrix_init_quantum();
71 }
72
73 uint8_t matrix_scan(void) {
74   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
75     select_col(col);
76     _delay_us(3);
77     uint8_t rows = read_rows();
78     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
79       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
80       bool curr_bit = rows & (1<<row);
81       if (prev_bit != curr_bit) {
82         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
83         debouncing = DEBOUNCING_DELAY;
84       }
85     }
86   }
87   
88   if (debouncing) {
89     if (--debouncing) {
90       _delay_ms(1);
91     }
92     else {
93       for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
94         matrix[i] = matrix_debouncing[i];
95       }
96     }
97   }
98   matrix_scan_quantum();
99   return 1;
100 }
101
102 bool matrix_is_modified(void) {
103   if (debouncing)
104     return false;
105   else
106     return true;
107 }
108
109 inline bool matrix_is_on(uint8_t row, uint8_t col) {
110   return (matrix[row] & ((matrix_row_t)1<<col));
111 }
112
113 inline matrix_row_t matrix_get_row(uint8_t row) {
114   return matrix[row];
115 }
116
117 void matrix_print(void) {
118   print("\nr/c 0123456789ABCDEF\n");
119   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
120     xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
121   }
122 }
123
124 uint8_t matrix_key_count(void) {
125   uint8_t count = 0;
126   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
127     count += bitpop32(matrix[i]);
128   }
129   return count;
130 }
131
132 static uint8_t read_rows(void) {
133   return
134     (PINC&(1<<7) ? 0 : (1<<0)) |
135     (PIND&(1<<5) ? 0 : (1<<1)) |
136     (PIND&(1<<3) ? 0 : (1<<2)) |
137     (PIND&(1<<1) ? 0 : (1<<3)) |
138     (PINC&(1<<2) ? 0 : (1<<4)) |
139     (PIND&(1<<2) ? 0 : (1<<5)) |
140     (PIND&(1<<4) ? 0 : (1<<6)) |
141     (PIND&(1<<6) ? 0 : (1<<7));
142 }
143
144 static void select_col(uint8_t col) {
145   switch (col) {
146   case  0: PORTB = (PORTB & ~0b01111111) | 0b01100100; break;
147   case  1: PORTB = (PORTB & ~0b01111111) | 0b01101100; break;
148   case  2: PORTB = (PORTB & ~0b01111111) | 0b01100010; break;
149   case  3: PORTB = (PORTB & ~0b01111111) | 0b01111010; break;
150   case  4: PORTB = (PORTB & ~0b01111111) | 0b01100110; break;
151   case  5: PORTB = (PORTB & ~0b01111111) | 0b01110110; break;
152   case  6: PORTB = (PORTB & ~0b01111111) | 0b01101110; break;
153   case  7: PORTB = (PORTB & ~0b01111111) | 0b01111110; break;
154   case  8: PORTB = (PORTB & ~0b01111111) | 0b01000001; break;
155   case  9: PORTB = (PORTB & ~0b01111111) | 0b00100001; break;
156   case 10: PORTB = (PORTB & ~0b01111111) | 0b01101010; break;
157   case 11: PORTB = (PORTB & ~0b01111111) | 0b01110010; break;
158   case 12: PORTB = (PORTB & ~0b01111111) | 0b01111100; break;
159   case 13: PORTB = (PORTB & ~0b01111111) | 0b01110100; break;
160   case 14: PORTB = (PORTB & ~0b01111111) | 0b01111000; break;
161   case 15: PORTB = (PORTB & ~0b01111111) | 0b01110000; break;
162   case 16: PORTB = (PORTB & ~0b01111111) | 0b01100000; break;
163   case 17: PORTB = (PORTB & ~0b01111111) | 0b01101000; break;
164   }
165 }