]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/usbMuxUart/output_com.c
Fixing default ErgoDox layout and adding FlashMode button
[kiibohd-controller.git] / Output / usbMuxUart / output_com.c
1 /* Copyright (C) 2014-2015 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 <led.h>
30 #include <print.h>
31 #include <scan_loop.h>
32
33 // USB Includes
34 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
35 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_)
36 #include <arm/uart_serial.h>
37 #include <arm/usb_dev.h>
38 #include <arm/usb_keyboard.h>
39 #include <arm/usb_serial.h>
40 #endif
41
42 // Local Includes
43 #include "output_com.h"
44
45
46
47 // ----- Macros -----
48
49 // Used to build a bitmap lookup table from a byte addressable array
50 #define byteLookup( byte ) case (( byte ) * ( 8 )):         bytePosition = byte; byteShift = 0; break; \
51                            case (( byte ) * ( 8 ) + ( 1 )): bytePosition = byte; byteShift = 1; break; \
52                            case (( byte ) * ( 8 ) + ( 2 )): bytePosition = byte; byteShift = 2; break; \
53                            case (( byte ) * ( 8 ) + ( 3 )): bytePosition = byte; byteShift = 3; break; \
54                            case (( byte ) * ( 8 ) + ( 4 )): bytePosition = byte; byteShift = 4; break; \
55                            case (( byte ) * ( 8 ) + ( 5 )): bytePosition = byte; byteShift = 5; break; \
56                            case (( byte ) * ( 8 ) + ( 6 )): bytePosition = byte; byteShift = 6; break; \
57                            case (( byte ) * ( 8 ) + ( 7 )): bytePosition = byte; byteShift = 7; break
58
59
60
61 // ----- Function Declarations -----
62
63 void cliFunc_kbdProtocol( char* args );
64 void cliFunc_outputDebug( char* args );
65 void cliFunc_readLEDs   ( char* args );
66 void cliFunc_readUART   ( char* args );
67 void cliFunc_sendKeys   ( char* args );
68 void cliFunc_sendUART   ( char* args );
69 void cliFunc_setKeys    ( char* args );
70 void cliFunc_setMod     ( char* args );
71
72
73
74 // ----- Variables -----
75
76 // Output Module command dictionary
77 CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode" );
78 CLIDict_Entry( outputDebug, "Toggle Output Debug mode." );
79 CLIDict_Entry( readLEDs,    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." );
80 CLIDict_Entry( readUART,    "Read UART buffer until empty." );
81 CLIDict_Entry( sendKeys,    "Send the prepared list of USB codes and modifier byte." );
82 CLIDict_Entry( sendUART,    "Send characters over UART0." );
83 CLIDict_Entry( setKeys,     "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m." );
84 CLIDict_Entry( setMod,      "Set the modfier byte:" NL "\t\t1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI" );
85
86 CLIDict_Def( outputCLIDict, "USB Module Commands" ) = {
87         CLIDict_Item( kbdProtocol ),
88         CLIDict_Item( outputDebug ),
89         CLIDict_Item( readLEDs ),
90         CLIDict_Item( readUART ),
91         CLIDict_Item( sendKeys ),
92         CLIDict_Item( sendUART ),
93         CLIDict_Item( setKeys ),
94         CLIDict_Item( setMod ),
95         { 0, 0, 0 } // Null entry for dictionary end
96 };
97
98
99 // Which modifier keys are currently pressed
100 // 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
101 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
102 uint8_t  USBKeys_Modifiers    = 0;
103 uint8_t  USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
104
105 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
106 uint8_t  USBKeys_Keys   [USB_NKRO_BITFIELD_SIZE_KEYS];
107 uint8_t  USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer
108
109 // System Control and Consumer Control 1KRO containers
110 uint8_t  USBKeys_SysCtrl;
111 uint16_t USBKeys_ConsCtrl;
112
113 // The number of keys sent to the usb in the array
114 uint8_t  USBKeys_Sent    = 0;
115 uint8_t  USBKeys_SentCLI = 0;
116
117 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
118 volatile uint8_t  USBKeys_LEDs = 0;
119
120 // Protocol setting from the host.
121 // 0 - Boot Mode
122 // 1 - NKRO Mode (Default, unless set by a BIOS or boot interface)
123 volatile uint8_t  USBKeys_Protocol = 1;
124
125 // Indicate if USB should send update
126 // OS only needs update if there has been a change in state
127 USBKeyChangeState USBKeys_Changed = USBKeyChangeState_None;
128
129 // the idle configuration, how often we send the report to the
130 // host (ms * 4) even when it hasn't changed
131 uint8_t  USBKeys_Idle_Config = 125;
132
133 // count until idle timeout
134 uint8_t  USBKeys_Idle_Count = 0;
135
136 // Indicates whether the Output module is fully functional
137 // 0 - Not fully functional, 1 - Fully functional
138 // 0 is often used to show that a USB cable is not plugged in (but has power)
139 volatile uint8_t  Output_Available = 0;
140
141 // Debug control variable for Output modules
142 // 0 - Debug disabled (default)
143 // 1 - Debug enabled
144 uint8_t  Output_DebugMode = 0;
145
146
147
148 // ----- Capabilities -----
149
150 // Set Boot Keyboard Protocol
151 void Output_kbdProtocolBoot_capability( uint8_t state, uint8_t stateType, uint8_t *args )
152 {
153         // Display capability name
154         if ( stateType == 0xFF && state == 0xFF )
155         {
156                 print("Output_kbdProtocolBoot()");
157                 return;
158         }
159
160         // Only set if necessary
161         if ( USBKeys_Protocol == 0 )
162                 return;
163
164         // TODO Analog inputs
165         // Only set on key press
166         if ( stateType != 0x01 )
167                 return;
168
169         // Flush the key buffers
170         Output_flushBuffers();
171
172         // Set the keyboard protocol to Boot Mode
173         USBKeys_Protocol = 0;
174 }
175
176
177 // Set NKRO Keyboard Protocol
178 void Output_kbdProtocolNKRO_capability( uint8_t state, uint8_t stateType, uint8_t *args )
179 {
180         // Display capability name
181         if ( stateType == 0xFF && state == 0xFF )
182         {
183                 print("Output_kbdProtocolNKRO()");
184                 return;
185         }
186
187         // Only set if necessary
188         if ( USBKeys_Protocol == 1 )
189                 return;
190
191         // TODO Analog inputs
192         // Only set on key press
193         if ( stateType != 0x01 )
194                 return;
195
196         // Flush the key buffers
197         Output_flushBuffers();
198
199         // Set the keyboard protocol to NKRO Mode
200         USBKeys_Protocol = 1;
201 }
202
203
204 // Sends a Consumer Control code to the USB Output buffer
205 void Output_consCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
206 {
207         // Display capability name
208         if ( stateType == 0xFF && state == 0xFF )
209         {
210                 print("Output_consCtrlSend(consCode)");
211                 return;
212         }
213
214         // Not implemented in Boot Mode
215         if ( USBKeys_Protocol == 0 )
216         {
217                 warn_print("Consumer Control is not implemented for Boot Mode");
218                 return;
219         }
220
221         // TODO Analog inputs
222         // Only indicate USB has changed if either a press or release has occured
223         if ( state == 0x01 || state == 0x03 )
224                 USBKeys_Changed |= USBKeyChangeState_Consumer;
225
226         // Only send keypresses if press or hold state
227         if ( stateType == 0x00 && state == 0x03 ) // Release state
228         {
229                 USBKeys_ConsCtrl = 0;
230                 return;
231         }
232
233         // Set consumer control code
234         USBKeys_ConsCtrl = *(uint16_t*)(&args[0]);
235 }
236
237
238 // Ignores the given key status update
239 // Used to prevent fall-through, this is the None keyword in KLL
240 void Output_noneSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
241 {
242         // Display capability name
243         if ( stateType == 0xFF && state == 0xFF )
244         {
245                 print("Output_noneSend()");
246                 return;
247         }
248
249         // Nothing to do, because that's the point :P
250 }
251
252
253 // Sends a System Control code to the USB Output buffer
254 void Output_sysCtrlSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
255 {
256         // Display capability name
257         if ( stateType == 0xFF && state == 0xFF )
258         {
259                 print("Output_sysCtrlSend(sysCode)");
260                 return;
261         }
262
263         // Not implemented in Boot Mode
264         if ( USBKeys_Protocol == 0 )
265         {
266                 warn_print("System Control is not implemented for Boot Mode");
267                 return;
268         }
269
270         // TODO Analog inputs
271         // Only indicate USB has changed if either a press or release has occured
272         if ( state == 0x01 || state == 0x03 )
273                 USBKeys_Changed |= USBKeyChangeState_System;
274
275         // Only send keypresses if press or hold state
276         if ( stateType == 0x00 && state == 0x03 ) // Release state
277         {
278                 USBKeys_SysCtrl = 0;
279                 return;
280         }
281
282         // Set system control code
283         USBKeys_SysCtrl = args[0];
284 }
285
286
287 // Adds a single USB Code to the USB Output buffer
288 // Argument #1: USB Code
289 void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
290 {
291         // Display capability name
292         if ( stateType == 0xFF && state == 0xFF )
293         {
294                 print("Output_usbCodeSend(usbCode)");
295                 return;
296         }
297
298         // Depending on which mode the keyboard is in the USB needs Press/Hold/Release events
299         uint8_t keyPress = 0; // Default to key release, only used for NKRO
300         switch ( USBKeys_Protocol )
301         {
302         case 0: // Boot Mode
303                 // TODO Analog inputs
304                 // Only indicate USB has changed if either a press or release has occured
305                 if ( state == 0x01 || state == 0x03 )
306                         USBKeys_Changed = USBKeyChangeState_MainKeys;
307
308                 // Only send keypresses if press or hold state
309                 if ( stateType == 0x00 && state == 0x03 ) // Release state
310                         return;
311                 break;
312         case 1: // NKRO Mode
313                 // Only send press and release events
314                 if ( stateType == 0x00 && state == 0x02 ) // Hold state
315                         return;
316
317                 // Determine if setting or unsetting the bitfield (press == set)
318                 if ( stateType == 0x00 && state == 0x01 ) // Press state
319                         keyPress = 1;
320                 break;
321         }
322
323         // Get the keycode from arguments
324         uint8_t key = args[0];
325
326         // Depending on which mode the keyboard is in, USBKeys_Keys array is used differently
327         // Boot mode - Maximum of 6 byte codes
328         // NKRO mode - Each bit of the 26 byte corresponds to a key
329         //  Bits   0 -  45 (bytes  0 -  5) correspond to USB Codes   4 -  49 (Main)
330         //  Bits  48 - 161 (bytes  6 - 20) correspond to USB Codes  51 - 164 (Secondary)
331         //  Bits 168 - 213 (bytes 21 - 26) correspond to USB Codes 176 - 221 (Tertiary)
332         //  Bits 214 - 216                 unused
333         uint8_t bytePosition = 0;
334         uint8_t byteShift = 0;
335         switch ( USBKeys_Protocol )
336         {
337         case 0: // Boot Mode
338                 // Set the modifier bit if this key is a modifier
339                 if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
340                 {
341                         USBKeys_Modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
342                 }
343                 // Normal USB Code
344                 else
345                 {
346                         // USB Key limit reached
347                         if ( USBKeys_Sent >= USB_BOOT_MAX_KEYS )
348                         {
349                                 warn_print("USB Key limit reached");
350                                 return;
351                         }
352
353                         // Make sure key is within the USB HID range
354                         if ( key <= 104 )
355                         {
356                                 USBKeys_Keys[USBKeys_Sent++] = key;
357                         }
358                         // Invalid key
359                         else
360                         {
361                                 warn_msg("USB Code above 104/0x68 in Boot Mode: ");
362                                 printHex( key );
363                                 print( NL );
364                         }
365                 }
366                 break;
367
368         case 1: // NKRO Mode
369                 // Set the modifier bit if this key is a modifier
370                 if ( (key & 0xE0) == 0xE0 ) // AND with 0xE0 (Left Ctrl, first modifier)
371                 {
372                         if ( keyPress )
373                         {
374                                 USBKeys_Modifiers |= 1 << (key ^ 0xE0); // Left shift 1 by key XOR 0xE0
375                         }
376                         else // Release
377                         {
378                                 USBKeys_Modifiers &= ~(1 << (key ^ 0xE0)); // Left shift 1 by key XOR 0xE0
379                         }
380
381                         USBKeys_Changed |= USBKeyChangeState_Modifiers;
382                         break;
383                 }
384                 // First 6 bytes
385                 else if ( key >= 4 && key <= 49 )
386                 {
387                         // Lookup (otherwise division or multiple checks are needed to do alignment)
388                         // Starting at 0th position, each byte has 8 bits, starting at 4th bit
389                         uint8_t keyPos = key + (0 * 8 - 4); // Starting position in array, Ignoring 4 keys
390                         switch ( keyPos )
391                         {
392                                 byteLookup( 0 );
393                                 byteLookup( 1 );
394                                 byteLookup( 2 );
395                                 byteLookup( 3 );
396                                 byteLookup( 4 );
397                                 byteLookup( 5 );
398                         }
399
400                         USBKeys_Changed |= USBKeyChangeState_MainKeys;
401                 }
402                 // Next 14 bytes
403                 else if ( key >= 51 && key <= 155 )
404                 {
405                         // Lookup (otherwise division or multiple checks are needed to do alignment)
406                         // Starting at 6th byte position, each byte has 8 bits, starting at 51st bit
407                         uint8_t keyPos = key + (6 * 8 - 51); // Starting position in array
408                         switch ( keyPos )
409                         {
410                                 byteLookup( 6 );
411                                 byteLookup( 7 );
412                                 byteLookup( 8 );
413                                 byteLookup( 9 );
414                                 byteLookup( 10 );
415                                 byteLookup( 11 );
416                                 byteLookup( 12 );
417                                 byteLookup( 13 );
418                                 byteLookup( 14 );
419                                 byteLookup( 15 );
420                                 byteLookup( 16 );
421                                 byteLookup( 17 );
422                                 byteLookup( 18 );
423                                 byteLookup( 19 );
424                         }
425
426                         USBKeys_Changed |= USBKeyChangeState_SecondaryKeys;
427                 }
428                 // Next byte
429                 else if ( key >= 157 && key <= 164 )
430                 {
431                         // Lookup (otherwise division or multiple checks are needed to do alignment)
432                         uint8_t keyPos = key + (20 * 8 - 157); // Starting position in array, Ignoring 6 keys
433                         switch ( keyPos )
434                         {
435                                 byteLookup( 20 );
436                         }
437
438                         USBKeys_Changed |= USBKeyChangeState_TertiaryKeys;
439                 }
440                 // Last 6 bytes
441                 else if ( key >= 176 && key <= 221 )
442                 {
443                         // Lookup (otherwise division or multiple checks are needed to do alignment)
444                         uint8_t keyPos = key + (21 * 8 - 176); // Starting position in array
445                         switch ( keyPos )
446                         {
447                                 byteLookup( 21 );
448                                 byteLookup( 22 );
449                                 byteLookup( 23 );
450                                 byteLookup( 24 );
451                                 byteLookup( 25 );
452                                 byteLookup( 26 );
453                         }
454
455                         USBKeys_Changed |= USBKeyChangeState_QuartiaryKeys;
456                 }
457                 // Received 0x00
458                 // This is a special USB Code that internally indicates a "break"
459                 // It is used to send "nothing" in order to break up sequences of USB Codes
460                 else if ( key == 0x00 )
461                 {
462                         USBKeys_Changed |= USBKeyChangeState_MainKeys;
463
464                         // Also flush out buffers just in case
465                         Output_flushBuffers();
466                         break;
467                 }
468                 // Invalid key
469                 else
470                 {
471                         warn_msg("USB Code not within 4-49 (0x4-0x31), 51-155 (0x33-0x9B), 157-164 (0x9D-0xA4), 176-221 (0xB0-0xDD) or 224-231 (0xE0-0xE7) NKRO Mode: ");
472                         printHex( key );
473                         print( NL );
474                         break;
475                 }
476
477                 // Set/Unset
478                 if ( keyPress )
479                 {
480                         USBKeys_Keys[bytePosition] |= (1 << byteShift);
481                         USBKeys_Sent++;
482                 }
483                 else // Release
484                 {
485                         USBKeys_Keys[bytePosition] &= ~(1 << byteShift);
486                         USBKeys_Sent++;
487                 }
488
489                 break;
490         }
491 }
492
493 void Output_flashMode_capability( uint8_t state, uint8_t stateType, uint8_t *args )
494 {
495         // Display capability name
496         if ( stateType == 0xFF && state == 0xFF )
497         {
498                 print("Output_flashMode(usbCode)");
499                 return;
500         }
501
502         // Start flash mode
503         Output_firmwareReload();
504 }
505
506
507
508 // ----- Functions -----
509
510 // Flush Key buffers
511 void Output_flushBuffers()
512 {
513         // Zero out USBKeys_Keys array
514         for ( uint8_t c = 0; c < USB_NKRO_BITFIELD_SIZE_KEYS; c++ )
515                 USBKeys_Keys[ c ] = 0;
516
517         // Zero out other key buffers
518         USBKeys_ConsCtrl = 0;
519         USBKeys_Modifiers = 0;
520         USBKeys_SysCtrl = 0;
521 }
522
523
524 // USB Module Setup
525 inline void Output_setup()
526 {
527         // Setup UART
528         uart_serial_setup();
529
530         // Initialize the USB
531         // If a USB connection does not exist, just ignore it
532         // All usb related functions will non-fatally fail if called
533         // If the USB initialization is delayed, then functionality will just be delayed
534         usb_init();
535
536         // Register USB Output CLI dictionary
537         CLI_registerDictionary( outputCLIDict, outputCLIDictName );
538
539         // Flush key buffers
540         Output_flushBuffers();
541 }
542
543
544 // USB Data Send
545 inline void Output_send()
546 {
547         // Boot Mode Only, unset stale keys
548         if ( USBKeys_Protocol == 0 )
549                 for ( uint8_t c = USBKeys_Sent; c < USB_BOOT_MAX_KEYS; c++ )
550                         USBKeys_Keys[c] = 0;
551
552         // Send keypresses while there are pending changes
553         while ( USBKeys_Changed )
554                 usb_keyboard_send();
555
556         // Clear keys sent
557         USBKeys_Sent = 0;
558
559         // Signal Scan Module we are finished
560         switch ( USBKeys_Protocol )
561         {
562         case 0: // Boot Mode
563                 // Clear modifiers only in boot mode
564                 USBKeys_Modifiers = 0;
565                 Scan_finishedWithOutput( USBKeys_Sent <= USB_BOOT_MAX_KEYS ? USBKeys_Sent : USB_BOOT_MAX_KEYS );
566                 break;
567         case 1: // NKRO Mode
568                 Scan_finishedWithOutput( USBKeys_Sent );
569                 break;
570         }
571 }
572
573
574 // Sets the device into firmware reload mode
575 void Output_firmwareReload()
576 {
577         usb_device_reload();
578 }
579
580
581 // USB Input buffer available
582 inline unsigned int Output_availablechar()
583 {
584         return usb_serial_available() + uart_serial_available();
585 }
586
587
588 // USB Get Character from input buffer
589 inline int Output_getchar()
590 {
591         // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
592         if ( usb_serial_available() > 0 )
593         {
594                 return (int)usb_serial_getchar();
595         }
596
597         if ( uart_serial_available() > 0 )
598         {
599                 return (int)uart_serial_getchar();
600         }
601
602         return -1;
603 }
604
605
606 // USB Send Character to output buffer
607 inline int Output_putchar( char c )
608 {
609         // First send to UART
610         uart_serial_putchar( c );
611
612         // Then send to USB
613         return usb_serial_putchar( c );
614 }
615
616
617 // USB Send String to output buffer, null terminated
618 inline int Output_putstr( char* str )
619 {
620 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
621         uint16_t count = 0;
622 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
623         uint32_t count = 0;
624 #endif
625         // Count characters until NULL character, then send the amount counted
626         while ( str[count] != '\0' )
627                 count++;
628
629         // First send to UART
630         uart_serial_write( str, count );
631
632         // Then send to USB
633         return usb_serial_write( str, count );
634 }
635
636
637 // Soft Chip Reset
638 inline void Output_softReset()
639 {
640         usb_device_software_reset();
641 }
642
643
644 // ----- CLI Command Functions -----
645
646 void cliFunc_kbdProtocol( char* args )
647 {
648         print( NL );
649         info_msg("Keyboard Protocol: ");
650         printInt8( USBKeys_Protocol );
651 }
652
653
654 void cliFunc_outputDebug( char* args )
655 {
656         // Parse number from argument
657         //  NOTE: Only first argument is used
658         char* arg1Ptr;
659         char* arg2Ptr;
660         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
661
662         // Default to 1 if no argument is given
663         Output_DebugMode = 1;
664
665         if ( arg1Ptr[0] != '\0' )
666         {
667                 Output_DebugMode = (uint16_t)numToInt( arg1Ptr );
668         }
669 }
670
671
672 void cliFunc_readLEDs( char* args )
673 {
674         print( NL );
675         info_msg("LED State: ");
676         printInt8( USBKeys_LEDs );
677 }
678
679
680 void cliFunc_readUART( char* args )
681 {
682         print( NL );
683
684         // Read UART buffer until empty
685         while ( uart_serial_available() > 0 )
686         {
687                 char out[] = { (char)uart_serial_getchar(), '\0' };
688                 dPrint( out );
689         }
690 }
691
692
693 void cliFunc_sendKeys( char* args )
694 {
695         // Copy USBKeys_KeysCLI to USBKeys_Keys
696         for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
697         {
698                 // TODO
699                 //USBKeys_Keys[key] = USBKeys_KeysCLI[key];
700         }
701         USBKeys_Sent = USBKeys_SentCLI;
702
703         // Set modifier byte
704         USBKeys_Modifiers = USBKeys_ModifiersCLI;
705 }
706
707
708 void cliFunc_sendUART( char* args )
709 {
710         // Write all args to UART
711         uart_serial_write( args, lenStr( args ) );
712 }
713
714
715 void cliFunc_setKeys( char* args )
716 {
717         char* curArgs;
718         char* arg1Ptr;
719         char* arg2Ptr = args;
720
721         // Parse up to USBKeys_MaxSize args (whichever is least)
722         for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USB_BOOT_MAX_KEYS; ++USBKeys_SentCLI )
723         {
724                 curArgs = arg2Ptr;
725                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
726
727                 // Stop processing args if no more are found
728                 if ( *arg1Ptr == '\0' )
729                         break;
730
731                 // Add the USB code to be sent
732                 // TODO
733                 //USBKeys_KeysCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
734         }
735 }
736
737
738 void cliFunc_setMod( char* args )
739 {
740         // Parse number from argument
741         //  NOTE: Only first argument is used
742         char* arg1Ptr;
743         char* arg2Ptr;
744         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
745
746         USBKeys_ModifiersCLI = numToInt( arg1Ptr );
747 }
748