1 /* Copyright (C) 2014 by Jacob Alexander
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 // ----- Includes -----
25 #include <Lib/OutputLib.h>
31 #include <scan_loop.h>
34 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
35 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
36 #include "arm/uart_serial.h"
40 #include "output_com.h"
44 // ----- Function Declarations -----
46 void cliFunc_kbdProtocol( char* args );
47 void cliFunc_readLEDs ( char* args );
48 void cliFunc_sendKeys ( char* args );
49 void cliFunc_setKeys ( char* args );
50 void cliFunc_setMod ( char* args );
54 // ----- Variables -----
56 // Output Module command dictionary
57 const char outputCLIDictName[] = "USB Module Commands";
58 const CLIDictItem outputCLIDict[] = {
59 { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
60 { "readLEDs", "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
61 { "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
62 { "setKeys", "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
63 { "setMod", "Set the modfier byte:" NL "\t\t1 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
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
74 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
75 uint8_t USBKeys_Keys [USB_NKRO_BITFIELD_SIZE_KEYS];
76 uint8_t USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer
78 // System Control and Consumer Control 1KRO containers
79 uint8_t USBKeys_SysCtrl;
80 uint16_t USBKeys_ConsCtrl;
82 // The number of keys sent to the usb in the array
83 uint8_t USBKeys_Sent = 0;
84 uint8_t USBKeys_SentCLI = 0;
86 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
87 volatile uint8_t USBKeys_LEDs = 0;
89 // Protocol setting from the host.
91 // 1 - NKRO Mode (Default, unless set by a BIOS or boot interface)
92 volatile uint8_t USBKeys_Protocol = 0;
94 // Indicate if USB should send update
95 // OS only needs update if there has been a change in state
96 USBKeyChangeState USBKeys_Changed = USBKeyChangeState_None;
98 // the idle configuration, how often we send the report to the
99 // host (ms * 4) even when it hasn't changed
100 uint8_t USBKeys_Idle_Config = 125;
102 // count until idle timeout
103 uint8_t USBKeys_Idle_Count = 0;
107 // ----- Capabilities -----
109 // Adds a single USB Code to the USB Output buffer
110 // Argument #1: USB Code
111 void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
113 // Display capability name
114 if ( stateType == 0xFF && state == 0xFF )
116 print("Output_usbCodeSend(usbCode)");
117 print("Not used in uartOut...");
124 // ----- Functions -----
127 inline void Output_setup()
132 // Register USB Output CLI dictionary
133 CLI_registerDictionary( outputCLIDict, outputCLIDictName );
138 inline void Output_send(void)
144 // Sets the device into firmware reload mode
145 inline void Output_firmwareReload()
147 uart_device_reload();
151 // USB Input buffer available
152 inline unsigned int Output_availablechar()
154 return uart_serial_available();
158 // USB Get Character from input buffer
159 inline int Output_getchar()
161 // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
162 return (int)uart_serial_getchar();
166 // USB Send Character to output buffer
167 inline int Output_putchar( char c )
169 return uart_serial_putchar( c );
173 // USB Send String to output buffer, null terminated
174 inline int Output_putstr( char* str )
176 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
178 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
181 // Count characters until NULL character, then send the amount counted
182 while ( str[count] != '\0' )
185 return uart_serial_write( str, count );
190 inline void Output_softReset()
192 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
193 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
199 // ----- CLI Command Functions -----
201 void cliFunc_kbdProtocol( char* args )
204 info_msg("Keyboard Protocol: ");
205 printInt8( USBKeys_Protocol );
209 void cliFunc_readLEDs( char* args )
212 info_msg("LED State: ");
213 printInt8( USBKeys_LEDs );
217 void cliFunc_sendKeys( char* args )
219 // Copy USBKeys_KeysCLI to USBKeys_Keys
220 for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
223 //USBKeys_Keys[key] = USBKeys_KeysCLI[key];
225 USBKeys_Sent = USBKeys_SentCLI;
228 USBKeys_Modifiers = USBKeys_ModifiersCLI;
232 void cliFunc_setKeys( char* args )
236 char* arg2Ptr = args;
238 // Parse up to USBKeys_MaxSize args (whichever is least)
239 for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USB_BOOT_MAX_KEYS; ++USBKeys_SentCLI )
242 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
244 // Stop processing args if no more are found
245 if ( *arg1Ptr == '\0' )
248 // Add the USB code to be sent
250 //USBKeys_KeysCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
255 void cliFunc_setMod( char* args )
257 // Parse number from argument
258 // NOTE: Only first argument is used
261 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
263 USBKeys_ModifiersCLI = numToInt( arg1Ptr );