annotate Output/pjrcUSB/output_com.c @ 122:2bb16439e6ca

Preparing Teensy 3.1 and CLI merge for DPH controller code.
author Jacob Alexander <haata@kiibohd.com>
date Sat, 22 Mar 2014 14:32:06 -0700
parents 814b5d3d7da6
children 0c5d1fe99302
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 119
diff changeset
1 /* Copyright (C) 2011-2014 by Jacob Alexander
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 119
diff changeset
2 *
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
4 * of this software and associated documentation files (the "Software"), to deal
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
5 * in the Software without restriction, including without limitation the rights
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
7 * copies of the Software, and to permit persons to whom the Software is
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
8 * furnished to do so, subject to the following conditions:
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 119
diff changeset
9 *
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
10 * The above copyright notice and this permission notice shall be included in
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
11 * all copies or substantial portions of the Software.
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 119
diff changeset
12 *
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
19 * THE SOFTWARE.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
20 */
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
21
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
22 // ----- Includes -----
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
23
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
24 // Compiler Includes
118
b61ca96b7c24 File and macro modifications for supporting Teensy 3.1
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
25 #include <Lib/OutputLib.h>
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
26
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
27 // Project Includes
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
28 #include <scan_loop.h>
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
29
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
30 // USB Includes
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
31 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
32 #include "avr/usb_keyboard_debug.h"
119
814b5d3d7da6 Finished USB for Teensy 3.1 (Now 3.1 compatible!)
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
33 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
34 #include "arm/usb_keyboard.h"
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
35 #include "arm/usb_dev.h"
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
36 #endif
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
37
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
38 // Local Includes
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
39 #include "output_com.h"
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
40
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
41
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
42
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
43 // ----- Variables -----
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
44
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
45 // which modifier keys are currently pressed
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
46 // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
47 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
48 uint8_t USBKeys_Modifiers = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
49
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
50 // which keys are currently pressed, up to 6 keys may be down at once
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
51 uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
52
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
53 // The number of keys sent to the usb in the array
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
54 uint8_t USBKeys_Sent;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
55
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
56 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
57 volatile uint8_t USBKeys_LEDs = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
58
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
59 // protocol setting from the host. We use exactly the same report
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
60 // either way, so this variable only stores the setting since we
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
61 // are required to be able to report which setting is in use.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
62 uint8_t USBKeys_Protocol = 1;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
63
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
64 // the idle configuration, how often we send the report to the
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
65 // host (ms * 4) even when it hasn't changed
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
66 uint8_t USBKeys_Idle_Config = 125;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
67
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
68 // count until idle timeout
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
69 uint8_t USBKeys_Idle_Count = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
70
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
71
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
72
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
73 // ----- Functions -----
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
74
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
75 // USB Module Setup
109
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
76 inline void output_setup()
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
77 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
78 // Initialize the USB, and then wait for the host to set configuration.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
79 // If the Teensy is powered without a PC connected to the USB port,
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
80 // this will wait forever.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
81 usb_init();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
82 while ( !usb_configured() ) /* wait */ ;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
83
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
84 // Wait an extra second for the PC's operating system to load drivers
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
85 // and do whatever it does to actually be ready for input
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
86 //_delay_ms(1000); // TODO
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
87 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
88
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
89
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
90 // USB Data Send
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 119
diff changeset
91 inline void output_send(void)
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
92 {
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
93 // TODO undo potentially old keys
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
94 for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
95 USBKeys_Array[c] = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
96
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
97 // Send keypresses
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
98 usb_keyboard_send();
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
99
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
100 // Clear modifiers and keys
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
101 USBKeys_Modifiers = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
102 USBKeys_Sent = 0;
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
103
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
104 // Signal Scan Module we are finishedA
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
105 scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
106 }
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
107
109
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
108
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
109 // Sets the device into firmware reload mode
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
110 inline void output_firmwareReload()
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
111 {
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
112 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
119
814b5d3d7da6 Finished USB for Teensy 3.1 (Now 3.1 compatible!)
Jacob Alexander <haata@kiibohd.com>
parents: 118
diff changeset
113 #elif defined(_mk20dx128_) || defined(_mk20dx256_)
109
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
114 usb_device_reload();
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
115 #endif
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
116 }
2c1a9e6ae70e Added more CLI commands.
Jacob Alexander <haata@kiibohd.com>
parents: 105
diff changeset
117