]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/output_com.c
Major code cleanup and preparation for PartialMap Macro Module
[kiibohd-controller.git] / Output / pjrcUSB / output_com.c
1 /* Copyright (C) 2011-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/OutputLib.h>
26
27 // Project Includes
28 #include <cli.h>
29 #include <print.h>
30 #include <scan_loop.h>
31
32 // USB Includes
33 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
34 #include "avr/usb_keyboard_serial.h"
35 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
36 #include "arm/usb_keyboard.h"
37 #include "arm/usb_dev.h"
38 #endif
39
40 // Local Includes
41 #include "output_com.h"
42
43
44
45 // ----- Function Declarations -----
46
47 void cliFunc_readLEDs  ( char* args );
48 void cliFunc_sendKeys  ( char* args );
49 void cliFunc_setKeys   ( char* args );
50 void cliFunc_setLEDs   ( char* args );
51 void cliFunc_setMod    ( char* args );
52
53
54 // ----- Variables -----
55
56 // Output Module command dictionary
57 char*       outputCLIDictName = "USB Module Commands";
58 CLIDictItem outputCLIDict[] = {
59         { "readLEDs", "Read LED byte. See \033[35msetLEDs\033[0m.", cliFunc_readLEDs },
60         { "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
61         { "setKeys",  "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
62         { "setLEDs",  "Set LED byte: 1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_setLEDs },
63         { "setMod",   "Set the modfier byte: 1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI", cliFunc_setMod },
64         { 0, 0, 0 } // Null entry for dictionary end
65 };
66
67
68 // Which modifier keys are currently pressed
69 // 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
70 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
71          uint8_t USBKeys_Modifiers    = 0;
72          uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
73
74 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
75          uint8_t USBKeys_Array   [USB_MAX_KEY_SEND];
76          uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
77
78 // The number of keys sent to the usb in the array
79          uint8_t USBKeys_Sent    = 0;
80          uint8_t USBKeys_SentCLI = 0;
81
82 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
83 volatile uint8_t USBKeys_LEDs = 0;
84
85 // protocol setting from the host.  We use exactly the same report
86 // either way, so this variable only stores the setting since we
87 // are required to be able to report which setting is in use.
88          uint8_t USBKeys_Protocol = 1;
89
90 // the idle configuration, how often we send the report to the
91 // host (ms * 4) even when it hasn't changed
92          uint8_t USBKeys_Idle_Config = 125;
93
94 // count until idle timeout
95          uint8_t USBKeys_Idle_Count = 0;
96
97
98 // ----- Functions -----
99
100 // USB Module Setup
101 inline void Output_setup()
102 {
103         // Initialize the USB, and then wait for the host to set configuration.
104         // If the Teensy is powered without a PC connected to the USB port,
105         // this will wait forever.
106         usb_init();
107         while ( !usb_configured() ) /* wait */ ;
108
109         // Register USB Output CLI dictionary
110         CLI_registerDictionary( outputCLIDict, outputCLIDictName );
111
112         // Wait an extra second for the PC's operating system to load drivers
113         // and do whatever it does to actually be ready for input
114         //_delay_ms(1000); // TODO (is this actually necessary?)
115 }
116
117
118 // USB Data Send
119 inline void Output_send(void)
120 {
121                 // TODO undo potentially old keys
122                 for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
123                         USBKeys_Array[c] = 0;
124
125                 // Send keypresses
126                 usb_keyboard_send();
127
128                 // Clear modifiers and keys
129                 USBKeys_Modifiers = 0;
130                 USBKeys_Sent      = 0;
131
132                 // Signal Scan Module we are finishedA
133                 Scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
134 }
135
136
137 // Sets the device into firmware reload mode
138 inline void Output_firmwareReload()
139 {
140 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
141         usb_debug_reload();
142 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
143         usb_device_reload();
144 #endif
145 }
146
147
148 // USB Input buffer available
149 inline unsigned int Output_availablechar()
150 {
151         return usb_serial_available();
152 }
153
154
155 // USB Get Character from input buffer
156 inline int Output_getchar()
157 {
158 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
159         // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes)
160         return (int)usb_serial_getchar();
161 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
162         return usb_serial_getchar();
163 #endif
164 }
165
166
167 // USB Send Character to output buffer
168 inline int Output_putchar( char c )
169 {
170         return usb_serial_putchar( c );
171 }
172
173
174 // USB Send String to output buffer, null terminated
175 inline int Output_putstr( char* str )
176 {
177 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
178         uint16_t count = 0;
179 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
180         uint32_t count = 0;
181 #endif
182         // Count characters until NULL character, then send the amount counted
183         while ( str[count] != '\0' )
184                 count++;
185
186         return usb_serial_write( str, count );
187 }
188
189
190 // Soft Chip Reset
191 inline void Output_softReset()
192 {
193 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
194         usb_debug_software_reset();
195 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
196         SOFTWARE_RESET();
197 #endif
198 }
199
200
201 // ----- CLI Command Functions -----
202
203 void cliFunc_readLEDs( char* args )
204 {
205         print( NL );
206         info_msg("LED State: ");
207         printInt8( USBKeys_LEDs );
208 }
209
210
211 void cliFunc_sendKeys( char* args )
212 {
213         // Copy USBKeys_ArrayCLI to USBKeys_Array
214         for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
215         {
216                 USBKeys_Array[key] = USBKeys_ArrayCLI[key];
217         }
218         USBKeys_Sent = USBKeys_SentCLI;
219
220         // Set modifier byte
221         USBKeys_Modifiers = USBKeys_ModifiersCLI;
222 }
223
224
225 void cliFunc_setKeys( char* args )
226 {
227         char* curArgs;
228         char* arg1Ptr;
229         char* arg2Ptr = args;
230
231         // Parse up to USBKeys_MaxSize args (whichever is least)
232         for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
233         {
234                 curArgs = arg2Ptr;
235                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
236
237                 // Stop processing args if no more are found
238                 if ( *arg1Ptr == '\0' )
239                         break;
240
241                 // Add the USB code to be sent
242                 USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
243         }
244 }
245
246
247 void cliFunc_setLEDs( char* args )
248 {
249         // Parse number from argument
250         //  NOTE: Only first argument is used
251         char* arg1Ptr;
252         char* arg2Ptr;
253         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
254
255         USBKeys_LEDs = decToInt( arg1Ptr );
256 }
257
258
259 void cliFunc_setMod( char* args )
260 {
261         // Parse number from argument
262         //  NOTE: Only first argument is used
263         char* arg1Ptr;
264         char* arg2Ptr;
265         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
266
267         USBKeys_ModifiersCLI = decToInt( arg1Ptr );
268 }
269