Skip to content

Commit 9017f9f

Browse files
committed
added usb midi and its example
1 parent ee5bc43 commit 9017f9f

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

examples/MIDI/midi_test/midi_test.ino

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*********************************************************************
2+
Adafruit invests time and resources providing this open source code,
3+
please support Adafruit and open-source hardware by purchasing
4+
products from Adafruit!
5+
6+
MIT license, check LICENSE for more information
7+
Copyright (c) 2019 Ha Thach for Adafruit Industries
8+
All text above, and the splash screen below must be included in
9+
any redistribution
10+
*********************************************************************/
11+
12+
#include <Arduino.h>
13+
#include "Adafruit_TinyUSB.h"
14+
#include <MIDI.h>
15+
16+
// USB MIDI object
17+
Adafruit_USBD_MIDI usb_midi;
18+
19+
// Create a new instance of the Arduino MIDI Library,
20+
// and attach usb_midi as the transport.
21+
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);
22+
23+
// Variable that holds the current position in the sequence.
24+
uint32_t position = 0;
25+
26+
// Store example melody as an array of note values
27+
byte note_sequence[] = {
28+
74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78,
29+
74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61,
30+
56,61,64,68,74,78,81,86,90,93,98,102
31+
};
32+
33+
void setup()
34+
{
35+
pinMode(LED_BUILTIN, OUTPUT);
36+
37+
// Initialize MIDI, and listen to all MIDI channels
38+
// This will also call usb_midi's begin()
39+
MIDI.begin(MIDI_CHANNEL_OMNI);
40+
41+
// Attach the handleNoteOn function to the MIDI Library. It will
42+
// be called whenever the Bluefruit receives MIDI Note On messages.
43+
MIDI.setHandleNoteOn(handleNoteOn);
44+
45+
// Do the same for MIDI Note Off messages.
46+
MIDI.setHandleNoteOff(handleNoteOff);
47+
48+
Serial.begin(115200);
49+
50+
// wait for native usb serial to connect
51+
while(!Serial) delay(1);
52+
53+
Serial.println("Adafruit TinyUSB MIDI example");
54+
}
55+
56+
void loop()
57+
{
58+
static uint32_t start_ms = 0;
59+
if ( millis() - start_ms > 266 )
60+
{
61+
start_ms += 266;
62+
63+
// Setup variables for the current and previous
64+
// positions in the note sequence.
65+
int previous = position - 1;
66+
67+
// If we currently are at position 0, set the
68+
// previous position to the last note in the sequence.
69+
if (previous < 0) {
70+
previous = sizeof(note_sequence) - 1;
71+
}
72+
73+
// Send Note On for current position at full velocity (127) on channel 1.
74+
MIDI.sendNoteOn(note_sequence[position], 127, 1);
75+
76+
// Send Note Off for previous note.
77+
MIDI.sendNoteOff(note_sequence[previous], 0, 1);
78+
79+
// Increment position
80+
position++;
81+
82+
// If we are at the end of the sequence, start over.
83+
if (position >= sizeof(note_sequence)) {
84+
position = 0;
85+
}
86+
}
87+
88+
// read any new MIDI messages
89+
MIDI.read();
90+
}
91+
92+
void handleNoteOn(byte channel, byte pitch, byte velocity)
93+
{
94+
// Log when a note is pressed.
95+
Serial.printf("Note on: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity);
96+
Serial.println();
97+
}
98+
99+
void handleNoteOff(byte channel, byte pitch, byte velocity)
100+
{
101+
// Log when a note is released.
102+
Serial.printf("Note off: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity);
103+
Serial.println();
104+
}

src/Adafruit_TinyUSB.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727

2828
#include "Adafruit_USBD_MSC.h"
2929
#include "Adafruit_USBD_HID.h"
30+
#include "Adafruit_USBD_MIDI.h"
3031

3132
#endif /* ADAFRUIT_TINYUSB_H_ */

src/Adafruit_USBD_MIDI.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 hathach for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "Adafruit_USBD_MIDI.h"
26+
27+
#if CFG_TUD_MIDI
28+
29+
//--------------------------------------------------------------------+
30+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
31+
//--------------------------------------------------------------------+
32+
#define EPOUT 0x00
33+
#define EPIN 0x80
34+
#define EPSIZE 64
35+
36+
Adafruit_USBD_MIDI::Adafruit_USBD_MIDI(void)
37+
{
38+
39+
}
40+
41+
bool Adafruit_USBD_MIDI::begin(void)
42+
{
43+
if ( !USBDevice.addInterface(*this) ) return false;
44+
45+
return true;
46+
}
47+
48+
uint16_t Adafruit_USBD_MIDI::getDescriptor(uint8_t* buf, uint16_t bufsize)
49+
{
50+
// usb core will automatically update interface number and endpoint number
51+
uint8_t desc[] = { TUD_MIDI_DESCRIPTOR(0, 0, EPOUT, EPIN, EPSIZE) };
52+
uint16_t const len = sizeof(desc);
53+
54+
if ( bufsize < len ) return 0;
55+
memcpy(buf, desc, len);
56+
return len;
57+
}
58+
59+
int Adafruit_USBD_MIDI::read (void)
60+
{
61+
uint8_t ch;
62+
return tud_midi_read(&ch, 1) ? (int) ch : (-1);
63+
}
64+
65+
size_t Adafruit_USBD_MIDI::write (uint8_t b)
66+
{
67+
return tud_midi_write(0, &b, 1);
68+
}
69+
70+
int Adafruit_USBD_MIDI::available (void)
71+
{
72+
return tud_midi_available();
73+
}
74+
75+
int Adafruit_USBD_MIDI::peek (void)
76+
{
77+
// MIDI Library doen't use peek
78+
return -1;
79+
}
80+
81+
void Adafruit_USBD_MIDI::flush (void)
82+
{
83+
// MIDI Library doen't use flush
84+
}
85+
86+
#endif
87+
88+

src/Adafruit_USBD_MIDI.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 hathach for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef ADAFRUIT_USBD_MIDI_H_
26+
#define ADAFRUIT_USBD_MIDI_H_
27+
28+
#include "Adafruit_TinyUSB_Core.h"
29+
30+
class Adafruit_USBD_MIDI : public Stream, Adafruit_USBD_Interface
31+
{
32+
public:
33+
Adafruit_USBD_MIDI(void);
34+
35+
bool begin(void);
36+
37+
// for MIDI library
38+
bool begin(uint32_t baud) { (void) baud; return begin(); }
39+
40+
// Stream interface to use with MIDI Library
41+
virtual int read ( void );
42+
virtual size_t write ( uint8_t b );
43+
virtual int available ( void );
44+
virtual int peek ( void );
45+
virtual void flush ( void );
46+
47+
// fron Adafruit_USBD_Interface
48+
virtual uint16_t getDescriptor(uint8_t* buf, uint16_t bufsize);
49+
};
50+
51+
#endif /* ADAFRUIT_USBD_MIDI_H_ */

0 commit comments

Comments
 (0)