forked from pichenettes/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtides.cc
executable file
·167 lines (150 loc) · 4.87 KB
/
tides.cc
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
// Copyright 2013 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
#include <stm32f10x_conf.h>
#include "tides/drivers/adc.h"
#include "tides/drivers/dac.h"
#include "tides/drivers/gate_input.h"
#include "tides/drivers/gate_output.h"
#include "tides/drivers/system.h"
#include "tides/cv_scaler.h"
#include "tides/generator.h"
#include "tides/plotter.h"
#include "tides/ui.h"
using namespace tides;
using namespace stmlib;
Adc adc;
CvScaler cv_scaler;
Dac dac;
GateOutput gate_output;
GateInput gate_input;
Generator generator;
Plotter plotter;
System sys;
Ui ui;
// Default interrupt handlers.
extern "C" {
void HardFault_Handler() { while (1); }
void MemManage_Handler() { while (1); }
void BusFault_Handler() { while (1); }
void UsageFault_Handler() { while (1); }
void NMI_Handler() { }
void SVC_Handler() { }
void DebugMon_Handler() { }
void PendSV_Handler() { }
}
// These counters are used to divide the 96kHz DAC update clock into:
// * a 48kHz or 6kHz clock for refreshing the DAC values.
// * a 6kHz clock for reading and smoothing the ADC values (at the exception
// of the LEVEL CV which is read at the sample rate).
static uint32_t dac_divider = 0;
static uint32_t adc_divider = 0;
static const bool debug_rendering = false;
extern "C" {
void SysTick_Handler() {
if (ui.mode() == UI_MODE_FACTORY_TESTING) {
ui.UpdateFactoryTestingFlags(gate_input.Read(), adc.values());
}
ui.Poll();
}
static uint32_t saw_counter = 0;
void TIM1_UP_IRQHandler(void) {
if (TIM_GetITStatus(TIM1, TIM_IT_Update) == RESET) {
return;
}
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
dac.Update();
if (ui.mode() == UI_MODE_FACTORY_TESTING) {
if (dac.ready()) {
dac.Write(saw_counter >> 16, saw_counter >> 16);
saw_counter += 8947848;
gate_output.Write(saw_counter & 0x80000000, saw_counter & 0x80000000);
}
} else if (ui.mode() == UI_MODE_PAQUES) {
if (dac.ready()) {
plotter.Run();
dac.Write(plotter.x(), plotter.y());
}
} else {
if (dac.ready()) {
++dac_divider;
if (dac_divider >= generator.clock_divider()) {
dac_divider = 0;
const GeneratorSample& sample = generator.Process(gate_input.Read());
uint32_t uni = sample.unipolar;
int32_t bi = sample.bipolar;
uint32_t level = cv_scaler.level();
if (ui.mode() >= UI_MODE_CALIBRATION_C2) {
level = 65535; // Bypass VCA in calibration mode!
}
uni = uni * level >> 16;
bi = -bi * level >> 16;
dac.Write(uni, bi + 32768);
if (!debug_rendering) {
gate_output.Write(
sample.flags & FLAG_END_OF_ATTACK,
sample.flags & FLAG_END_OF_RELEASE);
}
ui.set_led_color(uni, sample.flags & FLAG_END_OF_ATTACK);
}
cv_scaler.ProcessSampleRate(adc.values());
}
++adc_divider;
if ((adc_divider & 7) == 0) {
cv_scaler.ProcessControlRate(adc.values());
}
}
}
}
#include "tides/easter_egg/plotter_program.h"
void Init() {
sys.Init(F_CPU / (48000 * 2) - 1, true);
adc.Init(false);
cv_scaler.Init();
dac.Init();
gate_output.Init();
gate_input.Init();
generator.Init();
plotter.Init(plotter_program, sizeof(plotter_program) / sizeof(PlotInstruction));
ui.Init(&generator, &cv_scaler);
sys.StartTimers();
}
int main(void) {
Init();
while (1) {
if (generator.writable_block()) {
if (debug_rendering) {
gate_output.Write(true, true);
}
generator.set_pitch(cv_scaler.pitch());
generator.set_shape(cv_scaler.shape());
generator.set_slope(cv_scaler.slope());
generator.set_smoothness(cv_scaler.smoothness());
generator.Process();
if (debug_rendering) {
gate_output.Write(false, false);
}
}
ui.DoEvents();
}
}