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>
30 #include <scan_loop.h>
33 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
34 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
35 #include "../uartOut/arm/uart_serial.h"
36 #include "../pjrcUSB/arm/usb_dev.h"
37 #include "../pjrcUSB/arm/usb_keyboard.h"
38 #include "../pjrcUSB/arm/usb_serial.h"
42 #include "output_com.h"
46 // ----- Function Declarations -----
48 void cliFunc_kbdProtocol( char* args );
49 void cliFunc_readLEDs ( char* args );
50 void cliFunc_readUART ( char* args );
51 void cliFunc_sendKeys ( char* args );
52 void cliFunc_sendUART ( char* args );
53 void cliFunc_setKeys ( char* args );
54 void cliFunc_setMod ( char* args );
57 // ----- Variables -----
59 // Output Module command dictionary
60 char* outputCLIDictName = "USB Module Commands - NOT WORKING";
61 CLIDictItem outputCLIDict[] = {
62 { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
63 { "readLEDs", "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
64 { "readUART", "Read UART buffer until empty.", cliFunc_readUART },
65 { "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
66 { "sendUART", "Send characters over UART0.", cliFunc_sendUART },
67 { "setKeys", "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
68 { "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 },
69 { 0, 0, 0 } // Null entry for dictionary end
73 // Which modifier keys are currently pressed
74 // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
75 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
76 uint8_t USBKeys_Modifiers = 0;
77 uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
79 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
80 uint8_t USBKeys_Array [USB_MAX_KEY_SEND];
81 uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
83 // The number of keys sent to the usb in the array
84 uint8_t USBKeys_Sent = 0;
85 uint8_t USBKeys_SentCLI = 0;
87 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
88 volatile uint8_t USBKeys_LEDs = 0;
90 // Protocol setting from the host.
91 // 0 - Boot Mode (Default, until set by the host)
93 volatile uint8_t USBKeys_Protocol = 1;
95 // the idle configuration, how often we send the report to the
96 // host (ms * 4) even when it hasn't changed
97 uint8_t USBKeys_Idle_Config = 125;
99 // count until idle timeout
100 uint8_t USBKeys_Idle_Count = 0;
103 // ----- Functions -----
106 inline void Output_setup()
108 // Initialize the USB, and then wait for the host to set configuration.
109 // If the Teensy is powered without a PC connected to the USB port,
110 // this will wait forever.
121 while ( !usb_configured() ) /* wait */ ;
123 // Register USB Output CLI dictionary
124 CLI_registerDictionary( outputCLIDict, outputCLIDictName );
129 inline void Output_send(void)
131 // TODO undo potentially old keys
132 for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
133 USBKeys_Array[c] = 0;
138 // Clear modifiers and keys
139 USBKeys_Modifiers = 0;
142 // Signal Scan Module we are finishedA
143 Scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
147 // Sets the device into firmware reload mode
148 inline void Output_firmwareReload()
150 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
151 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
152 uart_device_reload();
157 // USB Input buffer available
158 inline unsigned int Output_availablechar()
160 return usb_serial_available() + uart_serial_available();
164 // USB Get Character from input buffer
165 inline int Output_getchar()
167 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
168 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
169 if ( usb_serial_available() > 0 )
171 return usb_serial_getchar();
174 if ( uart_serial_available() > 0 )
176 return uart_serial_getchar();
184 // USB Send Character to output buffer
185 inline int Output_putchar( char c )
187 // First send to UART
188 uart_serial_putchar( c );
191 return usb_serial_putchar( c );
195 // USB Send String to output buffer, null terminated
196 inline int Output_putstr( char* str )
198 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
199 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
202 // Count characters until NULL character, then send the amount counted
203 while ( str[count] != '\0' )
206 // Make sure USB is configured first
207 if ( usb_configured() )
209 // First send to UART
210 uart_serial_write( str, count );
214 return usb_serial_write( str, count );
219 inline void Output_softReset()
221 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
222 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
228 // ----- CLI Command Functions -----
230 void cliFunc_kbdProtocol( char* args )
233 info_msg("Keyboard Protocol: ");
234 printInt8( USBKeys_Protocol );
238 void cliFunc_readLEDs( char* args )
241 info_msg("LED State (This doesn't work yet...): ");
242 printInt8( USBKeys_LEDs );
246 void cliFunc_readUART( char* args )
250 // Read UART buffer until empty
251 while ( uart_serial_available() > 0 )
253 char out[] = { (char)uart_serial_getchar(), '\0' };
259 void cliFunc_sendKeys( char* args )
261 // Copy USBKeys_ArrayCLI to USBKeys_Array
262 for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
264 USBKeys_Array[key] = USBKeys_ArrayCLI[key];
266 USBKeys_Sent = USBKeys_SentCLI;
269 USBKeys_Modifiers = USBKeys_ModifiersCLI;
273 void cliFunc_sendUART( char* args )
275 // Write all args to UART
276 uart_serial_write( args, lenStr( args ) );
280 void cliFunc_setKeys( char* args )
284 char* arg2Ptr = args;
286 // Parse up to USBKeys_MaxSize args (whichever is least)
287 for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
290 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
292 // Stop processing args if no more are found
293 if ( *arg1Ptr == '\0' )
296 // Add the USB code to be sent
297 USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
302 void cliFunc_setMod( char* args )
304 // Parse number from argument
305 // NOTE: Only first argument is used
308 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
310 USBKeys_ModifiersCLI = decToInt( arg1Ptr );