]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/output_com.c
Adding CLI and CDC Serial support for Teensy 2.0 and Teensy 2.0++
[kiibohd-controller.git] / Output / pjrcUSB / output_com.c
1 /* Copyright (C) 2011-2014 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 <print.h>
30 #include <scan_loop.h>
31
32 // USB Includes
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"
38 #endif
39
40 // Local Includes
41 #include "output_com.h"
42
43
44
45 // ----- Function Declarations -----
46
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 );
53
54
55 // ----- Variables -----
56
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
67 };
68
69
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;
74
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};
77
78 // The number of keys sent to the usb in the array
79          uint8_t USBKeys_Sent;
80
81 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
82 volatile uint8_t USBKeys_LEDs = 0;
83
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;
88
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;
92
93 // count until idle timeout
94          uint8_t USBKeys_Idle_Count = 0;
95
96
97 // ----- Functions -----
98
99 // USB Module Setup
100 inline void output_setup()
101 {
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.
105         usb_init();
106         while ( !usb_configured() ) /* wait */ ;
107
108         // Register USB Output dictionary
109         registerDictionary_cli( outputCLIDict, outputCLIDictName );
110
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?)
114 }
115
116
117 // USB Data Send
118 inline void output_send(void)
119 {
120                 // TODO undo potentially old keys
121                 for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
122                         USBKeys_Array[c] = 0;
123
124                 // Send keypresses
125                 usb_keyboard_send();
126
127                 // Clear modifiers and keys
128                 USBKeys_Modifiers = 0;
129                 USBKeys_Sent      = 0;
130
131                 // Signal Scan Module we are finishedA
132                 scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
133 }
134
135
136 // Sets the device into firmware reload mode
137 inline void output_firmwareReload()
138 {
139 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
140         usb_debug_reload();
141 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
142         usb_device_reload();
143 #endif
144 }
145
146
147 // USB Input buffer available
148 inline unsigned int output_availablechar()
149 {
150         return usb_serial_available();
151 }
152
153
154 // USB Get Character from input buffer
155 inline int output_getchar()
156 {
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();
162 #endif
163 }
164
165
166 // USB Send Character to output buffer
167 inline int output_putchar( char c )
168 {
169         return usb_serial_putchar( c );
170 }
171
172
173 // USB Send String to output buffer, null terminated
174 inline int output_putstr( char* str )
175 {
176 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
177         uint16_t count = 0;
178 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
179         uint32_t count = 0;
180 #endif
181         // Count characters until NULL character, then send the amount counted
182         while ( str[count] != '\0' )
183                 count++;
184
185         return usb_serial_write( str, count );
186 }
187
188
189 // Soft Chip Reset
190 inline void output_softReset()
191 {
192 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
193         usb_debug_software_reset();
194 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
195         SOFTWARE_RESET();
196 #endif
197 }
198
199
200 // ----- CLI Command Functions -----
201
202 void cliFunc_holdKey( char* args )
203 {
204         // TODO
205 }
206
207
208 void cliFunc_readLEDs( char* args )
209 {
210         // TODO
211 }
212
213
214 void cliFunc_releaseKey( char* args )
215 {
216         // TODO
217 }
218
219
220 void cliFunc_sendKey( char* args )
221 {
222         // TODO Argument handling
223         USBKeys_Array[0] = 4; // KEY_A
224         USBKeys_Sent = 1;
225 }
226
227
228 void cliFunc_setLEDs( char* args )
229 {
230         // TODO
231 }
232
233
234 void cliFunc_setMod( char* args )
235 {
236         // TODO
237 }
238