-
Notifications
You must be signed in to change notification settings - Fork 1
/
MidiController.cpp
executable file
·156 lines (123 loc) · 5.07 KB
/
MidiController.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
153
154
155
156
#include <Arduino.h>
#include "config.h"
#include "LEDAnimation.h"
#include "MidiController.h"
#include "MidiCCButton.h"
#include "MidiNoteButton.h"
#include "MidiExpressionPedal.h"
MidiController::MidiController( const char *cDeviceName )
{
deviceName = cDeviceName;
last_pass_had_subscriber_connected = false;
// Setup our MIDI BLE device...
midiBleDevice = new MidiBLEDevice( (char *) deviceName );
}
void MidiController::addCCButton( int buttonPinNumber, int ledPinNumber, int midiChannelNumber, int midiControlNumber )
{
// Double-check to make sure we're not exceeding our bounds...
if ( buttonCount >= _MAX_MIDI_CONTROLLER_BUTTONS ) {
Serial.println("ERROR: MAXIMUM NUMBER OF BUTTONS REACHED!!!");
return;
}
// Create the new button...
int buttonId = _get_next_id();
MidiCCButton *buttonObj = new MidiCCButton( midiBleDevice, buttonId, buttonPinNumber, ledPinNumber, midiChannelNumber, midiControlNumber );
// Add it to our list (incrementing our counter too)...
buttonList[buttonCount] = buttonObj;
buttonCount++;
}
void MidiController::addNoteButton( int buttonPinNumber, int ledPinNumber, int midiChannelNumber, int midiNoteNumber, int buttonType )
{
// Double-check to make sure we're not exceeding our bounds...
if ( buttonCount >= _MAX_MIDI_CONTROLLER_BUTTONS ) {
Serial.println("ERROR: MAXIMUM NUMBER OF BUTTONS REACHED!!!");
return;
}
// Create the new button...
int buttonId = _get_next_id();
MidiNoteButton *buttonObj = new MidiNoteButton( midiBleDevice, buttonId, buttonPinNumber, ledPinNumber, midiChannelNumber, midiNoteNumber, buttonType );
// Add it to our list (incrementing our counter too)...
buttonList[buttonCount] = buttonObj;
buttonCount++;
}
void MidiController::addExpressionPedal( int expressionPedalPinNumber, int midiChannelNumber, int midiControlNumber, int pedalDirection, int curveType )
{
// Double-check to make sure we're not exceeding our bounds...
if ( expressionPedalCount >= _MAX_MIDI_CONTROLLER_EXPRESSION_PEDALS ) {
Serial.println("ERROR: MAXIMUM NUMBER OF EXPRESSION PEDALS REACHED!!!");
return;
}
// Create the new expressionPedal...
int expressionPedalId = _get_next_id();
MidiExpressionPedal *expressionPedalObj = new MidiExpressionPedal( midiBleDevice, expressionPedalId, expressionPedalPinNumber, midiChannelNumber, midiControlNumber, pedalDirection, curveType);
// Add it to our list (incrementing our counter too)...
expressionPedalList[expressionPedalCount] = expressionPedalObj;
expressionPedalCount++;
}
int MidiController::_get_next_id()
{
static int id = 1;
return id++;
}
void MidiController::loop()
{
// Our main loop for the controller...handle all controls from here...
// Call the MIDI-BLE device's loop() so it can detect, and track the bluetooth related stuff...
midiBleDevice->loop();
bool has_subscriber_connected = midiBleDevice->hasSubscriberConnected();
if ( !has_subscriber_connected ) {
// No bluetooth midi subscriber...
if ( last_pass_had_subscriber_connected ) {
// We just lost our subscriber...
Serial.println("Lost MIDI BLE subscriber");
// Restart the connection animation...
restart_bluetooth_animation_sequence(buttonList, buttonCount);
} else {
// Waiting for a subscriber...
Serial.println("Waiting for MIDI BLE subscriber");
// Do connection animation while we wait...
show_current_bluetooth_animation_frame(buttonList, buttonCount);
}
} else if ( !last_pass_had_subscriber_connected ) {
// A MIDI BLE subscriber has just connected...
Serial.println("New MIDI BLE subscriber");
// Reset our button states (incase this was from a previous BLE disconnect)...
Serial.println("Initializing button states");
for ( int i = 0; i < buttonCount; i++) {
buttonList[i]->resetState();
}
for ( int i = 0; i < expressionPedalCount; i++) {
expressionPedalList[i]->resetState();
}
} else {
// We have a MIDI BLE subscriber...
// Handle any button presses...
for ( int i = 0; i < buttonCount; i++ ) {
buttonList[i]->handleState();
}
// Handle any expression pedal movement...
for ( int i = 0; i < expressionPedalCount; i++ ) {
expressionPedalList[i]->handleState();
}
delay(DELAY_TIME_BETWEEN_INPUT_SCANS);
}
// Keep track of the connection state so we know WHEN it changes for proper resetting...
last_pass_had_subscriber_connected = has_subscriber_connected;
}
void MidiController::start() {
// Initialize our microcontroller pins...
_init_input_pins();
// Show the bootup sequence...
show_bootup_animation_sequence(this->buttonList, this->buttonCount);
}
void MidiController::_init_input_pins() {
// Setup all of the I/O pins for each component
// Buttons...
for ( int i = 0; i < buttonCount; i++) {
buttonList[i]->initGPIOPins();
}
// Expression pedals...
for ( int i = 0; i < expressionPedalCount; i++) {
buttonList[i]->initGPIOPins();
}
}