forked from kiibohd/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_gen.c
250 lines (190 loc) · 7.31 KB
/
output_gen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright (C) 2011-2018 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/OutputLib.h>
// Project Includes
#include <cli.h>
#include <print.h>
// KLL
#include <kll.h>
// Interface Includes
#include <output_com.h>
// ----- Macros -----
// ----- Function Declarations -----
void cliFunc_current ( char* args );
void cliFunc_outputDebug( char* args );
// ----- Variables -----
// Output Module command dictionary
CLIDict_Entry( current, "Shows the current negotiated current." );
CLIDict_Entry( outputDebug, "Toggle Output Debug mode." );
CLIDict_Def( outputCLIDict, "Output Module Commands" ) = {
CLIDict_Item( current ),
CLIDict_Item( outputDebug ),
{ 0, 0, 0 } // Null entry for dictionary end
};
// Indicates whether the Output module is fully functional
// 0 - Not fully functional, 1 - Fully functional
// 0 is often used to show that a USB cable is not plugged in (but has power)
volatile uint8_t Output_Available;
// Debug control variable for Output modules
// 0 - Debug disabled (default)
// 1 - Debug enabled
uint8_t Output_DebugMode;
// mA - Set by outside module if not using USB (i.e. Interconnect)
// Generally set to 100 mA (low power) or 500 mA (high power)
uint16_t Output_ExtCurrent_Available;
// mA - Set by USB module (if exists)
// Initially 100 mA, but may be negotiated higher (e.g. 500 mA)
uint16_t Output_USBCurrent_Available;
// ----- Capabilities -----
// Ignored capabilities
void Output_ignored_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args ) {}
#if !defined(_APPLE_)
void Output_kbdProtocolBoot_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_kbdProtocolNKRO_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_toggleKbdProtocol_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_consCtrlSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_noneSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_sysCtrlSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_usbCodeSend_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_usbMouse_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
void Output_usbMouseWheel_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
__attribute__ ((weak, alias("Output_ignored_capability")));
#endif
// Jump to bootloader capability
void Output_flashMode_capability( TriggerMacro *trigger, uint8_t state, uint8_t stateType, uint8_t *args )
{
// Display capability name
if ( stateType == 0xFF && state == 0xFF )
{
print("Output_flashMode()");
return;
}
// Start flash mode
Output_firmwareReload();
}
// ----- Functions -----
// Output Module Setup
inline void OutputGen_setup()
{
// Register USB Output CLI dictionary
CLI_registerDictionary( outputCLIDict, outputCLIDictName );
// Initialize variables
Output_Available = 0;
Output_DebugMode = 0;
Output_ExtCurrent_Available = 0;
Output_USBCurrent_Available = 0;
#if defined(_sam_)
// HACK: Will be corrected when usb support is added
Output_USBCurrent_Available = 500;
#endif
}
// Update USB current (mA)
// Triggers power change event
void Output_update_usb_current( unsigned int current )
{
// Only signal if changed
if ( current == Output_USBCurrent_Available )
return;
// Update USB current
Output_USBCurrent_Available = current;
/* XXX Affects sleep states due to USB messages
unsigned int total_current = Output_current_available();
info_msg("USB Available Current Changed. Total Available: ");
printInt32( total_current );
print(" mA" NL);
*/
// Send new total current to the Scan Modules
Scan_currentChange( Output_current_available() );
}
// Update external current (mA)
// Triggers power change event
void Output_update_external_current( unsigned int current )
{
// Only signal if changed
if ( current == Output_ExtCurrent_Available )
return;
// Update external current
Output_ExtCurrent_Available = current;
unsigned int total_current = Output_current_available();
info_msg("External Available Current Changed. Total Available: ");
printInt32( total_current );
print(" mA" NL);
// Send new total current to the Scan Modules
Scan_currentChange( Output_current_available() );
}
// Power/Current Available
unsigned int Output_current_available()
{
unsigned int total_current = 0;
// Check for USB current source
total_current += Output_USBCurrent_Available;
// Check for external current source
total_current += Output_ExtCurrent_Available;
// XXX If the total available current is still 0
// Set to 100 mA, which is generally a safe assumption at startup
// before we've been able to determine actual available current
if ( total_current == 0 )
{
total_current = 100;
}
return total_current;
}
// ----- CLI Command Functions -----
void cliFunc_current( char* args )
{
// Parse number from argument
// NOTE: Only first argument is used
char* arg1Ptr;
char* arg2Ptr;
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
if ( arg1Ptr[0] != '\0' )
{
Output_update_external_current( (unsigned int)numToInt( arg1Ptr ) );
}
print( NL );
info_msg("Current available: ");
printInt16( Output_current_available() );
print(" mA");
}
void cliFunc_outputDebug( char* args )
{
// Parse number from argument
// NOTE: Only first argument is used
char* arg1Ptr;
char* arg2Ptr;
CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
// Default to 1 if no argument is given
Output_DebugMode = Output_DebugMode ? 0 : 1;
if ( arg1Ptr[0] != '\0' )
{
Output_DebugMode = (uint16_t)numToInt( arg1Ptr );
}
}