]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/uartOut/output_com.c
Fixed bug with standalone UART CLI
[kiibohd-controller.git] / Output / uartOut / output_com.c
1 /* Copyright (C) 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 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
35 #include "arm/uart_serial.h"
36 #endif
37
38 // Local Includes
39 #include "output_com.h"
40
41
42
43 // ----- Function Declarations -----
44
45 void cliFunc_kbdProtocol( char* args );
46 void cliFunc_readLEDs   ( char* args );
47 void cliFunc_sendKeys   ( char* args );
48 void cliFunc_setKeys    ( char* args );
49 void cliFunc_setMod     ( char* args );
50
51
52 // ----- Variables -----
53
54 // Output Module command dictionary
55 char*       outputCLIDictName = "USB Module Commands - NOT WORKING";
56 CLIDictItem outputCLIDict[] = {
57         { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
58         { "readLEDs",    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
59         { "sendKeys",    "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
60         { "setKeys",     "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
61         { "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 },
62         { 0, 0, 0 } // Null entry for dictionary end
63 };
64
65
66 // Which modifier keys are currently pressed
67 // 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
68 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
69          uint8_t USBKeys_Modifiers    = 0;
70          uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
71
72 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
73          uint8_t USBKeys_Array   [USB_MAX_KEY_SEND];
74          uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
75
76 // The number of keys sent to the usb in the array
77          uint8_t USBKeys_Sent    = 0;
78          uint8_t USBKeys_SentCLI = 0;
79
80 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
81 volatile uint8_t USBKeys_LEDs = 0;
82
83 // Protocol setting from the host.
84 // 0 - Boot Mode (Default, until set by the host)
85 // 1 - NKRO Mode
86 volatile uint8_t USBKeys_Protocol = 1;
87
88 // the idle configuration, how often we send the report to the
89 // host (ms * 4) even when it hasn't changed
90          uint8_t USBKeys_Idle_Config = 125;
91
92 // count until idle timeout
93          uint8_t USBKeys_Idle_Count = 0;
94
95
96 // ----- Functions -----
97
98 // USB Module Setup
99 inline void Output_setup()
100 {
101         // Setup UART
102         uart_serial_setup();
103
104         // Register USB Output CLI dictionary
105         CLI_registerDictionary( outputCLIDict, outputCLIDictName );
106 }
107
108
109 // USB Data Send
110 inline void Output_send(void)
111 {
112         // TODO
113 }
114
115
116 // Sets the device into firmware reload mode
117 inline void Output_firmwareReload()
118 {
119 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
120 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
121         uart_device_reload();
122 #endif
123 }
124
125
126 // USB Input buffer available
127 inline unsigned int Output_availablechar()
128 {
129         return uart_serial_available();
130 }
131
132
133 // USB Get Character from input buffer
134 inline int Output_getchar()
135 {
136 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
137 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
138         return uart_serial_getchar();
139 #endif
140 }
141
142
143 // USB Send Character to output buffer
144 inline int Output_putchar( char c )
145 {
146         return uart_serial_putchar( c );
147 }
148
149
150 // USB Send String to output buffer, null terminated
151 inline int Output_putstr( char* str )
152 {
153 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
154 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
155         uint32_t count = 0;
156 #endif
157         // Count characters until NULL character, then send the amount counted
158         while ( str[count] != '\0' )
159                 count++;
160
161         return uart_serial_write( str, count );
162 }
163
164
165 // Soft Chip Reset
166 inline void Output_softReset()
167 {
168 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
169 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
170         SOFTWARE_RESET();
171 #endif
172 }
173
174
175 // ----- CLI Command Functions -----
176
177 void cliFunc_kbdProtocol( char* args )
178 {
179         print( NL );
180         info_msg("Keyboard Protocol: ");
181         printInt8( USBKeys_Protocol );
182 }
183
184
185 void cliFunc_readLEDs( char* args )
186 {
187         print( NL );
188         info_msg("LED State (This doesn't work yet...): ");
189         printInt8( USBKeys_LEDs );
190 }
191
192
193 void cliFunc_sendKeys( char* args )
194 {
195         // Copy USBKeys_ArrayCLI to USBKeys_Array
196         for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
197         {
198                 USBKeys_Array[key] = USBKeys_ArrayCLI[key];
199         }
200         USBKeys_Sent = USBKeys_SentCLI;
201
202         // Set modifier byte
203         USBKeys_Modifiers = USBKeys_ModifiersCLI;
204 }
205
206
207 void cliFunc_setKeys( char* args )
208 {
209         char* curArgs;
210         char* arg1Ptr;
211         char* arg2Ptr = args;
212
213         // Parse up to USBKeys_MaxSize args (whichever is least)
214         for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
215         {
216                 curArgs = arg2Ptr;
217                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
218
219                 // Stop processing args if no more are found
220                 if ( *arg1Ptr == '\0' )
221                         break;
222
223                 // Add the USB code to be sent
224                 USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
225         }
226 }
227
228
229 void cliFunc_setMod( char* args )
230 {
231         // Parse number from argument
232         //  NOTE: Only first argument is used
233         char* arg1Ptr;
234         char* arg2Ptr;
235         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
236
237         USBKeys_ModifiersCLI = decToInt( arg1Ptr );
238 }
239