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