forked from ghztomash/MIDIElements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotentiometer.cpp
152 lines (127 loc) · 3.41 KB
/
Potentiometer.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
// MIDI Elements Potentiometer class
// Library to simplifly handling of compontents for MIDI controllers
// Created by Tomash Ghz
// www.tomashg.com
#include "Potentiometer.h"
//-----------------------------------------------------------------------------------
// constructor
Potentiometer::Potentiometer(byte p){
Potentiometer(p,0,0,false,true);
}
Potentiometer::Potentiometer(byte p, byte c, byte n){
Potentiometer(p,c,n,false,false);
}
Potentiometer::Potentiometer(byte p, byte c, byte n, bool sec){
Potentiometer(p,c,n,sec,false);
}
Potentiometer::Potentiometer(byte p, byte c, byte n, bool sec, bool debug){ // pin, number, channel
pin=p;
number=n;
channel=c;
secondary=sec;
debugging=debug;
mapped=false;
}
// destructor
Potentiometer::~Potentiometer(){
}
// read
void Potentiometer::read(){
if(mapped){
tempRead=constrain(analogRead(pin),inMin,inMax);
tempRead=map(tempRead,inMin,inMax,0,127);
}
else
tempRead=map(analogRead(pin), 0, 1023, 0, 127);
if (tempRead!=lastValue) { //value changed
midiCC(tempRead, lastValue);
}
lastValue=tempRead;
}
// read
void Potentiometer::readAvr(){
tempRead=0;
for(int i=0; i<10; i++){
tempRead+=analogRead(pin);
}
tempRead=tempRead/10;
if(mapped)
{
tempRead=map(constrain(tempRead,inMin,inMax),inMin,inMax,0,127);
}
else
tempRead=map(tempRead, 0, 1023, 0, 127);
if (tempRead!=lastValue) { //value changed
midiCC(tempRead, lastValue);
}
lastValue=tempRead;
}
// enable maped values
void Potentiometer::bound(int iMin, int iMax){
mapped=true;
inMin=iMin;
inMax=iMax;
}
// read value
int Potentiometer::readValue(bool &changed){
tempRead=map(analogRead(pin), 0, 1023, 0, 127);
changed=tempRead!=lastValue; //value changed
lastValue=tempRead;
return tempRead;
}
// read value
int Potentiometer::readValueAvr(bool &changed){
tempRead=0;
for(int i=0; i<10; i++){
tempRead+=analogRead(pin);
}
tempRead=tempRead/10;
if(mapped)
{
tempRead=map(constrain(tempRead,inMin,inMax),inMin,inMax,0,127);
}
else
tempRead=map(tempRead, 0, 1023, 0, 127);
changed=tempRead!=lastValue; //value changed
lastValue=tempRead;
return tempRead;
}
// function to handle cc outgoing messages
void Potentiometer::midiCC(int v, int oldv) {
if (debugging) {//debbuging enabled
Serial.print("Potentiometer ");
Serial.print(number);
Serial.print(" changed value to ");
Serial.println(v);
}
else {
usbMIDI.sendControlChange(number, v, channel);
//send the secondary midi messages
// 0 3 64 124 127
// |--|-------------|-------------|--| - full range
//
// |0=============================127| - CC A
// |__|on____________________________| - note A
// |off___________________________|on| - note B
// 3 124
if (secondary) {
if ((v>3)&&(oldv<=3)) { // send the 0 value note on
usbMIDI.sendNoteOn(number, 127, channel+1);
}
else if ((v<=3)&&(oldv>3)) {
usbMIDI.sendNoteOff(number, 127, channel+1);
}
if ((v>124)&&(oldv<=124)) { // send the 127 value note on
usbMIDI.sendNoteOn(number+1, 127, channel+1);
}
else if ((v<=124)&&(oldv>124)) {
usbMIDI.sendNoteOff(number+1, 127, channel+1);
}
}
}
}
//-----------------------------------------------------------------------------------
void Potentiometer::changeSecondary( bool s){
secondary=s;
}