]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/matrix/matrix.c
Formalizing code module structure and inheritance (Large Commit)
[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 // ----- Includes -----
23
24 // Local Includes
25 #include "matrix.h"
26
27
28
29 // ----- Macros -----
30
31 #define REG_SET(reg)    reg |= (1 << ( matrix[row][col] % 10 ) )
32                         
33 #define PIN_SET_COL(pin) \
34                         switch ( scanMode ) { \
35                         case scanCol: \
36                         case scanCol_powrRow: \
37                         case scanDual: \
38                                 REG_SET(port##pin); break; \
39                         case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
40                         } \
41                         break
42
43 #define PIN_SET_ROW(pin) \
44                         switch ( scanMode ) { \
45                         case scanRow: \
46                         case scanRow_powrCol: \
47                         case scanDual: \
48                                 REG_SET(port##pin); break; \
49                         case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
50                         } \
51                         break
52
53 #define PIN_CASE(pinLetter) \
54                         case pin##pinLetter##0: \
55                         case pin##pinLetter##1: \
56                         case pin##pinLetter##2: \
57                         case pin##pinLetter##3: \
58                         case pin##pinLetter##4: \
59                         case pin##pinLetter##5: \
60                         case pin##pinLetter##6: \
61                         case pin##pinLetter##7
62
63 #define PIN_TEST_COL(pin) \
64                         if ( !( pin & ( 1 << ( matrix[0][col] % 10 ) ) \
65                                 detectArray[matrix[row][col]]++; \
66                         break
67
68
69
70 // ----- Variables -----
71
72 uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
73
74
75
76 // ----- Functions -----
77
78 void matrix_pinSetup( uint8_t *matrix )
79 {
80         // Setup the variables
81         uint8_t portA = 0x00;
82         uint8_t portB = 0x00;
83         uint8_t portC = 0x00;
84         uint8_t portD = 0x00;
85         uint8_t portE = 0x00;
86         uint8_t portF = 0x00;
87
88         uint8_t ddrA = 0x00;
89         uint8_t ddrB = 0x00;
90         uint8_t ddrC = 0x00;
91         uint8_t ddrD = 0x00;
92         uint8_t ddrE = 0x00;
93         uint8_t ddrF = 0x00;
94
95         // Loop through all the pin assignments, for the initial pin settings
96         int row, col;
97
98         // Rows
99         for ( row = 1; row < sizeof(matrix); row++ ) {
100                 switch ( matrix[row][col] ) {
101                 PIN_CASE(A):
102                         PIN_SET_ROW(A);
103                 PIN_CASE(B):
104                         PIN_SET_ROW(B);
105                 PIN_CASE(C):
106                         PIN_SET_ROW(C);
107                 PIN_CASE(D):
108                         PIN_SET_ROW(D);
109                 PIN_CASE(E):
110                         PIN_SET_ROW(E);
111                 PIN_CASE(F):
112                         PIN_SET_ROW(F);
113
114                 default:
115                         continue;
116                 }
117         }
118
119         // Columns
120         for ( col = 1; col < sizeof(matrix[0]); row++ ) {
121                 switch ( matrix[row][col] ) {
122                 PIN_CASE(A):
123                         PIN_SET_COL(A);
124                 PIN_CASE(B):
125                         PIN_SET_COL(B);
126                 PIN_CASE(C):
127                         PIN_SET_COL(C);
128                 PIN_CASE(D):
129                         PIN_SET_COL(D);
130                 PIN_CASE(E):
131                         PIN_SET_COL(E);
132                 PIN_CASE(F):
133                         PIN_SET_COL(F);
134
135                 default:
136                         continue;
137                 }
138         }
139
140         // Setting the pins
141         DDRA = ddrA;
142         DDRB = ddrB;
143         DDRC = ddrC;
144         DDRD = ddrD;
145         DDRE = ddrE;
146         DDRF = ddrF;
147
148         PORTA = portA;
149         PORTB = portB;
150         PORTC = portC;
151         PORTD = portD;
152         PORTE = portE;
153         PORTF = portF;
154 }
155
156 // TODO Proper matrix scanning
157 void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
158 {
159         // Column Scan
160 #if scanMode == scanCol
161         uint8_t col = 1;
162         uint8_t row = 1;
163         for ( ; col < sizeof(matrix[1]); col++ ) {
164                 switch ( matrix[0][col] / 10 ) {
165                 case 0: // PINA
166                         PIN_TEST_COL(PINA);
167                 case 1: // PINB
168                         PIN_TEST_COL(PINB);
169                 case 2: // PINC
170                         PIN_TEST_COL(PINC);
171                 case 3: // PIND
172                         PIN_TEST_COL(PIND);
173                 case 4: // PINE
174                         PIN_TEST_COL(PINE);
175                 case 5: // PINF
176                         PIN_TEST_COL(PINF);
177                 }
178         }
179 #endif
180
181         // Row Scan
182 #if scanMode == scanRow
183 #endif
184
185         // Column Scan, Power Row
186 #if scanMode == scanCol_powrRow
187 #endif
188
189         // Row Scan, Power Column
190 #if scanMode == scanRow_powrCol
191 #endif
192
193         // Dual Scan
194 #if scanMode == scanDual
195 #endif
196 }
197