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