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