-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrillRackInterface.cpp
236 lines (224 loc) · 6.01 KB
/
TrillRackInterface.cpp
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
#include "TrillRackInterface.h"
#include <string.h>
#include <algorithm>
#ifdef STM32
#include <stdint.h>
extern "C" { int32_t HAL_GetTick(void); };
#else // STM32
#include <Bela.h>
#endif // STM32
TrillRackInterface::TrillRackInterface(unsigned int anInCh, unsigned int anOutCh0, unsigned int anOutCh1, unsigned int diInCh0, unsigned int diOutCh0, unsigned int diOutCh1)
{
setup(anInCh, anOutCh0, anOutCh1, diInCh0, diOutCh0, diOutCh1);
}
int TrillRackInterface::setup(unsigned int anInCh, unsigned int anOutCh0, unsigned int anOutCh1, unsigned int diInCh0, unsigned int diOutCh0, unsigned int diOutCh1)
{
this->anInCh = anInCh;
this->diInCh = diInCh0;
this->anOutCh[0] = anOutCh0;
this->anOutCh[1] = anOutCh1;
this->diOutCh[0] = diOutCh0;
this->diOutCh[1] = diOutCh1;
debounceCounter = 0;
firstRun = true;
anIn = 0;
for(unsigned int c = 0; c < nDigOut; ++c)
ledOut[c] = 0;
lastTimeMs = 0;
#ifdef USE_SCOPE
scopeInited = false;
memset(scopeData, 0, sizeof(scopeData));
#endif //USE_SCOPE
memset(anOut, 0, sizeof(anOut));
return 0;
}
float TrillRackInterface::analogRead()
{
return anIn;
}
float TrillRackInterface::digitalRead(unsigned int channel)
{
if(channel < 1)
return diIn;
else
return 0;
}
void TrillRackInterface::buttonLedWrite(unsigned int ch, float val)
{
if(ch < nDigOut)
ledOut[ch] = val;
}
void TrillRackInterface::buttonLedSet(ButtonLedStyle style, ButtonLedColor color, float intensity, float durationMs)
{
for(size_t n = 0; n < kNumButtonColors; ++n)
{
if(int(color) == n || color == kAll)
{
auto& lct = buttonLedColorTimeouts[n];
float phase = lct.phase;
if(style != lct.style || intensity != lct.intensity)
phase = 0;
lct = { style, intensity, durationMs, phase };
}
}
}
void TrillRackInterface::analogWrite(unsigned int channel, float val)
{
if(channel < nAnOut)
anOut[channel] = val;
}
void TrillRackInterface::scopeWrite(float* data)
{
#ifdef USE_SCOPE
memcpy(scopeData, data, sizeof(scopeData));
#endif // USE_SCOPE
}
void TrillRackInterface::scopeWrite(unsigned int channel, float val)
{
#ifdef USE_SCOPE
if(channel < kScopeChannels)
scopeData[channel] = val;
#endif // USE_SCOPE
}
double TrillRackInterface::getTimeMs() {
return lastTimeMs;
}
#include <stdio.h>
void TrillRackInterface::process(BelaContext* context)
{
#ifdef USE_SCOPE
lastTimeMs = context->audioFramesElapsed / context->audioSampleRate * 1000;
unsigned int scopeEvery = 4;
if(!scopeInited) {
// we initialise here so setup and constructor don't need context
scope.setup(kScopeChannels, context->audioSampleRate / scopeEvery);
scopeInited = true;
}
for(unsigned int n = 0; n < context->audioFrames / scopeEvery; ++n)
scope.log(scopeData);
if(anInCh < context->analogInChannels)
anIn = ::analogRead(context, 0, anInCh);
for(unsigned int m = 0; m < nDiIn; ++m)
if(diInCh[m] < context->digitalChannels)
diIn[m] = ::digitalRead(context, 0, diInCh[m]);
if(diOutCh < context->digitalChannels)
::pinMode(context, 0, diOutCh, OUTPUT); // Set diOutCh as output
::digitalWrite(context, 0, diOutCh, diOut);
for(unsigned int i = 0; i < nAnOut; ++i)
if(anOutCh[i] < context->analogOutChannels)
::analogWrite(context, 0, anOutCh[i], anOut[i]);
#endif // USE_SCOPE
#ifdef STM32
lastTimeMs = HAL_GetTick();
if(firstRun)
{
//emulation of Bela's setup()
firstRun = false;
if(diInCh >= context->digitalChannels
|| diOutCh[0] >= context->digitalChannels
|| diOutCh[1] >= context->digitalChannels
|| anInCh >= context->analogInChannels
|| anOutCh[0] >= context->analogOutChannels
|| anOutCh[1] >= context->analogOutChannels
)
{
printf("Invalid channels\n\r");
return; //false
}
pinMode(context, 0, diInCh, INPUT);
for(unsigned int c = 0; c < nDigOut; ++c)
pinMode(context, 0, c, OUTPUT);
return; //true
}
for(size_t n = 0; n < context->digitalFrames; ++n)
{
bool currentIn = ::digitalRead(context, 0, diInCh);
if(debounceCounter)
{
debounceCounter--;
} else {
if(currentIn != diIn)
debounceCounter = 0.01f * context->digitalSampleRate; // ms debounce
diIn = currentIn;
}
}
anIn = ::analogRead(context, 0, anInCh);
tr_render(context);
// process color events
float blockMs = context->analogFrames / context->analogSampleRate * 1000.f;
ledOut[0] = 0;
ledOut[1] = 0;
for(size_t n = 0; n < kNumButtonColors; ++n)
{
LedColorsTimeout& lct = buttonLedColorTimeouts[n];
float coeff = 1;
switch(lct.style)
{
case kOff:
coeff = 0;
break;
case kSolid:
coeff = 1;
break;
case kGlow:
coeff = std::abs(lct.phase - 0.5f) * 0.9f + 0.1f;
lct.phase += 1.5f * (context->analogFrames / context->analogSampleRate);
if(lct.phase > 1)
lct.phase -= 1;
break;
}
float intensity = lct.intensity * (lct.ms > 0) * (lct.style != kOff) * coeff;
// map colors to out channels with appropriate intensity adjustments
// the latter would override the others
if(intensity)
{
switch(n)
{
case kR:
ledOut[1] = intensity;
break;
case kG:
ledOut[0] = intensity;
break;
case kY:
ledOut[0] = intensity * 0.15f;
ledOut[1] = intensity;
}
}
if(lct.ms)
{
lct.ms -= blockMs;
if(lct.ms < 0)
lct.ms = 0;
}
}
static constexpr size_t kLedPwmPeriod = 256;
static_assert((sizeof(ledPwmIdx) << 8) >= kLedPwmPeriod);
for(size_t n = 0; n < context->digitalFrames; ++n)
{
// scramble bit pattern to try and
static_assert(kLedPwmPeriod <= 256);
size_t idx = ledPwmIdx;
size_t activeC = idx & 1;
for(size_t c = 0; c < nDigOut; ++c)
{
bool isActive;
if(c == activeC)
isActive = idx < ledOut[activeC] * float(kLedPwmPeriod);
else
isActive = false;
::digitalWriteOnce(context, n, diOutCh[c], isActive);
}
ledPwmIdx++;
if(kLedPwmPeriod == ledPwmIdx) // never actually getting here if kLedPwmPeriod == 256
ledPwmIdx = 0;
}
#if 0
static unsigned int count = 0;
gDacNext[0] = count++ / 4096.f;
gDacNext[1] = count / 4096.f;
if(count >= 4096)
count = 0;
#endif
#endif // STM32
}