]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/PartialMap/macro.c
Major code cleanup and preparation for PartialMap Macro Module
[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 // Output Module command dictionary
57 char*       macroCLIDictName = "Macro Module Commands";
58 CLIDictItem macroCLIDict[] = {
59         { "capList",     "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
60         { "capSelect",   "Triggers the specified capability. U10 is USB Code 0x0A (G). K11 is keyboard capability 0x0B.", cliFunc_capSelect },
61         { "lookComb",    "Do a lookup on the Combined map. S10 specifies Scancode 0x0A. U10 specified USB keycode 0x0A.", cliFunc_lookComb },
62         { "lookDefault", "Do a lookup on the Default map. S10 specifies Scancode 0x0A. USB keycodes are not valid.", cliFunc_lookDefault },
63         { "lookPartial", "Do a lookup on the layered partial map. S10 specifies Scancode 0x0A. U10 specifies USB keycode 0x0A.", 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
70 // ----- Functions -----
71
72 inline void Macro_bufferAdd( uint8_t byte )
73 {
74         // Make sure we haven't overflowed the key buffer
75         // Default function for adding keys to the KeyIndex_Buffer, does a DefaultMap_Lookup
76         if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER )
77         {
78                 KeyIndex_Buffer[KeyIndex_BufferUsed++] = DefaultMap_Lookup[byte];
79         }
80 }
81
82 inline void Macro_finishWithUSBBuffer( uint8_t sentKeys )
83 {
84 }
85
86 inline void Macro_process()
87 {
88         // Only do one round of macro processing between Output Module timer sends
89         if ( USBKeys_Sent != 0 )
90                 return;
91
92         // Loop through input buffer
93         for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
94         {
95                 // Get the keycode from the buffer
96                 uint8_t key = KeyIndex_Buffer[index];
97
98                 // Set the modifier bit if this key is a modifier
99                 if ( key & KEY_LCTRL ) // AND with 0xE0
100                 {
101                         USBKeys_Modifiers |= 1 << (key ^ KEY_LCTRL); // Left shift 1 by key XOR 0xE0
102
103                         // Modifier processed, move on to the next key
104                         continue;
105                 }
106
107                 // Too many keys
108                 if ( USBKeys_Sent >= USBKeys_MaxSize )
109                 {
110                         info_print("USB Key limit reached");
111                         errorLED( 1 );
112                         break;
113                 }
114
115                 // Allow ignoring keys with 0's
116                 if ( key != 0 )
117                 {
118                         USBKeys_Array[USBKeys_Sent++] = key;
119                 }
120                 else
121                 {
122                         // Key was not mapped
123                         // TODO Add dead key map
124                         erro_dPrint( "Key not mapped... - " );
125                         printHex( key );
126                         errorLED( 1 );
127                 }
128         }
129
130         // Signal buffer that we've used it
131         Scan_finishedWithBuffer( KeyIndex_BufferUsed );
132 }
133
134 inline void Macro_setup()
135 {
136         // Register Macro CLI dictionary
137         CLI_registerDictionary( macroCLIDict, macroCLIDictName );
138 }
139
140
141 // ----- CLI Command Functions -----
142
143 void cliFunc_capList( char* args )
144 {
145         // TODO
146 }
147
148 void cliFunc_capSelect( char* args )
149 {
150         // TODO
151 }
152
153 void cliFunc_lookComb( char* args )
154 {
155         // TODO
156 }
157
158 void cliFunc_lookDefault( char* args )
159 {
160         // TODO
161 }
162
163 void cliFunc_lookPartial( char* args )
164 {
165         // TODO
166 }
167
168 void cliFunc_macroDebug( char* args )
169 {
170         // TODO
171 }
172