]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/PartialMap/kll.h
Code cleanup
[kiibohd-controller.git] / Macro / PartialMap / kll.h
1 /* Copyright (C) 2014-2015 by Jacob Alexander
2  *
3  * This file is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This file is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this file.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #pragma once
18
19 // ----- Includes -----
20
21 // KLL Generated Defines
22 #include <kll_defs.h>
23
24 // Project Includes
25 #include <print.h>
26 #include <scan_loop.h>
27 #include <macro.h>
28 #include <output_com.h>
29
30 // USB HID Keymap list
31 #include <usb_hid.h>
32
33
34
35 // ----- Types -----
36
37 // - NOTE -
38 // It is possible to change the maximum state and indexing positions of the state machine.
39 // This usually affects the SRAM usage quite a bit, so it can be used to fit the code on smaller uCs
40 // Or to allow for nearly infinite states.
41 #if StateWordSize_define == 32
42 typedef uint32_t var_uint_t;
43 #elif StateWordSize_define == 16
44 typedef uint16_t var_uint_t;
45 #elif StateWordSize_define == 8
46 typedef uint8_t  var_uint_t;
47 #else
48 #error "Invalid StateWordSize, possible values: 32, 16 and 8."
49 #endif
50
51 // - NOTE -
52 // Native pointer length
53 // This needs to be defined per microcontroller
54 // e.g. mk20s  -> 32 bit
55 //      atmega -> 16 bit
56 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
57 typedef uint32_t nat_ptr_t;
58 #elif defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
59 typedef uint16_t nat_ptr_t;
60 #endif
61
62
63
64 // ----- Structs -----
65
66 // -- Result Macro
67 // Defines the sequence of combinations to as the Result of Trigger Macro
68 // For RAM optimization reasons, ResultMacro has been split into ResultMacro and ResultMacroRecord structures
69 //
70 // Capability + args per USB send
71 // Default Args (always sent): key state/analog of last key
72 // Combo Length of 0 signifies end of sequence
73 //
74 // ResultMacro.guide -> [<combo length>|<capability index>|<arg1>|<argn>|<capability index>|...|<combo length>|...|0]
75 //
76 // ResultMacroRecord.pos       -> <current combo position>
77 // ResultMacroRecord.state     -> <last key state>
78 // ResultMacroRecord.stateType -> <last key state type>
79
80 // ResultMacro struct, one is created per ResultMacro, no duplicates
81 typedef struct ResultMacro {
82         const uint8_t *guide;
83 } ResultMacro;
84
85 typedef struct ResultMacroRecord {
86         var_uint_t pos;
87         uint8_t  state;
88         uint8_t  stateType;
89 } ResultMacroRecord;
90
91 // Guide, key element
92 #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount
93 typedef struct ResultGuide {
94         uint8_t index;
95         uint8_t args; // This is used as an array pointer (but for packing purposes, must be 8 bit)
96 } ResultGuide;
97
98
99
100 // -- Trigger Macro
101 // Defines the sequence of combinations to Trigger a Result Macro
102 // For RAM optimization reasons TriggerMacro has been split into TriggerMacro and TriggerMacroRecord
103 // Key Types:
104 //   * 0x00 Normal (Press/Hold/Release)
105 //   * 0x01 LED State (On/Off)
106 //   * 0x02 Analog (Threshold)
107 //   * 0x03-0xFE Reserved
108 //   * 0xFF Debug State
109 //
110 // Key State:
111 //   * Off                - 0x00 (all flag states)
112 //   * On                 - 0x01
113 //   * Press/Hold/Release - 0x01/0x02/0x03
114 //   * Threshold (Range)  - 0x01 (Released), 0x10 (Light press), 0xFF (Max press)
115 //   * Debug              - 0xFF (Print capability name)
116 //
117 // Combo Length of 0 signifies end of sequence
118 //
119 // TriggerMacro.guide  -> [<combo length>|<key1 type>|<key1 state>|<key1>...<keyn type>|<keyn state>|<keyn>|<combo length>...|0]
120 // TriggerMacro.result -> <index to result macro>
121 //
122 // TriggerMacroRecord.pos   -> <current combo position>
123 // TriggerMacroRecord.state -> <status of the macro pos>
124
125 // TriggerMacro states
126 typedef enum TriggerMacroState {
127         TriggerMacro_Press,   // Combo in sequence is passing
128         TriggerMacro_Release, // Move to next combo in sequence (or finish if at end of sequence)
129         TriggerMacro_Waiting, // Awaiting user input
130 } TriggerMacroState;
131
132 // TriggerMacro struct, one is created per TriggerMacro, no duplicates
133 typedef struct TriggerMacro {
134         const uint8_t *guide;
135         const var_uint_t result;
136 } TriggerMacro;
137
138 typedef struct TriggerMacroRecord {
139         var_uint_t pos;
140         TriggerMacroState state;
141 } TriggerMacroRecord;
142
143 // Guide, key element
144 #define TriggerGuideSize sizeof( TriggerGuide )
145 typedef struct TriggerGuide {
146         uint8_t type;
147         uint8_t state;
148         uint8_t scanCode;
149 } TriggerGuide;
150
151
152
153 // ----- Capabilities -----
154
155 // Capability
156 typedef struct Capability {
157         const void *func;
158         const uint8_t argCount;
159 } Capability;
160
161 // Total Number of Capabilities
162 #define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
163
164
165 // -- Result Macros
166
167 // Guide_RM / Define_RM Pair
168 // Guide_RM( index ) = result;
169 //  * index  - Result Macro index number
170 //  * result - Result Macro guide (see ResultMacro)
171 // Define_RM( index );
172 //  * index  - Result Macro index number
173 //  Must be used after Guide_RM
174 #define Guide_RM( index ) const uint8_t rm##index##_guide[]
175 #define Define_RM( index ) { rm##index##_guide }
176
177
178 // -- Result Macro List
179
180 // Total number of result macros (rm's)
181 // Used to create pending rm's table
182 #define ResultMacroNum sizeof( ResultMacroList ) / sizeof( ResultMacro )
183
184
185 // -- Trigger Macros
186
187 // Guide_TM / Define_TM Trigger Setup
188 // Guide_TM( index ) = trigger;
189 //  * index   - Trigger Macro index number
190 //  * trigger - Trigger Macro guide (see TriggerMacro)
191 // Define_TM( index, result );
192 //  * index   - Trigger Macro index number
193 //  * result  - Result Macro index number which is triggered by this Trigger Macro
194 #define Guide_TM( index ) const uint8_t tm##index##_guide[]
195 #define Define_TM( index, result ) { tm##index##_guide, result }
196
197
198 // -- Trigger Macro List
199
200 // Total number of trigger macros (tm's)
201 // Used to create pending tm's table
202 #define TriggerMacroNum sizeof( TriggerMacroList ) / sizeof( TriggerMacro )
203
204
205
206 // ----- Trigger Maps -----
207
208 // Define_TL( layer, scanCode ) = triggerList;
209 //  * layer       - basename of the layer
210 //  * scanCode    - Hex value of the scanCode
211 //  * triggerList - Trigger List (see Trigger Lists)
212 #define Define_TL( layer, scanCode ) const nat_ptr_t layer##_tl_##scanCode[]
213
214
215
216 // ----- Layer Index -----
217
218 // Defines each map of trigger macro lists
219 // Layer 0 is always the default map
220 // Layer States:
221 //   * Off   - 0x00
222 //   * Shift - 0x01
223 //   * Latch - 0x02
224 //   * Lock  - 0x04
225 // Layer states are stored in the LayerState array
226 //
227 // Except for Off, all states an exist simultaneously for each layer
228 // For example:
229 // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
230 //
231 // First defines the first used scan code (most keyboards start at 0, some start higher e.g. 0x40)
232 //  - Compiler calculates this
233 //
234 // Last defines the last scan code used (helps reduce RAM usage)
235 //
236 // The name is defined for cli debugging purposes (Null terminated string)
237
238 typedef struct Layer {
239         const nat_ptr_t **triggerMap;
240         const char *name;
241         const uint8_t first;
242         const uint8_t last;
243 } Layer;
244
245 // Layer_IN( map, name, first );
246 //  * map   - Trigger map
247 //  * name  - Name of the trigger map
248 //  * first - First scan code used (most keyboards start at 0, some start higher e.g. 0x40)
249 #define Layer_IN( map, name, first ) { map, name, first, sizeof( map ) / sizeof( nat_ptr_t ) - 1 + first }
250
251 // Total number of layers
252 #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )
253