forked from marcel-licence/esp32_basic_synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc_module.ino
177 lines (152 loc) · 4.63 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
/*
* 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
*/
#ifdef ADC_TO_MIDI_LOOKUP_SIZE
struct adc_to_midi_s
{
uint8_t ch;
uint8_t cc;
};
extern struct adc_to_midi_s adcToMidiLookUp[]; /* definition in z_config.ino */
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);
analogSetCycles(1);
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;
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);
#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)
{
Midi_ControlChange(adcToMidiLookUp[idx].ch, adcToMidiLookUp[idx].cc, midiValueU7);
lastSendVal[idx] = midiValueU7;
}
}
#ifdef ADC_DEBUG_CHANNEL0_DATA
switch (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;
}
#endif