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-2018 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 //#define DEBUG_USB_HOST
30 #include "Usb.h"
31 
32 #define MIDI_MAX_ENDPOINTS 5 //endpoint 0, bulk_IN(MIDI), bulk_OUT(MIDI), bulk_IN(VSP), bulk_OUT(VSP)
33 #define USB_SUBCLASS_MIDISTREAMING 3
34 #define DESC_BUFF_SIZE 256
35 #define MIDI_EVENT_PACKET_SIZE 64
36 #define MIDI_MAX_SYSEX_SIZE 256
37 class USBH_MIDI;
38 
39 class USBH_MIDI : public USBDeviceConfig
40 {
41 protected:
42  static const uint8_t epDataInIndex; // DataIn endpoint index(MIDI)
43  static const uint8_t epDataOutIndex; // DataOUT endpoint index(MIDI)
44  static const uint8_t epDataInIndexVSP; // DataIn endpoint index(Vendor Specific Protocl)
45  static const uint8_t epDataOutIndexVSP; // DataOUT endpoint index(Vendor Specific Protocl)
46 
47  /* mandatory members */
49  uint8_t bAddress;
50  uint8_t bConfNum; // configuration number
51  uint8_t bNumEP; // total number of EP in the configuration
54  uint16_t pid, vid; // ProductID, VendorID
56  /* Endpoint data structure */
58  /* MIDI Event packet buffer */
60  uint8_t readPtr;
61 
62  uint8_t parseConfigDescr(uint8_t addr, uint8_t conf);
63  uint16_t countSysExDataSize(uint8_t *dataptr);
64  void setupDeviceSpecific();
65 #ifdef DEBUG_USB_HOST
66  void PrintEndpointDescriptor( const USB_ENDPOINT_DESCRIPTOR* ep_ptr );
67 #endif
68 public:
69  USBH_MIDI(USB *p);
70  // Misc functions
71  operator bool() { return (pUsb->getUsbTaskState()==USB_STATE_RUNNING); }
72  uint16_t idVendor() { return vid; }
73  uint16_t idProduct() { return pid; }
74  // Methods for recieving and sending data
75  uint8_t RecvData(uint16_t *bytes_rcvd, uint8_t *dataptr);
76  uint8_t RecvData(uint8_t *outBuf, bool isRaw=false);
77  uint8_t RecvRawData(uint8_t *outBuf);
78  uint8_t SendData(uint8_t *dataptr, uint8_t nCable=0);
79  uint8_t lookupMsgSize(uint8_t midiMsg, uint8_t cin=0);
80  uint8_t SendSysEx(uint8_t *dataptr, uint16_t datasize, uint8_t nCable=0);
81  uint8_t extractSysExData(uint8_t *p, uint8_t *buf);
82  uint8_t SendRawData(uint16_t bytes_send, uint8_t *dataptr);
83  // backward compatibility functions
84  inline uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr) { return RecvData(bytes_rcvd, dataptr); };
85  inline uint8_t RcvData(uint8_t *outBuf) { return RecvData(outBuf); };
86 
87  // USBDeviceConfig implementation
88  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
89  virtual uint8_t Release();
90  virtual uint8_t GetAddress() { return bAddress; };
91 };
92 #endif //_USBH_MIDI_H_
uint8_t SendSysEx(uint8_t *dataptr, uint16_t datasize, uint8_t nCable=0)
Definition: usbh_midi.cpp:573
static const uint8_t epDataInIndexVSP
Definition: usbh_midi.h:44
uint8_t bTransferTypeMask
Definition: usbh_midi.h:55
uint8_t readPtr
Definition: usbh_midi.h:60
uint8_t RcvData(uint16_t *bytes_rcvd, uint8_t *dataptr)
Definition: usbh_midi.h:84
uint8_t RecvData(uint16_t *bytes_rcvd, uint8_t *dataptr)
Definition: usbh_midi.cpp:374
uint16_t vid
Definition: usbh_midi.h:54
virtual uint8_t GetAddress()
Definition: usbh_midi.h:90
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed)
Definition: usbh_midi.cpp:108
USB * pUsb
Definition: usbh_midi.h:48
uint8_t SendRawData(uint16_t bytes_send, uint8_t *dataptr)
Definition: usbh_midi.cpp:631
uint16_t countSysExDataSize(uint8_t *dataptr)
Definition: usbh_midi.cpp:549
static const uint8_t epDataOutIndexVSP
Definition: usbh_midi.h:45
void setupDeviceSpecific()
Definition: usbh_midi.cpp:352
#define USB_STATE_RUNNING
Definition: UsbCore.h:131
uint8_t bNumEP
Definition: usbh_midi.h:51
uint8_t bAddress
Definition: usbh_midi.h:49
Definition: address.h:39
uint16_t pid
Definition: usbh_midi.h:54
virtual uint8_t Release()
Definition: usbh_midi.cpp:341
uint8_t RecvRawData(uint8_t *outBuf)
Definition: usbh_midi.cpp:428
bool isMidiFound
Definition: usbh_midi.h:53
bool bPollEnable
Definition: usbh_midi.h:52
USBH_MIDI(USB *p)
Definition: usbh_midi.cpp:87
uint16_t idProduct()
Definition: usbh_midi.h:73
static const uint8_t epDataInIndex
Definition: usbh_midi.h:42
#define MIDI_EVENT_PACKET_SIZE
Definition: usbh_midi.h:35
uint8_t lookupMsgSize(uint8_t midiMsg, uint8_t cin=0)
Definition: usbh_midi.cpp:498
uint8_t recvBuf[MIDI_EVENT_PACKET_SIZE]
Definition: usbh_midi.h:59
uint8_t getUsbTaskState(void)
Definition: Usb.cpp:36
uint8_t bConfNum
Definition: usbh_midi.h:50
EpInfo epInfo[MIDI_MAX_ENDPOINTS]
Definition: usbh_midi.h:57
uint16_t idVendor()
Definition: usbh_midi.h:72
uint8_t RcvData(uint8_t *outBuf)
Definition: usbh_midi.h:85
uint8_t extractSysExData(uint8_t *p, uint8_t *buf)
Definition: usbh_midi.cpp:637
uint8_t SendData(uint8_t *dataptr, uint8_t nCable=0)
Definition: usbh_midi.cpp:434
static const uint8_t epDataOutIndex
Definition: usbh_midi.h:43
#define MIDI_MAX_ENDPOINTS
Definition: usbh_midi.h:32
uint8_t parseConfigDescr(uint8_t addr, uint8_t conf)
Definition: usbh_midi.cpp:260
Definition: UsbCore.h:210