]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/matrix/matrix_scan.c
Basic matrix module for the hall effect keypad now working.
[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
27 // Project Includes
28 #include <print.h>
29
30 // Local Includes
31 #include "matrix_scan.h"
32
33 // Matrix Configuration
34 #include <matrix.h>
35
36
37
38 // ----- Macros -----
39
40 #define REG_SET(reg)    reg |= (1 << ( matrix[row*(MAX_ROW_SIZE+1)+col] % 10 ) )
41                         
42 #define PIN_SET_COL(pin) \
43                         switch ( scanMode ) { \
44                         case scanCol: \
45                         case scanCol_powrRow: \
46                         case scanDual: \
47                                 REG_SET(port##pin); break; \
48                         case scanRow_powrCol: REG_SET(ddr##pin); REG_SET(port##pin); break; \
49                         } \
50                         break
51
52 #define PIN_SET_ROW(pin) \
53                         switch ( scanMode ) { \
54                         case scanRow: \
55                         case scanRow_powrCol: \
56                         case scanDual: \
57                                 REG_SET(port##pin); break; \
58                         case scanCol_powrRow: REG_SET(ddr##pin); REG_SET(port##pin); break; \
59                         } \
60                         break
61
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
71
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]]++; \
75                         break
76
77
78
79 // ----- Variables -----
80
81
82
83 // ----- Functions -----
84
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 )
87 {
88         // Setup the variables
89         uint8_t portA = 0x00;
90         uint8_t portB = 0x00;
91         uint8_t portC = 0x00;
92         uint8_t portD = 0x00;
93         uint8_t portE = 0x00;
94         uint8_t portF = 0x00;
95
96         uint8_t ddrA = 0x00;
97         uint8_t ddrB = 0x00;
98         uint8_t ddrC = 0x00;
99         uint8_t ddrD = 0x00;
100         uint8_t ddrE = 0x00;
101         uint8_t ddrF = 0x00;
102
103         // Loop through all the pin assignments, for the initial pin settings
104         uint16_t row, col;
105
106         // Rows
107         for ( col = 0, row = 1; row < MAX_COL_SIZE + 1; row++ )
108         {
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] )
111                 {
112                 PIN_CASE(A):
113                         PIN_SET_ROW(A);
114                 PIN_CASE(B):
115                         PIN_SET_ROW(B);
116                 PIN_CASE(C):
117                         PIN_SET_ROW(C);
118                 PIN_CASE(D):
119                         PIN_SET_ROW(D);
120                 PIN_CASE(E):
121                         PIN_SET_ROW(E);
122                 PIN_CASE(F):
123                         PIN_SET_ROW(F);
124
125                 default:
126                         continue;
127                 }
128         }
129
130         // Columns
131         for ( col = 1, row = 0; col < (MAX_ROW_SIZE+1) + 1; col++ )
132         {
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] )
135                 {
136                 PIN_CASE(A):
137                         PIN_SET_COL(A);
138                 PIN_CASE(B):
139                         PIN_SET_COL(B);
140                 PIN_CASE(C):
141                         PIN_SET_COL(C);
142                 PIN_CASE(D):
143                         PIN_SET_COL(D);
144                 PIN_CASE(E):
145                         PIN_SET_COL(E);
146                 PIN_CASE(F):
147                         PIN_SET_COL(F);
148
149                 default:
150                         continue;
151                 }
152         }
153
154         // Pin Status
155         char tmpStr[6];
156         info_print("Initial Matrix Pin Setup");
157         info_print(" ddrA  ddrB  ddrC  ddrD  ddrE  ddrF");
158         print("      ");
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 );
165         print("\n");
166         info_print("portA portB portC portD portE portF");
167         print("      ");
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 );
174         print("\n");
175
176         // Setting the pins
177 #if defined(__AVR_AT90USB1286__)
178         DDRA = ddrA;
179 #endif
180         DDRB = ddrB;
181         DDRC = ddrC;
182         DDRD = ddrD;
183         DDRE = ddrE;
184         DDRF = ddrF;
185
186 #if defined(__AVR_AT90USB1286__)
187         PORTA = portA;
188 #endif
189         PORTB = portB;
190         PORTC = portC;
191         PORTD = portD;
192         PORTE = portE;
193         PORTF = portF;
194 }
195
196 // TODO Proper matrix scanning
197 inline void matrix_scan( uint8_t *matrix, uint8_t *detectArray )
198 {
199         // Column Scan
200 #if scanMode == scanCol
201         uint16_t col = 1;
202         uint16_t row = 1;
203         for ( ; col < (MAX_ROW_SIZE+1) + 1; col++ ) 
204         {
205                 switch ( matrix[0*(MAX_ROW_SIZE+1)+col] / 10 )
206                 {
207 #if defined(__AVR_AT90USB1286__)
208                 case 0: // PINA
209                         PIN_TEST_COL(PINA);
210 #endif
211                 case 1: // PINB
212                         PIN_TEST_COL(PINB);
213                 case 2: // PINC
214                         PIN_TEST_COL(PINC);
215                 case 3: // PIND
216                         PIN_TEST_COL(PIND);
217                 case 4: // PINE
218                         PIN_TEST_COL(PINE);
219                 case 5: // PINF
220                         PIN_TEST_COL(PINF);
221                 }
222         }
223 #endif
224
225         // Row Scan
226 #if scanMode == scanRow
227 #endif
228
229         // Column Scan, Power Row
230 #if scanMode == scanCol_powrRow
231 #endif
232
233         // Row Scan, Power Column
234 #if scanMode == scanRow_powrCol
235 #endif
236
237         // Dual Scan
238 #if scanMode == scanDual
239 #endif
240 }
241