-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathadc_module.ino
226 lines (200 loc) · 6.51 KB
/
adc_module.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
/*
* The GNU GENERAL PUBLIC LICENSE (GNU GPLv3)
*
* Copyright (c) 2023 Marcel Licence
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
* veröffentlichten Version, weiter verteilen und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
* OHNE JEDE GEWÄHR,; sogar ohne die implizite
* Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Einzelheiten.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
*/
/*
* This module is run adc with a multiplexer
* tested with ESP32 Audio Kit V2.2
* Only tested with 8 inputs
*
* Define your adc mapping in the lookup table
*
* Author: Marcel Licence
*
* Reference: https://youtu.be/l8GrNxElRkc
*/
struct adc_to_midi_s
{
uint8_t ch;
uint8_t cc;
};
struct adc_to_midi_mapping_s
{
struct adc_to_midi_s *adcToMidi;
int adcToMidiCount;
void(*callback)(uint8_t ch, uint8_t cc, uint8_t value);
};
extern struct adc_to_midi_s adcToMidiLookUp[]; /* definition in z_config.ino */
extern struct adc_to_midi_mapping_s adcToMidiMapping;
uint8_t lastSendVal[ADC_TO_MIDI_LOOKUP_SIZE]; /* define ADC_TO_MIDI_LOOKUP_SIZE in top level file */
#define ADC_INVERT
#define ADC_THRESHOLD (1.0f/200.0f)
#define ADC_OVERSAMPLING 2048
//#define ADC_DYNAMIC_RANGE
//#define ADC_DEBUG_CHANNEL0_DATA
static float adcChannelValue[ADC_INPUTS];
void AdcMul_Init(void)
{
for (int i = 0; i < ADC_INPUTS; i++)
{
adcChannelValue[i] = 0.5f;
}
memset(lastSendVal, 0xFF, sizeof(lastSendVal));
analogReadResolution(10);
analogSetAttenuation(ADC_11db);
#if 0
analogSetCycles(1);
#endif
analogSetClockDiv(1);
adcAttachPin(ADC_MUL_SIG_PIN);
pinMode(ADC_MUL_S0_PIN, OUTPUT);
#if ADC_INPUTS > 2
pinMode(ADC_MUL_S1_PIN, OUTPUT);
#endif
#if ADC_INPUTS > 4
pinMode(ADC_MUL_S2_PIN, OUTPUT);
#endif
#if ADC_INPUTS > 8
pinMode(ADC_MUL_S3_PIN, OUTPUT);
#endif
}
void AdcMul_Process(void)
{
static float readAccu = 0;
static float adcMin = 0;//4000;
static float adcMax = 420453;//410000;
for (int j = 0; j < ADC_INPUTS; j++)
{
digitalWrite(ADC_MUL_S0_PIN, ((j & (1 << 0)) > 0) ? HIGH : LOW);
#if ADC_INPUTS > 2
digitalWrite(ADC_MUL_S1_PIN, ((j & (1 << 1)) > 0) ? HIGH : LOW);
#endif
#if ADC_INPUTS > 4
digitalWrite(ADC_MUL_S2_PIN, ((j & (1 << 2)) > 0) ? HIGH : LOW);
#endif
#if ADC_INPUTS > 8
digitalWrite(ADC_MUL_S3_PIN, ((j & (1 << 3)) > 0) ? HIGH : LOW);
#endif
/* give some time for transition */
delay(1);
readAccu = 0;
#if 0
adcStart(ADC_MUL_SIG_PIN);
for (int i = 0 ; i < ADC_OVERSAMPLING; i++)
{
if (adcBusy(ADC_MUL_SIG_PIN) == false)
{
readAccu += adcEnd(ADC_MUL_SIG_PIN);
adcStart(ADC_MUL_SIG_PIN);
}
}
adcEnd(ADC_MUL_SIG_PIN);
#else
for (int i = 0 ; i < ADC_OVERSAMPLING; i++)
{
readAccu += analogRead(ADC_MUL_SIG_PIN);
}
#endif
#ifdef ADC_DYNAMIC_RANGE
if (readAccu < adcMin - 0.5f)
{
adcMin = readAccu + 0.5f;
Serial.printf("adcMin: %0.3f\n", readAccu);
}
if (readAccu > adcMax + 0.5f)
{
adcMax = readAccu - 0.5f;
Serial.printf("adcMax: %0.3f\n", readAccu);
}
#endif
if (adcMax > adcMin)
{
/*
* normalize value to range from 0.0 to 1.0
*/
float readValF = (readAccu - adcMin) / ((adcMax - adcMin));
readValF *= (1 + 2.0f * ADC_THRESHOLD); /* extend to go over thresholds */
readValF -= ADC_THRESHOLD; /* shift down to allow go under low threshold */
bool midiMsg = false;
/* check if value has been changed */
if (readValF > adcChannelValue[j] + ADC_THRESHOLD)
{
adcChannelValue[j] = (readValF - ADC_THRESHOLD);
midiMsg = true;
}
if (readValF < adcChannelValue[j] - ADC_THRESHOLD)
{
adcChannelValue[j] = (readValF + ADC_THRESHOLD);
midiMsg = true;
}
/* keep value in range from 0 to 1 */
if (adcChannelValue[j] < 0.0f)
{
adcChannelValue[j] = 0.0f;
}
if (adcChannelValue[j] > 1.0f)
{
adcChannelValue[j] = 1.0f;
}
/* MIDI adoption */
if (midiMsg)
{
uint32_t midiValueU7 = (adcChannelValue[j] * 127.999);
if (j < ADC_TO_MIDI_LOOKUP_SIZE)
{
#ifdef ADC_INVERT
uint8_t idx = (ADC_INPUTS - 1) - j;
#else
uint8_t idx = j;
#endif
if (lastSendVal[idx] != midiValueU7)
{
if (adcToMidiMapping.callback != NULL)
{
adcToMidiMapping.callback(adcToMidiLookUp[idx].ch, adcToMidiLookUp[idx].cc, midiValueU7);
}
lastSendVal[idx] = midiValueU7;
}
}
#ifdef ADC_DEBUG_CHANNEL0_DATA
if (j == 0)
{
float adcValFrac = (adcChannelValue[j] * 127.999) - midiValueU7;
Serial.printf("adcChannelValue[j]: %f -> %0.3f -> %0.3f-> %d, %0.3f\n", readAccu, readValF, adcChannelValue[j], midiValueU7, adcValFrac);
}
#endif
}
}
}
}
float *AdcMul_GetValues(void)
{
return adcChannelValue;
}