]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/PartialMap/macro.c
CLI Formatting cleanup.
[kiibohd-controller.git] / Macro / PartialMap / macro.c
1 /* Copyright (C) 2014 by Jacob Alexander
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <Lib/MacroLib.h>
26
27 // Project Includes
28 #include <cli.h>
29 #include <led.h>
30 #include <print.h>
31 #include <scan_loop.h>
32 #include <output_com.h>
33
34 // Keymaps
35 #include "usb_hid.h"
36 #include <defaultMap.h>
37
38 // Local Includes
39 #include "macro.h"
40
41
42
43 // ----- Function Declarations -----
44
45 void cliFunc_capList    ( char* args );
46 void cliFunc_capSelect  ( char* args );
47 void cliFunc_lookComb   ( char* args );
48 void cliFunc_lookDefault( char* args );
49 void cliFunc_lookPartial( char* args );
50 void cliFunc_macroDebug ( char* args );
51
52
53
54 // ----- Variables -----
55
56 // Macro Module command dictionary
57 char*       macroCLIDictName = "Macro Module Commands (Not all commands fully work yet...)";
58 CLIDictItem macroCLIDict[] = {
59         { "capList",     "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
60         { "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 },
61         { "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 },
62         { "lookDefault", "Do a lookup on the Default map." NL "\t\t\033[35mS10\033[0m Scancode 0x0A", cliFunc_lookDefault },
63         { "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 },
64         { "macroDebug",  "Disables/Enables sending USB keycodes to the Output Module and prints U/K codes.", cliFunc_macroDebug },
65         { 0, 0, 0 } // Null entry for dictionary end
66 };
67
68
69 // Macro debug flag - If set, clears the USB Buffers after signalling processing completion
70 uint8_t macroDebugMode = 0;
71
72
73
74 // ----- Functions -----
75
76 inline void Macro_bufferAdd( uint8_t byte )
77 {
78         // Make sure we haven't overflowed the key buffer
79         // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
80         if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
81         {
82                 KeyIndex_Buffer[KeyIndex_BufferUsed++] = DefaultMap_Lookup[byte];
83         }
84 }
85
86 inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
87 {
88 }
89
90 inline void Macro_process()
91 {
92         // Only do one round of macro processing between Output Module timer sends
93         if ( USBKeys_Sent != 0 )
94                 return;
95
96         // Loop through input buffer
97         for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
98         {
99                 // Get the keycode from the buffer
100                 uint8_t key = KeyIndex_Buffer[index];
101
102                 // Set the modifier bit if this key is a modifier
103                 if ( key & KEY_LCTRL ) // AND with 0xE0
104                 {
105                         USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
106
107                         // Modifier processed, move on to the next key
108                         continue;
109                 }
110
111                 // Too many keys
112                 if ( USBKeys_Sent >= USBKeys_MaxSize )
113                 {
114                         warn_msg("USB Key limit reached");
115                         errorLED( 1 );
116                         break;
117                 }
118
119                 // Allow ignoring keys with 0's
120                 if ( key != 0 )
121                 {
122                         USBKeys_Array[USBKeys_Sent++] = key;
123                 }
124                 else
125                 {
126                         // Key was not mapped
127                         erro_msg( "Key not mapped... - " );
128                         printHex( key );
129                         errorLED( 1 );
130                 }
131         }
132
133         // Signal buffer that we've used it
134         Scan_finishedWithBuffer( KeyIndex_BufferUsed );
135
136         // If Macro debug mode is set, clear the USB Buffer
137         if ( macroDebugMode )
138         {
139                 USBKeys_Modifiers = 0;
140                 USBKeys_Sent = 0;
141         }
142 }
143
144 inline void Macro_setup()
145 {
146         // Register Macro CLI dictionary
147         CLI_registerDictionary( macroCLIDict, macroCLIDictName );
148
149         // Disable Macro debug mode
150         macroDebugMode = 0;
151 }
152
153
154 // ----- CLI Command Functions -----
155
156 void cliFunc_capList( char* args )
157 {
158         // TODO
159 }
160
161 void cliFunc_capSelect( char* args )
162 {
163         // Parse code from argument
164         //  NOTE: Only first argument is used
165         char* arg1Ptr;
166         char* arg2Ptr;
167         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
168
169         // Depending on the first character, the lookup changes
170         switch ( arg1Ptr[0] )
171         {
172         // Keyboard Capability
173         case 'K':
174                 // TODO
175                 break;
176
177         // Scancode
178         case 'S':
179                 // Add to the USB Buffer using the DefaultMap lookup
180                 Macro_bufferAdd( decToInt( &arg1Ptr[1] ) );
181                 break;
182
183         // USB Code
184         case 'U':
185                 // Just add the key to the USB Buffer
186                 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
187                 {
188                         KeyIndex_Buffer[KeyIndex_BufferUsed++] = decToInt( &arg1Ptr[1] );
189                 }
190                 break;
191         }
192 }
193
194 void cliFunc_lookComb( char* args )
195 {
196         // Parse code from argument
197         //  NOTE: Only first argument is used
198         char* arg1Ptr;
199         char* arg2Ptr;
200         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
201
202         // Depending on the first character, the lookup changes
203         switch ( arg1Ptr[0] )
204         {
205         // Scancode
206         case 'S':
207                 // TODO
208                 break;
209
210         // USB Code
211         case 'U':
212                 // TODO
213                 break;
214         }
215 }
216
217 void cliFunc_lookDefault( char* args )
218 {
219         // Parse code from argument
220         //  NOTE: Only first argument is used
221         char* arg1Ptr;
222         char* arg2Ptr;
223         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
224
225         // Depending on the first character, the lookup changes
226         switch ( arg1Ptr[0] )
227         {
228         // Scancode
229         case 'S':
230                 print( NL );
231                 printInt8( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
232                 print(" ");
233                 printHex( DefaultMap_Lookup[decToInt( &arg1Ptr[1] )] );
234                 break;
235         }
236 }
237
238 void cliFunc_lookPartial( char* args )
239 {
240         // Parse code from argument
241         //  NOTE: Only first argument is used
242         char* arg1Ptr;
243         char* arg2Ptr;
244         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
245
246         // Depending on the first character, the lookup changes
247         switch ( arg1Ptr[0] )
248         {
249         // Scancode
250         case 'S':
251                 // TODO
252                 break;
253
254         // USB Code
255         case 'U':
256                 // TODO
257                 break;
258         }
259 }
260
261 void cliFunc_macroDebug( char* args )
262 {
263         // Toggle macro debug mode
264         macroDebugMode = macroDebugMode ? 0 : 1;
265
266         print( NL );
267         info_msg("Macro Debug Mode: ");
268         printInt8( macroDebugMode );
269 }
270