]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/PartialMap/macro.c
Initial work for partial layers and macros.
[kiibohd-controller.git] / Macro / PartialMap / macro.c
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 // ----- Includes -----
18
19 // Compiler Includes
20 #include <Lib/MacroLib.h>
21
22 // Project Includes
23 #include <cli.h>
24 #include <led.h>
25 #include <print.h>
26 #include <scan_loop.h>
27 #include <output_com.h>
28
29 // Keymaps
30 #include "usb_hid.h"
31 #include <defaultMap.h>
32 #include "generatedKeymap.h" // TODO Use actual generated version
33
34 // Local Includes
35 #include "macro.h"
36
37
38
39 // ----- Function Declarations -----
40
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 );
47
48
49
50 // ----- Variables -----
51
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
62 };
63
64
65 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
66 uint8_t macroDebugMode = 0;
67
68 // Key Trigger List Buffer
69 //  * Item 1: scan code
70 //  * Item 2: state
71 //    ...
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;
74
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];
78
79
80
81 // ----- Functions -----
82
83 // Looks up the trigger list for the given scan code (from the active layer)
84 unsigned int *Macro_layerLookup( uint8_t scanCode )
85 {
86         // TODO - No layer fallthrough lookup
87         return default_scanMap[ scanCode ];
88 }
89
90
91 // Update the scancode key state
92 // States:
93 //   * 0x00 - Reserved
94 //   * 0x01 - Pressed
95 //   * 0x02 - Held
96 //   * 0x03 - Released
97 //   * 0x04 - Unpressed (this is currently ignored)
98 inline void Macro_keyState( uint8_t scanCode, uint8_t state )
99 {
100         // Only add to macro trigger list if one of three states
101         switch ( state )
102         {
103         case 0x01: // Pressed
104         case 0x02: // Held
105         case 0x03: // Released
106                 macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = scanCode;
107                 macroTriggerListBuffer[ macroTriggerListBufferSize++ ] = state;
108                 break;
109         }
110 }
111
112
113 // Update the scancode analog state
114 // States:
115 //   * 0x00      - Reserved
116 //   * 0x01      - Released
117 //   * 0x02-0xFF - Analog value (low to high)
118 inline void Macro_analogState( uint8_t scanCode, uint8_t state )
119 {
120         // TODO
121 }
122
123
124 // Update led state
125 // States:
126 //   * 0x00 - Reserved
127 //   * 0x01 - On
128 //   * 0x02 - Off
129 inline void Macro_ledState( uint8_t ledCode, uint8_t state )
130 {
131         // TODO
132 }
133
134
135 // Evaluate/Update the TriggerMacro
136 void Macro_evalTriggerMacro( TriggerMacro *triggerMacro )
137 {
138         // Which combo in the sequence is being evaluated
139         unsigned int comboPos = triggerMacro->pos;
140
141         // If combo length is more than 1, cancel trigger macro if an incorrect key is found
142         uint8_t comboLength = triggerMacro->guide[ comboPos ];
143
144         // Iterate over list of keys currently pressed
145         for ( uint8_t keyPressed = 0; keyPressed < macroTriggerListBufferSize; keyPressed += 2 )
146         {
147                 // Compare with keys in combo
148                 for ( unsigned int comboKey = 0; comboKey < comboLength; comboKey++ )
149                 {
150                         // Lookup key in combo
151                         uint8_t guideKey = triggerMacro->guide[ comboPos + comboKey + 2 ]; // TODO Only Press/Hold/Release atm
152
153                         // Sequence Case
154                         if ( comboLength == 1 )
155                         {
156                                 // If key matches and only 1 key pressed, increment the TriggerMacro combo position
157                                 if ( guideKey == macroTriggerListBuffer[ keyPressed ] && macroTriggerListBufferSize == 1 )
158                                 {
159                                         triggerMacro->pos += comboLength * 2 + 1;
160                                         // TODO check if TriggerMacro is finished, register ResultMacro
161                                         return;
162                                 }
163
164                                 // If key does not match or more than 1 key pressed, reset the TriggerMacro combo position
165                                 triggerMacro->pos = 0;
166                                 return;
167                         }
168                         // Combo Case
169                         else
170                         {
171                                 // TODO
172                         }
173                 }
174         }
175 }
176
177
178
179
180
181 inline void Macro_bufferAdd( uint8_t byte )
182 {
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 )
186         {
187                 uint8_t key = DefaultMap_Lookup[byte];
188                 for ( uint8_t c = 0; c < KeyIndex_BufferUsed; c++ )
189                 {
190                         // Key already in the buffer
191                         if ( KeyIndex_Buffer[c] == key )
192                                 return;
193                 }
194
195                 // Add to the buffer
196                 KeyIndex_Buffer[KeyIndex_BufferUsed++] = key;
197         }
198 }
199
200 inline void Macro_bufferRemove( uint8_t byte )
201 {
202         uint8_t key = DefaultMap_Lookup[byte];
203
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++ )
206         {
207                 // Key to release found
208                 if ( KeyIndex_Buffer[c] == key )
209                 {
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];
213
214                         // Decrement Buffer
215                         KeyIndex_BufferUsed--;
216
217                         return;
218                 }
219         }
220
221         // Error case (no key to release)
222         erro_msg("Could not find key to release: ");
223         printHex( key );
224 }
225
226 inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
227 {
228 }
229
230 inline void Macro_process()
231 {
232         // Only do one round of macro processing between Output Module timer sends
233         if ( USBKeys_Sent != 0 )
234                 return;
235
236         // Loop through macro trigger buffer
237         for ( uint8_t index = 0; index < macroTriggerListBufferSize; index += 2 )
238         {
239                 // Get scanCode, first item of macroTriggerListBuffer pairs
240                 uint8_t scanCode = macroTriggerListBuffer[ index ];
241
242                 // Lookup trigger list for this key
243                 unsigned int *triggerList = Macro_layerLookup( scanCode );
244
245                 // The first element is the length of the trigger list
246                 unsigned int triggerListSize = triggerList[0];
247
248                 // Loop through the trigger list
249                 for ( unsigned int trigger = 0; trigger < triggerListSize; trigger++ )
250                 {
251                         // Lookup TriggerMacro
252                         TriggerMacro *triggerMacro = (TriggerMacro*)triggerList[ trigger + 1 ];
253
254                         // Get triggered state of scan code, second item of macroTriggerListBuffer pairs
255                         uint8_t state = macroTriggerListBuffer[ index + 1 ];
256
257                         // Evaluate Macro
258                         Macro_evalTriggerMacro( triggerMacro );
259                 }
260         }
261
262
263
264
265
266         /* TODO
267         // Loop through input buffer
268         for ( uint8_t index = 0; index < KeyIndex_BufferUsed && !macroDebugMode; index++ )
269         {
270                 //print(" KEYS: ");
271                 //printInt8( KeyIndex_BufferUsed );
272                 // Get the keycode from the buffer
273                 uint8_t key = KeyIndex_Buffer[index];
274
275                 // Set the modifier bit if this key is a modifier
276                 if ( (key & KEY_LCTRL) == KEY_LCTRL ) // AND with 0xE0
277                 {
278                         USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
279
280                         // Modifier processed, move on to the next key
281                         continue;
282                 }
283
284                 // Too many keys
285                 if ( USBKeys_Sent >= USBKeys_MaxSize )
286                 {
287                         warn_msg("USB Key limit reached");
288                         errorLED( 1 );
289                         break;
290                 }
291
292                 // Allow ignoring keys with 0's
293                 if ( key != 0 )
294                 {
295                         USBKeys_Array[USBKeys_Sent++] = key;
296                 }
297                 else
298                 {
299                         // Key was not mapped
300                         erro_msg( "Key not mapped... - " );
301                         printHex( key );
302                         errorLED( 1 );
303                 }
304         }
305         */
306
307         // Signal buffer that we've used it
308         Scan_finishedWithBuffer( KeyIndex_BufferUsed );
309
310         // If Macro debug mode is set, clear the USB Buffer
311         if ( macroDebugMode )
312         {
313                 USBKeys_Modifiers = 0;
314                 USBKeys_Sent = 0;
315         }
316 }
317
318 inline void Macro_setup()
319 {
320         // Register Macro CLI dictionary
321         CLI_registerDictionary( macroCLIDict, macroCLIDictName );
322
323         // Disable Macro debug mode
324         macroDebugMode = 0;
325
326         // Make sure macro trigger buffer is empty
327         macroTriggerListBufferSize = 0;
328 }
329
330
331 // ----- CLI Command Functions -----
332
333 void cliFunc_capList( char* args )
334 {
335         // TODO
336 }
337
338 void cliFunc_capSelect( char* args )
339 {
340         // Parse code from argument
341         //  NOTE: Only first argument is used
342         char* arg1Ptr;
343         char* arg2Ptr;
344         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
345
346         // Depending on the first character, the lookup changes
347         switch ( arg1Ptr[0] )
348         {
349         // Keyboard Capability
350         case 'K':
351                 // TODO
352                 break;
353
354         // Scancode
355         case 'S':
356                 // Add to the USB Buffer using the DefaultMap lookup
357                 Macro_bufferAdd( decToInt( &arg1Ptr[1] ) );
358                 break;
359
360         // USB Code
361         case 'U':
362                 // Just add the key to the USB Buffer
363                 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
364                 {
365                         KeyIndex_Buffer[KeyIndex_BufferUsed++] = decToInt( &arg1Ptr[1] );
366                 }
367                 break;
368         }
369 }
370
371 void cliFunc_lookComb( char* args )
372 {
373         // Parse code from argument
374         //  NOTE: Only first argument is used
375         char* arg1Ptr;
376         char* arg2Ptr;
377         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
378
379         // Depending on the first character, the lookup changes
380         switch ( arg1Ptr[0] )
381         {
382         // Scancode
383         case 'S':
384                 // TODO
385                 break;
386
387         // USB Code
388         case 'U':
389                 // TODO
390                 break;
391         }
392 }
393
394 void cliFunc_lookDefault( char* args )
395 {
396         // Parse code from argument
397         //  NOTE: Only first argument is used
398         char* arg1Ptr;
399         char* arg2Ptr;
400         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
401
402         // Depending on the first character, the lookup changes
403         switch ( arg1Ptr[0] )
404         {
405         // Scancode
406         case 'S':
407                 print( NL );
408                 printInt8( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
409                 print(" ");
410                 printHex( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
411                 break;
412         }
413 }
414
415 void cliFunc_lookPartial( char* args )
416 {
417         // Parse code from argument
418         //  NOTE: Only first argument is used
419         char* arg1Ptr;
420         char* arg2Ptr;
421         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
422
423         // Depending on the first character, the lookup changes
424         switch ( arg1Ptr[0] )
425         {
426         // Scancode
427         case 'S':
428                 // TODO
429                 break;
430
431         // USB Code
432         case 'U':
433                 // TODO
434                 break;
435         }
436 }
437
438 void cliFunc_macroDebug( char* args )
439 {
440         // Toggle macro debug mode
441         macroDebugMode = macroDebugMode ? 0 : 1;
442
443         print( NL );
444         info_msg("Macro Debug Mode: ");
445         printInt8( macroDebugMode );
446 }
447