Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbh_midi.h Source File

usbh_midi.h

00001 /*
00002  *******************************************************************************
00003  * USB-MIDI class driver for USB Host Shield 2.0 Library
00004  * Copyright (c) 2012-2018 Yuuichi Akagawa
00005  *
00006  * Idea from LPK25 USB-MIDI to Serial MIDI converter
00007  *   by Collin Cunningham - makezine.com, narbotic.com
00008  *
00009  * for use with USB Host Shield 2.0 from Circuitsathome.com
00010  * https://github.com/felis/USB_Host_Shield_2.0
00011  *******************************************************************************
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00024  *******************************************************************************
00025  */
00026 
00027 #if !defined(_USBH_MIDI_H_)
00028 #define _USBH_MIDI_H_
00029 //#define DEBUG_USB_HOST
00030 #include "Usb.h"
00031 
00032 #define MIDI_MAX_ENDPOINTS 5 //endpoint 0, bulk_IN(MIDI), bulk_OUT(MIDI), bulk_IN(VSP), bulk_OUT(VSP)
00033 #define USB_SUBCLASS_MIDISTREAMING 3
00034 #define DESC_BUFF_SIZE        256
00035 #define MIDI_EVENT_PACKET_SIZE 64
00036 #define MIDI_MAX_SYSEX_SIZE   256
00037 class USBH_MIDI;
00038 
00039 class USBH_MIDI : public USBDeviceConfig
00040 {
00041 protected:
00042         static const uint8_t    epDataInIndex;          // DataIn endpoint index(MIDI)
00043         static const uint8_t    epDataOutIndex;         // DataOUT endpoint index(MIDI)
00044         static const uint8_t    epDataInIndexVSP;       // DataIn endpoint index(Vendor Specific Protocl)
00045         static const uint8_t    epDataOutIndexVSP;      // DataOUT endpoint index(Vendor Specific Protocl)
00046 
00047         /* mandatory members */
00048         Usb      *pUsb;
00049         uint8_t  bAddress;
00050         uint8_t  bConfNum;    // configuration number
00051         uint8_t  bNumEP;      // total number of EP in the configuration
00052         bool     bPollEnable;
00053         bool     isMidiFound;
00054         uint16_t pid, vid;    // ProductID, VendorID
00055         uint8_t  bTransferTypeMask;
00056         /* Endpoint data structure */
00057         EpInfo  epInfo[MIDI_MAX_ENDPOINTS];
00058         /* MIDI Event packet buffer */
00059         uint8_t recvBuf[MIDI_EVENT_PACKET_SIZE];
00060         uint8_t readPtr;
00061 
00062         uint8_t parseConfigDescr(uint8_t addr, uint8_t conf);
00063         uint16_t countSysExDataSize(uint8_t *dataptr);
00064         void setupDeviceSpecific();
00065 #ifdef DEBUG_USB_HOST
00066         void PrintEndpointDescriptor( const USB_ENDPOINT_DESCRIPTOR* ep_ptr );
00067 #endif
00068 public:
00069         USBH_MIDI(Usb *p);
00070         // Misc functions
00071         operator bool() { return (pUsb->getUsbTaskState()==USB_STATE_RUNNING); }
00072         uint16_t idVendor() { return vid; }
00073         uint16_t idProduct() { return pid; }
00074         // Methods for recieving and sending data
00075         uint8_t RecvData(uint16_t *bytes_rcvd, uint8_t *dataptr);
00076         uint8_t RecvData(uint8_t *outBuf, bool isRaw=false);
00077         uint8_t RecvRawData(uint8_t *outBuf);
00078         uint8_t SendData(uint8_t *dataptr, uint8_t nCable=0);
00079         uint8_t lookupMsgSize(uint8_t midiMsg, uint8_t cin=0);
00080         uint8_t SendSysEx(uint8_t *dataptr, uint16_t datasize, uint8_t nCable=0);
00081         uint8_t extractSysExData(uint8_t *p, uint8_t *buf);
00082         uint8_t SendRawData(uint16_t bytes_send, uint8_t *dataptr);
00083         // backward compatibility functions
00084         inline uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr) { return RecvData(bytes_rcvd, dataptr); };
00085         inline uint8_t RcvData(uint8_t *outBuf) { return RecvData(outBuf); };
00086 
00087         // USBDeviceConfig implementation
00088         virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
00089         virtual uint8_t Release();
00090         virtual uint8_t GetAddress() { return bAddress; };
00091 };
00092 #endif //_USBH_MIDI_H_