-
Notifications
You must be signed in to change notification settings - Fork 14
/
Button.cpp
98 lines (84 loc) · 2 KB
/
Button.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
// MIDI Elements button class
// Library to simplifly handling of compontents for MIDI controllers
// Created by Tomash Ghz
// www.tomashg.com
#include "Button.h"
//-----------------------------------------------------------------------------------
// constructor
Button::Button(byte p){
Button(p,0,0,false,true);
}
Button::Button(byte p, byte c, byte n){
Button(p,c,n,false,false);
}
Button::Button(byte p, byte c, byte n, bool sec){
Button(p,c,n,sec,false);
}
Button::Button(byte p, byte c, byte n, bool sec, bool debug){ // pin, number, channel
pin=p;
number=n;
channel=c;
secondary=sec;
debugging=debug;
velocity=127;
pinMode(pin, INPUT_PULLUP); // enable the pin for input
bButn = new MIDIBounce(pin, 10); // create new bounce object for pin
}
// destructor
Button::~Button(){
delete bButn;
}
// read
void Button::read(){
if (bButn->update()) {//state changed
if (bButn->read()==LOW) {//is pressed
noteOnOff(true);
}
else {
noteOnOff(false);
}
}
}
// read value
bool Button::readValue(bool &changed){
changed=bButn->update(); //state changed
if (bButn->read()==LOW) {//is pressed
return true;
}
else {
return false;
}
}
// set note on velocity
void Button::setVelocity(byte v){
velocity=v;
}
//send midinote on off
void Button::noteOnOff(bool v){
if(v){
if (debugging) {//debbuging enabled
Serial.print("Button ");
Serial.print(number);
Serial.println(" pressed.");
}
else{ // send midi note
usbMIDI.sendNoteOn(number, velocity, channel);
if(secondary)
usbMIDI.sendControlChange(number, 127, channel);
}
}
else{
if (debugging) {//debbuging enabled
Serial.print("Button ");
Serial.print(number);
Serial.println(" released.");
}
else{
if(secondary)
usbMIDI.sendControlChange(number, 0, channel);
usbMIDI.sendNoteOff(number, 0, channel);
}
}
}
//-----------------------------------------------------------------------------------