-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMidiNoteButton.cpp
executable file
·97 lines (83 loc) · 2.78 KB
/
MidiNoteButton.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
#include "MidiNoteButton.h"
#include "config.h"
MidiNoteButton::MidiNoteButton( MidiBLEDevice *cMidiBleDevice, int cid, int cbutton_pin, int cled_pin, int cmidi_channel, int cmidi_note, int cbutton_type ) {
id = cid;
midiBleDevice = cMidiBleDevice;
button_pin = cbutton_pin;
led_pin = cled_pin;
midi_channel = cmidi_channel;
midi_note = cmidi_note;
button_type = cbutton_type;
is_on = false;
last_button_state = BUTTON_NOT_PRESSED;
}
void MidiNoteButton::handleState() {
// Default to our current value...
int new_is_on = is_on;
// Read in the current button state...
int temp_button_state = digitalRead(button_pin);
switch (button_type) {
case BUTTON_PRESS_TYPE_MOMENTARY:
if ( temp_button_state == BUTTON_PRESSED ) {
new_is_on = true;
} else {
new_is_on = false;
}
break;
case BUTTON_PRESS_TYPE_LATCHING:
if ( ( temp_button_state == BUTTON_PRESSED ) and ( temp_button_state != last_button_state ) ) {
// Button has been pressed, when it previously was not on the last pass...so we need to toggle...
if ( is_on ) {
// Currently on...toggle off...
new_is_on = false;
} else {
// Currently off...toggle on...
new_is_on = true;
}
} // else no change in state, nothing to update
break;
default:
Serial.print("ERROR: Unknown value for button type: ");
Serial.print(button_type);
Serial.println();
break;
}
if ( new_is_on != is_on ) {
// New button press state...handle it...
if ( new_is_on == true ) {
turnLEDOn();
midiBleDevice->sendMIDINoteOn( midi_channel, midi_note );
} else {
turnLEDOff();
midiBleDevice->sendMIDINoteOff( midi_channel, midi_note );
}
// Store the new on/off state to match what we just did...
is_on = new_is_on;
}
// Update our last known status so we can detect when it changes
// (separate from whether the button is currently on or off)...
last_button_state = temp_button_state;
}
void MidiNoteButton::resetState() {
/*
This is called whenever a button needs to be reset back to its initial state. It is called when a new BLE subscriber connects (or re-connects).
*/
is_on = false;
last_button_state = BUTTON_NOT_PRESSED;
turnLEDOff();
}
void MidiNoteButton::initGPIOPins() {
/*
Setup our button, and LED pins
*/
pinMode(button_pin, INPUT_PULLUP);
pinMode(led_pin, OUTPUT);
// Turn off the LED immediately
turnLEDOff();
}
void MidiNoteButton::turnLEDOff() {
digitalWrite(led_pin, LED_OFF);
}
void MidiNoteButton::turnLEDOn() {
digitalWrite(led_pin, LED_ON);
}