-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02-ui24r.ino
339 lines (290 loc) · 11 KB
/
02-ui24r.ino
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#define INPUTS 22
#define AUX 8
#include <stdlib.h>
const uint32_t levelFactor = 1000000;
//const uint32_t zeroDbPos = levelFactor * 0.7647058823529421;
const uint32_t zeroDbPos = 764705;
uint32_t auxLevels[AUX];
uint32_t inputToMasterLevels[INPUTS];
uint32_t inputToAuxLevels[INPUTS][AUX];
uint32_t groupMixValue = 300000;
int8_t inputStereoIndex[INPUTS];
int8_t auxStereoIndex[AUX];
char inputNames[INPUTS][30];
char auxNames[AUX][30];
bool inputMutes[INPUTS];
bool auxMutes[AUX];
// needed to determine if we have all initial params from mixer
bool allDataCollected = false;
uint8_t collectedInputStereoIndex = 0;
uint8_t collectedInputNames = 0;
uint8_t collectedInputMutes = 0;
uint8_t collectedInputToMasterLevels = 0;
uint16_t collectedInputToAuxLevels = 0;
uint8_t collectedAuxNames = 0;
uint8_t collectedAuxStereoIndex = 0;
uint8_t collectedAuxMutes = 0;
uint8_t collectedAuxLevels = 0;
/**
* this function splits the message by \n and drops out a huge
* amount of data that is not relevant for us
*/
void handleIncomingMixerMessage(String msg) {
String isolatedMessage = "";
bool dropMessage = false;
// we need to split by new line
for(unsigned int i = 0; i<msg.length(); i++) {
if(msg[i] == '\n') {
if(dropMessage == true) {
////debug("dropping msg");
isolatedMessage = "";
dropMessage = false;
continue;
}
parseIncomingMessage(isolatedMessage);
dropMessage = false;
isolatedMessage = "";
continue;
}
// isolatedMessage += (msg[i] == '^') ? '=' : msg[i]; // regex lib cant handle circumflex char. at least i wasn't able to write the regex
isolatedMessage += msg[i];
if(isolatedMessage == "2::" || isolatedMessage == "3:::") {
isolatedMessage = "";
continue;
}
if(isolatedMessage.length() == 4) {
if(isolatedMessage == "SETS" || isolatedMessage == "SETD") {
continue;
}
dropMessage = true;
////debug("going to drop " + isolatedMessage);
}
}
if(dropMessage == false) {
parseIncomingMessage(isolatedMessage);
isolatedMessage = "";
}
}
void incrementDataCollectCounter(String subject) {
if(allDataCollected == true) {
updateGroupMix();
return;
}
////debug(String(getPercentCollectedAllData()));
if(subject == "collectedAuxStereoIndex") { collectedAuxStereoIndex++; return; }
if(subject == "collectedAuxLevels") { collectedAuxLevels++; return; }
if(subject == "collectedInputToAuxLevels") { collectedInputToAuxLevels++; return; }
if(subject == "collectedAuxNames") { collectedAuxNames++; return; }
if(subject == "collectedInputToMasterLevels") { collectedInputToMasterLevels++; return; }
if(subject == "collectedInputStereoIndex") { collectedInputStereoIndex++; return; }
if(subject == "collectedInputNames") { collectedInputNames++; return; }
if(subject == "collectedInputMutes") { collectedInputMutes++; return; }
if(subject == "collectedAuxMutes") { collectedAuxMutes++; return; }
}
/**
due to performance reasons we can't use String::indexOf() or Regex lib ( https://github.com/nickgammon/Regexp )
so we check for specific character positions that are hopefully unique.
based on those we can filter which params are useful and which can be ignored
*/
void parseIncomingMessage(String msg) {
bool isInput = false;
bool isAux = false;
if (msg[6] != '.') {
// we are only interested in i.* and a.*
// this will dropout [automix.time]
return;
}
if (msg[5] == 'a') {
isAux = true;
}
if (msg[5] == 'i') {
isInput = true;
}
if (isAux == false && isInput == false) {
////debug("dropping (not i or a): ");
////debug(msg);
return;
}
if (msg[8] != 'a' && msg[9] == 'm' && msg[10] == 'i' && msg[11] == 'x') { // we have mix param with one digit as channel [i.1.mix, a.7.mix]
// not to confuse with mtx [a.0.mtx.4.postproc]
// drop out stuff like a.0.mtx.4.postproc, i.4.amixgroup, i.4.amix
return handleParamMix(isInput, isAux, false, msg);
}
if (msg[9] != 'a' && msg[10] == 'm' && msg[11] == 'i' && msg[12] == 'x') { // we have mix param with two digit as channel [i.10.mix]
return handleParamMix(isInput, isAux, true, msg);
}
if (msg[15] == 'I' && msg[19] == 'x') { // we have stereoIndex param with one digit as channel [i.0.stereoIndex, a.7.stereoIndex]
return handleParamStereoIndex(isInput, isAux, false, msg);
}
if (msg[16] == 'I' && msg[20] == 'x') { // we have stereoIndex param with two digits as channel [i.18.stereoIndex]
return handleParamStereoIndex(isInput, isAux, true, msg);
}
if (msg[10] == 'u' && msg[18] == 'u') { // we have send aux param with one digit as channel [i.1.aux.2.value]
return handleParamAuxSend(false, msg);
}
if (msg[11] == 'u' && msg[19] == 'u') { // we have send aux param with two digits as channel [i.14.aux.2.value]
return handleParamAuxSend(true, msg);
}
if (msg[9] == 'n' && msg[12] == 'e') { // we have name param with one digit as channel [a.0.name]
return handleParamName(isInput, isAux, false, msg);
}
if (msg[10] == 'n' && msg[13] == 'e') { // we have name param with two digits as channel [i.12.name]
return handleParamName(isInput, isAux, true, msg);
}
if (msg[9] == 'm' && msg[12] == 'e') { // we have mute param with one digit as channel [a.0.mute]
return handleParamMute(isInput, isAux, false, msg);
}
if (msg[10] == 'm' && msg[13] == 'e') { // we have mute param with two digits as channel [i.12.mute]
return handleParamMute(isInput, isAux, true, msg);
}
}
void handleParamMix(bool isInput, bool isOutput, bool isTwoDigitChannel, String msg) {
////debug("handleParamMix() "); //debug(msg);
uint8_t channelNumberInt = extractChannelNumberFromMessage(msg, isTwoDigitChannel);
if (isInput == true) {
if (channelNumberInt >= INPUTS) {
return; // input index too big
}
inputToMasterLevels[channelNumberInt] = levelToInternal(getDelimitedValue(msg, '^', 2));
incrementDataCollectCounter("collectedInputToMasterLevels");
return;
}
if (channelNumberInt >= AUX) {
return; // aux index too big
}
auxLevels[channelNumberInt] = levelToInternal(getDelimitedValue(msg, '^', 2));
collectedAuxLevels++;
incrementDataCollectCounter("collectedAuxLevels");
}
void handleParamStereoIndex(bool isInput, bool isOutput, bool isTwoDigitChannel, String msg) {
////debug("handleParamStereoIndex() "); //debug(msg);
uint8_t channelNumberInt = extractChannelNumberFromMessage(msg, isTwoDigitChannel);
int8_t incomingStereoValue = stereoIndexValue(getDelimitedValue(msg, '^', 2));
if (isInput == true) {
if (channelNumberInt >= INPUTS) {
return; // input index too big
}
inputStereoIndex[channelNumberInt] = incomingStereoValue;
incrementDataCollectCounter("collectedInputStereoIndex");
return;
}
if (channelNumberInt >= AUX) {
return; // aux index too big
}
auxStereoIndex[channelNumberInt] = incomingStereoValue;
incrementDataCollectCounter("collectedAuxStereoIndex");
}
void handleParamAuxSend(bool isTwoDigitChannel, String msg) {
////debug("handleParamAuxSend() "); //debug(msg);
uint8_t channelNumberInt = extractChannelNumberFromMessage(msg, isTwoDigitChannel);
uint8_t auxChannel = extractAuxSendChannelNumberFromMessage(msg, isTwoDigitChannel);
if (channelNumberInt >= INPUTS) {
return; // input index too big
}
if (auxChannel >= AUX) {
return; // aux index too big
}
////debug("with internal handleParamAuxSend: ");
////debug(levelToInternal(getDelimitedValue(msg, '^', 2)));
inputToAuxLevels[channelNumberInt][auxChannel] = levelToInternal(getDelimitedValue(msg, '^', 2));
incrementDataCollectCounter("collectedInputToAuxLevels");
}
void handleParamName(bool isInput, bool isOutput, bool isTwoDigitChannel, String msg) {
////debug("handleParamName() "); //debug(msg);
uint8_t channelNumberInt = extractChannelNumberFromMessage(msg, isTwoDigitChannel);
if (isInput == true) {
if (channelNumberInt >= INPUTS) {
return; // input index too big
}
getDelimitedValue(msg, '^', 2).toCharArray(inputNames[channelNumberInt], 30);
incrementDataCollectCounter("collectedInputNames");
return;
}
if (channelNumberInt >= AUX) {
return; // aux index too big
}
getDelimitedValue(msg, '^', 2).toCharArray(auxNames[channelNumberInt], 30);
incrementDataCollectCounter("collectedAuxNames");
}
void handleParamMute(bool isInput, bool isOutput, bool isTwoDigitChannel, String msg) {
////debug("handleParamMute() " + msg);
uint8_t channelNumberInt = extractChannelNumberFromMessage(msg, isTwoDigitChannel);
if (isInput == true) {
if (channelNumberInt >= INPUTS) {
return; // input index too big
}
inputMutes[channelNumberInt] = ( getDelimitedValue(msg, '^', 2) == "1") ? true : false;
incrementDataCollectCounter("collectedInputMutes");
return;
}
if (channelNumberInt >= AUX) {
return; // aux index too big
}
auxMutes[channelNumberInt] = ( getDelimitedValue(msg, '^', 2) == "1") ? true : false;
incrementDataCollectCounter("collectedAuxMutes");
}
uint8_t extractChannelNumberFromMessage(String msg, bool isTwoDigitChannel) {
char channelNumberString[3];
channelNumberString[0] = msg[7];
if (isTwoDigitChannel == true) {
channelNumberString[1] = msg[8];
}
return atoi(channelNumberString);
}
uint8_t extractAuxSendChannelNumberFromMessage(String msg, bool isTwoDigitChannel) {
char channelNumberString[2];
channelNumberString[0] = (isTwoDigitChannel == true) ? msg[14] : msg[13];
return atoi(channelNumberString);
}
int8_t stereoIndexValue(String level) {
char sVal[4];
level.toCharArray(sVal, 3);
if (strcmp(sVal, "-1") == 0) {
return -1;
}
if (strcmp(sVal, "1") == 0) {
return 1;
}
return 0;
}
String getDelimitedValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
/**
instead of dealing with floatvalues like "SETD=i.11.mix=0.1015118791" our
internal handling is with unsigned integers
*/
uint32_t levelToInternal(String level) {
if (level == "0") {
return 0;
}
uint32_t convertedLevel = level.toFloat() * levelFactor;
return (convertedLevel >= levelFactor) ? levelFactor : convertedLevel;
}
/**
* convert the internal level handling (1 - 1000000) to ui24r compatible float value (0-1)
* thanks to https://forum.arduino.cc/index.php?topic=243660.msg1748437#msg1748437
*/
String internalToLevel(uint32_t level) {
if(level >= levelFactor) {
return String("1");
}
if(level <= 0) {
return String("0");
}
float val = level / (levelFactor + 0.0);
char buff[10];
dtostrf(val, 4, 6, buff); //4 is mininum width, 6 is precision
return String(buff);
}