My Fork of F401RE-USBHost. Add USBHostMIDI functions (originaled by Kaoru Shoji http://mbed.org/users/kshoji/code/USBHostMIDI/)

Dependencies:   FATFileSystem

Dependents:   F401RE-USBHostMIDI_RecieveExample

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
hsgw
Date:
Wed Sep 17 14:29:11 2014 +0000
Revision:
23:9c1d09c6a2b6
Parent:
22:81d8c59d1070
Child:
24:5281ea9f6e68
revert rev20

Who changed what in which revision?

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