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 -----
31 #include "matrix_scan.h"
33 // Matrix Configuration
40 #define REG_SET(reg) reg |= (1 << ( matrix[row*(MAX_ROW_SIZE+1)+col] % 10 ) )
42 #define PIN_SET_COL(pin) \
43 switch ( scanMode ) { \
45 case scanCol_powrRow: \
47 REG_SET(port##pin); break; \
48 case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
52 #define PIN_SET_ROW(pin) \
53 switch ( scanMode ) { \
55 case scanRow_powrCol: \
57 REG_SET(port##pin); break; \
58 case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
62 #define PIN_CASE(pinLetter) \
63 case pin##pinLetter##0: \
64 case pin##pinLetter##1: \
65 case pin##pinLetter##2: \
66 case pin##pinLetter##3: \
67 case pin##pinLetter##4: \
68 case pin##pinLetter##5: \
69 case pin##pinLetter##6: \
70 case pin##pinLetter##7
72 #define PIN_TEST_COL(pin) \
73 if ( !( pin & ( 1 << ( matrix[0*(MAX_ROW_SIZE+1)+col] % 10 ) ) ) ) \
74 detectArray[matrix[row*(MAX_ROW_SIZE+1)+col]]++; \
79 // ----- Variables -----
83 // ----- Functions -----
85 // Goes through the defined matrix and matrix mode, and sets the initial state of all of the available pins
86 inline void matrix_pinSetup( uint8_t *matrix )
88 // Setup the variables
103 // Loop through all the pin assignments, for the initial pin settings
107 for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
109 // We can't pass 2D arrays, so just point to the first element and calculate directly
110 switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
131 for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
133 // We can't pass 2D arrays, so just point to the first element and calculate directly
134 switch ( matrix[row*(MAX_ROW_SIZE+1)+col] )
156 info_print("Initial Matrix Pin Setup");
157 info_print(" ddrA ddrB ddrC ddrD ddrE ddrF");
159 hexToStr_op( ddrA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
160 hexToStr_op( ddrB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
161 hexToStr_op( ddrC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
162 hexToStr_op( ddrD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
163 hexToStr_op( ddrE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
164 hexToStr_op( ddrF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
166 info_print("portA portB portC portD portE portF");
168 hexToStr_op( portA, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
169 hexToStr_op( portB, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
170 hexToStr_op( portC, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
171 hexToStr_op( portD, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
172 hexToStr_op( portE, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
173 hexToStr_op( portF, tmpStr, 2 ); dPrintStrs( " 0x", tmpStr );
177 #if defined(__AVR_AT90USB1286__)
186 #if defined(__AVR_AT90USB1286__)
196 // TODO Proper matrix scanning
197 inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
200 #if scanMode == scanCol
203 for ( ; col < (MAX_ROW_SIZE+1) + 1; col++ )
205 switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
207 #if defined(__AVR_AT90USB1286__)
226 #if scanMode == scanRow
229 // Column Scan, Power Row
230 #if scanMode == scanCol_powrRow
233 // Row Scan, Power Column
234 #if scanMode == scanRow_powrCol
238 #if scanMode == scanDual