1 /* Copyright (C) 2011 by Jacob Alexander
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 // ----- Includes -----
26 #include <util/delay.h>
32 #include "matrix_scan.h"
34 // Matrix Configuration
41 // -- pinSetup Macros --
42 #define REG_SET(reg) reg |= (1 << ( matrix[row*(MAX_ROW_SIZE+1)+col] % 10 ) )
44 #define PIN_SET_COL(pin,scan) \
47 case scanRow_powrCol: \
49 REG_SET(port##pin); break; \
50 case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
54 #define PIN_SET_ROW(pin,scan) \
57 case scanCol_powrRow: \
59 REG_SET(port##pin); break; \
60 case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
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
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 ) ) ) ) \
79 detectArray[scanCode]++; \
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]++; \
90 // -- Scan Dual Macros --
91 #define PIN_DUALTEST_ROW(pin) \
92 scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
94 && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) \
95 && detectArray[scanCode] & 0x01 ) \
97 detectArray[scanCode]++; \
101 if ( detectArray[scanCode] & 0x01 ) \
102 detectArray[scanCode]--; \
108 // ----- Variables -----
112 // ----- Functions -----
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 )
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;
132 // Loop through all the pin assignments, for the initial pin settings
136 for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
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] )
142 PIN_SET_ROW(A, scanType);
144 PIN_SET_ROW(B, scanType);
146 PIN_SET_ROW(C, scanType);
148 PIN_SET_ROW(D, scanType);
150 PIN_SET_ROW(E, scanType);
152 PIN_SET_ROW(F, scanType);
160 for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
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] )
166 PIN_SET_COL(A, scanType);
168 PIN_SET_COL(B, scanType);
170 PIN_SET_COL(C, scanType);
172 PIN_SET_COL(D, scanType);
174 PIN_SET_COL(E, scanType);
176 PIN_SET_COL(F, scanType);
184 if ( scanType == scanMode )
187 info_print("Initial Matrix Pin Setup");
188 info_print(" ddrA ddrB ddrC ddrD ddrE ddrF");
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 );
197 info_print("portA portB portC portD portE portF");
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 );
206 int8ToStr( scanType, tmpStr );
210 #if defined(__AVR_AT90USB1286__)
219 #if defined(__AVR_AT90USB1286__)
229 // Scans the given matrix determined by the scanMode method
230 inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
232 // Loop variables for all modes
235 uint16_t scanCode = 0;
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++ )
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 )
246 #if defined(__AVR_AT90USB1286__)
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++ )
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 )
273 #if defined(__AVR_AT90USB1286__)
293 #if scanMode == scanDual
294 // First do a scan of all of the columns, marking each one
295 matrix_pinSetup( matrix, scanCol_powrRow );
297 for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
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 )
303 #if defined(__AVR_AT90USB1286__)
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 );
326 for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ )
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 )
332 #if defined(__AVR_AT90USB1286__)
334 PIN_DUALTEST_ROW(PINA);
337 PIN_DUALTEST_ROW(PINB);
339 PIN_DUALTEST_ROW(PINC);
341 PIN_DUALTEST_ROW(PIND);
343 PIN_DUALTEST_ROW(PINE);
345 PIN_DUALTEST_ROW(PINF);