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