]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/uartOut/output_com.c
Fixing default ErgoDox layout and adding FlashMode button
[kiibohd-controller.git] / Output / uartOut / output_com.c
1 /* Copyright (C) 2014-2015 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 <led.h>
30 #include <print.h>
31 #include <scan_loop.h>
32
33 // USB Includes
34 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
35 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_)
36 #include "arm/uart_serial.h"
37 #endif
38
39 // Local Includes
40 #include "output_com.h"
41
42
43
44 // ----- Function Declarations -----
45
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 );
51
52
53
54 // ----- Variables -----
55
56 // Output Module command dictionary
57 CLIDict_Entry( kbdProtocol, "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode" );
58 CLIDict_Entry( readLEDs,    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc." );
59 CLIDict_Entry( sendKeys,    "Send the prepared list of USB codes and modifier byte." );
60 CLIDict_Entry( setKeys,     "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m." );
61 CLIDict_Entry( setMod,      "Set the modfier byte:" NL "\t\t1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI" );
62
63 CLIDict_Def( outputCLIDict, "USB Module Commands" ) = {
64         CLIDict_Item( kbdProtocol ),
65         CLIDict_Item( readLEDs ),
66         CLIDict_Item( sendKeys ),
67         CLIDict_Item( setKeys ),
68         CLIDict_Item( setMod ),
69         { 0, 0, 0 } // Null entry for dictionary end
70 };
71
72
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
78
79 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
80         uint8_t  USBKeys_Keys   [USB_NKRO_BITFIELD_SIZE_KEYS];
81         uint8_t  USBKeys_KeysCLI[USB_NKRO_BITFIELD_SIZE_KEYS]; // Separate CLI send buffer
82
83 // System Control and Consumer Control 1KRO containers
84         uint8_t  USBKeys_SysCtrl;
85         uint16_t USBKeys_ConsCtrl;
86
87 // The number of keys sent to the usb in the array
88         uint8_t  USBKeys_Sent    = 0;
89         uint8_t  USBKeys_SentCLI = 0;
90
91 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
92 volatile uint8_t  USBKeys_LEDs = 0;
93
94 // Protocol setting from the host.
95 // 0 - Boot Mode
96 // 1 - NKRO Mode (Default, unless set by a BIOS or boot interface)
97 volatile uint8_t  USBKeys_Protocol = 0;
98
99 // Indicate if USB should send update
100 // OS only needs update if there has been a change in state
101 USBKeyChangeState USBKeys_Changed = USBKeyChangeState_None;
102
103 // the idle configuration, how often we send the report to the
104 // host (ms * 4) even when it hasn't changed
105         uint8_t  USBKeys_Idle_Config = 125;
106
107 // count until idle timeout
108         uint8_t  USBKeys_Idle_Count = 0;
109
110 // Indicates whether the Output module is fully functional
111 // 0 - Not fully functional, 1 - Fully functional
112 // 0 is often used to show that a USB cable is not plugged in (but has power)
113         uint8_t  Output_Available = 0;
114
115
116
117 // ----- Capabilities -----
118
119 // Adds a single USB Code to the USB Output buffer
120 // Argument #1: USB Code
121 void Output_usbCodeSend_capability( uint8_t state, uint8_t stateType, uint8_t *args )
122 {
123         // Display capability name
124         if ( stateType == 0xFF && state == 0xFF )
125         {
126                 print("Output_usbCodeSend(usbCode)");
127                 print("Not used in uartOut...");
128                 return;
129         }
130 }
131
132 void Output_flashMode_capability( uint8_t state, uint8_t stateType, uint8_t *args )
133 {
134         // Display capability name
135         if ( stateType == 0xFF && state == 0xFF )
136         {
137                 print("Output_flashMode(usbCode)");
138                 return;
139         }
140
141         // Start flash mode
142         Output_firmwareReload();
143 }
144
145
146
147 // ----- Functions -----
148
149 // USB Module Setup
150 inline void Output_setup()
151 {
152         // Setup UART
153         uart_serial_setup();
154
155         // Register USB Output CLI dictionary
156         CLI_registerDictionary( outputCLIDict, outputCLIDictName );
157 }
158
159
160 // USB Data Send
161 inline void Output_send(void)
162 {
163         // TODO
164 }
165
166
167 // Sets the device into firmware reload mode
168 inline void Output_firmwareReload()
169 {
170         uart_device_reload();
171 }
172
173
174 // USB Input buffer available
175 inline unsigned int Output_availablechar()
176 {
177         return uart_serial_available();
178 }
179
180
181 // USB Get Character from input buffer
182 inline int Output_getchar()
183 {
184         // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
185         return (int)uart_serial_getchar();
186 }
187
188
189 // USB Send Character to output buffer
190 inline int Output_putchar( char c )
191 {
192         return uart_serial_putchar( c );
193 }
194
195
196 // USB Send String to output buffer, null terminated
197 inline int Output_putstr( char* str )
198 {
199 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
200         uint16_t count = 0;
201 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
202         uint32_t count = 0;
203 #endif
204         // Count characters until NULL character, then send the amount counted
205         while ( str[count] != '\0' )
206                 count++;
207
208         return uart_serial_write( str, count );
209 }
210
211
212 // Soft Chip Reset
213 inline void Output_softReset()
214 {
215 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
216 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
217         SOFTWARE_RESET();
218 #endif
219 }
220
221
222 // ----- CLI Command Functions -----
223
224 void cliFunc_kbdProtocol( char* args )
225 {
226         print( NL );
227         info_msg("Keyboard Protocol: ");
228         printInt8( USBKeys_Protocol );
229 }
230
231
232 void cliFunc_readLEDs( char* args )
233 {
234         print( NL );
235         info_msg("LED State: ");
236         printInt8( USBKeys_LEDs );
237 }
238
239
240 void cliFunc_sendKeys( char* args )
241 {
242         // Copy USBKeys_KeysCLI to USBKeys_Keys
243         for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
244         {
245                 // TODO
246                 //USBKeys_Keys[key] = USBKeys_KeysCLI[key];
247         }
248         USBKeys_Sent = USBKeys_SentCLI;
249
250         // Set modifier byte
251         USBKeys_Modifiers = USBKeys_ModifiersCLI;
252 }
253
254
255 void cliFunc_setKeys( char* args )
256 {
257         char* curArgs;
258         char* arg1Ptr;
259         char* arg2Ptr = args;
260
261         // Parse up to USBKeys_MaxSize args (whichever is least)
262         for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USB_BOOT_MAX_KEYS; ++USBKeys_SentCLI )
263         {
264                 curArgs = arg2Ptr;
265                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
266
267                 // Stop processing args if no more are found
268                 if ( *arg1Ptr == '\0' )
269                         break;
270
271                 // Add the USB code to be sent
272                 // TODO
273                 //USBKeys_KeysCLI[USBKeys_SentCLI] = numToInt( arg1Ptr );
274         }
275 }
276
277
278 void cliFunc_setMod( char* args )
279 {
280         // Parse number from argument
281         //  NOTE: Only first argument is used
282         char* arg1Ptr;
283         char* arg2Ptr;
284         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
285
286         USBKeys_ModifiersCLI = numToInt( arg1Ptr );
287 }
288