]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/matrix/matrix_scan.c
Cleaning up the BudKeypad module for the Buffered Macro Module
[kiibohd-controller.git] / Scan / matrix / matrix_scan.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 // AVR Includes
25 #include <avr/io.h>
26 #include <util/delay.h>
27
28 // Project Includes
29 #include <print.h>
30
31 // Local Includes
32 #include "matrix_scan.h"
33
34 // Matrix Configuration
35 #include <matrix.h>
36
37
38
39 // ----- Macros -----
40
41 // -- pinSetup Macros --
42 #define REG_SET(reg)    reg |= (1 << ( matrix[row*(MAX_ROW_SIZE+1)+col] % 10 ) )
43                         
44 #define PIN_SET_COL(pin,scan) \
45                         switch ( scan ) { \
46                         case scanCol: \
47                         case scanRow_powrCol: \
48                         case scanDual: \
49                                 REG_SET(port##pin); break; \
50                         case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
51                         } \
52                         break
53
54 #define PIN_SET_ROW(pin,scan) \
55                         switch ( scan ) { \
56                         case scanRow: \
57                         case scanCol_powrRow: \
58                         case scanDual: \
59                                 REG_SET(port##pin); break; \
60                         case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
61                         } \
62                         break
63
64 #define PIN_CASE(pinLetter) \
65                         case pin##pinLetter##0: \
66                         case pin##pinLetter##1: \
67                         case pin##pinLetter##2: \
68                         case pin##pinLetter##3: \
69                         case pin##pinLetter##4: \
70                         case pin##pinLetter##5: \
71                         case pin##pinLetter##6: \
72                         case pin##pinLetter##7
73
74 // -- Column Scan Macros --
75 #define PIN_TEST_COL(pin) \
76                         scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
77                         if ( scanCode && !( pin & ( 1 << ( matrix[0*(MAX_ROW_SIZE+1)+col] % 10 ) ) ) ) \
78                         { \
79                                 detectArray[scanCode]++; \
80                         } \
81                         break
82
83 // -- Row Scan Macros --
84 #define PIN_TEST_ROW(pin) \
85                         scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
86                         if ( scanCode && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) ) \
87                                 detectArray[scanCode]++; \
88                         break
89
90 // -- Scan Dual Macros --
91 #define PIN_DUALTEST_ROW(pin) \
92                         scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
93                         if ( scanCode \
94                           && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) \
95                           && detectArray[scanCode] & 0x01 ) \
96                         { \
97                                 detectArray[scanCode]++; \
98                         } \
99                         else \
100                         { \
101                                 if ( detectArray[scanCode] & 0x01 ) \
102                                         detectArray[scanCode]--; \
103                         } \
104                         break
105
106
107
108 // ----- Variables -----
109
110
111
112 // ----- Functions -----
113
114 // Goes through the defined matrix and matrix mode, and sets the initial state of all of the available pins
115 void matrix_pinSetup( uint8_t *matrix, uint8_t scanType )
116 {
117         // Setup the variables
118         uint8_t portA = 0x00;
119         uint8_t portB = 0x00;
120         uint8_t portC = 0x00;
121         uint8_t portD = 0x00;
122         uint8_t portE = 0x00;
123         uint8_t portF = 0x00;
124
125         uint8_t ddrA = 0x00;
126         uint8_t ddrB = 0x00;
127         uint8_t ddrC = 0x00;
128         uint8_t ddrD = 0x00;
129         uint8_t ddrE = 0x00;
130         uint8_t ddrF = 0x00;
131
132         // Loop through all the pin assignments, for the initial pin settings
133         uint16_t row, col;
134
135         // Rows
136         for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
137         {
138                 // We can't pass 2D arrays, so just point to the first element and calculate directly
139                 switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
140                 {
141                 PIN_CASE(A):
142                         PIN_SET_ROW(A, scanType);
143                 PIN_CASE(B):
144                         PIN_SET_ROW(B, scanType);
145                 PIN_CASE(C):
146                         PIN_SET_ROW(C, scanType);
147                 PIN_CASE(D):
148                         PIN_SET_ROW(D, scanType);
149                 PIN_CASE(E):
150                         PIN_SET_ROW(E, scanType);
151                 PIN_CASE(F):
152                         PIN_SET_ROW(F, scanType);
153
154                 default:
155                         continue;
156                 }
157         }
158
159         // Columns
160         for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
161         {
162                 // We can't pass 2D arrays, so just point to the first element and calculate directly
163                 switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
164                 {
165                 PIN_CASE(A):
166                         PIN_SET_COL(A, scanType);
167                 PIN_CASE(B):
168                         PIN_SET_COL(B, scanType);
169                 PIN_CASE(C):
170                         PIN_SET_COL(C, scanType);
171                 PIN_CASE(D):
172                         PIN_SET_COL(D, scanType);
173                 PIN_CASE(E):
174                         PIN_SET_COL(E, scanType);
175                 PIN_CASE(F):
176                         PIN_SET_COL(F, scanType);
177
178                 default:
179                         continue;
180                 }
181         }
182
183         // Pin Status
184         if ( scanType == scanMode )
185         {
186                 char tmpStr[6];
187                 info_print("Initial Matrix Pin Setup");
188                 info_print(" ddrA  ddrB  ddrC  ddrD  ddrE  ddrF");
189                 print("      ");
190                 hexToStr_op( ddrA, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
191                 hexToStr_op( ddrB, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
192                 hexToStr_op( ddrC, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
193                 hexToStr_op( ddrD, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
194                 hexToStr_op( ddrE, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
195                 hexToStr_op( ddrF, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
196                 print("\n");
197                 info_print("portA portB portC portD portE portF");
198                 print("      ");
199                 hexToStr_op( portA, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
200                 hexToStr_op( portB, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
201                 hexToStr_op( portC, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
202                 hexToStr_op( portD, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
203                 hexToStr_op( portE, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
204                 hexToStr_op( portF, tmpStr, 2 ); dPrintStrs( "  0x", tmpStr );
205                 print("\n");
206                 int8ToStr( scanType, tmpStr );
207         }
208
209         // Setting the pins
210 #if defined(__AVR_AT90USB1286__)
211         DDRA = ddrA;
212 #endif
213         DDRB = ddrB;
214         DDRC = ddrC;
215         DDRD = ddrD;
216         DDRE = ddrE;
217         DDRF = ddrF;
218
219 #if defined(__AVR_AT90USB1286__)
220         PORTA = portA;
221 #endif
222         PORTB = portB;
223         PORTC = portC;
224         PORTD = portD;
225         PORTE = portE;
226         PORTF = portF;
227 }
228
229 // Scans the given matrix determined by the scanMode method
230 inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
231 {
232         // Loop variables for all modes
233         uint16_t col = 1;
234         uint16_t row = 1;
235         uint16_t scanCode = 0;
236
237
238         // Column Scan and Column Scan, Power Row
239 #if scanMode == scanCol || scanMode == scanCol_powrRow
240         for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
241         {
242                 // Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
243                 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
244                 switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
245                 {
246 #if defined(__AVR_AT90USB1286__)
247                 case 0: // PINA
248                         PIN_TEST_COL(PINA);
249 #endif
250                 case 1: // PINB
251                         PIN_TEST_COL(PINB);
252                 case 2: // PINC
253                         PIN_TEST_COL(PINC);
254                 case 3: // PIND
255                         PIN_TEST_COL(PIND);
256                 case 4: // PINE
257                         PIN_TEST_COL(PINE);
258                 case 5: // PINF
259                         PIN_TEST_COL(PINF);
260                 }
261         }
262 #endif // scanMode
263
264
265         // Row Scan and Row Scan, Power Row
266 #if scanMode == scanRow || scanMode == scanRow_powrCol
267         for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ ) 
268         {
269                 // Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
270                 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
271                 switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
272                 {
273 #if defined(__AVR_AT90USB1286__)
274                 case 0: // PINA
275                         PIN_TEST_ROW(PINA);
276 #endif
277                 case 1: // PINB
278                         PIN_TEST_ROW(PINB);
279                 case 2: // PINC
280                         PIN_TEST_ROW(PINC);
281                 case 3: // PIND
282                         PIN_TEST_ROW(PIND);
283                 case 4: // PINE
284                         PIN_TEST_ROW(PINE);
285                 case 5: // PINF
286                         PIN_TEST_ROW(PINF);
287                 }
288         }
289 #endif // scanMode
290
291
292         // Dual Scan
293 #if scanMode == scanDual
294         // First do a scan of all of the columns, marking each one
295         matrix_pinSetup( matrix, scanCol_powrRow );
296         _delay_us( 1 );
297         for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
298         {
299                 // Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
300                 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
301                 switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
302                 {
303 #if defined(__AVR_AT90USB1286__)
304                 case 0: // PINA
305                         PIN_TEST_COL(PINA);
306 #endif
307                 case 1: // PINB
308                         PIN_TEST_COL(PINB);
309                 case 2: // PINC
310                         PIN_TEST_COL(PINC);
311                 case 3: // PIND
312                         PIN_TEST_COL(PIND);
313                 case 4: // PINE
314                         PIN_TEST_COL(PINE);
315                 case 5: // PINF
316                         PIN_TEST_COL(PINF);
317                 }
318         }
319
320         // Next, do a scan of all of the rows, clearing any "vague" keys (only detected on row, but not column, or vice-versa)
321         // And marking any keys that are detected on the row and column
322         matrix_pinSetup( matrix, scanRow_powrCol );
323         _delay_us( 1 );
324         col = 1;
325         row = 1;
326         for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ ) 
327         {
328                 // Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
329                 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
330                 switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
331                 {
332 #if defined(__AVR_AT90USB1286__)
333                 case 0: // PINA
334                         PIN_DUALTEST_ROW(PINA);
335 #endif
336                 case 1: // PINB
337                         PIN_DUALTEST_ROW(PINB);
338                 case 2: // PINC
339                         PIN_DUALTEST_ROW(PINC);
340                 case 3: // PIND
341                         PIN_DUALTEST_ROW(PIND);
342                 case 4: // PINE
343                         PIN_DUALTEST_ROW(PINE);
344                 case 5: // PINF
345                         PIN_DUALTEST_ROW(PINF);
346                 }
347         }
348 #endif
349 }
350