USB Host Shield 2.0
usbh_midi.h
Go to the documentation of this file.
1 /*
2  *******************************************************************************
3  * USB-MIDI class driver for USB Host Shield 2.0 Library
4  * Copyright (c) 2012-2016 Yuuichi Akagawa
5  *
6  * Idea from LPK25 USB-MIDI to Serial MIDI converter
7  * by Collin Cunningham - makezine.com, narbotic.com
8  *
9  * for use with USB Host Shield 2.0 from Circuitsathome.com
10  * https://github.com/felis/USB_Host_Shield_2.0
11  *******************************************************************************
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>
24  *******************************************************************************
25  */
26 
27 #if !defined(_USBH_MIDI_H_)
28 #define _USBH_MIDI_H_
29 #include "Usb.h"
30 
31 #define MIDI_MAX_ENDPOINTS 5 //endpoint 0, bulk_IN(MIDI), bulk_OUT(MIDI), bulk_IN(VSP), bulk_OUT(VSP)
32 #define USB_SUBCLASS_MIDISTREAMING 3
33 #define DESC_BUFF_SIZE 256
34 #define MIDI_EVENT_PACKET_SIZE 64
35 class USBH_MIDI;
36 
37 class USBH_MIDI : public USBDeviceConfig
38 {
39 private:
40  uint8_t lookupMsgSize(uint8_t midiMsg);
41 
42 protected:
43  static const uint8_t epDataInIndex; // DataIn endpoint index(MIDI)
44  static const uint8_t epDataOutIndex; // DataOUT endpoint index(MIDI)
45  static const uint8_t epDataInIndexVSP; // DataIn endpoint index(Vendor Specific Protocl)
46  static const uint8_t epDataOutIndexVSP; // DataOUT endpoint index(Vendor Specific Protocl)
47 
48  /* mandatory members */
50  uint8_t bAddress;
51  uint8_t bConfNum; // configuration number
52  uint8_t bNumEP; // total number of EP in the configuration
54 
56  /* Endpoint data structure */
58  /* MIDI Event packet buffer */
60  uint8_t readPtr;
61 
62  void parseConfigDescr(byte addr, byte conf);
63  unsigned int countSysExDataSize(uint8_t *dataptr);
64 #ifdef DEBUG_USB_HOST
65  void PrintEndpointDescriptor( const USB_ENDPOINT_DESCRIPTOR* ep_ptr );
66 #endif
67 public:
68  uint16_t pid, vid;
69  USBH_MIDI(USB *p);
70  // Methods for recieving and sending data
71  uint8_t RecvData(uint16_t *bytes_rcvd, uint8_t *dataptr);
72  uint8_t RecvData(uint8_t *outBuf);
73  uint8_t SendData(uint8_t *dataptr, byte nCable=0);
74  uint8_t SendSysEx(uint8_t *dataptr, unsigned int datasize, byte nCable=0);
75  // backward compatibility functions
76  inline uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr){ return RecvData(bytes_rcvd, dataptr); };
77  inline uint8_t RcvData(uint8_t *outBuf){ return RecvData(outBuf); };
78 
79  // USBDeviceConfig implementation
80  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
81  virtual uint8_t Release();
82  virtual uint8_t GetAddress() { return bAddress; };
83 };
84 
85 #endif //_USBH_MIDI_H_
static const uint8_t epDataInIndexVSP
Definition: usbh_midi.h:45
uint8_t readPtr
Definition: usbh_midi.h:60
uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr)
Definition: usbh_midi.h:76
uint8_t RecvData(uint16_t *bytes_rcvd, uint8_t *dataptr)
Definition: usbh_midi.cpp:321
uint16_t vid
Definition: usbh_midi.h:68
virtual uint8_t GetAddress()
Definition: usbh_midi.h:82
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: usbh_midi.cpp:110
USB * pUsb
Definition: usbh_midi.h:49
void parseConfigDescr(byte addr, byte conf)
Definition: usbh_midi.cpp:240
static const uint8_t epDataOutIndexVSP
Definition: usbh_midi.h:46
uint8_t bNumEP
Definition: usbh_midi.h:52
uint8_t bAddress
Definition: usbh_midi.h:50
Definition: address.h:32
uint16_t pid
Definition: usbh_midi.h:68
virtual uint8_t Release()
Definition: usbh_midi.cpp:310
bool isMidiFound
Definition: usbh_midi.h:55
unsigned int countSysExDataSize(uint8_t *dataptr)
Definition: usbh_midi.cpp:487
bool bPollEnable
Definition: usbh_midi.h:53
USBH_MIDI(USB *p)
Definition: usbh_midi.cpp:87
static const uint8_t epDataInIndex
Definition: usbh_midi.h:43
#define MIDI_EVENT_PACKET_SIZE
Definition: usbh_midi.h:34
uint8_t SendData(uint8_t *dataptr, byte nCable=0)
Definition: usbh_midi.cpp:373
uint8_t recvBuf[MIDI_EVENT_PACKET_SIZE]
Definition: usbh_midi.h:59
uint8_t bConfNum
Definition: usbh_midi.h:51
EpInfo epInfo[MIDI_MAX_ENDPOINTS]
Definition: usbh_midi.h:57
uint8_t RcvData(uint8_t *outBuf)
Definition: usbh_midi.h:77
static const uint8_t epDataOutIndex
Definition: usbh_midi.h:44
#define MIDI_MAX_ENDPOINTS
Definition: usbh_midi.h:31
Definition: UsbCore.h:197
uint8_t SendSysEx(uint8_t *dataptr, unsigned int datasize, byte nCable=0)
Definition: usbh_midi.cpp:511