Added incomplete MIDIToneGenerator project.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MIDI.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_DEVICE)
34
35 #define __INCLUDE_FROM_MIDI_DRIVER
36 #include "MIDI.h"
37
38 void MIDI_Device_ProcessControlRequest(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
39 {
40
41 }
42
43 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
44 {
45 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
46
47 if (MIDIInterfaceInfo->Config.DataINEndpointNumber)
48 {
49 if (!(Endpoint_ConfigureEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK,
50 ENDPOINT_DIR_IN, MIDIInterfaceInfo->Config.DataINEndpointSize,
51 MIDIInterfaceInfo->Config.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
52 {
53 return false;
54 }
55 }
56
57 if (MIDIInterfaceInfo->Config.DataOUTEndpointNumber)
58 {
59 if (!(Endpoint_ConfigureEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK,
60 ENDPOINT_DIR_OUT, MIDIInterfaceInfo->Config.DataOUTEndpointSize,
61 MIDIInterfaceInfo->Config.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
62 {
63 return false;
64 }
65 }
66
67 return true;
68 }
69
70 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)
71 {
72 if (USB_DeviceState != DEVICE_STATE_Configured)
73 return ENDPOINT_RWSTREAM_DeviceDisconnected;
74
75 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
76
77 if (Endpoint_IsReadWriteAllowed());
78 {
79 uint8_t ErrorCode;
80
81 if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != ENDPOINT_RWSTREAM_NoError)
82 return ErrorCode;
83
84 if (!(Endpoint_IsReadWriteAllowed()))
85 Endpoint_ClearIN();
86 }
87
88 return ENDPOINT_RWSTREAM_NoError;
89 }
90
91 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
92 {
93 if (USB_DeviceState != DEVICE_STATE_Configured)
94 return ENDPOINT_RWSTREAM_DeviceDisconnected;
95
96 uint8_t ErrorCode;
97
98 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
99
100 if (Endpoint_BytesInEndpoint())
101 {
102 Endpoint_ClearIN();
103
104 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
105 return ErrorCode;
106 }
107
108 return ENDPOINT_READYWAIT_NoError;
109 }
110
111 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)
112 {
113 if (USB_DeviceState != DEVICE_STATE_Configured)
114 return false;
115
116 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);
117
118 if (!(Endpoint_IsReadWriteAllowed()))
119 return false;
120
121 Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
122
123 if (!(Endpoint_IsReadWriteAllowed()))
124 Endpoint_ClearOUT();
125
126 return true;
127 }
128
129 #endif