]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/matrix/matrix.c
Reorganization for use with the CMake "Modules"
[kiibohd-controller.git] / Scan / matrix / matrix.c
1 /* Copyright (C) 2011 by Jacob Alexander
2  * 
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 #include "matrix.h"
23
24 #define REG_SET(reg)    reg |= (1 << ( matrix[row][col] % 10 ) )
25                         
26 #define PIN_SET_COL(pin) \
27                         switch ( scanMode ) { \
28                         case scanCol: \
29                         case scanCol_powrRow: \
30                         case scanDual: \
31                                 REG_SET(port##pin); break; \
32                         case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
33                         } \
34                         break
35
36 #define PIN_SET_ROW(pin) \
37                         switch ( scanMode ) { \
38                         case scanRow: \
39                         case scanRow_powrCol: \
40                         case scanDual: \
41                                 REG_SET(port##pin); break; \
42                         case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
43                         } \
44                         break
45
46 #define PIN_CASE(pinLetter) \
47                         case pin##pinLetter##0: \
48                         case pin##pinLetter##1: \
49                         case pin##pinLetter##2: \
50                         case pin##pinLetter##3: \
51                         case pin##pinLetter##4: \
52                         case pin##pinLetter##5: \
53                         case pin##pinLetter##6: \
54                         case pin##pinLetter##7
55
56 #define PIN_TEST_COL(pin) \
57                         if ( !( pin & ( 1 << ( matrix[0][col] % 10 ) ) \
58                                 detectArray[matrix[row][col]]++; \
59                         break
60
61
62 void matrix_pinSetup( uint8_t *matrix )
63 {
64         // Setup the variables
65         uint8_t portA = 0x00;
66         uint8_t portB = 0x00;
67         uint8_t portC = 0x00;
68         uint8_t portD = 0x00;
69         uint8_t portE = 0x00;
70         uint8_t portF = 0x00;
71
72         uint8_t ddrA = 0x00;
73         uint8_t ddrB = 0x00;
74         uint8_t ddrC = 0x00;
75         uint8_t ddrD = 0x00;
76         uint8_t ddrE = 0x00;
77         uint8_t ddrF = 0x00;
78
79         // Loop through all the pin assignments, for the initial pin settings
80         int row, col;
81
82         // Rows
83         for ( row = 1; row < sizeof(matrix); row++ ) {
84                 switch ( matrix[row][col] ) {
85                 PIN_CASE(A):
86                         PIN_SET_ROW(A);
87                 PIN_CASE(B):
88                         PIN_SET_ROW(B);
89                 PIN_CASE(C):
90                         PIN_SET_ROW(C);
91                 PIN_CASE(D):
92                         PIN_SET_ROW(D);
93                 PIN_CASE(E):
94                         PIN_SET_ROW(E);
95                 PIN_CASE(F):
96                         PIN_SET_ROW(F);
97
98                 default:
99                         continue;
100                 }
101         }
102
103         // Columns
104         for ( col = 1; col < sizeof(matrix[0]); row++ ) {
105                 switch ( matrix[row][col] ) {
106                 PIN_CASE(A):
107                         PIN_SET_COL(A);
108                 PIN_CASE(B):
109                         PIN_SET_COL(B);
110                 PIN_CASE(C):
111                         PIN_SET_COL(C);
112                 PIN_CASE(D):
113                         PIN_SET_COL(D);
114                 PIN_CASE(E):
115                         PIN_SET_COL(E);
116                 PIN_CASE(F):
117                         PIN_SET_COL(F);
118
119                 default:
120                         continue;
121                 }
122         }
123
124         // Setting the pins
125         DDRA = ddrA;
126         DDRB = ddrB;
127         DDRC = ddrC;
128         DDRD = ddrD;
129         DDRE = ddrE;
130         DDRF = ddrF;
131
132         PORTA = portA;
133         PORTB = portB;
134         PORTC = portC;
135         PORTD = portD;
136         PORTE = portE;
137         PORTF = portF;
138 }
139
140 // TODO Proper matrix scanning
141 void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
142 {
143         // Column Scan
144 #if scanMode == scanCol
145         uint8_t col = 1;
146         uint8_t row = 1;
147         for ( ; col < sizeof(matrix[1]); col++ ) {
148                 switch ( matrix[0][col] / 10 ) {
149                 case 0: // PINA
150                         PIN_TEST_COL(PINA);
151                 case 1: // PINB
152                         PIN_TEST_COL(PINB);
153                 case 2: // PINC
154                         PIN_TEST_COL(PINC);
155                 case 3: // PIND
156                         PIN_TEST_COL(PIND);
157                 case 4: // PINE
158                         PIN_TEST_COL(PINE);
159                 case 5: // PINF
160                         PIN_TEST_COL(PINF);
161                 }
162         }
163 #endif
164
165         // Row Scan
166 #if scanMode == scanRow
167 #endif
168
169         // Column Scan, Power Row
170 #if scanMode == scanCol_powrRow
171 #endif
172
173         // Row Scan, Power Column
174 #if scanMode == scanRow_powrCol
175 #endif
176
177         // Dual Scan
178 #if scanMode == scanDual
179 #endif
180 }
181