Repository navigation
Expand file tree
/
Copy pathChannelManager.h
More file actions
251 lines (215 loc) · 11 KB
/
Copy pathChannelManager.h
File metadata and controls
251 lines (215 loc) · 11 KB
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
250
251
#include <stdint.h>
#include "UI.h"
//#include "usbh_helper.h"
#include "Adafruit_TinyUSB.h"
#include "pio_usb.h"
#include "MultiModule.h"
#ifndef CHANNEL_MANAGER_H
#define CHANNEL_MANAGER_H
//#define MAX(a,b) ((a) > (b) ? (a) : (b))
//MAX_INPUTS > MAX_CHANNELS to allow for unused inputs from other input sources. 128 allows for USB keyboard, multiple controllers, etc
#define MAX_INPUTS 128
//15 characters plus null-termination
#define INPUT_NAME_LEN 16
#define MIXER_NAME_LEN 16
#define OUTPUT_NAME_LEN 16
//may want to increase mixers beyond max_channels to allow more complex nested mixes
#define MAX_MIXERS MAX_CHANNELS
#define MAX_USB_DEVICE_DESCRIPTORS 8
#define MAX_INPUT_CHANNELS_PER_USB_DEVICE 16
#define USB_INPUT_CHANNEL_NAME_LEN 4 //3 chars plus null, such as "Lx", "A", "Ch2", etc
//valid for USB FS only, USB 2 reports may be up to 512 bytes long
#define HID_REPORT_BUFSIZE 64
//range of output values supported by multi module, also the 'native' range used by the system
#define NATIVE_MIN_VALUE 0
#define NATIVE_MID_VALUE 1024
#define NATIVE_MAX_VALUE 2047
#define NATIVE_BITCOUNT 11
typedef enum {
INPUT_FUNCTION_CONST_FIXED, //hardcoded, nonconfigurable value
INPUT_FUNCTION_CONFIG_VALUE, //default value, but configurable
INPUT_FUNCTION_IO_ADC, //taken by ADC input to pi pico
INPUT_FUNCTION_IO_SWITCH, //boolean value from hardwired switch
INPUT_FUNCTION_USB_VALUE, //analog value from USB device input //todo implement/precisely define
INPUT_FUNCTION_USB_SWITCH, //boolean value from USB device switch //todo implement/precisely define
INPUT_FUNCTION_IR_SWITCH, //boolean value from IR remote //todo implement/precisely define
INPUT_FUNCTION_USB_KB_NUMBER, //number 0-9 from USB keyboard
INPUT_FUNCTION_USB_GAMEPAD_STICK, //analog stick, or other 1-byte values in USB report array
INPUT_FUNCTION_MIXER, //internal mixer 'virtual input'
INPUT_FUNCTION_USB_GAMEPAD_BUTTON, //digital 1-bit inputs from USB report array
INPUT_FUNCTION_UNKNOWN,
} InputFunctionType;
typedef struct InputChannelDescriptor InputChannelDescriptor;
//the input function to get updated data. If id is used when generating a value, any given function MUST only be used to handle a single type as defined by InputFunctionType
//outChannelId must be a valid channel number or -1. It should be used only as required for populating fallback values
typedef int (*GetChannelInputFn)(void* context, int id, int outChannelId);
//function for configuring settings for a given input. If no settings available, null ptr instead
typedef bool (*ConfigChannelInputFn)(struct InputChannelDescriptor *channel, int value);
typedef void (*CleanupInputContextFn)(void* context, InputFunctionType inputFunctionType);
struct InputChannelDescriptor{
GetChannelInputFn getLatestInputData; // Function pointer to retrieve latest input data, pass the ID as argument
ConfigChannelInputFn configureChannelInput;
CleanupInputContextFn cleanupInputContextFn; //if non-null, call this function to cleanup context data (such as SimpleFreeInputContext for malloc)
int id; // Unique ID number
InputFunctionType inputFunctionType; //type is used as a namespace for ID numbers, and to describe which operations are supported
void * context; //reserved to allow passing additional context to the function, with exact layout expected to be consistient within a given InputFunctionType
int minRange; // Minimum range value
int maxRange; // Maximum range value
char name[INPUT_NAME_LEN]; //short, NULL-terminated ASCII name
};
/* pool structure, allow adding/removing inputs when devices connect */
typedef struct {
InputChannelDescriptor items[MAX_INPUTS];
bool used[MAX_INPUTS];
int capacity;
int count;
} InputDescriptorPool;
//mixer operations: a suffix of S indicates the processing targets signed values (-1024 to 1023) rather than raw 0 to 1047 range.
typedef enum {
MIXER_OP_CH1_ONLY, //ignore Channel2, only process channel 1 mix
MIXER_OP_ADD,
MIXER_OP_ADDS,
MIXER_OP_AVG,
MIXER_OP_DIFF, //get absolute value of difference
MIXER_OP_SUB,
MIXER_OP_SUBS,
MIXER_OP_MUL, //multiply values
MIXER_OP_MULS,
MIXER_OP_DIV, //divide Channel1 by Channel2
MIXER_OP_DIVS,
MIXER_OP_MIN, //useful for lock-out switch
MIXER_OP_MAX,
MIXER_OP_AMIN,
MIXER_OP_AMAX,
MIXER_OP_ENUM_OVERFLOW,
MIXER_OP_ENUM_UNDERFLOW=-1
} MixerCombineOperation;
static const char* MixerCombineOperationString [] = {
"Ch1",
"ADD",
"ADDS",
"AVG",
"DIFF",
"SUB",
"SUBS",
"MUL",
"MULS",
"DIV",
"DIVS",
"MIN",
"MAX",
"AMIN",
"AMAX",
"Err"
};
typedef struct {
int id;
MixerCombineOperation operation;
InputChannelDescriptor *inputChannel1Descriptor;
int channel1Offset;
int channel1Scale; //scale as percentage
bool channel1Invert;
InputChannelDescriptor *inputChannel2Descriptor;
int channel2Offset;
int channel2Scale; //scale as percentage
bool channel2Invert;
char name[MIXER_NAME_LEN];
InputChannelDescriptor *mixerResultDescriptor;
} MixerChannelDescriptor; //short, NULL-terminated ASCII name
typedef enum {
MIXER_EDIT_STATE_CHANGE_MIXER_ITEM = 0,
MIXER_EDIT_STATE_OPERATION,
MIXER_EDIT_STATE_A_SOURCE,
MIXER_EDIT_STATE_A_SCALE,
MIXER_EDIT_STATE_A_INVERT,
MIXER_EDIT_STATE_A_OFFSET,
MIXER_EDIT_STATE_B_SOURCE,
MIXER_EDIT_STATE_B_SCALE,
MIXER_EDIT_STATE_B_INVERT,
MIXER_EDIT_STATE_B_OFFSET,
MIXER_EDIT_STATE_ENUM_OVERFLOW,
MIXER_EDIT_STATE_ENUM_UNDERFLOW=-1
} mixerEditTargetState;
typedef struct {
MixerChannelDescriptor *currentlyEditingMixer;
mixerEditTargetState mixerActiveEditState;
} mixerMenuItemHandlerContext;
typedef struct {
InputChannelDescriptor *inputChannelDescriptor; // Pointer to input channel descriptor
uint8_t outputChannelNumber; // Output channel number 0-15 (matches index of array)
int minRange; // Minimum range value
int maxRange; // Maximum range value
char name[OUTPUT_NAME_LEN]; //short, NULL-terminated ASCII name
} OutputChannelDescriptor;
/// HID Interface Protocol - extended (based on tinyUSB hid_interface_protocol_enum_t)
typedef enum
{
USB_DESCRIPTOR_PROTOCOL_NONE = 0, ///< None
USB_DESCRIPTOR_PROTOCOL_KEYBOARD = 1, ///< Keyboard
USB_DESCRIPTOR_PROTOCOL_MOUSE = 2, ///< Mouse
USB_DESCRIPTOR_PROTOCOL_GAMEPAD = 3 // new, for USB gamepads (may need additional sub-categories)
}hid_interface_protocol_extended_enum_t;
typedef struct {
char name[INPUT_NAME_LEN-USB_INPUT_CHANNEL_NAME_LEN];
uint16_t vid, pid;
uint8_t reportLength;
uint8_t analogInputCount;
uint8_t analogInputReportOffsets[MAX_INPUT_CHANNELS_PER_USB_DEVICE]; //what byte in the HID report corresponds to this input
char analogInputNames[MAX_INPUT_CHANNELS_PER_USB_DEVICE][USB_INPUT_CHANNEL_NAME_LEN]; //array of names for channels
uint8_t digitalInputCount;
uint8_t digitalInputReportByteOffsets[MAX_INPUT_CHANNELS_PER_USB_DEVICE]; //what byte in the HID report contains this input
uint8_t digitalInputReportBitMask[MAX_INPUT_CHANNELS_PER_USB_DEVICE]; //mask to filter which bit from above byte contains this input
char digitalInputNames[MAX_INPUT_CHANNELS_PER_USB_DEVICE][USB_INPUT_CHANNEL_NAME_LEN];
} USBGamepadLayoutDefinition;
typedef struct {
tusb_desc_device_t desc_device;
uint16_t manufacturer[32];
uint16_t product[48];
uint16_t serial[16];
bool mounted;
} usb_dev_info_t;
typedef struct {
usb_dev_info_t usb_dev_info;
uint8_t hidInterfaceType; //intended to match values in hid_interface_protocol_extended_enum_t
uint16_t vid, pid;
uint8_t dev_addr, instance; //note: device may have multiple instances, in which case each will have a distinct USBInputDeviceDescriptor
char name[INPUT_NAME_LEN];
uint8_t latest_report[HID_REPORT_BUFSIZE];
uint8_t deviceNum; //descriptor number within USBDeviceDescriptors (max of MAX_USB_DEVICE_DESCRIPTORS)
InputChannelDescriptor *inputChannels[MAX_INPUT_CHANNELS_PER_USB_DEVICE]; //array of pointers to inputs owned by this USB device
const USBGamepadLayoutDefinition *layoutDef; //pointer to the relevant layout definition
void* contextPointer; //pointer to the device-type-specific context, used for individual input devices, to free when USB device disconnected
} USBInputDeviceDescriptor;
typedef struct {
USBInputDeviceDescriptor *parentDeviceDescriptor;
int latestValue;
unsigned long lastUpdateMillis;
} USBKeyboardContextType;
typedef struct {
USBInputDeviceDescriptor *parentDeviceDescriptor;
unsigned long lastUpdateMillis;
} USBGamepadContextType;
int normalizeScaleRange(int value, int inMin, int inMax, int outMin, int outMax);
int normalizeScaleRange(int value, int inMin, int inMax);
int Pool_FindIndexById(InputDescriptorPool *pool, int id);
void Pool_Init(InputDescriptorPool *pool, const InputChannelDescriptor *prototype);
void Pool_Release(InputDescriptorPool *pool, InputChannelDescriptor *desc);
InputChannelDescriptor *Pool_FindByIdAndType(InputDescriptorPool *pool, int id, InputFunctionType type);
int Pool_FindIndexByIdAndType(InputDescriptorPool *pool, int id, InputFunctionType type);
int Pool_FindNextUsedIndex(InputDescriptorPool *pool, int index);
int Pool_FindPreviousUsedIndex(InputDescriptorPool *pool, int index);
InputChannelDescriptor* findInputDescriptorWithTypeNameAndId(InputDescriptorPool *pool, InputFunctionType type, const char* name, int id);
void initDefaultInputDescriptors(InputDescriptorPool *pool);
void initOutputAndDefaultInputChannelDescriptors(OutputChannelDescriptor *outputChannels,InputDescriptorPool *inputChannelPool, MixerChannelDescriptor (&mixerChannels)[MAX_MIXERS], InputChannelDescriptor* (&failsafeChannels)[MAX_CHANNELS]);
void assignInputChannelDescriptor(OutputChannelDescriptor *outputChannel, InputChannelDescriptor *inputChannel);
int getLatestInputData(InputChannelDescriptor* input, int outChannelId); //use DataProducer to get latest input data
int evaluateSingleChannelMixerValue(MixerChannelDescriptor* mixer, bool channel1);
int defaultInputDataProducer(void* context, int id, int outChannelId);
int fixedInputDataProducer(void* context, int id, int outChannelId);
int getFirstFreeUSBInputDescriptorIndex(USBInputDeviceDescriptor* arr);
void releaseUSBInputChannels(InputDescriptorPool *pool, USBInputDeviceDescriptor *desc, OutputChannelDescriptor (&outChannelsArr)[MAX_CHANNELS], MixerChannelDescriptor (&mixerChannels)[MAX_MIXERS], InputChannelDescriptor* (&failsafeChannels)[MAX_CHANNELS]);
USBInputDeviceDescriptor* findUSBDescriptorByDevAddrAndInstance(USBInputDeviceDescriptor* arr, uint8_t dev_addr, uint8_t instance);
InputChannelDescriptor *AllocateUSBKeyboardNumberInputChannel(InputDescriptorPool *pool, int id, const char* name, USBInputDeviceDescriptor * descriptor);
USBGamepadContextType* AllocateUSBGamepadStickChannels(InputDescriptorPool *pool, USBInputDeviceDescriptor * descriptor);
InputChannelDescriptor *FindInputChannelDescriptorByInputNameSuffix(InputDescriptorPool *pool, const char *inputName, int suffixLength);
#endif