1 /* Copyright (C) 2011-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 #include "avr/usb_keyboard_serial.h"
35 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
36 #include "arm/usb_keyboard.h"
37 #include "arm/usb_dev.h"
41 #include "output_com.h"
45 // ----- Function Declarations -----
47 void cliFunc_holdKey ( char* args );
48 void cliFunc_readLEDs ( char* args );
49 void cliFunc_releaseKey( char* args );
50 void cliFunc_sendKey ( char* args );
51 void cliFunc_setLEDs ( char* args );
52 void cliFunc_setMod ( char* args );
55 // ----- Variables -----
57 // Output Module command dictionary
58 char* outputCLIDictName = "USB Module Commands";
59 CLIDictItem outputCLIDict[] = {
60 { "holdKey", "Hold a space separated list of USB codes. Ignores already pressed keys.", cliFunc_holdKey },
61 { "readLEDs", "Read LED byte. See setLEDs.", cliFunc_readLEDs },
62 { "releaseKey", "Release a space separated list of USB codes. Ignores unpressed keys.", cliFunc_releaseKey },
63 { "sendKey", "Send a space separated list of USB codes. Press/Release.", cliFunc_sendKey },
64 { "setLEDs", "Set LED byte: 1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_setLEDs },
65 { "setMod", "Set the modfier byte: 1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI", cliFunc_setMod },
66 { 0, 0, 0 } // Null entry for dictionary end
70 // which modifier keys are currently pressed
71 // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
72 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
73 uint8_t USBKeys_Modifiers = 0;
75 // which keys are currently pressed, up to 6 keys may be down at once
76 uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
78 // The number of keys sent to the usb in the array
81 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
82 volatile uint8_t USBKeys_LEDs = 0;
84 // protocol setting from the host. We use exactly the same report
85 // either way, so this variable only stores the setting since we
86 // are required to be able to report which setting is in use.
87 uint8_t USBKeys_Protocol = 1;
89 // the idle configuration, how often we send the report to the
90 // host (ms * 4) even when it hasn't changed
91 uint8_t USBKeys_Idle_Config = 125;
93 // count until idle timeout
94 uint8_t USBKeys_Idle_Count = 0;
97 // ----- Functions -----
100 inline void output_setup()
102 // Initialize the USB, and then wait for the host to set configuration.
103 // If the Teensy is powered without a PC connected to the USB port,
104 // this will wait forever.
106 while ( !usb_configured() ) /* wait */ ;
108 // Register USB Output dictionary
109 registerDictionary_cli( outputCLIDict, outputCLIDictName );
111 // Wait an extra second for the PC's operating system to load drivers
112 // and do whatever it does to actually be ready for input
113 //_delay_ms(1000); // TODO (is this actually necessary?)
118 inline void output_send(void)
120 // TODO undo potentially old keys
121 for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
122 USBKeys_Array[c] = 0;
127 // Clear modifiers and keys
128 USBKeys_Modifiers = 0;
131 // Signal Scan Module we are finishedA
132 scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
136 // Sets the device into firmware reload mode
137 inline void output_firmwareReload()
139 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
141 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
147 // USB Input buffer available
148 inline unsigned int output_availablechar()
150 return usb_serial_available();
154 // USB Get Character from input buffer
155 inline int output_getchar()
157 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
158 // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes)
159 return (int)usb_serial_getchar();
160 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
161 return usb_serial_getchar();
166 // USB Send Character to output buffer
167 inline int output_putchar( char c )
169 return usb_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(_mk20dx256_) // ARM
181 // Count characters until NULL character, then send the amount counted
182 while ( str[count] != '\0' )
185 return usb_serial_write( str, count );
190 inline void output_softReset()
192 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
193 usb_debug_software_reset();
194 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
200 // ----- CLI Command Functions -----
202 void cliFunc_holdKey( char* args )
208 void cliFunc_readLEDs( char* args )
214 void cliFunc_releaseKey( char* args )
220 void cliFunc_sendKey( char* args )
222 // TODO Argument handling
223 USBKeys_Array[0] = 4; // KEY_A
228 void cliFunc_setLEDs( char* args )
234 void cliFunc_setMod( char* args )