1 /* Copyright (C) 2014 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/>.
20 // ----- Includes -----
24 #include <scan_loop.h>
26 #include <output_com.h>
28 // USB HID Keymap list
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;
45 // Native pointer length
46 // This needs to be defined per microcontroller
47 // e.g. mk20s -> 32 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;
57 // ----- Structs -----
60 // Defines the sequence of combinations to as the Result of Trigger Macro
62 // Capability + args per USB send
63 // Default Args (always sent): key state/analog of last key
64 // Combo Length of 0 signifies end of sequence
66 // ResultMacro.guide -> [<combo length>|<capability index>|<arg1>|<argn>|<capability index>|...|<combo length>|...|0]
67 // ResultMacro.pos -> <current combo position>
68 // ResultMacro.state -> <last key state>
69 // ResultMacro.stateType -> <last key state type>
71 // ResultMacro struct, one is created per ResultMacro, no duplicates
72 typedef struct ResultMacro {
80 #define ResultGuideSize( guidePtr ) sizeof( ResultGuide ) - 1 + CapabilitiesList[ (guidePtr)->index ].argCount
81 typedef struct ResultGuide {
83 uint8_t args; // This is used as an array pointer (but for packing purposes, must be 8 bit)
89 // Defines the sequence of combinations to Trigger a Result Macro
91 // * 0x00 Normal (Press/Hold/Release)
92 // * 0x01 LED State (On/Off)
93 // * 0x02 Analog (Threshold)
94 // * 0x03-0xFE Reserved
98 // * Off - 0x00 (all flag states)
100 // * Press/Hold/Release - 0x01/0x02/0x03
101 // * Threshold (Range) - 0x01 (Released), 0x10 (Light press), 0xFF (Max press)
102 // * Debug - 0xFF (Print capability name)
104 // Combo Length of 0 signifies end of sequence
106 // TriggerMacro.guide -> [<combo length>|<key1 type>|<key1 state>|<key1>...<keyn type>|<keyn state>|<keyn>|<combo length>...|0]
107 // TriggerMacro.result -> <index to result macro>
108 // TriggerMacro.pos -> <current combo position>
109 // TriggerMacro.state -> <status of the macro pos>
111 // TriggerMacro states
112 typedef enum TriggerMacroState {
113 TriggerMacro_Press, // Combo in sequence is passing
114 TriggerMacro_Release, // Move to next combo in sequence (or finish if at end of sequence)
115 TriggerMacro_Waiting, // Awaiting user input
118 // TriggerMacro struct, one is created per TriggerMacro, no duplicates
119 typedef struct TriggerMacro {
120 const uint8_t *guide;
121 const var_uint_t result;
123 TriggerMacroState state;
126 // Guide, key element
127 #define TriggerGuideSize sizeof( TriggerGuide )
128 typedef struct TriggerGuide {
136 // ----- Capabilities -----
139 typedef struct Capability {
141 const uint8_t argCount;
144 // Total Number of Capabilities
145 #define CapabilitiesNum sizeof( CapabilitiesList ) / sizeof( Capability )
150 // Guide_RM / Define_RM Pair
151 // Guide_RM( index ) = result;
152 // * index - Result Macro index number
153 // * result - Result Macro guide (see ResultMacro)
154 // Define_RM( index );
155 // * index - Result Macro index number
156 // Must be used after Guide_RM
157 #define Guide_RM( index ) const uint8_t rm##index##_guide[]
158 #define Define_RM( index ) { rm##index##_guide, 0, 0, 0 }
161 // -- Result Macro List
163 // Total number of result macros (rm's)
164 // Used to create pending rm's table
165 #define ResultMacroNum sizeof( ResultMacroList ) / sizeof( ResultMacro )
170 // Guide_TM / Define_TM Trigger Setup
171 // Guide_TM( index ) = trigger;
172 // * index - Trigger Macro index number
173 // * trigger - Trigger Macro guide (see TriggerMacro)
174 // Define_TM( index, result );
175 // * index - Trigger Macro index number
176 // * result - Result Macro index number which is triggered by this Trigger Macro
177 #define Guide_TM( index ) const uint8_t tm##index##_guide[]
178 #define Define_TM( index, result ) { tm##index##_guide, result, 0, TriggerMacro_Waiting }
181 // -- Trigger Macro List
183 // Total number of trigger macros (tm's)
184 // Used to create pending tm's table
185 #define TriggerMacroNum sizeof( TriggerMacroList ) / sizeof( TriggerMacro )
189 // ----- Trigger Maps -----
191 // Define_TL( layer, scanCode ) = triggerList;
192 // * layer - basename of the layer
193 // * scanCode - Hex value of the scanCode
194 // * triggerList - Trigger List (see Trigger Lists)
195 #define Define_TL( layer, scanCode ) const nat_ptr_t layer##_tl_##scanCode[]
199 // ----- Layer Index -----
201 // Defines each map of trigger macro lists
202 // Layer 0 is always the default map
208 // Layer states are stored in the LayerState array
210 // Except for Off, all states an exist simultaneously for each layer
212 // state -> 0x04 + 0x01 = 0x05 (Shift + Lock), which is effectively Off (0x00)
214 // First defines the first used scan code (most keyboards start at 0, some start higher e.g. 0x40)
215 // - Compiler calculates this
217 // Last defines the last scan code used (helps reduce RAM usage)
219 // The name is defined for cli debugging purposes (Null terminated string)
221 typedef struct Layer {
222 const nat_ptr_t **triggerMap;
228 // Layer_IN( map, name, first );
229 // * map - Trigger map
230 // * name - Name of the trigger map
231 // * first - First scan code used (most keyboards start at 0, some start higher e.g. 0x40)
232 #define Layer_IN( map, name, first ) { map, name, first, sizeof( map ) / sizeof( nat_ptr_t ) - 1 + first }
234 // Total number of layers
235 #define LayerNum sizeof( LayerIndex ) / sizeof( Layer )