Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
USBMIDI.h
00001 /* 00002 * Copyright (c) 2018-2019, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef USBMIDI_H 00019 #define USBMIDI_H 00020 00021 /* These headers are included for child class. */ 00022 #include "USBDescriptor.h" 00023 #include "USBDevice_Types.h" 00024 00025 #include "USBDevice.h" 00026 #include "MIDIMessage.h" 00027 #include "EventFlags.h" 00028 #include "Mutex.h" 00029 #include "Callback.h" 00030 00031 #define DEFAULT_CONFIGURATION (1) 00032 00033 /** 00034 * \defgroup drivers_USBMIDI USBMIDI class 00035 * \ingroup drivers-public-api-usb 00036 * @{ 00037 */ 00038 00039 /** 00040 * USBMIDI example 00041 * 00042 * @code 00043 * #include "mbed.h" 00044 * #include "USBMIDI.h" 00045 * 00046 * USBMIDI midi; 00047 * 00048 * int main() { 00049 * while (1) { 00050 * for(int i=48; i<83; i++) { // send some messages! 00051 * midi.write(MIDIMessage::NoteOn(i)); 00052 * wait(0.25); 00053 * midi.write(MIDIMessage::NoteOff(i)); 00054 * wait(0.5); 00055 * } 00056 * } 00057 * } 00058 * @endcode 00059 */ 00060 class USBMIDI: public USBDevice { 00061 public: 00062 00063 /** 00064 * Basic constructor 00065 * 00066 * Construct this object optionally connecting and blocking until it is ready. 00067 * 00068 * @note Do not use this constructor in derived classes. 00069 * 00070 * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state 00071 * @param vendor_id Your vendor_id 00072 * @param product_id Your product_id 00073 * @param product_release Your product_release 00074 */ 00075 USBMIDI(bool connect_blocking = true, uint16_t vendor_id = 0x0700, uint16_t product_id = 0x0101, uint16_t product_release = 0x0001); 00076 00077 /** 00078 * Fully featured constructor 00079 * 00080 * Construct this object with the supplied USBPhy and parameters. The user 00081 * this object is responsible for calling connect() or init(). 00082 * 00083 * @note Derived classes must use this constructor and call init() or 00084 * connect() themselves. Derived classes should also call deinit() in 00085 * their destructor. This ensures that no interrupts can occur when the 00086 * object is partially constructed or destroyed. 00087 * 00088 * @param phy USB phy to use 00089 * @param vendor_id Your vendor_id 00090 * @param product_id Your product_id 00091 * @param product_release Your product_release 00092 */ 00093 USBMIDI(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release); 00094 00095 /** 00096 * Destroy this object 00097 * 00098 * Any classes which inherit from this class must call deinit 00099 * before this destructor runs. 00100 */ 00101 virtual ~USBMIDI(); 00102 00103 /** 00104 * Check if this class is ready 00105 * 00106 * @return true if configured, false otherwise 00107 */ 00108 bool ready(); 00109 00110 /** 00111 * Block until this device is configured 00112 */ 00113 void wait_ready(); 00114 00115 /** 00116 * Send a MIDIMessage 00117 * 00118 * @param m The MIDIMessage to send 00119 * @return true if the message was sent, false otherwise 00120 */ 00121 bool write(MIDIMessage m); 00122 00123 /** 00124 * Check if a message can be read 00125 * 00126 * @return true if a packet can be read false otherwise 00127 * @note USBMIDI::attach must be called to enable the receiver 00128 */ 00129 bool readable(); 00130 00131 /** 00132 * Read a message 00133 * 00134 * @param m The MIDIMessage to fill 00135 * @return true if a message was read, false otherwise 00136 */ 00137 bool read(MIDIMessage *m); 00138 00139 /** 00140 * Attach a callback for when a MIDIEvent is received 00141 * 00142 * @param callback code to call when a packet is received 00143 */ 00144 void attach(mbed::Callback<void()> callback); 00145 00146 00147 protected: 00148 00149 virtual void callback_state_change(DeviceState new_state); 00150 00151 virtual void callback_request(const setup_packet_t *setup); 00152 00153 virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted); 00154 00155 virtual void callback_set_configuration(uint8_t configuration); 00156 00157 virtual void callback_set_interface(uint16_t interface, uint8_t alternate); 00158 00159 virtual const uint8_t *string_iproduct_desc(); 00160 00161 virtual const uint8_t *string_iinterface_desc(); 00162 00163 virtual const uint8_t *configuration_desc(uint8_t index); 00164 00165 private: 00166 static const uint32_t MaxSize = 64; 00167 00168 uint8_t _bulk_buf[MaxSize]; 00169 uint32_t _bulk_buf_pos; 00170 uint32_t _bulk_buf_size; 00171 00172 bool _data_ready; 00173 uint8_t _data[MAX_MIDI_MESSAGE_SIZE + 1]; 00174 uint32_t _cur_data; 00175 00176 rtos::EventFlags _flags; 00177 rtos::Mutex _write_mutex; 00178 00179 usb_ep_t _bulk_in; 00180 usb_ep_t _bulk_out; 00181 uint8_t _config_descriptor[0x65]; 00182 00183 mbed::Callback<void()> _callback; 00184 00185 void _init(); 00186 void _in_callback(); 00187 void _out_callback(); 00188 bool _next_message(); 00189 }; 00190 00191 /** @}*/ 00192 00193 #endif
Generated on Tue Jul 12 2022 13:55:02 by
1.7.2