My Fork of F401RE-USBHost. Add USBHostMIDI functions (originaled by Kaoru Shoji http://mbed.org/users/kshoji/code/USBHostMIDI/)
Dependents: F401RE-USBHostMIDI_RecieveExample
Fork of F401RE-USBHost by
USBHostMIDI/USBHostMIDI.h@18:bac56d0365e1, 2014-06-25 (annotated)
- Committer:
- hsgw
- Date:
- Wed Jun 25 20:48:01 2014 +0000
- Revision:
- 18:bac56d0365e1
- Child:
- 22:81d8c59d1070
Add USBHostMIDI(porting https://mbed.org/users/kshoji/code/USBHostMIDI/)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hsgw | 18:bac56d0365e1 | 1 | /* USBHostMidi library |
hsgw | 18:bac56d0365e1 | 2 | * Originaled by k.shoji |
hsgw | 18:bac56d0365e1 | 3 | * porting by Takuya Urakawa |
hsgw | 18:bac56d0365e1 | 4 | */ |
hsgw | 18:bac56d0365e1 | 5 | |
hsgw | 18:bac56d0365e1 | 6 | /* mbed USBHost Library |
hsgw | 18:bac56d0365e1 | 7 | * Copyright (c) 2006-2013 ARM Limited |
hsgw | 18:bac56d0365e1 | 8 | * |
hsgw | 18:bac56d0365e1 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hsgw | 18:bac56d0365e1 | 10 | * you may not use this file except in compliance with the License. |
hsgw | 18:bac56d0365e1 | 11 | * You may obtain a copy of the License at |
hsgw | 18:bac56d0365e1 | 12 | * |
hsgw | 18:bac56d0365e1 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
hsgw | 18:bac56d0365e1 | 14 | * |
hsgw | 18:bac56d0365e1 | 15 | * Unless required by applicable law or agreed to in writing, software |
hsgw | 18:bac56d0365e1 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
hsgw | 18:bac56d0365e1 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hsgw | 18:bac56d0365e1 | 18 | * See the License for the specific language governing permissions and |
hsgw | 18:bac56d0365e1 | 19 | * limitations under the License. |
hsgw | 18:bac56d0365e1 | 20 | */ |
hsgw | 18:bac56d0365e1 | 21 | |
hsgw | 18:bac56d0365e1 | 22 | #ifndef USBHOSTMIDI_H |
hsgw | 18:bac56d0365e1 | 23 | #define USBHOSTMIDI_H |
hsgw | 18:bac56d0365e1 | 24 | |
hsgw | 18:bac56d0365e1 | 25 | #include "USBHostConf.h" |
hsgw | 18:bac56d0365e1 | 26 | |
hsgw | 18:bac56d0365e1 | 27 | #if USBHOST_MIDI |
hsgw | 18:bac56d0365e1 | 28 | |
hsgw | 18:bac56d0365e1 | 29 | #include "USBHost.h" |
hsgw | 18:bac56d0365e1 | 30 | |
hsgw | 18:bac56d0365e1 | 31 | // STM nucleo boards do not support mbed-rtos |
hsgw | 18:bac56d0365e1 | 32 | //#include "MtxCircBuffer.h" |
hsgw | 18:bac56d0365e1 | 33 | #include "CircBuffer.h" |
hsgw | 18:bac56d0365e1 | 34 | |
hsgw | 18:bac56d0365e1 | 35 | /** |
hsgw | 18:bac56d0365e1 | 36 | * A class to communicate a USB MIDI device |
hsgw | 18:bac56d0365e1 | 37 | */ |
hsgw | 18:bac56d0365e1 | 38 | class USBHostMIDI : public IUSBEnumerator { |
hsgw | 18:bac56d0365e1 | 39 | public: |
hsgw | 18:bac56d0365e1 | 40 | /** |
hsgw | 18:bac56d0365e1 | 41 | * Constructor |
hsgw | 18:bac56d0365e1 | 42 | */ |
hsgw | 18:bac56d0365e1 | 43 | USBHostMIDI(); |
hsgw | 18:bac56d0365e1 | 44 | |
hsgw | 18:bac56d0365e1 | 45 | /** |
hsgw | 18:bac56d0365e1 | 46 | * Check if a USB MIDI device is connected |
hsgw | 18:bac56d0365e1 | 47 | * |
hsgw | 18:bac56d0365e1 | 48 | * @returns true if a midi device is connected |
hsgw | 18:bac56d0365e1 | 49 | */ |
hsgw | 18:bac56d0365e1 | 50 | bool connected(); |
hsgw | 18:bac56d0365e1 | 51 | |
hsgw | 18:bac56d0365e1 | 52 | /** |
hsgw | 18:bac56d0365e1 | 53 | * Try to connect a midi device |
hsgw | 18:bac56d0365e1 | 54 | * |
hsgw | 18:bac56d0365e1 | 55 | * @return true if connection was successful |
hsgw | 18:bac56d0365e1 | 56 | */ |
hsgw | 18:bac56d0365e1 | 57 | bool connect(); |
hsgw | 18:bac56d0365e1 | 58 | |
hsgw | 18:bac56d0365e1 | 59 | /** |
hsgw | 18:bac56d0365e1 | 60 | * Attach a callback called when note on is received |
hsgw | 18:bac56d0365e1 | 61 | * |
hsgw | 18:bac56d0365e1 | 62 | * @param ptr function pointer |
hsgw | 18:bac56d0365e1 | 63 | */ |
hsgw | 18:bac56d0365e1 | 64 | inline void attachNoteOn(void (*fn)(unsigned char, unsigned char, unsigned char)) { |
hsgw | 18:bac56d0365e1 | 65 | if (fn != NULL) { |
hsgw | 18:bac56d0365e1 | 66 | noteOn = fn; |
hsgw | 18:bac56d0365e1 | 67 | } |
hsgw | 18:bac56d0365e1 | 68 | } |
hsgw | 18:bac56d0365e1 | 69 | |
hsgw | 18:bac56d0365e1 | 70 | /** |
hsgw | 18:bac56d0365e1 | 71 | * Attach a callback called when note off is received |
hsgw | 18:bac56d0365e1 | 72 | * |
hsgw | 18:bac56d0365e1 | 73 | * @param ptr function pointer |
hsgw | 18:bac56d0365e1 | 74 | */ |
hsgw | 18:bac56d0365e1 | 75 | inline void attachNoteOff(void (*fn)(unsigned char, unsigned char, unsigned char)) { |
hsgw | 18:bac56d0365e1 | 76 | if (fn != NULL) { |
hsgw | 18:bac56d0365e1 | 77 | noteOff = fn; |
hsgw | 18:bac56d0365e1 | 78 | } |
hsgw | 18:bac56d0365e1 | 79 | } |
hsgw | 18:bac56d0365e1 | 80 | |
hsgw | 18:bac56d0365e1 | 81 | /** |
hsgw | 18:bac56d0365e1 | 82 | * Attach a callback called when control change is received |
hsgw | 18:bac56d0365e1 | 83 | * |
hsgw | 18:bac56d0365e1 | 84 | * @param ptr function pointer |
hsgw | 18:bac56d0365e1 | 85 | */ |
hsgw | 18:bac56d0365e1 | 86 | inline void attachControlChange(void (*fn)(unsigned char, unsigned char, unsigned char)) { |
hsgw | 18:bac56d0365e1 | 87 | if (fn != NULL) { |
hsgw | 18:bac56d0365e1 | 88 | controlChange = fn; |
hsgw | 18:bac56d0365e1 | 89 | } |
hsgw | 18:bac56d0365e1 | 90 | } |
hsgw | 18:bac56d0365e1 | 91 | |
hsgw | 18:bac56d0365e1 | 92 | /** |
hsgw | 18:bac56d0365e1 | 93 | * Attach a callback called when program change is received |
hsgw | 18:bac56d0365e1 | 94 | * |
hsgw | 18:bac56d0365e1 | 95 | * @param ptr function pointer |
hsgw | 18:bac56d0365e1 | 96 | */ |
hsgw | 18:bac56d0365e1 | 97 | inline void attachProgramChange(void (*fn)(unsigned char, unsigned char)) { |
hsgw | 18:bac56d0365e1 | 98 | if (fn != NULL) { |
hsgw | 18:bac56d0365e1 | 99 | programChange = fn; |
hsgw | 18:bac56d0365e1 | 100 | } |
hsgw | 18:bac56d0365e1 | 101 | } |
hsgw | 18:bac56d0365e1 | 102 | |
hsgw | 18:bac56d0365e1 | 103 | /** |
hsgw | 18:bac56d0365e1 | 104 | * Attach a callback called when pitch bend is received |
hsgw | 18:bac56d0365e1 | 105 | * |
hsgw | 18:bac56d0365e1 | 106 | * @param ptr function pointer |
hsgw | 18:bac56d0365e1 | 107 | */ |
hsgw | 18:bac56d0365e1 | 108 | inline void attachPitchBend(void (*fn)(unsigned char, unsigned int)) { |
hsgw | 18:bac56d0365e1 | 109 | if (fn != NULL) { |
hsgw | 18:bac56d0365e1 | 110 | pitchBend = fn; |
hsgw | 18:bac56d0365e1 | 111 | } |
hsgw | 18:bac56d0365e1 | 112 | } |
hsgw | 18:bac56d0365e1 | 113 | |
hsgw | 18:bac56d0365e1 | 114 | int sendNoteOn(unsigned char channel, unsigned char note, unsigned char velocity); |
hsgw | 18:bac56d0365e1 | 115 | int sendNoteOff(unsigned char channel, unsigned char note, unsigned char velocity); |
hsgw | 18:bac56d0365e1 | 116 | int sendControlChange(unsigned char channel, unsigned char key, unsigned char value); |
hsgw | 18:bac56d0365e1 | 117 | int sendProgramChange(unsigned char channel, unsigned char program); |
hsgw | 18:bac56d0365e1 | 118 | int sendPitchBend(unsigned char channel, unsigned int value); |
hsgw | 18:bac56d0365e1 | 119 | |
hsgw | 18:bac56d0365e1 | 120 | void poll(); |
hsgw | 18:bac56d0365e1 | 121 | |
hsgw | 18:bac56d0365e1 | 122 | protected: |
hsgw | 18:bac56d0365e1 | 123 | //From IUSBEnumerator |
hsgw | 18:bac56d0365e1 | 124 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
hsgw | 18:bac56d0365e1 | 125 | virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed |
hsgw | 18:bac56d0365e1 | 126 | virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used |
hsgw | 18:bac56d0365e1 | 127 | |
hsgw | 18:bac56d0365e1 | 128 | private: |
hsgw | 18:bac56d0365e1 | 129 | USBHost * host; |
hsgw | 18:bac56d0365e1 | 130 | USBDeviceConnected * dev; |
hsgw | 18:bac56d0365e1 | 131 | USBEndpoint * bulk_in; |
hsgw | 18:bac56d0365e1 | 132 | USBEndpoint * bulk_out; |
hsgw | 18:bac56d0365e1 | 133 | uint32_t size_bulk_in; |
hsgw | 18:bac56d0365e1 | 134 | uint32_t size_bulk_out; |
hsgw | 18:bac56d0365e1 | 135 | |
hsgw | 18:bac56d0365e1 | 136 | bool dev_connected; |
hsgw | 18:bac56d0365e1 | 137 | |
hsgw | 18:bac56d0365e1 | 138 | void init(); |
hsgw | 18:bac56d0365e1 | 139 | |
hsgw | 18:bac56d0365e1 | 140 | CircBuffer<uint8_t, 128> circ_buf; |
hsgw | 18:bac56d0365e1 | 141 | |
hsgw | 18:bac56d0365e1 | 142 | uint8_t buf[128]; |
hsgw | 18:bac56d0365e1 | 143 | |
hsgw | 18:bac56d0365e1 | 144 | void rxHandler(); |
hsgw | 18:bac56d0365e1 | 145 | |
hsgw | 18:bac56d0365e1 | 146 | void (*noteOn)(unsigned char, unsigned char, unsigned char); |
hsgw | 18:bac56d0365e1 | 147 | void (*noteOff)(unsigned char, unsigned char, unsigned char); |
hsgw | 18:bac56d0365e1 | 148 | void (*controlChange)(unsigned char, unsigned char, unsigned char); |
hsgw | 18:bac56d0365e1 | 149 | void (*programChange)(unsigned char, unsigned char); |
hsgw | 18:bac56d0365e1 | 150 | void (*pitchBend)(unsigned char, unsigned int); |
hsgw | 18:bac56d0365e1 | 151 | |
hsgw | 18:bac56d0365e1 | 152 | int midi_intf; |
hsgw | 18:bac56d0365e1 | 153 | bool midi_device_found; |
hsgw | 18:bac56d0365e1 | 154 | |
hsgw | 18:bac56d0365e1 | 155 | }; |
hsgw | 18:bac56d0365e1 | 156 | |
hsgw | 18:bac56d0365e1 | 157 | #endif |
hsgw | 18:bac56d0365e1 | 158 | |
hsgw | 18:bac56d0365e1 | 159 | #endif |