Fixed LowLevel JoystickHostWithParser demo not saving the chosen HID interface's...
[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 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
39 {
40 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
41
42 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
43 {
44 uint16_t Size;
45 uint8_t Type;
46 uint8_t Direction;
47 bool DoubleBanked;
48
49 if (EndpointNum == MIDIInterfaceInfo->Config.DataINEndpointNumber)
50 {
51 Size = MIDIInterfaceInfo->Config.DataINEndpointSize;
52 Direction = ENDPOINT_DIR_IN;
53 Type = EP_TYPE_BULK;
54 DoubleBanked = MIDIInterfaceInfo->Config.DataINEndpointDoubleBank;
55 }
56 else if (EndpointNum == MIDIInterfaceInfo->Config.DataOUTEndpointNumber)
57 {
58 Size = MIDIInterfaceInfo->Config.DataOUTEndpointSize;
59 Direction = ENDPOINT_DIR_OUT;
60 Type = EP_TYPE_BULK;
61 DoubleBanked = MIDIInterfaceInfo->Config.DataOUTEndpointDoubleBank;
62 }
63 else
64 {
65 continue;
66 }
67
68 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
69 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
70 {
71 return false;
72 }
73 }
74
75 return true;
76 }
77
78 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
79 const MIDI_EventPacket_t* const Event)
80 {
81 if (USB_DeviceState != DEVICE_STATE_Configured)
82 return ENDPOINT_RWSTREAM_DeviceDisconnected;
83
84 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
85
86 if (Endpoint_IsReadWriteAllowed())
87 {
88 uint8_t ErrorCode;
89
90 if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != ENDPOINT_RWSTREAM_NoError)
91 return ErrorCode;
92
93 if (!(Endpoint_IsReadWriteAllowed()))
94 Endpoint_ClearIN();
95 }
96
97 return ENDPOINT_RWSTREAM_NoError;
98 }
99
100 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
101 {
102 if (USB_DeviceState != DEVICE_STATE_Configured)
103 return ENDPOINT_RWSTREAM_DeviceDisconnected;
104
105 uint8_t ErrorCode;
106
107 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpointNumber);
108
109 if (Endpoint_BytesInEndpoint())
110 {
111 Endpoint_ClearIN();
112
113 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
114 return ErrorCode;
115 }
116
117 return ENDPOINT_READYWAIT_NoError;
118 }
119
120 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
121 MIDI_EventPacket_t* const Event)
122 {
123 if (USB_DeviceState != DEVICE_STATE_Configured)
124 return false;
125
126 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpointNumber);
127
128 if (!(Endpoint_IsReadWriteAllowed()))
129 return false;
130
131 Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
132
133 if (!(Endpoint_IsReadWriteAllowed()))
134 Endpoint_ClearOUT();
135
136 return true;
137 }
138
139 #endif