-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuttons4.ino
283 lines (246 loc) · 7.66 KB
/
buttons4.ino
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Four button membrane keypad sends MIDI messages.
*/
/* Install using the Arduino IDE library manager */
/* https://github.com/thomasfredericks/Bounce2 */
#include <Bounce2.h>
#include <MIDIUSB.h>
/* Not used except for some definitions such as midi::TimeCodeQuarterFrame */
#include <MIDI.h>
// These 4 digital pins are conveniently located next to a ground pin on a
// SparkFun Pro Micro.
const uint8_t BUTTONS[] = {3, 2, 5, 4};
// Instantiate a Bounce object
Bounce debouncer[sizeof(BUTTONS)];
/*************** MIDI USB functions ****************************/
// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).
void USBNoteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t evt = {0x08, (byte)(0x80 | (channel - 1)), pitch, velocity};
MidiUSB.sendMIDI(evt);
}
void USBNoteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t evt = {0x09, (byte)(0x90 | (channel - 1)), pitch, velocity};
MidiUSB.sendMIDI(evt);
}
// First parameter is the event type (0x0A = polyphonic key pressure)
// Second parameter is poly-keypress, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the pressure on the key after it "bottoms out"
void USBAfterTouchPoly(byte channel, byte pitch, byte pressure) {
midiEventPacket_t evt = {0x0A, (byte)(0xA0 | (channel - 1)), pitch, pressure};
MidiUSB.sendMIDI(evt);
}
// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).
void USBControlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, (byte)(0xB0 | (channel - 1)), control, value};
MidiUSB.sendMIDI(event);
}
// First parameter is the event type (0x0C = program change)
// Second parameter is program number, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the program number.
// Fourth parameter is 0.
void USBProgramChange(byte channel, byte number) {
midiEventPacket_t evt = {0x0C, (byte)(0xC0 | (channel - 1)), number, 0};
MidiUSB.sendMIDI(evt);
}
// First parameter is the event type (0x0D = after touch channel pressure)
// Second parameter is channel pressure, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the channel pressure value.
// Fourth parameter is 0.
void USBAfterTouchChannel(byte channel, byte pressure) {
midiEventPacket_t evt = {0x0D, (byte)(0xD0 | (channel - 1)), pressure, 0};
MidiUSB.sendMIDI(evt);
}
// First parameter is the event type (0x0E = pitch bend)
// Second parameter is pitch bend, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the least significant 7 bits of pitch bend
// Fourth parameter is the most significant 7 bits of pitch bend
void USBPitchBend(byte channel, int bend) {
uint16_t uint_bend = (uint16_t)(bend - MIDI_PITCHBEND_MIN);
midiEventPacket_t evt = {0x0E, (byte)(0xE0 | (channel - 1)),
(byte)(uint_bend & 0x7F), (byte)((uint_bend >> 7) & 0x7F)};
MidiUSB.sendMIDI(evt);
}
// Be sure to include 0xF0 and 0xF7.
void USBSystemExclusive(unsigned size, byte *data) {
uint8_t bytesOut;
midiEventPacket_t evt;
while (size > 0) {
bytesOut = min(3, size);
evt.byte1 = *data++;
size -= bytesOut;
switch (bytesOut) {
case 1:
evt.header = 0x05;
evt.byte2 = evt.byte3 = 0;
break;
case 2:
evt.header = 0x06;
evt.byte2 = *data++;
evt.byte3 = 0;
break;
case 3:
evt.header = (size > 0) ? 0x04 : 0x07;
evt.byte2 = *data++;
evt.byte3 = *data++;
break;
default:
break;
}
MidiUSB.sendMIDI(evt);
}
}
// Be sure to include 0xF0 and 0xF7.
// This version reads a buffer stored in PROGMEM
void USBSystemExclusive_P(unsigned size, byte *data) {
uint8_t bytesOut;
midiEventPacket_t evt;
while (size > 0) {
bytesOut = min(3, size);
evt.byte1 = pgm_read_byte_near(data++);
size -= bytesOut;
switch (bytesOut) {
case 1:
evt.header = 0x05;
evt.byte2 = evt.byte3 = 0;
break;
case 2:
evt.header = 0x06;
evt.byte2 = pgm_read_byte_near(data++);
evt.byte3 = 0;
break;
case 3:
evt.header = (size > 0) ? 0x04 : 0x07;
evt.byte2 = pgm_read_byte_near(data++);
evt.byte3 = pgm_read_byte_near(data++);
break;
default:
break;
}
MidiUSB.sendMIDI(evt);
}
}
void USBTimeCodeQuarterFrame(byte data)
{
midiEventPacket_t evt = {0x02, midi::TimeCodeQuarterFrame, data, 0};
MidiUSB.sendMIDI(evt);
}
void USBSongSelect(byte songnumber)
{
midiEventPacket_t evt = {0x02, midi::SongSelect, songnumber, 0};
MidiUSB.sendMIDI(evt);
}
void USBSongPosition(unsigned beats)
{
midiEventPacket_t evt = {0x03, midi::SongPosition, (byte)(beats & 0x7F),
(byte)((beats >> 7) & 0x7F)};
MidiUSB.sendMIDI(evt);
}
inline void USBRealTime(midi::MidiType midiType)
{
midiEventPacket_t evt = {0x0F, (byte)midiType, 0, 0};
MidiUSB.sendMIDI(evt);
}
void USBTuneRequest(void)
{
USBRealTime(midi::TuneRequest);
}
void USBTimingClock(void)
{
USBRealTime(midi::Clock);
}
void USBStart(void)
{
USBRealTime(midi::Start);
}
void USBContinue(void)
{
USBRealTime(midi::Continue);
}
void USBStop(void)
{
USBRealTime(midi::Stop);
}
void USBActiveSensing(void)
{
USBRealTime(midi::ActiveSensing);
}
void USBSystemReset(void)
{
USBRealTime(midi::SystemReset);
}
void setup()
{
SERIAL_PORT_MONITOR.begin(115200);
SERIAL_PORT_MONITOR.println("Buttons 4 all!");
for (uint8_t i = 0; i < sizeof(BUTTONS); i++) {
pinMode(BUTTONS[i], INPUT_PULLUP);
debouncer[i] = Bounce();
// After setting up the button, setup the Bounce instance :
debouncer[i].attach(BUTTONS[i]);
debouncer[i].interval(5); // interval in ms
}
}
const byte SYSEX1[] PROGMEM = {
0xF0, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF7
};
void loop() {
for (uint8_t i = 0; i < sizeof(BUTTONS); i++) {
debouncer[i].update();
if (debouncer[i].fell()) {
SERIAL_PORT_MONITOR.print(F("Button "));
SERIAL_PORT_MONITOR.print(i);
SERIAL_PORT_MONITOR.println(F(" down"));
switch (i) {
case 0:
USBNoteOn(1, 60, 127);
break;
case 1:
USBProgramChange(1, 16);
break;
case 2:
USBSystemReset();
break;
case 3:
USBSystemExclusive_P(sizeof(SYSEX1), SYSEX1);
break;
default:
SERIAL_PORT_MONITOR.println(F("This should never happen!"));
break;
}
MidiUSB.flush();
}
else if (debouncer[i].rose()) {
SERIAL_PORT_MONITOR.print(F("Button "));
SERIAL_PORT_MONITOR.print(i);
SERIAL_PORT_MONITOR.println(F(" up"));
switch (i) {
case 0:
USBNoteOff(1, 60, 127);
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
SERIAL_PORT_MONITOR.println(F("This should never happen either!"));
break;
}
MidiUSB.flush();
}
}
}