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