Mistake on this page?
Report an issue in GitHub or email us
USBMIDI.h
1 /*
2  * Copyright (c) 2018-2019, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef USBMIDI_H
19 #define USBMIDI_H
20 
21 /* These headers are included for child class. */
22 #include "USBDescriptor.h"
23 #include "USBDevice_Types.h"
24 
25 #include "USBDevice.h"
26 #include "MIDIMessage.h"
27 #include "EventFlags.h"
28 #include "rtos/Mutex.h"
29 #include "Callback.h"
30 
31 #define DEFAULT_CONFIGURATION (1)
32 
33 /**
34  * \defgroup drivers_USBMIDI USBMIDI class
35  * \ingroup drivers-public-api-usb
36  * @{
37  */
38 
39 /**
40 * USBMIDI example
41 *
42 * @code
43 * #include "mbed.h"
44 * #include "USBMIDI.h"
45 *
46 * USBMIDI midi;
47 *
48 * int main() {
49 * while (1) {
50 * for(int i=48; i<83; i++) { // send some messages!
51 * midi.write(MIDIMessage::NoteOn(i));
52 * ThisThread::sleep_for(250);
53 * midi.write(MIDIMessage::NoteOff(i));
54 * ThisThread::sleep_for(500);
55 * }
56 * }
57 * }
58 * @endcode
59 */
60 class USBMIDI: public USBDevice {
61 public:
62 
63  /**
64  * Basic constructor
65  *
66  * Construct this object optionally connecting and blocking until it is ready.
67  *
68  * @note Do not use this constructor in derived classes.
69  *
70  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
71  * @param vendor_id Your vendor_id
72  * @param product_id Your product_id
73  * @param product_release Your product_release
74  */
75  USBMIDI(bool connect_blocking = true, uint16_t vendor_id = 0x0700, uint16_t product_id = 0x0101, uint16_t product_release = 0x0001);
76 
77  /**
78  * Fully featured constructor
79  *
80  * Construct this object with the supplied USBPhy and parameters. The user
81  * this object is responsible for calling connect() or init().
82  *
83  * @note Derived classes must use this constructor and call init() or
84  * connect() themselves. Derived classes should also call deinit() in
85  * their destructor. This ensures that no interrupts can occur when the
86  * object is partially constructed or destroyed.
87  *
88  * @param phy USB phy to use
89  * @param vendor_id Your vendor_id
90  * @param product_id Your product_id
91  * @param product_release Your product_release
92  */
93  USBMIDI(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
94 
95  /**
96  * Destroy this object
97  *
98  * Any classes which inherit from this class must call deinit
99  * before this destructor runs.
100  */
101  virtual ~USBMIDI();
102 
103  /**
104  * Check if this class is ready
105  *
106  * @return true if configured, false otherwise
107  */
108  bool ready();
109 
110  /**
111  * Block until this device is configured
112  */
113  void wait_ready();
114 
115  /**
116  * Send a MIDIMessage
117  *
118  * @param m The MIDIMessage to send
119  * @return true if the message was sent, false otherwise
120  */
121  bool write(MIDIMessage m);
122 
123  /**
124  * Check if a message can be read
125  *
126  * @return true if a packet can be read false otherwise
127  * @note USBMIDI::attach must be called to enable the receiver
128  */
129  bool readable();
130 
131  /**
132  * Read a message
133  *
134  * @param m The MIDIMessage to fill
135  * @return true if a message was read, false otherwise
136  */
137  bool read(MIDIMessage *m);
138 
139  /**
140  * Attach a callback for when a MIDIEvent is received
141  *
142  * @param callback code to call when a packet is received
143  */
144  void attach(mbed::Callback<void()> callback);
145 
146 
147 protected:
148 
149  virtual void callback_state_change(DeviceState new_state);
150 
151  virtual void callback_request(const setup_packet_t *setup);
152 
153  virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
154 
155  virtual void callback_set_configuration(uint8_t configuration);
156 
157  virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
158 
159  virtual const uint8_t *string_iproduct_desc();
160 
161  virtual const uint8_t *string_iinterface_desc();
162 
163  virtual const uint8_t *configuration_desc(uint8_t index);
164 
165 private:
166  static const uint32_t MaxSize = 64;
167 
168  uint8_t _bulk_buf[MaxSize];
169  uint32_t _bulk_buf_pos;
170  uint32_t _bulk_buf_size;
171 
172  bool _data_ready;
173  uint8_t _data[MAX_MIDI_MESSAGE_SIZE + 1];
174  uint32_t _cur_data;
175 
176  rtos::EventFlags _flags;
177  rtos::Mutex _write_mutex;
178 
179  usb_ep_t _bulk_in;
180  usb_ep_t _bulk_out;
181  uint8_t _config_descriptor[0x65];
182 
183  mbed::Callback<void()> _callback;
184 
185  void _init();
186  void _in_callback();
187  void _out_callback();
188  bool _next_message();
189 };
190 
191 /** @}*/
192 
193 #endif
bool write(MIDIMessage m)
Send a MIDIMessage.
virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted)
Called by USBDevice on data stage completion.
The EventFlags class is used to control event flags or wait for event flags other threads control...
Definition: EventFlags.h:53
USBMIDI(bool connect_blocking=true, uint16_t vendor_id=0x0700, uint16_t product_id=0x0101, uint16_t product_release=0x0001)
Basic constructor.
bool readable()
Check if a message can be read.
void attach(mbed::Callback< void()> callback)
Attach a callback for when a MIDIEvent is received.
USBMIDI example.
Definition: USBMIDI.h:60
bool ready()
Check if this class is ready.
bool read(MIDIMessage *m)
Read a message.
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=nullptr) noexcept
Create a callback class with type inferred from the arguments.
Definition: Callback.h:678
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
virtual ~USBMIDI()
Destroy this object.
A MIDI message container.
Definition: MIDIMessage.h:51
virtual void callback_request(const setup_packet_t *setup)
Called by USBDevice on Endpoint0 request.
virtual void callback_state_change(DeviceState new_state)
Called when USB changes state.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
Core USB Device driver.
Definition: USBDevice.h:38
void wait_ready()
Block until this device is configured.
Callback class based on template specialization.
Definition: Callback.h:53
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.