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 ) ) ) ) \
80 detectArray[scanCode]++; \
84 // -- Row Scan Macros --
85 #define PIN_TEST_ROW(pin) \
86 scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
87 if ( scanCode && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) ) \
88 detectArray[scanCode]++; \
91 // -- Scan Dual Macros --
92 #define PIN_DUALTEST_ROW(pin) \
93 scanCode = matrix[row*(MAX_ROW_SIZE+1)+col]; \
95 && !( pin & ( 1 << ( matrix[row*(MAX_ROW_SIZE+1)+0] % 10 ) ) ) \
96 && detectArray[scanCode] & 0x01 ) \
98 detectArray[scanCode]++; \
102 if ( detectArray[scanCode] & 0x01 ) \
103 detectArray[scanCode]--; \
109 // ----- Variables -----
113 // ----- Functions -----
115 // Goes through the defined matrix and matrix mode, and sets the initial state of all of the available pins
116 void matrix_pinSetup( uint8_t *matrix, uint8_t scanType )
118 // Setup the variables
119 uint8_t portA = 0x00;
120 uint8_t portB = 0x00;
121 uint8_t portC = 0x00;
122 uint8_t portD = 0x00;
123 uint8_t portE = 0x00;
124 uint8_t portF = 0x00;
133 // Loop through all the pin assignments, for the initial pin settings
137 for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
139 // We can't pass 2D arrays, so just point to the first element and calculate directly
140 switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
143 PIN_SET_ROW(A, scanType);
145 PIN_SET_ROW(B, scanType);
147 PIN_SET_ROW(C, scanType);
149 PIN_SET_ROW(D, scanType);
151 PIN_SET_ROW(E, scanType);
153 PIN_SET_ROW(F, scanType);
161 for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
163 // We can't pass 2D arrays, so just point to the first element and calculate directly
164 switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
167 PIN_SET_COL(A, scanType);
169 PIN_SET_COL(B, scanType);
171 PIN_SET_COL(C, scanType);
173 PIN_SET_COL(D, scanType);
175 PIN_SET_COL(E, scanType);
177 PIN_SET_COL(F, scanType);
185 if ( scanType == scanMode )
188 info_print("Initial Matrix Pin Setup");
189 info_print(" ddrA ddrB ddrC ddrD ddrE ddrF");
191 hexToStr_op( ddrA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
192 hexToStr_op( ddrB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
193 hexToStr_op( ddrC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
194 hexToStr_op( ddrD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
195 hexToStr_op( ddrE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
196 hexToStr_op( ddrF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
198 info_print("portA portB portC portD portE portF");
200 hexToStr_op( portA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
201 hexToStr_op( portB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
202 hexToStr_op( portC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
203 hexToStr_op( portD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
204 hexToStr_op( portE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
205 hexToStr_op( portF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
207 int8ToStr( scanType, tmpStr );
211 #if defined(__AVR_AT90USB1286__)
220 #if defined(__AVR_AT90USB1286__)
230 // Scans the given matrix determined by the scanMode method
231 inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
233 // Loop variables for all modes
236 uint16_t scanCode = 0;
239 // Column Scan and Column Scan, Power Row
240 #if scanMode == scanCol || scanMode == scanCol_powrRow
241 for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
243 // Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
244 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
245 switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
246 REG_SET(port##pin); break; \
248 #if defined(__AVR_AT90USB1286__)
267 // Row Scan and Row Scan, Power Row
268 #if scanMode == scanRow || scanMode == scanRow_powrCol
269 for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ )
271 // Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
272 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
273 switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
275 #if defined(__AVR_AT90USB1286__)
295 #if scanMode == scanDual
296 // First do a scan of all of the columns, marking each one
297 matrix_pinSetup( matrix, scanCol_powrRow );
299 for ( ; row < (MAX_COL_SIZE+1); row++ ) for ( ; col < (MAX_ROW_SIZE+1); col++ )
301 // Scan over the pins for each of the columns, and using the pin alias to determine which pin to set
302 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
303 switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
305 #if defined(__AVR_AT90USB1286__)
322 // Next, do a scan of all of the rows, clearing any "vague" keys (only detected on row, but not column, or vice-versa)
323 // And marking any keys that are detected on the row and column
324 matrix_pinSetup( matrix, scanRow_powrCol );
328 for ( ; col < (MAX_ROW_SIZE+1); col++ ) for ( ; row < (MAX_COL_SIZE+1); row++ )
330 // Scan over the pins for each of the rows, and using the pin alias to determine which pin to set
331 // (e.g. / 10 is for the pin name (A,B,C,etc.) and % 10 is for the position of the pin (A1,A2,etc.))
332 switch ( matrix[row*(MAX_ROW_SIZE+1)+0] / 10 )
334 #if defined(__AVR_AT90USB1286__)
336 PIN_DUALTEST_ROW(PINA);
339 PIN_DUALTEST_ROW(PINB);
341 PIN_DUALTEST_ROW(PINC);
343 PIN_DUALTEST_ROW(PIND);
345 PIN_DUALTEST_ROW(PINE);
347 PIN_DUALTEST_ROW(PINF);