1 /* Copyright (C) 2014-2015 by Jacob Alexander
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.
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.
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/>.
19 // ----- Includes -----
21 // KLL Generated Defines
26 #include <scan_loop.h>
28 #include <output_com.h>
30 // USB HID Keymap list
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;
48 #error "Invalid StateWordSize, possible values: 32, 16 and 8."
52 // Native pointer length
53 // This needs to be defined per microcontroller
54 // e.g. mk20s -> 32 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;
64 // ----- Structs -----
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
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
74 // ResultMacro.guide -> [<combo length>|<capability index>|<arg1>|<argn>|<capability index>|...|<combo length>|...|0]
76 // ResultMacroRecord.pos -> <current combo position>
77 // ResultMacroRecord.state -> <last key state>
78 // ResultMacroRecord.stateType -> <last key state type>
80 // ResultMacro struct, one is created per ResultMacro, no duplicates
81 typedef struct ResultMacro {
85 typedef struct ResultMacroRecord {
92 #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount
93 typedef struct ResultGuide {
95 uint8_t args; // This is used as an array pointer (but for packing purposes, must be 8 bit)
101 // Defines the sequence of combinations to Trigger a Result Macro
102 // For RAM optimization reasons TriggerMacro has been split into TriggerMacro and TriggerMacroRecord
104 // * 0x00 Normal (Press/Hold/Release)
105 // * 0x01 LED State (On/Off)
106 // * 0x02 Analog (Threshold)
107 // * 0x03-0xFE Reserved
108 // * 0xFF Debug State
111 // * Off - 0x00 (all flag states)
113 // * Press/Hold/Release - 0x01/0x02/0x03
114 // * Threshold (Range) - 0x01 (Released), 0x10 (Light press), 0xFF (Max press)
115 // * Debug - 0xFF (Print capability name)
117 // Combo Length of 0 signifies end of sequence
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>
122 // TriggerMacroRecord.pos -> <current combo position>
123 // TriggerMacroRecord.state -> <status of the macro pos>
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
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;
138 typedef struct TriggerMacroRecord {
140 TriggerMacroState state;
141 } TriggerMacroRecord;
143 // Guide, key element
144 #define TriggerGuideSize sizeof( TriggerGuide )
145 typedef struct TriggerGuide {
153 // ----- Capabilities -----
156 typedef struct Capability {
158 const uint8_t argCount;
161 // Total Number of Capabilities
162 #define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
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 }
178 // -- Result Macro List
180 // Total number of result macros (rm's)
181 // Used to create pending rm's table
182 #define ResultMacroNum sizeof( ResultMacroList ) / sizeof( ResultMacro )
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 }
198 // -- Trigger Macro List
200 // Total number of trigger macros (tm's)
201 // Used to create pending tm's table
202 #define TriggerMacroNum sizeof( TriggerMacroList ) / sizeof( TriggerMacro )
206 // ----- Trigger Maps -----
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[]
216 // ----- Layer Index -----
218 // Defines each map of trigger macro lists
219 // Layer 0 is always the default map
225 // Layer states are stored in the LayerState array
227 // Except for Off, all states an exist simultaneously for each layer
229 // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
231 // First defines the first used scan code (most keyboards start at 0, some start higher e.g. 0x40)
232 // - Compiler calculates this
234 // Last defines the last scan code used (helps reduce RAM usage)
236 // The name is defined for cli debugging purposes (Null terminated string)
238 typedef struct Layer {
239 const nat_ptr_t **triggerMap;
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 }
251 // Total number of layers
252 #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )