]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/MatrixARM/matrix_scan.h
13839a192d88edd69efa29014ca8c122d88583e7
[kiibohd-controller.git] / Scan / MatrixARM / matrix_scan.h
1 /* Copyright (C) 2014-2015 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 #ifndef __MATRIX_SCAN_H
23 #define __MATRIX_SCAN_H
24
25 // ----- Includes -----
26
27 // KLL Generated Defines
28 #include <kll_defs.h>
29
30
31
32 // ----- Defines -----
33
34 #if   ( DebounceDivThreshold_define < 0xFF + 1 )
35 #define DebounceCounter uint8_t
36 #elif ( DebounceDivThreshold_define < 0xFFFF + 1 )
37 #define DebounceCounter uint16_t
38 #elif ( DebounceDivThreshold_define < 0xFFFFFFFF + 1 )
39 #define DebounceCounter uint32_t
40 #else
41 #error "Debounce threshold is too high... 32 bit max. Check .kll defines."
42 #endif
43
44
45
46 // ----- Enums -----
47
48 // Freescale MK20s have GPIO ports A...E
49 typedef enum Port {
50         Port_A = 0,
51         Port_B = 1,
52         Port_C = 2,
53         Port_D = 3,
54         Port_E = 4,
55 } Port;
56
57 // Each port has a possible 32 pins
58 typedef enum Pin {
59         Pin_0  = 0,
60         Pin_1  = 1,
61         Pin_2  = 2,
62         Pin_3  = 3,
63         Pin_4  = 4,
64         Pin_5  = 5,
65         Pin_6  = 6,
66         Pin_7  = 7,
67         Pin_8  = 8,
68         Pin_9  = 9,
69         Pin_10 = 10,
70         Pin_11 = 11,
71         Pin_12 = 12,
72         Pin_13 = 13,
73         Pin_14 = 14,
74         Pin_15 = 15,
75         Pin_16 = 16,
76         Pin_17 = 17,
77         Pin_18 = 18,
78         Pin_19 = 19,
79         Pin_20 = 20,
80         Pin_21 = 21,
81         Pin_22 = 22,
82         Pin_23 = 23,
83         Pin_24 = 24,
84         Pin_25 = 25,
85         Pin_26 = 26,
86         Pin_27 = 27,
87         Pin_28 = 28,
88         Pin_29 = 29,
89         Pin_30 = 30,
90         Pin_31 = 31,
91 } Pin;
92
93 // Type of pin
94 typedef enum Type {
95         Type_StrobeOn,
96         Type_StrobeOff,
97         Type_StrobeSetup,
98         Type_Sense,
99         Type_SenseSetup,
100 } Type;
101
102 // Sense/Strobe configuration
103 typedef enum Config {
104         Config_Pullup,    // Internal pull-up
105         Config_Pulldown,  // Internal pull-down
106         Config_Opendrain, // External pull resistor
107 } Config;
108
109 // Keypress States
110 typedef enum KeyPosition {
111         KeyState_Off     = 0,
112         KeyState_Press   = 1,
113         KeyState_Hold    = 2,
114         KeyState_Release = 3,
115         KeyState_Invalid,
116 } KeyPosition;
117
118
119
120 // ----- Structs -----
121
122 // Struct container for defining Rows (Sense) and Columns (Strobes)
123 typedef struct GPIO_Pin {
124         Port port;
125         Pin  pin;
126 } GPIO_Pin;
127
128 // Debounce Element
129 typedef struct KeyState {
130         KeyPosition     prevState;
131         KeyPosition     curState;
132         DebounceCounter activeCount;
133         DebounceCounter inactiveCount;
134 } KeyState;
135
136
137
138 // ----- Functions -----
139
140 void Matrix_setup();
141 void Matrix_scan( uint16_t scanNum );
142
143
144 #endif // __MATRIX_SCAN_H
145