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/>.
17 // ----- Includes -----
20 #include <Lib/MacroLib.h>
26 #include <scan_loop.h>
27 #include <output_com.h>
31 #include <defaultMap.h>
32 #include "generatedKeymap.h" // TODO Use actual generated version
39 // ----- Function Declarations -----
41 void cliFunc_capList ( char* args );
42 void cliFunc_capSelect ( char* args );
43 void cliFunc_lookComb ( char* args );
44 void cliFunc_lookDefault( char* args );
45 void cliFunc_lookPartial( char* args );
46 void cliFunc_macroDebug ( char* args );
50 // ----- Variables -----
52 // Macro Module command dictionary
53 char* macroCLIDictName = "Macro Module Commands (Not all commands fully work yet...)";
54 CLIDictItem macroCLIDict[] = {
55 { "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
56 { "capSelect", "Triggers the specified capability." NL "\t\t\033[35mU10\033[0m USB Code 0x0A, \033[35mK11\033[0m Keyboard Capability 0x0B, \033[35mS12\033[0m Scancode 0x0C", cliFunc_capSelect },
57 { "lookComb", "Do a lookup on the Combined map." NL "\t\t\033[35mS10\033[0m Scancode 0x0A, \033[35mU11\033[0m USB Code 0x0B", cliFunc_lookComb },
58 { "lookDefault", "Do a lookup on the Default map." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_lookDefault },
59 { "lookPartial", "Do a lookup on the layered Partial maps." NL "\t\t\033[35mS10\033[0m Scancode 0x0A, \033[35mU11\033[0m USB Code 0x0B", cliFunc_lookPartial },
60 { "macroDebug", "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
61 { 0, 0, 0 } // Null entry for dictionary end
65 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
66 uint8_t macroDebugMode = 0;
68 // Key Trigger List Buffer
69 // * Item 1: scan code
72 uint8_t macroTriggerListBuffer[0xFF * 2] = { 0 }; // Each key has a state to be cached (this can be decreased to save RAM)
73 uint8_t macroTriggerListBufferSize = 0;
75 // TODO, figure out a good way to scale this array size without wasting too much memory, but not rejecting macros
76 // Possibly could be calculated by the KLL compiler
77 TriggerMacro *triggerMacroPendingList[30];
81 // ----- Functions -----
83 // Looks up the trigger list for the given scan code (from the active layer)
84 unsigned int *Macro_layerLookup( uint8_t scanCode )
86 // TODO - No layer fallthrough lookup
87 return default_scanMap[ scanCode ];
91 // Update the scancode key state
97 // * 0x04 - Unpressed (this is currently ignored)
98 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
100 // Only add to macro trigger list if one of three states
103 case 0x01: // Pressed
105 case 0x03: // Released
106 macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = scanCode;
107 macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = state;
113 // Update the scancode analog state
117 // * 0x02-0xFF - Analog value (low to high)
118 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
129 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
135 // Evaluate/Update the TriggerMacro
136 void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
138 // Which combo in the sequence is being evaluated
139 unsigned int comboPos = triggerMacro->pos;
141 // If combo length is more than 1, cancel trigger macro if an incorrect key is found
142 uint8_t comboLength = triggerMacro->guide[ comboPos ];
144 // Iterate over list of keys currently pressed
145 for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed += 2 )
147 // Compare with keys in combo
148 for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
150 // Lookup key in combo
151 uint8_t guideKey = triggerMacro->guide[ comboPos + comboKey + 2 ]; // TODO Only Press/Hold/Release atm
154 if ( comboLength == 1 )
156 // If key matches and only 1 key pressed, increment the TriggerMacro combo position
157 if ( guideKey == macroTriggerListBuffer[ keyPressed ] && macroTriggerListBufferSize == 1 )
159 triggerMacro->pos += comboLength * 2 + 1;
160 // TODO check if TriggerMacro is finished, register ResultMacro
164 // If key does not match or more than 1 key pressed, reset the TriggerMacro combo position
165 triggerMacro->pos = 0;
181 inline void Macro_bufferAdd( uint8_t byte )
183 // Make sure we haven't overflowed the key buffer
184 // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
185 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
187 uint8_t key = DefaultMap_Lookup[byte];
188 for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
190 // Key already in the buffer
191 if ( KeyIndex_Buffer[c] == key )
196 KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
200 inline void Macro_bufferRemove( uint8_t byte )
202 uint8_t key = DefaultMap_Lookup[byte];
204 // Check for the released key, and shift the other keys lower on the buffer
205 for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
207 // Key to release found
208 if ( KeyIndex_Buffer[c] == key )
210 // Shift keys from c position
211 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
212 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
215 KeyIndex_BufferUsed--;
221 // Error case (no key to release)
222 erro_msg("Could not find key to release: ");
226 inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
230 inline void Macro_process()
232 // Only do one round of macro processing between Output Module timer sends
233 if ( USBKeys_Sent != 0 )
236 // Loop through macro trigger buffer
237 for ( uint8_t index = 0; index < macroTriggerListBufferSize; index += 2 )
239 // Get scanCode, first item of macroTriggerListBuffer pairs
240 uint8_t scanCode = macroTriggerListBuffer[ index ];
242 // Lookup trigger list for this key
243 unsigned int *triggerList = Macro_layerLookup( scanCode );
245 // The first element is the length of the trigger list
246 unsigned int triggerListSize = triggerList[0];
248 // Loop through the trigger list
249 for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
251 // Lookup TriggerMacro
252 TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
254 // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
255 uint8_t state = macroTriggerListBuffer[ index + 1 ];
258 Macro_evalTriggerMacro( triggerMacro );
267 // Loop through input buffer
268 for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
271 //printInt8( KeyIndex_BufferUsed );
272 // Get the keycode from the buffer
273 uint8_t key = KeyIndex_Buffer[index];
275 // Set the modifier bit if this key is a modifier
276 if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
278 USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
280 // Modifier processed, move on to the next key
285 if ( USBKeys_Sent >= USBKeys_MaxSize )
287 warn_msg("USB Key limit reached");
292 // Allow ignoring keys with 0's
295 USBKeys_Array[USBKeys_Sent++] = key;
299 // Key was not mapped
300 erro_msg( "Key not mapped... - " );
307 // Signal buffer that we've used it
308 Scan_finishedWithBuffer( KeyIndex_BufferUsed );
310 // If Macro debug mode is set, clear the USB Buffer
311 if ( macroDebugMode )
313 USBKeys_Modifiers = 0;
318 inline void Macro_setup()
320 // Register Macro CLI dictionary
321 CLI_registerDictionary( macroCLIDict, macroCLIDictName );
323 // Disable Macro debug mode
326 // Make sure macro trigger buffer is empty
327 macroTriggerListBufferSize = 0;
331 // ----- CLI Command Functions -----
333 void cliFunc_capList( char* args )
338 void cliFunc_capSelect( char* args )
340 // Parse code from argument
341 // NOTE: Only first argument is used
344 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
346 // Depending on the first character, the lookup changes
347 switch ( arg1Ptr[0] )
349 // Keyboard Capability
356 // Add to the USB Buffer using the DefaultMap lookup
357 Macro_bufferAdd( decToInt( &arg1Ptr[1] ) );
362 // Just add the key to the USB Buffer
363 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
365 KeyIndex_Buffer[KeyIndex_BufferUsed++] = decToInt( &arg1Ptr[1] );
371 void cliFunc_lookComb( char* args )
373 // Parse code from argument
374 // NOTE: Only first argument is used
377 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
379 // Depending on the first character, the lookup changes
380 switch ( arg1Ptr[0] )
394 void cliFunc_lookDefault( char* args )
396 // Parse code from argument
397 // NOTE: Only first argument is used
400 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
402 // Depending on the first character, the lookup changes
403 switch ( arg1Ptr[0] )
408 printInt8( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
410 printHex( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
415 void cliFunc_lookPartial( char* args )
417 // Parse code from argument
418 // NOTE: Only first argument is used
421 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
423 // Depending on the first character, the lookup changes
424 switch ( arg1Ptr[0] )
438 void cliFunc_macroDebug( char* args )
440 // Toggle macro debug mode
441 macroDebugMode = macroDebugMode ? 0 : 1;
444 info_msg("Macro Debug Mode: ");
445 printInt8( macroDebugMode );