-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBangDaButton.cpp
220 lines (164 loc) · 8.11 KB
/
BangDaButton.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
/*
BangDaButton
a cv sending button to trigger/switch things.
button provides gate/trig outs for press and release events as well
as two flipflops.
it also opens/closes 4 A/B switches 2 x 1to2 and 2 x 2to1
a "hidden" gate port can be used to control the button with external sources.
TODO:
toggle/momentary modes for the channels
toggles for the flipflops
*/////////////////////////////////////////////////////////////////////////////
#include "pvc.hpp"
#include "dsp/digital.hpp" // SchmittTrigger // PulseGenerator
struct BangDaButton : Module {
enum ParamIds {
DA_BUTTON,
NUM_PARAMS
};
enum InputIds {
UP_SW_A_IN,
UP_SW_B_IN,
UP_CH1_IN,
UP_MUX_IN,
UP_CH2_IN,
DOWN_SW_A_IN,
DOWN_SW_B_IN,
DOWN_CH1_IN,
DOWN_MUX_IN,
DOWN_CH2_IN,
DA_BUTTON_TRIG,
NUM_INPUTS
};
enum OutputIds {
UP_SW_OUT,
UP_CH1_OUT,
UP_MUX_A_OUT, UP_MUX_B_OUT,
UP_CH2_OUT,
UP_FLIP_OUT, UP_TRIG_OUT, UP_GATE_OUT,
DOWN_GATE_OUT, DOWN_TRIG_OUT, DOWN_FLIP_OUT,
DOWN_CH1_OUT,
DOWN_MUX_A_OUT, DOWN_MUX_B_OUT,
DOWN_CH2_OUT,
DOWN_SW_OUT,
NUM_OUTPUTS
};
enum LightIds {
UP_LED,
DOWN_LED,
NUM_LIGHTS
};
bool pressed = false;
bool flipUp = true;
bool flipDn = false;
SchmittTrigger upFlipTrg;
SchmittTrigger dnFlipTrg;
PulseGenerator upPulse;
PulseGenerator dnPulse;
BangDaButton() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
void step() override;
void reset() override {
pressed = false;
flipUp = true;
flipDn = false;
}
};
void BangDaButton::step() {
pressed = params[DA_BUTTON].value;
if (inputs[DA_BUTTON_TRIG].active) {
pressed = inputs[DA_BUTTON_TRIG].value > 0 ? !pressed : pressed;
}
if (dnFlipTrg.process(pressed)) { // HOLD
flipDn = !flipDn;
dnPulse.trigger(0.01);
}
if (upFlipTrg.process(!pressed)){ // RELEASE
flipUp = !flipUp;
upPulse.trigger(0.01);
}
// outputs[UP_CH1_OUT].value = pressed ? inputs[UP_CH1_IN].normalize(0.0) : 0.0f;
// outputs[UP_CH2_OUT].value = pressed ? 0.0f : inputs[UP_CH2_IN].normalize(0.0);
outputs[UP_SW_OUT].value = pressed ? inputs[UP_SW_A_IN].normalize(0.0f) : inputs[UP_SW_B_IN].normalize(0.0f);
outputs[UP_MUX_A_OUT].value = pressed * inputs[UP_MUX_IN].normalize(0.0f);
outputs[UP_MUX_B_OUT].value = !pressed * inputs[UP_MUX_IN].normalize(0.0f);
// outputs[DOWN_CH1_OUT].value = pressed ? inputs[DOWN_CH1_IN].normalize(0.0f) : 0.0f;
// outputs[DOWN_CH2_OUT].value = pressed ? 0.0f : inputs[DOWN_CH2_IN].normalize(0.0f);
outputs[DOWN_SW_OUT].value = pressed ? inputs[DOWN_SW_B_IN].normalize(0.0f) : inputs[DOWN_SW_A_IN].normalize(0.0f);
outputs[DOWN_MUX_A_OUT].value = !pressed * inputs[DOWN_MUX_IN].normalize(0.0f);
outputs[DOWN_MUX_B_OUT].value = pressed * inputs[DOWN_MUX_IN].normalize(0.0f);
outputs[UP_GATE_OUT].value = !pressed * 10.0f;
outputs[DOWN_GATE_OUT].value = pressed * 10.0f;
outputs[UP_FLIP_OUT].value = flipUp * 10.0f;
outputs[DOWN_FLIP_OUT].value = flipDn * 10.0f;
outputs[UP_TRIG_OUT].value = upPulse.process(1.0/engineGetSampleRate()) * 10.0f;
outputs[DOWN_TRIG_OUT].value = dnPulse.process(1.0/engineGetSampleRate()) * 10.0f;
lights[DOWN_LED].value = pressed;
lights[UP_LED].value = !pressed;
}
struct DaButton : SVGSwitch, MomentarySwitch {
DaButton() {
addFrame(SVG::load(assetPlugin(plugin, "res/components/DaButton_up.svg")));
addFrame(SVG::load(assetPlugin(plugin, "res/components/DaButton_dn.svg")));
box.size = Vec(82,82);
}
};
struct InPortT : SVGPort {
InPortT() {
background->svg = SVG::load(assetPlugin(plugin, "res/components/empty.svg"));
background->wrap();
box.size = Vec(22,22);
}
};
struct BangDaButtonWidget : ModuleWidget {
BangDaButtonWidget(BangDaButton *module);
};
BangDaButtonWidget::BangDaButtonWidget(BangDaButton *module) : ModuleWidget(module) {
setPanel(SVG::load(assetPlugin(plugin, "res/panels/BangDaButton.svg")));
// screws
addChild(Widget::create<ScrewHead3>(Vec(15, 0)));
addChild(Widget::create<ScrewHead2>(Vec(box.size.x - 30, 0)));
addChild(Widget::create<ScrewHead4>(Vec(15, 365)));
addChild(Widget::create<ScrewHead1>(Vec(box.size.x - 30, 365)));
addInput(Port::create<InPortAud>(Vec(4,22), Port::INPUT, module,BangDaButton::UP_SW_A_IN));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(28,30.5),module,BangDaButton::DOWN_LED));
addOutput(Port::create<OutPortVal>(Vec(34,22), Port::OUTPUT, module,BangDaButton::UP_SW_OUT));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(57,30.5),module,BangDaButton::UP_LED));
addInput(Port::create<InPortAud>(Vec(64,22), Port::INPUT, module,BangDaButton::UP_SW_B_IN));
// addInput(Port::create<InPortAud>(Vec(4,44), Port::INPUT, module,BangDaButton::UP_CH1_IN));
// addChild(ModuleLightWidget::create<FourPixLight<RedLight>>(Vec(27,52),module,BangDaButton::UP_LED));
// addOutput(Port::create<OutPortVal>(Vec(34,44), Port::OUTPUT, module,BangDaButton::UP_CH1_OUT));
addOutput(Port::create<OutPortVal>(Vec(4,68), Port::OUTPUT, module,BangDaButton::UP_MUX_A_OUT));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(27,76.5),module,BangDaButton::DOWN_LED));
addInput(Port::create<InPortAud>(Vec(34,68), Port::INPUT, module,BangDaButton::UP_MUX_IN));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(57,76.5),module,BangDaButton::UP_LED));
addOutput(Port::create<OutPortVal>(Vec(64,68), Port::OUTPUT, module,BangDaButton::UP_MUX_B_OUT));
// addInput(Port::create<InPortAud>(Vec(34,92), Port::INPUT, module,BangDaButton::UP_CH2_IN));
// addChild(ModuleLightWidget::create<FourPixLight<RedLight>>(Vec(57,100),module,BangDaButton::DOWN_LED));
// addOutput(Port::create<OutPortVal>(Vec(64,92), Port::OUTPUT, module,BangDaButton::UP_CH2_OUT));
addOutput(Port::create<OutPortBin>(Vec(6,124), Port::OUTPUT, module,BangDaButton::UP_GATE_OUT));
addOutput(Port::create<OutPortBin>(Vec(34,124), Port::OUTPUT, module,BangDaButton::UP_TRIG_OUT));
addOutput(Port::create<OutPortBin>(Vec(62,124), Port::OUTPUT, module,BangDaButton::UP_FLIP_OUT));
addParam(ParamWidget::create<DaButton>(Vec(4,149),module,BangDaButton::DA_BUTTON, 0, 1, 0));
addInput(Port::create<InPortT>(Vec(4,179), Port::INPUT, module,BangDaButton::DA_BUTTON_TRIG));
addOutput(Port::create<OutPortBin>(Vec(6,234), Port::OUTPUT, module,BangDaButton::DOWN_FLIP_OUT));
addOutput(Port::create<OutPortBin>(Vec(34,234), Port::OUTPUT, module,BangDaButton::DOWN_TRIG_OUT));
addOutput(Port::create<OutPortBin>(Vec(62,234), Port::OUTPUT, module,BangDaButton::DOWN_GATE_OUT));
// addInput(Port::create<InPortAud>(Vec(4,266), Port::INPUT, module,BangDaButton::DOWN_CH1_IN));
// addChild(ModuleLightWidget::create<FourPixLight<RedLight>>(Vec(27,274),module,BangDaButton::UP_LED));
// addOutput(Port::create<OutPortVal>(Vec(34,266), Port::OUTPUT, module,BangDaButton::DOWN_CH1_OUT));
addOutput(Port::create<OutPortVal>(Vec(4,290), Port::OUTPUT, module,BangDaButton::DOWN_MUX_A_OUT));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(27,298.5),module,BangDaButton::UP_LED));
addInput(Port::create<InPortAud>(Vec(34,290), Port::INPUT, module,BangDaButton::DOWN_MUX_IN));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(57,298.5),module,BangDaButton::DOWN_LED));
addOutput(Port::create<OutPortVal>(Vec(64,290), Port::OUTPUT, module,BangDaButton::DOWN_MUX_B_OUT));
// addInput(Port::create<InPortAud>(Vec(34,314), Port::INPUT, module,BangDaButton::DOWN_CH2_IN));
// addChild(ModuleLightWidget::create<FourPixLight<RedLight>>(Vec(57,322),module,BangDaButton::DOWN_LED));
// addOutput(Port::create<OutPortVal>(Vec(64,314), Port::OUTPUT, module,BangDaButton::DOWN_CH2_OUT));
addInput(Port::create<InPortAud>(Vec(4,336), Port::INPUT, module,BangDaButton::DOWN_SW_A_IN));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(27,344.5),module,BangDaButton::UP_LED));
addOutput(Port::create<OutPortVal>(Vec(34,336), Port::OUTPUT, module,BangDaButton::DOWN_SW_OUT));
addChild(ModuleLightWidget::create<FourPixLight<GreenLED>>(Vec(57,344.5),module,BangDaButton::DOWN_LED));
addInput(Port::create<InPortAud>(Vec(64,336), Port::INPUT, module,BangDaButton::DOWN_SW_B_IN));
}
Model *modelBangDaButton = Model::create<BangDaButton, BangDaButtonWidget>(
"PvC", "BangDaButton", "BangDaButton", CONTROLLER_TAG, SWITCH_TAG);