-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgurdycrank.cpp
276 lines (230 loc) · 7.55 KB
/
gurdycrank.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "gurdycrank.h"
void myisr() {
if (debounce_timer >= 1250) {
num_events = num_events + 1;
last_event = last_event_timer;
debounce_timer = 0;
}
}
/// @brief Constructor.
/// @details This class abstracts the cranking mechanism on Digi-Gurdies.
///
/// If you are not using an LED buzz indicator, a pin for it still needs to be specified here. The object won't touch it unless LED_KNOB is defined.
/// @warning This is for optical-sensor cranks. See GearCrank for a gear-motor crank version.
///
/// @warning
/// * A hidden member object here is a BuzzKnob.
/// * This class' header includes common.h and specific string objects are expected to exist. See common.h.
/// @param s_pin The digital pin coming from the optical sensor.
/// @param buzz_pin The pin of the buzz knob.
/// @param led_pin The pin of the LED indicator.
GurdyCrank::GurdyCrank(int s_pin, int buzz_pin, int led_pin) {
myKnob = new BuzzKnob(buzz_pin);
#ifdef LED_KNOB
myLED = new SimpleLED(led_pin);
#endif
sensor_pin = s_pin;
pinMode(sensor_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(sensor_pin), myisr, RISING);
expression = 0;
buzz_expression = 0;
the_buzz_timer = 0;
};
/// @brief Constructor (2-pin encoders).
/// @param s_pin A pin (digital, interrupt-capable)
/// @param s_pin2 B pin (digital, interrupt-capable)
/// @param buzz_pin The pin of the buzz knob.
/// @param led_pin The pin of the LED indicator.
/// @version *New in 2.9.5*
GurdyCrank::GurdyCrank(int s_pin, int s_pin2, int buzz_pin, int led_pin) {
myKnob = new BuzzKnob(buzz_pin);
#ifdef LED_KNOB
myLED = new SimpleLED(led_pin);
#endif
#ifdef USE_ENCODER
// This automatically enabled INPUT_PULLUP, FYI.
myEnc = new Encoder(s_pin2, s_pin);
last_event_timer = 0;
last_pulse = 0;
#endif
expression = 0;
buzz_expression = 0;
the_buzz_timer = 0;
};
/// @brief Reports if a crank is connected.
/// @return True
/// @note Currently this method is pointless. Gear-motor cranks do use this, however, and this is being kept for future changes.
/// It may be useful to expect any crank object to be able to report this in-code whether or not it's relevant...
bool GurdyCrank::isDetected() {
return true;
};
/// @brief Samples the crank and updates its state
/// @details Also updates the buzz knob, and calls updateExpression().
/// This should be run every loop(). It paces itself internally and expects to be run frequently.
void GurdyCrank::update() {
// Check if we need to update the knob reading...
myKnob->update();
#ifdef USE_ENCODER
if (eval_timer > 30000) {
cur_vel = (cur_vel) / 2.0;
eval_timer = 0;
return;
};
if (eval_timer > 10000) {
pulse = myEnc->read();
if (last_pulse != pulse) {
new_vel = (abs(last_pulse - pulse) * 30000000.0) / (NUM_SPOKES * last_event_timer);
cur_vel = cur_vel + (0.8 * (new_vel - cur_vel));
last_pulse = pulse;
last_event_timer = 0;
eval_timer = 0;
};
}
#else
if (eval_timer > 25250 && num_events > 1) {
double new_vel = (num_events * spoke_width * 60000000.0) / (last_event);
// cur_vel = cur_vel + (smoothing_factor * (new_vel - cur_vel)) + 1;
if (new_vel > cur_vel) {
cur_vel = cur_vel + (0.8 * (new_vel - cur_vel)) + 0.5;
}
else {
cur_vel = cur_vel + (0.75 * (new_vel - cur_vel));
}
num_events = 0;
last_event = 0;
last_event_timer = 0;
eval_timer = 0;
}
if (eval_timer > 31250 && num_events < 2) {
// cur_vel = cur_vel + (smoothing_factor * ( 0 - cur_vel)) - 2;
cur_vel = (cur_vel) / 2.0;
num_events = 0;
last_event = 0;
last_event_timer = 0;
eval_timer = 0;
}
#endif
updateExpression();
};
/// @brief Updates the expression value and applies it to the strings.
/// @details * Expression is MIDI CC11 which is usually interpreted as a volume adjustment independent of the channel volume.
/// * Here expression is calculated based off EXPRESSION_VMAX and EXPRESSION_START along with the current crank velocity. See config.h for those values.
/// * The end-user effect is that the volume "swells" as the user cranks faster up to a point.
void GurdyCrank::updateExpression() {
// Only do anything every 50ms (20x/sec)
if (the_expression_timer > 50) {
float cur_v = getVAvg();
int new_buzz_expression = int(((cur_v - myKnob->getThreshold())/(0.45 * myKnob->getThreshold())) * (42) + 85);
if (new_buzz_expression > 127) {
new_buzz_expression = 127;
};
if (cur_v > EXPRESSION_VMAX) {
cur_v = EXPRESSION_VMAX;
} else if (cur_v < V_THRESHOLD) {
cur_v = V_THRESHOLD;
}
int new_expression = int(((cur_v - V_THRESHOLD)/(EXPRESSION_VMAX - V_THRESHOLD)) * (127 - EXPRESSION_START) + EXPRESSION_START);
if (autocrank_toggle_on) {
new_expression = 90;
};
if (expression != new_expression) {
//Serial.println("EXPRESS LESS");
expression = new_expression;
mystring->setExpression(expression);
mylowstring->setExpression(expression);
mytromp->setExpression(expression);
mydrone->setExpression(expression);
};
if (buzz_expression != new_buzz_expression) {
buzz_expression = new_buzz_expression;
mybuzz->setExpression(buzz_expression);
};
the_expression_timer = 0;
};
};
/// @brief Reports whether the crank started spinning this update() cycle.
/// @return True if crank started spinning this cycle, false otherwise
bool GurdyCrank::startedSpinning() {
if (isSpinning()) {
if (!was_spinning) {
was_spinning = true;
return true;
} else {
return false;
}
} else {
return false;
}
};
/// @brief Reports whether the crank stopped spinning this update() cycle.
/// @return True if crank stopped spinning this cycle, false otherwise
bool GurdyCrank::stoppedSpinning() {
if (!isSpinning()) {
if (was_spinning) {
was_spinning = false;
return true;
} else {
return false;
}
} else {
return false;
}
};
/// @brief Reports whether the crank is currently spinning this update() cycle.
/// @return True if crank is spinning, false otherwise.
bool GurdyCrank::isSpinning() {
return (cur_vel > V_THRESHOLD);
};
/// @brief Reports whether buzzing began this update() cycle.
/// @return True if buzzing started thie cycle, false otherwise.
bool GurdyCrank::startedBuzzing() {
if (getVAvg() > myKnob->getThreshold()) {
if (!was_buzzing) {
was_buzzing = true;
the_buzz_timer = 0;
#ifdef LED_KNOB
myLED->on();
#endif
return true;
} else {
return false;
}
} else {
return false;
}
};
/// @brief Reports whether buzzing stopped this update() cycle.
/// @return True if buzzing stopped thie cycle, false otherwise.
bool GurdyCrank::stoppedBuzzing() {
if (getVAvg() <= (myKnob->getThreshold() * 0.95)) {
if (was_buzzing && the_buzz_timer > 50) {
was_buzzing = false;
#ifdef LED_KNOB
myLED->off();
#endif
return true;
} else {
return false;
}
};
return false;
};
/// @brief Returns the crank's current (heavily-adjusted) velocity.
/// @return The crank's measured current average velocity in estimated RPMs.
double GurdyCrank::getVAvg() {
return cur_vel;
};
/// @brief Disables the buzz LED indicator object.
/// @note This does nothing if LED_KNOB is not defined.
void GurdyCrank::disableLED() {
#ifdef LED_KNOB
myLED->disable();
#endif
};
/// @brief Enables the buzz LED indicator object.
/// @note This does nothing if LED_KNOB is not defined.
void GurdyCrank::enableLED() {
#ifdef LED_KNOB
myLED->enable();
#endif
};