]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Class/Device/MIDIClassDevice.c
Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Class / Device / MIDIClassDevice.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define __INCLUDE_FROM_MIDI_DRIVER
37 #define __INCLUDE_FROM_MIDI_DEVICE_C
38 #include "MIDIClassDevice.h"
39
40 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
41 {
42 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
43
44 MIDIInterfaceInfo->Config.DataINEndpoint.Type = EP_TYPE_BULK;
45 MIDIInterfaceInfo->Config.DataOUTEndpoint.Type = EP_TYPE_BULK;
46
47 if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataINEndpoint, 1)))
48 return false;
49
50 if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataOUTEndpoint, 1)))
51 return false;
52
53 return true;
54 }
55
56 void MIDI_Device_USBTask(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
57 {
58 if (USB_DeviceState != DEVICE_STATE_Configured)
59 return;
60
61 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
62 MIDI_Device_Flush(MIDIInterfaceInfo);
63 #endif
64 }
65
66 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
67 const MIDI_EventPacket_t* const Event)
68 {
69 if (USB_DeviceState != DEVICE_STATE_Configured)
70 return ENDPOINT_RWSTREAM_DeviceDisconnected;
71
72 uint8_t ErrorCode;
73
74 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
75
76 if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != ENDPOINT_RWSTREAM_NoError)
77 return ErrorCode;
78
79 if (!(Endpoint_IsReadWriteAllowed()))
80 Endpoint_ClearIN();
81
82 return ENDPOINT_RWSTREAM_NoError;
83 }
84
85 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
86 {
87 if (USB_DeviceState != DEVICE_STATE_Configured)
88 return ENDPOINT_RWSTREAM_DeviceDisconnected;
89
90 uint8_t ErrorCode;
91
92 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
93
94 if (Endpoint_BytesInEndpoint())
95 {
96 Endpoint_ClearIN();
97
98 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
99 return ErrorCode;
100 }
101
102 return ENDPOINT_READYWAIT_NoError;
103 }
104
105 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
106 MIDI_EventPacket_t* const Event)
107 {
108 if (USB_DeviceState != DEVICE_STATE_Configured)
109 return false;
110
111 Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpoint.Address);
112
113 if (!(Endpoint_IsReadWriteAllowed()))
114 return false;
115
116 Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
117
118 if (!(Endpoint_IsReadWriteAllowed()))
119 Endpoint_ClearOUT();
120
121 return true;
122 }
123
124 #endif
125
Imprint / Impressum