-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeatButton.cpp
More file actions
90 lines (70 loc) · 1.99 KB
/
BeatButton.cpp
File metadata and controls
90 lines (70 loc) · 1.99 KB
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
#include "BeatButton.h"
#include <arduino.h>
#define PRESS_MAX_GAP 1000000
void BeatButton::update(long now, long tpf) {
// Button
if(rot.getSwitchState()) {
if(!swState) {
swState = true;
hit = true;
doPress(now);
}
}
else {
swState = false;
}
int tempoAdjust = rot.getValue2() - lastTempoAdjust;
if(tempoAdjust != 0) {
microsPerBeat -= tempoAdjust * 250; // 0.25 ms
/*Serial.print("Tempo adjusted by ");
Serial.print(tempoAdjust);
Serial.print("ms to ");
Serial.println(microsPerBeat);*/
lastTempoAdjust = rot.getValue2();
tAutoBeat = 0;
}
// Auto beat
//if(tAutoBeat > 0)
tAutoBeat -= tpf;
if(tAutoBeat <= 0 && microsPerBeat > 0) {
hit = true;
//tAutoBeat = microsPerBeat;
tAutoBeat += microsPerBeat;
}
}
void BeatButton::doPress(long now) {
tAutoBeat = 30000000;
// Increase index first so we can access last press with current index
idxLastPress++;
if(idxLastPress >= NUM_PRESSES)
idxLastPress = 0;
lastPresses[idxLastPress] = now;
// Update bpm, find presses that count
int idx = idxLastPress;
int numPresses = 0;
long firstPress = now;
for(int i=0; i<NUM_PRESSES; i++) {
if(lastPresses[idx] >= firstPress - PRESS_MAX_GAP) {
numPresses++;
firstPress = lastPresses[idx];
}
else
break;
idx--;
if(idx < 0)
idx = NUM_PRESSES - 1;
}
if(numPresses < 4) {
microsPerBeat = 0;
return;
}
long tDiff = now - firstPress;
microsPerBeat = tDiff / (numPresses-1);
/*Serial.print("-- tDiff: ");
Serial.print(tDiff);
Serial.print(", numPresses: ");
Serial.print(numPresses);
Serial.print(", microsPerBeat: ");
Serial.println(microsPerBeat);*/
tAutoBeat = 0;
}