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 "Mutex.h"
29 #include "Callback.h"
30 
31 #define DEFAULT_CONFIGURATION (1)
32 
33 /**
34 * USBMIDI example
35 *
36 * @code
37 * #include "mbed.h"
38 * #include "USBMIDI.h"
39 *
40 * USBMIDI midi;
41 *
42 * int main() {
43 * while (1) {
44 * for(int i=48; i<83; i++) { // send some messages!
45 * midi.write(MIDIMessage::NoteOn(i));
46 * wait(0.25);
47 * midi.write(MIDIMessage::NoteOff(i));
48 * wait(0.5);
49 * }
50 * }
51 * }
52 * @endcode
53 */
54 class USBMIDI: public USBDevice {
55 public:
56 
57  /**
58  * Basic constructor
59  *
60  * Construct this object optionally connecting and blocking until it is ready.
61  *
62  * @note Do not use this constructor in derived classes.
63  *
64  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
65  * @param vendor_id Your vendor_id
66  * @param product_id Your product_id
67  * @param product_release Your product_release
68  */
69  USBMIDI(bool connect_blocking = true, uint16_t vendor_id = 0x0700, uint16_t product_id = 0x0101, uint16_t product_release = 0x0001);
70 
71  /**
72  * Fully featured constructor
73  *
74  * Construct this object with the supplied USBPhy and parameters. The user
75  * this object is responsible for calling connect() or init().
76  *
77  * @note Derived classes must use this constructor and call init() or
78  * connect() themselves. Derived classes should also call deinit() in
79  * their destructor. This ensures that no interrupts can occur when the
80  * object is partially constructed or destroyed.
81  *
82  * @param phy USB phy to use
83  * @param vendor_id Your vendor_id
84  * @param product_id Your product_id
85  * @param product_release Your product_release
86  */
87  USBMIDI(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
88 
89  /**
90  * Destroy this object
91  *
92  * Any classes which inherit from this class must call deinit
93  * before this destructor runs.
94  */
95  virtual ~USBMIDI();
96 
97  /**
98  * Check if this class is ready
99  *
100  * @return true if configured, false otherwise
101  */
102  bool ready();
103 
104  /**
105  * Block until this device is configured
106  */
107  void wait_ready();
108 
109  /**
110  * Send a MIDIMessage
111  *
112  * @param m The MIDIMessage to send
113  * @return true if the message was sent, false otherwise
114  */
115  bool write(MIDIMessage m);
116 
117  /**
118  * Check if a message can be read
119  *
120  * @return true if a packet can be read false otherwise
121  * @note USBMIDI::attach must be called to enable the receiver
122  */
123  bool readable();
124 
125  /**
126  * Read a message
127  *
128  * @param m The MIDIMessage to fill
129  * @return true if a message was read, false otherwise
130  */
131  bool read(MIDIMessage *m);
132 
133  /**
134  * Attach a callback for when a MIDIEvent is received
135  *
136  * @param callback code to call when a packet is received
137  */
138  void attach(mbed::Callback<void()> callback);
139 
140 
141 protected:
142 
143  virtual void callback_state_change(DeviceState new_state);
144 
145  virtual void callback_request(const setup_packet_t *setup);
146 
147  virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
148 
149  virtual void callback_set_configuration(uint8_t configuration);
150 
151  virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
152 
153  virtual const uint8_t *string_iproduct_desc();
154 
155  virtual const uint8_t *string_iinterface_desc();
156 
157  virtual const uint8_t *configuration_desc(uint8_t index);
158 
159 private:
160  static const uint32_t MaxSize = 64;
161 
162  uint8_t _bulk_buf[MaxSize];
163  uint32_t _bulk_buf_pos;
164  uint32_t _bulk_buf_size;
165 
166  bool _data_ready;
167  uint8_t _data[MAX_MIDI_MESSAGE_SIZE + 1];
168  uint32_t _cur_data;
169 
170  rtos::EventFlags _flags;
171  rtos::Mutex _write_mutex;
172 
173  usb_ep_t _bulk_in;
174  usb_ep_t _bulk_out;
175  uint8_t _config_descriptor[0x65];
176 
177  mbed::Callback<void()> _callback;
178 
179  void _init();
180  void _in_callback();
181  void _out_callback();
182  bool _next_message();
183 };
184 
185 #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:49
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:54
bool ready()
Check if this class is ready.
bool read(MIDIMessage *m)
Read a message.
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
virtual ~USBMIDI()
Destroy this object.
A MIDI message container.
Definition: MIDIMessage.h:44
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:66
Core USB Device driver.
Definition: USBDevice.h:43
void wait_ready()
Block until this device is configured.
Callback class based on template specialization.
Definition: Callback.h:39
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.