-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROTARY.h
77 lines (70 loc) · 2.27 KB
/
ROTARY.h
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
#include <RotaryEncoder.h>
int rotA_pin[2] = {12, 10};
int rotB_pin[2] = {11, 9};
class Rotary {
private:
RotaryEncoder encoder;
byte number;
int pin_A;
int pin_B;
int pos = 0;
public:
Rotary(int num) : encoder(rotA_pin[num], rotB_pin[num], RotaryEncoder::LatchMode::FOUR3) {
number = num;
for (int i = 0 ; i < NUM_LAYOUT ; i++) {
control[i] = default_rotary[i][number];
channel[i] = 16;
control_hold[i] = default_rotary_hold[i][number];
channel_hold[i] = 16;
}
}
byte control[NUM_LAYOUT];
byte channel[NUM_LAYOUT];
byte control_hold[NUM_LAYOUT];
byte channel_hold[NUM_LAYOUT];
int _value = 64;
int _value_hold = 64;
bool enc_state = LOW;
long unsigned _now = millis();
void update_rotary()
{
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
enc_state = HIGH;
if (b[6 + number].btn_state) {
int newValue = _value_hold;
// b[6 + number].latch = HIGH;
if (int(encoder.getDirection()) > 0) {
if ((millis() - _now) < 25) _value_hold = _value_hold + 4;
else _value_hold++;
}
else {
if ((millis() - _now) < 25) _value_hold = _value_hold - 4;
else _value_hold--;
}
_value_hold = constrain(_value_hold, 0, 127);
USB_MIDI.sendControlChange(control_hold[current_layout], _value_hold, channel_hold[current_layout]);
SERIAL_MIDI.sendControlChange(control_hold[current_layout], _value_hold, channel_hold[current_layout]);
}
else {
int newValue = _value;
if (int(encoder.getDirection()) > 0) {
if ((millis() - _now) < 25) _value = _value + 4;
else _value++;
}
else {
if ((millis() - _now) < 25) _value = _value - 4;
else _value--;
}
_value = constrain(_value, 0, 127);
USB_MIDI.sendControlChange(control[current_layout], _value, channel[current_layout]);
SERIAL_MIDI.sendControlChange(control[current_layout], _value, channel[current_layout]);
}
pos = newPos;
_now = millis();
enc_state = LOW;
}
};
}
r[2] = {Rotary(0), Rotary(1)};