]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/PartialMap/kll.h
Adding variable width state variable width.
[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 typedef uint32_t nat_ptr_t;
50 //typedef uint16_t nat_ptr_t;
51
52
53
54 // ----- Structs -----
55
56 // -- Result Macro
57 // Defines the sequence of combinations to as the Result of Trigger Macro
58 //
59 // Capability + args per USB send
60 // Default Args (always sent): key state/analog of last key
61 // Combo Length of 0 signifies end of sequence
62 //
63 // ResultMacro.guide     -> [<combo length>|<capability index>|<arg1>|<argn>|<capability index>|...|<combo length>|...|0]
64 // ResultMacro.pos       -> <current combo position>
65 // ResultMacro.state     -> <last key state>
66 // ResultMacro.stateType -> <last key state type>
67
68 // ResultMacro struct, one is created per ResultMacro, no duplicates
69 typedef struct ResultMacro {
70         const uint8_t *guide;
71         var_uint_t pos;
72         uint8_t  state;
73         uint8_t  stateType;
74 } ResultMacro;
75
76 // Guide, key element
77 #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount
78 typedef struct ResultGuide {
79         uint8_t index;
80         uint8_t args; // This is used as an array pointer (but for packing purposes, must be 8 bit)
81 } ResultGuide;
82
83
84
85 // -- Trigger Macro
86 // Defines the sequence of combinations to Trigger a Result Macro
87 // Key Types:
88 //   * 0x00 Normal (Press/Hold/Release)
89 //   * 0x01 LED State (On/Off)
90 //   * 0x02 Analog (Threshold)
91 //   * 0x03-0xFE Reserved
92 //   * 0xFF Debug State
93 //
94 // Key State:
95 //   * Off                - 0x00 (all flag states)
96 //   * On                 - 0x01
97 //   * Press/Hold/Release - 0x01/0x02/0x03
98 //   * Threshold (Range)  - 0x01 (Released), 0x10 (Light press), 0xFF (Max press)
99 //   * Debug              - 0xFF (Print capability name)
100 //
101 // Combo Length of 0 signifies end of sequence
102 //
103 // TriggerMacro.guide  -> [<combo length>|<key1 type>|<key1 state>|<key1>...<keyn type>|<keyn state>|<keyn>|<combo length>...|0]
104 // TriggerMacro.result -> <index to result macro>
105 // TriggerMacro.pos    -> <current combo position>
106 // TriggerMacro.state  -> <status of the macro pos>
107
108 // TriggerMacro states
109 typedef enum TriggerMacroState {
110         TriggerMacro_Press,   // Combo in sequence is passing
111         TriggerMacro_Release, // Move to next combo in sequence (or finish if at end of sequence)
112         TriggerMacro_Waiting, // Awaiting user input
113 } TriggerMacroState;
114
115 // TriggerMacro struct, one is created per TriggerMacro, no duplicates
116 typedef struct TriggerMacro {
117         const uint8_t *guide;
118         var_uint_t result;
119         var_uint_t pos;
120         TriggerMacroState state;
121 } TriggerMacro;
122
123 // Guide, key element
124 #define TriggerGuideSize sizeof( TriggerGuide )
125 typedef struct TriggerGuide {
126         uint8_t type;
127         uint8_t state;
128         uint8_t scanCode;
129 } TriggerGuide;
130
131
132
133 // ----- Capabilities -----
134
135 // Capability
136 typedef struct Capability {
137         void *func;
138         uint8_t argCount;
139 } Capability;
140
141 // Total Number of Capabilities
142 #define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
143
144
145 // -- Result Macros
146
147 // Guide_RM / Define_RM Pair
148 // Guide_RM( index ) = result;
149 //  * index  - Result Macro index number
150 //  * result - Result Macro guide (see ResultMacro)
151 // Define_RM( index );
152 //  * index  - Result Macro index number
153 //  Must be used after Guide_RM
154 #define Guide_RM( index ) const uint8_t rm##index##_guide[]
155 #define Define_RM( index ) { rm##index##_guide, 0, 0, 0 }
156
157
158 // -- Result Macro List
159
160 // Total number of result macros (rm's)
161 // Used to create pending rm's table
162 #define ResultMacroNum sizeof( ResultMacroList ) / sizeof( ResultMacro )
163
164
165 // -- Trigger Macros
166
167 // Guide_TM / Define_TM Trigger Setup
168 // Guide_TM( index ) = trigger;
169 //  * index   - Trigger Macro index number
170 //  * trigger - Trigger Macro guide (see TriggerMacro)
171 // Define_TM( index, result );
172 //  * index   - Trigger Macro index number
173 //  * result  - Result Macro index number which is triggered by this Trigger Macro
174 #define Guide_TM( index ) const uint8_t tm##index##_guide[]
175 #define Define_TM( index, result ) { tm##index##_guide, result, 0, TriggerMacro_Waiting }
176
177
178 // -- Trigger Macro List
179
180 // Total number of trigger macros (tm's)
181 // Used to create pending tm's table
182 #define TriggerMacroNum sizeof( TriggerMacroList ) / sizeof( TriggerMacro )
183
184
185
186 // ----- Trigger Maps -----
187
188 // Define_TL( layer, scanCode ) = triggerList;
189 //  * layer       - basename of the layer
190 //  * scanCode    - Hex value of the scanCode
191 //  * triggerList - Trigger List (see Trigger Lists)
192 #define Define_TL( layer, scanCode ) const nat_ptr_t layer##_tl_##scanCode[]
193
194
195
196 // ----- Layer Index -----
197
198 // Defines each map of trigger macro lists
199 // Layer 0 is always the default map
200 // Layer States:
201 //   * Off   - 0x00
202 //   * Shift - 0x01
203 //   * Latch - 0x02
204 //   * Lock  - 0x04
205 //
206 // Except for Off, all states an exist simultaneously for each layer
207 // For example:
208 // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
209 //
210 // Max defines the maximum number of keys in the map, maximum of 0xFF
211 //  - Compiler calculates this
212 //
213 // The name is defined for cli debugging purposes (Null terminated string)
214
215 typedef struct Layer {
216         const nat_ptr_t **triggerMap;
217         const char *name;
218         const uint8_t max;
219         uint8_t state;
220 } Layer;
221
222
223 // Layer_IN( map, name );
224 //  * map  - Trigger map
225 //  * name - Name of the trigger map
226 #define Layer_IN( map, name ) { map, name, sizeof( map ) / 4 - 1, 0 }
227
228 // Total number of layers
229 #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )
230
231
232
233 #endif // __kll_h
234